debian-forge-composer/internal/distro/distro_test.go
Tom Gundersen 7957feff48 distro.Manifest: take Customizations rather than Blueprint as argument
This makes two changes simultaneously, to avoid too much churn:
 - move accessors from being on the blueprint struct to the
   customizations struct, and
 - pass the customizations struct rather than the whole blueprint
   as argumnet to distro.Manifest().

@larskarlitski pointed out in a previous review that it feels
redundant to pass the whole blueprint as well as the list of
packages to the Manifest funciton. Indeed it is, so this
simplifies things a bit.

Signed-off-by: Tom Gundersen <teg@jklm.no>
2020-03-15 23:48:42 +01:00

99 lines
2.8 KiB
Go

package distro_test
import (
"encoding/json"
"io/ioutil"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/osbuild"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
)
func TestDistro_Manifest(t *testing.T) {
pipelinePath := "../../test/cases/"
fileInfos, err := ioutil.ReadDir(pipelinePath)
if err != nil {
t.Errorf("Could not read pipelines directory '%s': %v", pipelinePath, err)
}
for _, fileInfo := range fileInfos {
type composeRequest struct {
Distro string `json:"distro"`
Arch string `json:"arch"`
OutputFormat string `json:"output-format"`
Blueprint *blueprint.Blueprint `json:"blueprint"`
}
type rpmMD struct {
BuildPackages []rpmmd.PackageSpec `json:"build-packages"`
Packages []rpmmd.PackageSpec `json:"packages"`
}
var tt struct {
ComposeRequest *composeRequest `json:"compose-request"`
RpmMD *rpmMD `json:"rpmmd"`
Manifest *osbuild.Manifest `json:"manifest,omitempty"`
}
file, err := ioutil.ReadFile(pipelinePath + fileInfo.Name())
if err != nil {
t.Errorf("Could not read test-case '%s': %v", fileInfo.Name(), err)
}
err = json.Unmarshal([]byte(file), &tt)
if err != nil {
t.Errorf("Could not parse test-case '%s': %v", fileInfo.Name(), err)
}
if tt.ComposeRequest == nil || tt.ComposeRequest.Blueprint == nil {
t.Logf("Skipping '%s'.", fileInfo.Name())
continue
}
t.Run(tt.ComposeRequest.OutputFormat, func(t *testing.T) {
distros, err := distro.NewDefaultRegistry([]string{"../.."})
if err != nil {
t.Fatal(err)
}
d := distros.GetDistro(tt.ComposeRequest.Distro)
if d == nil {
t.Errorf("unknown distro: %v", tt.ComposeRequest.Distro)
return
}
size := d.GetSizeForOutputType(tt.ComposeRequest.OutputFormat, 0)
got, err := d.Manifest(tt.ComposeRequest.Blueprint.Customizations,
nil,
tt.RpmMD.Packages,
tt.RpmMD.BuildPackages,
tt.ComposeRequest.Arch,
tt.ComposeRequest.OutputFormat,
size)
if (err != nil) != (tt.Manifest == nil) {
t.Errorf("distro.Manifest() error = %v", err)
return
}
if tt.Manifest != nil {
if diff := cmp.Diff(got, tt.Manifest); diff != "" {
t.Errorf("d.Manifest() different from expected: %v", diff)
}
}
})
}
}
// Test that all distros are registered properly and that Registry.List() works.
func TestDistro_RegistryList(t *testing.T) {
expected := []string{
"fedora-30",
"fedora-31",
"fedora-32",
"rhel-8.1",
"rhel-8.2",
}
distros, err := distro.NewDefaultRegistry([]string{"../.."})
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(distros.List(), expected); diff != "" {
t.Errorf("unexpected list of distros: %v", diff)
}
}