Make TestDistro_Manifest more versatile
Move TestDistro_Manifest to a separate package and make it parametric, so that it can be used to test specific distros. Refs: #442
This commit is contained in:
parent
8d59d0c798
commit
46e230212b
2 changed files with 106 additions and 83 deletions
|
|
@ -1,103 +1,27 @@
|
|||
package distro_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/osbuild/osbuild-composer/internal/blueprint"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro/distro_test_common"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro/fedora30"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro/fedora31"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro/fedora32"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro/rhel81"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro/rhel82"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro/rhel83"
|
||||
"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)
|
||||
assert.NoErrorf(t, err, "Could not read pipelines directory '%s'", pipelinePath)
|
||||
|
||||
for _, fileInfo := range fileInfos {
|
||||
type repository struct {
|
||||
BaseURL string `json:"baseurl,omitempty"`
|
||||
Metalink string `json:"metalink,omitempty"`
|
||||
MirrorList string `json:"mirrorlist,omitempty"`
|
||||
GPGKey string `json:"gpgkey,omitempty"`
|
||||
}
|
||||
type composeRequest struct {
|
||||
Distro string `json:"distro"`
|
||||
Arch string `json:"arch"`
|
||||
ImageType string `json:"image-type"`
|
||||
Repositories []repository `json:"repositories"`
|
||||
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())
|
||||
assert.NoErrorf(t, err, "Could not read test-case '%s'", fileInfo.Name())
|
||||
|
||||
err = json.Unmarshal([]byte(file), &tt)
|
||||
assert.NoErrorf(t, err, "Could not parse test-case '%s'", fileInfo.Name())
|
||||
|
||||
if tt.ComposeRequest == nil || tt.ComposeRequest.Blueprint == nil {
|
||||
t.Logf("Skipping '%s'.", fileInfo.Name())
|
||||
continue
|
||||
}
|
||||
|
||||
repos := make([]rpmmd.RepoConfig, len(tt.ComposeRequest.Repositories))
|
||||
for i, repo := range tt.ComposeRequest.Repositories {
|
||||
repos[i] = rpmmd.RepoConfig{
|
||||
Id: fmt.Sprintf("repo-%d", i),
|
||||
BaseURL: repo.BaseURL,
|
||||
Metalink: repo.Metalink,
|
||||
MirrorList: repo.MirrorList,
|
||||
GPGKey: repo.GPGKey,
|
||||
}
|
||||
}
|
||||
t.Run(tt.ComposeRequest.ImageType, func(t *testing.T) {
|
||||
distros, err := distro.NewRegistry(fedora30.New(), fedora31.New(), fedora32.New(), rhel81.New(), rhel82.New(), rhel83.New())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := distros.GetDistro(tt.ComposeRequest.Distro)
|
||||
require.NotNilf(t, d, "unknown distro: %v", tt.ComposeRequest.Distro)
|
||||
|
||||
arch, err := d.GetArch(tt.ComposeRequest.Arch)
|
||||
require.NoErrorf(t, err, "unknown arch: %v", tt.ComposeRequest.Arch)
|
||||
|
||||
imageType, err := arch.GetImageType(tt.ComposeRequest.ImageType)
|
||||
require.NoError(t, err, "unknown image type: %v", tt.ComposeRequest.ImageType)
|
||||
|
||||
got, err := imageType.Manifest(tt.ComposeRequest.Blueprint.Customizations,
|
||||
repos,
|
||||
tt.RpmMD.Packages,
|
||||
tt.RpmMD.BuildPackages,
|
||||
imageType.Size(0))
|
||||
if (err != nil) != (tt.Manifest == nil) {
|
||||
t.Errorf("distro.Manifest() error = %v", err)
|
||||
return
|
||||
}
|
||||
if tt.Manifest != nil {
|
||||
assert.Equalf(t, tt.Manifest, got, "d.Manifest() different from expected")
|
||||
}
|
||||
})
|
||||
}
|
||||
distro_test_common.TestDistro_Manifest(
|
||||
t,
|
||||
"../../../test/cases/",
|
||||
"*",
|
||||
fedora30.New(), fedora31.New(), fedora32.New(), rhel81.New(), rhel82.New(), rhel83.New(),
|
||||
)
|
||||
}
|
||||
|
||||
// Test that all distros are registered properly and that Registry.List() works.
|
||||
|
|
|
|||
99
internal/distro/distro_test_common/distro_test_common.go
Normal file
99
internal/distro/distro_test_common/distro_test_common.go
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
package distro_test_common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"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"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDistro_Manifest(t *testing.T, pipelinePath string, prefix string, distros ...distro.Distro) {
|
||||
assert := assert.New(t)
|
||||
fileNames, err := filepath.Glob(filepath.Join(pipelinePath, prefix))
|
||||
assert.NoErrorf(err, "Could not read pipelines directory '%s': %v", pipelinePath, err)
|
||||
for _, fileName := range fileNames {
|
||||
type repository struct {
|
||||
BaseURL string `json:"baseurl,omitempty"`
|
||||
Metalink string `json:"metalink,omitempty"`
|
||||
MirrorList string `json:"mirrorlist,omitempty"`
|
||||
GPGKey string `json:"gpgkey,omitempty"`
|
||||
}
|
||||
type composeRequest struct {
|
||||
Distro string `json:"distro"`
|
||||
Arch string `json:"arch"`
|
||||
ImageType string `json:"image-type"`
|
||||
Repositories []repository `json:"repositories"`
|
||||
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(fileName)
|
||||
assert.NoErrorf(err, "Could not read test-case '%s': %v", fileName, err)
|
||||
err = json.Unmarshal([]byte(file), &tt)
|
||||
assert.NoErrorf(err, "Could not parse test-case '%s': %v", fileName, err)
|
||||
if tt.ComposeRequest == nil || tt.ComposeRequest.Blueprint == nil {
|
||||
t.Logf("Skipping '%s'.", fileName)
|
||||
continue
|
||||
}
|
||||
|
||||
repos := make([]rpmmd.RepoConfig, len(tt.ComposeRequest.Repositories))
|
||||
for i, repo := range tt.ComposeRequest.Repositories {
|
||||
repos[i] = rpmmd.RepoConfig{
|
||||
Id: fmt.Sprintf("repo-%d", i),
|
||||
BaseURL: repo.BaseURL,
|
||||
Metalink: repo.Metalink,
|
||||
MirrorList: repo.MirrorList,
|
||||
GPGKey: repo.GPGKey,
|
||||
}
|
||||
}
|
||||
t.Run(tt.ComposeRequest.ImageType, func(t *testing.T) {
|
||||
distros, err := distro.NewRegistry(distros...)
|
||||
require.NoError(t, err)
|
||||
d := distros.GetDistro(tt.ComposeRequest.Distro)
|
||||
if d == nil {
|
||||
t.Errorf("unknown distro: %v", tt.ComposeRequest.Distro)
|
||||
return
|
||||
}
|
||||
arch, err := d.GetArch(tt.ComposeRequest.Arch)
|
||||
if err != nil {
|
||||
t.Errorf("unknown arch: %v", tt.ComposeRequest.Arch)
|
||||
return
|
||||
}
|
||||
imageType, err := arch.GetImageType(tt.ComposeRequest.ImageType)
|
||||
if err != nil {
|
||||
t.Errorf("unknown image type: %v", tt.ComposeRequest.ImageType)
|
||||
return
|
||||
}
|
||||
got, err := imageType.Manifest(tt.ComposeRequest.Blueprint.Customizations,
|
||||
repos,
|
||||
tt.RpmMD.Packages,
|
||||
tt.RpmMD.BuildPackages,
|
||||
imageType.Size(0))
|
||||
|
||||
if (err == nil && tt.Manifest == nil) || (err != nil && tt.Manifest != nil) {
|
||||
t.Errorf("distro.Manifest() error = %v", err)
|
||||
return
|
||||
}
|
||||
if tt.Manifest != nil {
|
||||
diff := cmp.Diff(got, tt.Manifest)
|
||||
assert.Empty(diff, "d.Manifest() different from expected: %v", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue