From 3a2415d9704f71c6988f11be48dfd922bd40bb61 Mon Sep 17 00:00:00 2001 From: Tomas Hozza Date: Thu, 15 Jul 2021 17:21:38 +0200 Subject: [PATCH] distro/rhel85: add image type aliases for EDGE image types EDGE image types are defined under a different name for RHEL-8.5, specifically they don't contain the "rhel-" prefix any more. To ensure backward compatibility, add image type aliases for all EDGE image types with the "rhel-" prefix. Image type aliases are used only when getting a specific imageType instance by its name. When listing all available image types for an architecture, only the current image type names are returned, without any aliases. This prevents the image types from being exposed multiple times under different names via Weldr API. Extend the distro unit tests to test image type aliases. Signed-off-by: Tomas Hozza --- internal/distro/rhel85/distro.go | 53 +++++++++---- internal/distro/rhel85/distro_test.go | 103 ++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 16 deletions(-) diff --git a/internal/distro/rhel85/distro.go b/internal/distro/rhel85/distro.go index 72459c658..d3017d8d8 100644 --- a/internal/distro/rhel85/distro.go +++ b/internal/distro/rhel85/distro.go @@ -97,12 +97,13 @@ func (d *distribution) addArches(arches ...architecture) { } type architecture struct { - distro *distribution - name string - imageTypes map[string]distro.ImageType - packageSets map[string]rpmmd.PackageSet - legacy string - uefi bool + distro *distribution + name string + imageTypes map[string]distro.ImageType + imageTypeAliases map[string]string + packageSets map[string]rpmmd.PackageSet + legacy string + uefi bool } func (a *architecture) Name() string { @@ -121,7 +122,14 @@ func (a *architecture) ListImageTypes() []string { func (a *architecture) GetImageType(name string) (distro.ImageType, error) { t, exists := a.imageTypes[name] if !exists { - return nil, errors.New("invalid image type: " + name) + aliasForName, exists := a.imageTypeAliases[name] + if !exists { + return nil, errors.New("invalid image type: " + name) + } + t, exists = a.imageTypes[aliasForName] + if !exists { + panic(fmt.Sprintf("image type '%s' is an alias to a non-existing image type '%s'", name, aliasForName)) + } } return t, nil } @@ -134,6 +142,15 @@ func (a *architecture) addImageTypes(imageTypes ...imageType) { it := imageTypes[idx] it.arch = a a.imageTypes[it.name] = &it + for _, alias := range it.nameAliases { + if a.imageTypeAliases == nil { + a.imageTypeAliases = map[string]string{} + } + if existingAliasFor, exists := a.imageTypeAliases[alias]; exists { + panic(fmt.Sprintf("image type alias '%s' for '%s' is already defined for another image type '%s'", alias, it.name, existingAliasFor)) + } + a.imageTypeAliases[alias] = it.name + } } } @@ -146,6 +163,7 @@ type pipelinesFunc func(t *imageType, customizations *blueprint.Customizations, type imageType struct { arch *architecture name string + nameAliases []string filename string mimeType string packageSets map[string]rpmmd.PackageSet @@ -420,9 +438,10 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro { // Image Definitions edgeCommitImgType := imageType{ - name: "edge-commit", - filename: "commit.tar", - mimeType: "application/x-tar", + name: "edge-commit", + nameAliases: []string{"rhel-edge-commit"}, + filename: "commit.tar", + mimeType: "application/x-tar", packageSets: map[string]rpmmd.PackageSet{ buildPkgsKey: edgeBuildPackageSet(), osPkgsKey: edgeCommitPackageSet(), @@ -433,9 +452,10 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro { exports: []string{"commit-archive"}, } edgeOCIImgType := imageType{ - name: "edge-container", - filename: "container.tar", - mimeType: "application/x-tar", + name: "edge-container", + nameAliases: []string{"rhel-edge-container"}, + filename: "container.tar", + mimeType: "application/x-tar", packageSets: map[string]rpmmd.PackageSet{ buildPkgsKey: edgeBuildPackageSet(), osPkgsKey: edgeCommitPackageSet(), @@ -448,9 +468,10 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro { exports: []string{containerPkgsKey}, } edgeInstallerImgType := imageType{ - name: "edge-installer", - filename: "installer.iso", - mimeType: "application/x-iso9660-image", + name: "edge-installer", + nameAliases: []string{"rhel-edge-installer"}, + filename: "installer.iso", + mimeType: "application/x-iso9660-image", packageSets: map[string]rpmmd.PackageSet{ buildPkgsKey: edgeBuildPackageSet(), osPkgsKey: edgeCommitPackageSet(), diff --git a/internal/distro/rhel85/distro_test.go b/internal/distro/rhel85/distro_test.go index d3c4c17c3..371e2d124 100644 --- a/internal/distro/rhel85/distro_test.go +++ b/internal/distro/rhel85/distro_test.go @@ -1,6 +1,7 @@ package rhel85_test import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -94,6 +95,15 @@ func TestFilenameFromType(t *testing.T) { mimeType: "application/x-tar", }, }, + // Alias + { + name: "rhel-edge-commit", + args: args{"rhel-edge-commit"}, + want: wantResult{ + filename: "commit.tar", + mimeType: "application/x-tar", + }, + }, { name: "edge-container", args: args{"edge-container"}, @@ -102,6 +112,15 @@ func TestFilenameFromType(t *testing.T) { mimeType: "application/x-tar", }, }, + // Alias + { + name: "rhel-edge-container", + args: args{"rhel-edge-container"}, + want: wantResult{ + filename: "container.tar", + mimeType: "application/x-tar", + }, + }, { name: "edge-installer", args: args{"edge-installer"}, @@ -110,6 +129,15 @@ func TestFilenameFromType(t *testing.T) { mimeType: "application/x-iso9660-image", }, }, + // Alias + { + name: "rhel-edge-installer", + args: args{"rhel-edge-installer"}, + want: wantResult{ + filename: "installer.iso", + mimeType: "application/x-iso9660-image", + }, + }, { name: "invalid-output-type", args: args{"foobar"}, @@ -263,6 +291,81 @@ func TestImageType_Name(t *testing.T) { } } +func TestImageTypeAliases(t *testing.T) { + type args struct { + imageTypeAliases []string + } + type wantResult struct { + imageTypeName string + } + tests := []struct { + name string + args args + want wantResult + }{ + { + name: "edge-commit aliases", + args: args{ + imageTypeAliases: []string{"rhel-edge-commit"}, + }, + want: wantResult{ + imageTypeName: "edge-commit", + }, + }, + { + name: "edge-container aliases", + args: args{ + imageTypeAliases: []string{"rhel-edge-container"}, + }, + want: wantResult{ + imageTypeName: "edge-container", + }, + }, + { + name: "edge-installer aliases", + args: args{ + imageTypeAliases: []string{"rhel-edge-installer"}, + }, + want: wantResult{ + imageTypeName: "edge-installer", + }, + }, + } + for _, dist := range rhelFamilyDistros { + t.Run(dist.name, func(t *testing.T) { + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dist := dist.distro + for _, archName := range dist.ListArches() { + t.Run(archName, func(t *testing.T) { + arch, err := dist.GetArch(archName) + require.Nilf(t, err, + "failed to get architecture '%s', previously listed as supported for the distro '%s'", + archName, dist.Name()) + // Test image type aliases only if the aliased image type is supported for the arch + if _, err = arch.GetImageType(tt.want.imageTypeName); err != nil { + t.Skipf("aliased image type '%s' is not supported for architecture '%s'", + tt.want.imageTypeName, archName) + } + for _, alias := range tt.args.imageTypeAliases { + t.Run(fmt.Sprintf("'%s' alias for image type '%s'", alias, tt.want.imageTypeName), + func(t *testing.T) { + gotImage, err := arch.GetImageType(alias) + require.Nilf(t, err, "arch.GetImageType() for image type alias '%s' failed: %v", + alias, err) + assert.Equalf(t, tt.want.imageTypeName, gotImage.Name(), + "got unexpected image type name for alias '%s'. got = %s, want = %s", + alias, tt.want.imageTypeName, gotImage.Name()) + }) + } + }) + } + }) + } + }) + } +} + // Check that Manifest() function returns an error for unsupported // configurations. func TestDistro_ManifestError(t *testing.T) {