Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distro: fail on OSTree image options being provided for non-OSTree based image types #1071

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 68 additions & 4 deletions pkg/distro/distro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ func TestImageTypePipelineNames(t *testing.T) {
seed := int64(0)

// Add ostree options for image types that require them
options.OSTree = &ostree.ImageOptions{
URL: "https://example.com",
if imageType.OSTreeRef() != "" {
options.OSTree = &ostree.ImageOptions{
URL: "https://example.com",
}
}

// Pipelines that require package sets will fail if none
Expand Down Expand Up @@ -397,8 +399,10 @@ func TestPipelineRepositories(t *testing.T) {
options := distro.ImageOptions{}

// Add ostree options for image types that require them
options.OSTree = &ostree.ImageOptions{
URL: "https://example.com",
if imageType.OSTreeRef() != "" {
options.OSTree = &ostree.ImageOptions{
URL: "https://example.com",
}
}

repos := tCase.repos
Expand Down Expand Up @@ -567,3 +571,63 @@ func TestDistro_ManifestFIPSWarning(t *testing.T) {
}
}
}

// Test that passing options.OSTree for non-OSTree image types results in an error
func TestOSTreeOptionsErrorForNonOSTreeImgTypes(t *testing.T) {
assert := assert.New(t)
distroFactory := distrofactory.NewDefault()
assert.NotNil(distroFactory)

distros := distro_test_common.ListTestedDistros(t)
assert.NotEmpty(distros)

for _, distroName := range distros {
d := distroFactory.GetDistro(distroName)
assert.NotNil(d)

arches := d.ListArches()
assert.NotEmpty(arches)

for _, archName := range arches {
arch, err := d.GetArch(archName)
assert.Nil(err)

imgTypes := arch.ListImageTypes()
assert.NotEmpty(imgTypes)

for _, imageTypeName := range imgTypes {
t.Run(fmt.Sprintf("%s/%s/%s", distroName, archName, imageTypeName), func(t *testing.T) {
imageType, err := arch.GetImageType(imageTypeName)
assert.Nil(err)

// set up bare minimum args for image type
var customizations *blueprint.Customizations
if imageType.Name() == "edge-simplified-installer" || imageType.Name() == "iot-simplified-installer" {
customizations = &blueprint.Customizations{
InstallationDevice: "/dev/null",
}
}
bp := blueprint.Blueprint{
Customizations: customizations,
}
options := distro.ImageOptions{
OSTree: &ostree.ImageOptions{
URL: "https://example.com",
},
}

_, _, err = imageType.Manifest(&bp, options, nil, 0)
if imageType.OSTreeRef() == "" {
assert.Errorf(err,
"OSTree options should not be allowed for non-OSTree image type %s/%s/%s",
imageTypeName, archName, distroName)
} else {
assert.NoErrorf(err,
"OSTree options should be allowed for OSTree image type %s/%s/%s",
imageTypeName, archName, distroName)
}
})
}
}
}
}
19 changes: 17 additions & 2 deletions pkg/distro/distro_test_common/distro_test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ var knownKernels = []string{"kernel", "kernel-debug", "kernel-rt"}

// Returns the number of known kernels in the package list
func kernelCount(imgType distro.ImageType, bp blueprint.Blueprint) int {
ostreeOptions := &ostree.ImageOptions{
URL: "https://example.com", // required by some image types
var ostreeOptions *ostree.ImageOptions
// OSTree image types require OSTree options
if imgType.OSTreeRef() != "" {
ostreeOptions = &ostree.ImageOptions{
URL: "https://example.com",
}
}

manifest, _, err := imgType.Manifest(&bp, distro.ImageOptions{OSTree: ostreeOptions}, nil, 0)
if err != nil {
panic(err)
Expand Down Expand Up @@ -202,6 +207,11 @@ func TestDistro_OSTreeOptions(t *testing.T, d distro.Distro) {
imgType, err := arch.GetImageType(typeName)
assert.NoError(err)

if imgType.OSTreeRef() == "" {
// image type doesn't support OSTree options
t.Skipf("OSTree options not supported for image type %s/%s/%s", typeName, archName, d.Name())
}

ostreeOptions := ostree.ImageOptions{}
if typesWithPayload[typeName] {
// payload types require URL
Expand Down Expand Up @@ -250,6 +260,11 @@ func TestDistro_OSTreeOptions(t *testing.T, d distro.Distro) {
imgType, err := arch.GetImageType(typeName)
assert.NoError(err)

if imgType.OSTreeRef() == "" {
// image type doesn't support OSTree options
t.Skipf("OSTree options not supported for image type %s/%s/%s", typeName, archName, d.Name())
}

ostreeOptions := ostree.ImageOptions{
ImageRef: "test/x86_64/01",
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/distro/fedora/imagetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,12 @@ func (t *imageType) Manifest(bp *blueprint.Blueprint,
// checkOptions checks the validity and compatibility of options and customizations for the image type.
// Returns ([]string, error) where []string, if non-nil, will hold any generated warnings (e.g. deprecation notices).
func (t *imageType) checkOptions(bp *blueprint.Blueprint, options distro.ImageOptions) ([]string, error) {

customizations := bp.Customizations

if !t.rpmOstree && options.OSTree != nil {
return nil, fmt.Errorf("OSTree is not supported for %q", t.Name())
}

// we do not support embedding containers on ostree-derived images, only on commits themselves
if len(bp.Containers) > 0 && t.rpmOstree && (t.name != "iot-commit" && t.name != "iot-container") {
return nil, fmt.Errorf("embedding containers is not supported for %s on %s", t.name, t.arch.distro.name)
Expand Down
4 changes: 4 additions & 0 deletions pkg/distro/rhel/imagetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ func (t *ImageType) Manifest(bp *blueprint.Blueprint,
// checkOptions checks the validity and compatibility of options and customizations for the image type.
// Returns ([]string, error) where []string, if non-nil, will hold any generated warnings (e.g. deprecation notices).
func (t *ImageType) checkOptions(bp *blueprint.Blueprint, options distro.ImageOptions) ([]string, error) {
if !t.RPMOSTree && options.OSTree != nil {
return nil, fmt.Errorf("OSTree is not supported for %q", t.Name())
}

if t.arch.distro.CheckOptions != nil {
return t.arch.distro.CheckOptions(t, bp, options)
}
Expand Down
Loading