debian-forge-composer/internal/distro/distro_test.go
Tom Gundersen 5fcdd4bea5 distro: don't use a global variable to store the distro register
Introduce a DistroRegister object. For now this does not introduce
any functional changes, as the object is always instantited to be
the same. However, in follow-up patches it will get options.

Signed-off-by: Tom Gundersen <teg@jklm.no>
2020-01-03 22:35:52 +01:00

66 lines
2.1 KiB
Go

package distro_test
import (
"encoding/json"
"io/ioutil"
"reflect"
"testing"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/pipeline"
)
func TestDistro_Pipeline(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 compose struct {
Distro string `json:"distro"`
Arch string `json:"arch"`
OutputFormat string `json:"output-format"`
Checksums map[string]string `json:"checksums"`
Blueprint *blueprint.Blueprint `json:"blueprint"`
}
var tt struct {
Compose *compose `json:"compose"`
Pipeline *pipeline.Pipeline `json:"pipeline,omitempty"`
}
file, err := ioutil.ReadFile(pipelinePath + fileInfo.Name())
if err != nil {
t.Errorf("Colud not read test-case '%s': %v", fileInfo.Name(), err)
}
err = json.Unmarshal([]byte(file), &tt)
if err != nil {
t.Errorf("Colud not parse test-case '%s': %v", fileInfo.Name(), err)
}
if tt.Compose == nil || tt.Compose.Blueprint == nil {
t.Logf("Skipping '%s'.", fileInfo.Name())
continue
}
t.Run(tt.Compose.OutputFormat, func(t *testing.T) {
distros := distro.NewRegistry()
d := distros.GetDistro(tt.Compose.Distro)
if d == nil {
t.Errorf("unknown distro: %v", tt.Compose.Distro)
return
}
got, err := d.Pipeline(tt.Compose.Blueprint, nil, tt.Compose.Checksums, tt.Compose.Arch, tt.Compose.OutputFormat)
if (err != nil) != (tt.Pipeline == nil) {
t.Errorf("distro.Pipeline() error = %v", err)
return
}
if tt.Pipeline != nil {
if !reflect.DeepEqual(got, tt.Pipeline) {
// Without this the "difference" is just a list of pointers.
gotJson, _ := json.Marshal(got)
fileJson, _ := json.Marshal(tt.Pipeline)
t.Errorf("d.Pipeline() =\n%v,\nwant =\n%v", string(gotJson), string(fileJson))
}
}
})
}
}