test: Use test_distro.NewTestDistro from images

and move the ResolveContent function from the images test_distro
package. api_test is the only place using it, so it belongs there, not
in images.
This commit is contained in:
Brian C. Lane 2023-09-13 12:00:19 -07:00 committed by Achilleas Koutsou
parent 0db61e0c21
commit 61c9a66b7e
2 changed files with 53 additions and 10 deletions

View file

@ -3,6 +3,7 @@ package weldr
import (
"archive/tar"
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
@ -17,6 +18,7 @@ import (
"time"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distro/test_distro"
"github.com/osbuild/images/pkg/distroregistry"
@ -81,7 +83,7 @@ func createWeldrAPI(tempdir string, fixtureGenerator rpmmd_mock.FixtureGenerator
if err != nil {
panic(err)
}
distro2 := test_distro.New2()
distro2 := test_distro.NewTestDistro("test-distro-2", "platform:test-2", "2")
dr, err := distroregistry.New(distro1, distro1, distro2)
if err != nil {
@ -130,7 +132,7 @@ func createWeldrAPI2(tempdir string, fixtureGenerator rpmmd_mock.FixtureGenerato
if err != nil {
panic(err)
}
distro2 := test_distro.New2()
distro2 := test_distro.NewTestDistro("test-distro-2", "platform:test-2", "2")
dr, err := distroregistry.New(distro1, distro2)
if err != nil {
@ -149,6 +151,45 @@ func createWeldrAPI2(tempdir string, fixtureGenerator rpmmd_mock.FixtureGenerato
return NewTestAPI(solver, arch, dr, rr, nil, fixture.Store, fixture.Workers, "", distroImageTypeDenylist), fixture.Store
}
// ResolveContent transforms content source specs into resolved specs for serialization.
// For packages, it uses the dnfjson_mock.BaseDeps() every time, but retains
// the map keys from the input.
// For ostree commits it hashes the URL+Ref to create a checksum.
func ResolveContent(pkgs map[string][]rpmmd.PackageSet, containers map[string][]container.SourceSpec, commits map[string][]ostree.SourceSpec) (map[string][]rpmmd.PackageSpec, map[string][]container.Spec, map[string][]ostree.CommitSpec) {
pkgSpecs := make(map[string][]rpmmd.PackageSpec, len(pkgs))
for name := range pkgs {
pkgSpecs[name] = dnfjson_mock.BaseDeps()
}
containerSpecs := make(map[string][]container.Spec, len(containers))
for name := range containers {
containerSpecs[name] = make([]container.Spec, len(containers[name]))
for idx := range containers[name] {
containerSpecs[name][idx] = container.Spec{
Source: containers[name][idx].Source,
TLSVerify: containers[name][idx].TLSVerify,
LocalName: containers[name][idx].Name,
}
}
}
commitSpecs := make(map[string][]ostree.CommitSpec, len(commits))
for name := range commits {
commitSpecs[name] = make([]ostree.CommitSpec, len(commits[name]))
for idx := range commits[name] {
commitSpecs[name][idx] = ostree.CommitSpec{
Ref: commits[name][idx].Ref,
URL: commits[name][idx].URL,
Checksum: fmt.Sprintf("%x", sha256.Sum256([]byte(commits[name][idx].URL+commits[name][idx].Ref))),
}
fmt.Printf("Test distro spec: %+v\n", commitSpecs[name][idx])
}
}
return pkgSpecs, containerSpecs, commitSpecs
}
func TestBasic(t *testing.T) {
var cases = []struct {
Path string
@ -894,7 +935,7 @@ func TestCompose(t *testing.T) {
manifest, _, err := imgType.Manifest(nil, distro.ImageOptions{}, nil, 0)
require.NoError(t, err)
rPkgs, rContainers, rCommits := test_distro.ResolveContent(manifest.GetPackageSetChains(), manifest.GetContainerSourceSpecs(), manifest.GetOSTreeSourceSpecs())
rPkgs, rContainers, rCommits := ResolveContent(manifest.GetPackageSetChains(), manifest.GetContainerSourceSpecs(), manifest.GetOSTreeSourceSpecs())
mf, err := manifest.Serialize(rPkgs, rContainers, rCommits)
require.NoError(t, err)
@ -905,7 +946,7 @@ func TestCompose(t *testing.T) {
ostreeManifest, _, err := ostreeImgType.Manifest(nil, distro.ImageOptions{OSTree: &ostreeOptions}, nil, 0)
require.NoError(t, err)
rPkgs, rContainers, rCommits = test_distro.ResolveContent(ostreeManifest.GetPackageSetChains(), ostreeManifest.GetContainerSourceSpecs(), ostreeManifest.GetOSTreeSourceSpecs())
rPkgs, rContainers, rCommits = ResolveContent(ostreeManifest.GetPackageSetChains(), ostreeManifest.GetContainerSourceSpecs(), ostreeManifest.GetOSTreeSourceSpecs())
omf, err := ostreeManifest.Serialize(rPkgs, rContainers, rCommits)
require.NoError(t, err)
@ -1016,7 +1057,7 @@ func TestCompose(t *testing.T) {
ostreeManifestOther, _, err := ostreeImgType.Manifest(nil, distro.ImageOptions{OSTree: &ostreeOptionsOther}, nil, 0)
require.NoError(t, err)
rPkgs, rContainers, rCommits = test_distro.ResolveContent(ostreeManifestOther.GetPackageSetChains(), ostreeManifestOther.GetContainerSourceSpecs(), ostreeManifestOther.GetOSTreeSourceSpecs())
rPkgs, rContainers, rCommits = ResolveContent(ostreeManifestOther.GetPackageSetChains(), ostreeManifestOther.GetContainerSourceSpecs(), ostreeManifestOther.GetOSTreeSourceSpecs())
omfo, err := ostreeManifest.Serialize(rPkgs, rContainers, rCommits)
require.NoError(t, err)
@ -1050,14 +1091,15 @@ func TestCompose(t *testing.T) {
}
// For 2nd distribution
arch2, err := test_distro.New2().GetArch(test_distro.TestArchName)
distro2 := test_distro.NewTestDistro("test-distro-2", "platform:test-2", "2")
arch2, err := distro2.GetArch(test_distro.TestArchName)
require.NoError(t, err)
imgType2, err := arch2.GetImageType(test_distro.TestImageTypeName)
require.NoError(t, err)
manifest2, _, err := imgType.Manifest(nil, distro.ImageOptions{}, nil, 0)
require.NoError(t, err)
rPkgs, rContainers, rCommits = test_distro.ResolveContent(manifest2.GetPackageSetChains(), manifest2.GetContainerSourceSpecs(), manifest2.GetOSTreeSourceSpecs())
rPkgs, rContainers, rCommits = ResolveContent(manifest2.GetPackageSetChains(), manifest2.GetContainerSourceSpecs(), manifest2.GetOSTreeSourceSpecs())
mf2, err := manifest2.Serialize(rPkgs, rContainers, rCommits)
require.NoError(t, err)
@ -2040,7 +2082,8 @@ func TestComposeTypes_ImageTypeDenylist(t *testing.T) {
}
func TestComposePOST_ImageTypeDenylist(t *testing.T) {
arch, err := test_distro.New2().GetArch(test_distro.TestArch2Name)
distro2 := test_distro.NewTestDistro("test-distro-2", "platform:test-2", "2")
arch, err := distro2.GetArch(test_distro.TestArch2Name)
require.NoError(t, err)
imgType, err := arch.GetImageType(test_distro.TestImageTypeName)
require.NoError(t, err)
@ -2049,7 +2092,7 @@ func TestComposePOST_ImageTypeDenylist(t *testing.T) {
manifest, _, err := imgType.Manifest(nil, distro.ImageOptions{}, nil, 0)
require.NoError(t, err)
rPkgs, rContainers, rCommits := test_distro.ResolveContent(manifest.GetPackageSetChains(), manifest.GetContainerSourceSpecs(), manifest.GetOSTreeSourceSpecs())
rPkgs, rContainers, rCommits := ResolveContent(manifest.GetPackageSetChains(), manifest.GetContainerSourceSpecs(), manifest.GetOSTreeSourceSpecs())
mf, err := manifest.Serialize(rPkgs, rContainers, rCommits)
require.NoError(t, err)