diff --git a/cmd/build/main.go b/cmd/build/main.go index da9adf536c..167ae77205 100644 --- a/cmd/build/main.go +++ b/cmd/build/main.go @@ -49,7 +49,7 @@ func run() error { os.Exit(1) } - distroFac := distrofactory.NewDefault() + distroFac := distrofactory.NewDefault("") config, err := buildconfig.New(configFile, nil) if err != nil { return err diff --git a/cmd/gen-manifests/main.go b/cmd/gen-manifests/main.go index d264ac8326..48d255919f 100644 --- a/cmd/gen-manifests/main.go +++ b/cmd/gen-manifests/main.go @@ -435,7 +435,7 @@ func main() { panic(fmt.Sprintf("failed to create repo registry with tested distros: %v", err)) } - distroFac := distrofactory.NewDefault() + distroFac := distrofactory.NewDefault("") jobs := make([]manifestJob, 0) contentResolve := map[string]bool{ diff --git a/cmd/list-images/main.go b/cmd/list-images/main.go index 9cd2001147..4904b45137 100644 --- a/cmd/list-images/main.go +++ b/cmd/list-images/main.go @@ -79,7 +79,7 @@ func main() { if err != nil { panic(fmt.Sprintf("failed to create repo registry with tested distros: %v", err)) } - distroFac := distrofactory.NewDefault() + distroFac := distrofactory.NewDefault("") distros, invalidDistros := resolveArgValues(distros, testedRepoRegistry.ListDistros()) if len(invalidDistros) > 0 { fmt.Fprintf(os.Stderr, "WARNING: invalid distro names: [%s]\n", strings.Join(invalidDistros, ",")) diff --git a/cmd/osbuild-playground/main.go b/cmd/osbuild-playground/main.go index a4cdc96ea3..595468dabf 100644 --- a/cmd/osbuild-playground/main.go +++ b/cmd/osbuild-playground/main.go @@ -55,7 +55,7 @@ func main() { } } - distroFac := distrofactory.NewDefault() + distroFac := distrofactory.NewDefault("") var d distro.Distro if distroArg == "host" { d = distroFac.FromHost() diff --git a/pkg/distro/bootc/bootc.go b/pkg/distro/bootc/bootc.go index 92f8b174d7..f6a0f4400d 100644 --- a/pkg/distro/bootc/bootc.go +++ b/pkg/distro/bootc/bootc.go @@ -605,14 +605,14 @@ func NewBootcDistro(imgref string, opts *DistroOptions) (*BootcDistro, error) { // if no direct match can be found it will it will use the ID_LIKE. // This should ensure we work on every bootc image that puts a correct // ID_LIKE= in /etc/os-release -func newDistroYAMLFrom(sourceInfo *osinfo.Info) (*defs.DistroYAML, *distro.ID, error) { +func newDistroYAMLFrom(defsDir string, sourceInfo *osinfo.Info) (*defs.DistroYAML, *distro.ID, error) { for _, distroID := range append([]string{sourceInfo.OSRelease.ID}, sourceInfo.OSRelease.IDLike...) { nameVer := fmt.Sprintf("%s-%s", distroID, sourceInfo.OSRelease.VersionID) id, err := distro.ParseID(nameVer) if err != nil { return nil, nil, err } - distroYAML, err := defs.NewDistroYAML(nameVer) + distroYAML, err := defs.NewDistroYAML(defsDir, nameVer) if err != nil { return nil, nil, err } @@ -631,7 +631,8 @@ func (t *BootcImageType) manifestForLegacyISO(bp *blueprint.Blueprint, options d if t.arch.distro.imgref == "" { return nil, nil, fmt.Errorf("pipeline: no base image defined") } - distroYAML, id, err := newDistroYAMLFrom(t.arch.distro.sourceInfo) + // XXX defsdir? + distroYAML, id, err := newDistroYAMLFrom("", t.arch.distro.sourceInfo) if err != nil { return nil, nil, err } @@ -766,7 +767,7 @@ func newBootcDistroAfterIntrospect(archStr string, info *osinfo.Info, imgref, de arch: archi, } - distroYAML, err := defs.LoadDistroWithoutImageTypes("bootc-generic-1") + distroYAML, err := defs.LoadDistroWithoutImageTypes("", "bootc-generic-1") if err != nil { return nil, err } @@ -793,7 +794,7 @@ func newBootcDistroAfterIntrospect(archStr string, info *osinfo.Info, imgref, de // anything but tests. var NewBootcDistroForTesting = newBootcDistroAfterIntrospect -func DistroFactory(idStr string) distro.Distro { +func DistroFactory(defsDir string, idStr string) distro.Distro { l := strings.SplitN(idStr, ":", 2) if l[0] != "bootc" { return nil diff --git a/pkg/distro/defs/id.go b/pkg/distro/defs/id.go index 4800ee219b..6acde020ca 100644 --- a/pkg/distro/defs/id.go +++ b/pkg/distro/defs/id.go @@ -62,7 +62,8 @@ func matchAndNormalize(reStr, nameVer string) (string, error) { // // If no match is found it will "nil" and no error ( func ParseID(nameVer string) (*distro.ID, error) { - distros, err := loadDistros() + // XXX defsDir? + distros, err := loadDistros("") if err != nil { return nil, err } diff --git a/pkg/distro/defs/loader.go b/pkg/distro/defs/loader.go index 60458cb465..e479d47497 100644 --- a/pkg/distro/defs/loader.go +++ b/pkg/distro/defs/loader.go @@ -41,10 +41,13 @@ var ( // this can be overriden in tests var defaultDataFS fs.FS = distrodefs.Data -func dataFS() fs.FS { +func dataFS(defsDir string) fs.FS { // XXX: this is a short term measure, pass a set of // searchPaths down the stack instead dataFS := defaultDataFS + if defsDir != "" { + dataFS = os.DirFS(defsDir) + } if overrideDir := experimentalflags.String("yamldir"); overrideDir != "" { olog.Printf("WARNING: using experimental override dir %q", overrideDir) dataFS = os.DirFS(overrideDir) @@ -110,6 +113,8 @@ type DistroYAML struct { // set by the loader ID distro.ID + + defsRoot string } func (d *DistroYAML) ImageTypes() map[string]ImageTypeYAML { @@ -166,8 +171,8 @@ func (d *DistroYAML) runTemplates(id distro.ID) error { // are appended together. // Note that files are read separately from each other, so anchors and other // references can only be done within the same file. -func loadDistros() (*distrosYAML, error) { - dents, err := fs.Glob(dataFS(), "*.yaml") +func loadDistros(defsDir string) (*distrosYAML, error) { + dents, err := fs.Glob(dataFS(defsDir), "*.yaml") if err != nil { return nil, err } @@ -175,7 +180,7 @@ func loadDistros() (*distrosYAML, error) { var allDistros distrosYAML for _, name := range dents { - f, err := dataFS().Open(name) + f, err := dataFS(defsDir).Open(name) if err != nil { return nil, err } @@ -202,8 +207,8 @@ func loadDistros() (*distrosYAML, error) { // that returns all known distros but for now we keep compatibility // with the way distrofactory/reporegistry work which is by defining // distros via repository files. -func NewDistroYAML(nameVer string) (*DistroYAML, error) { - foundDistro, err := LoadDistroWithoutImageTypes(nameVer) +func NewDistroYAML(defsDir string, nameVer string) (*DistroYAML, error) { + foundDistro, err := LoadDistroWithoutImageTypes(defsDir, nameVer) if err != nil { return nil, err } @@ -211,14 +216,16 @@ func NewDistroYAML(nameVer string) (*DistroYAML, error) { return nil, nil } + foundDistro.defsRoot = defsDir + if err := foundDistro.LoadImageTypes(); err != nil { return nil, err } return foundDistro, nil } -func LoadDistroWithoutImageTypes(nameVer string) (*DistroYAML, error) { - distros, err := loadDistros() +func LoadDistroWithoutImageTypes(defsDir, nameVer string) (*DistroYAML, error) { + distros, err := loadDistros(defsDir) if err != nil { return nil, err } @@ -259,7 +266,7 @@ func LoadDistroWithoutImageTypes(nameVer string) (*DistroYAML, error) { } func (d *DistroYAML) LoadImageTypes() error { - f, err := dataFS().Open(filepath.Join(d.DefsPath, "imagetypes.yaml")) + f, err := dataFS(d.defsRoot).Open(filepath.Join(d.DefsPath, "imagetypes.yaml")) if err != nil { return err } diff --git a/pkg/distro/defs/loader_test.go b/pkg/distro/defs/loader_test.go index 68360561b7..5dbae6bb49 100644 --- a/pkg/distro/defs/loader_test.go +++ b/pkg/distro/defs/loader_test.go @@ -34,7 +34,7 @@ func makeTestImageType(t *testing.T, fakeContent string) defs.ImageTypeYAML { restore := defs.MockDataFS(baseDir) t.Cleanup(restore) - distro, err := defs.NewDistroYAML("test-distro-1") + distro, err := defs.NewDistroYAML("", "test-distro-1") require.NoError(t, err) it, ok := distro.ImageTypes()["test_type"] require.True(t, ok, "cannot find test_type in %s", fakeContent) @@ -163,7 +163,7 @@ image_types: fakeBaseDir := makeFakeDistrosYAML(t, "", fakeImgTypesYAML) t.Setenv("IMAGE_BUILDER_EXPERIMENTAL", fmt.Sprintf("yamldir=%s", fakeBaseDir)) - dist := generic.DistroFactory("test-distro-1") + dist := generic.DistroFactory("", "test-distro-1") assert.NotNil(t, dist) ar, err := dist.GetArch("x86_64") assert.NoError(t, err) @@ -344,7 +344,7 @@ image_types: baseDir := makeFakeDistrosYAML(t, fakeDistrosYaml, fakeImageTypesYaml) restore := defs.MockDataFS(baseDir) defer restore() - td, err := defs.NewDistroYAML("test-distro-1") + td, err := defs.NewDistroYAML("", "test-distro-1") require.NoError(t, err) it := td.ImageTypes()["test_type"] require.NotNil(t, it) @@ -395,7 +395,7 @@ image_types: baseDir := makeFakeDistrosYAML(t, fakeDistrosYaml, fakeImageTypesYaml) restore := defs.MockDataFS(baseDir) defer restore() - td, err := defs.NewDistroYAML("test-distro-1") + td, err := defs.NewDistroYAML("", "test-distro-1") require.NoError(t, err) it := td.ImageTypes()["test_type"] require.NotNil(t, it) @@ -437,7 +437,7 @@ image_types: baseDir := makeFakeDistrosYAML(t, fakeDistrosYaml, fakeImageTypesYaml) restore := defs.MockDataFS(baseDir) defer restore() - _, err := defs.NewDistroYAML("test-distro-1") + _, err := defs.NewDistroYAML("", "test-distro-1") assert.EqualError(t, err, `no default fs set: mount "/" requires a filesystem but none set`) } @@ -645,7 +645,7 @@ image_types: ` makeTestImageType(t, fakeDistroYaml) - dist, err := defs.NewDistroYAML("test-distro-1") + dist, err := defs.NewDistroYAML("", "test-distro-1") assert.NoError(t, err) assert.Equal(t, dist.ImageConfig(), &distro.ImageConfig{ Locale: common.ToPtr("C.UTF-8"), @@ -757,7 +757,7 @@ image_types: ` makeTestImageType(t, fakeDistroYaml) - distro, err := defs.NewDistroYAML("test-distro-1") + distro, err := defs.NewDistroYAML("", "test-distro-1") require.NoError(t, err) imgTypes := distro.ImageTypes() @@ -803,7 +803,7 @@ image_types: restore := defs.MockDataFS(baseDir) defer restore() - _, err := defs.NewDistroYAML("test-distro-1") + _, err := defs.NewDistroYAML("", "test-distro-1") require.ErrorContains(t, err, `cannot execute template for "vendor" field (is it set?)`) } @@ -962,7 +962,7 @@ func TestDistrosLoadingExact(t *testing.T) { restore := defs.MockDataFS(baseDir) defer restore() - dist, err := defs.NewDistroYAML("fedora-43") + dist, err := defs.NewDistroYAML("", "fedora-43") require.NoError(t, err) assert.Equal(t, dist, &defs.DistroYAML{ Name: "fedora-43", @@ -987,7 +987,7 @@ func TestDistrosLoadingExact(t *testing.T) { ID: distro.ID{Name: "fedora", MajorVersion: 43, MinorVersion: -1}, }) - dist, err = defs.NewDistroYAML("centos-10") + dist, err = defs.NewDistroYAML("", "centos-10") require.NoError(t, err) assert.Equal(t, dist, &defs.DistroYAML{ Name: "centos-10", @@ -1008,7 +1008,7 @@ func TestDistrosLoadingFactoryCompat(t *testing.T) { restore := defs.MockDataFS(baseDir) defer restore() - dist, err := defs.NewDistroYAML("rhel-10.1") + dist, err := defs.NewDistroYAML("", "rhel-10.1") require.NoError(t, err) assert.Equal(t, dist, &defs.DistroYAML{ Name: "rhel-10.1", @@ -1024,7 +1024,7 @@ func TestDistrosLoadingFactoryCompat(t *testing.T) { ID: distro.ID{Name: "rhel", MajorVersion: 10, MinorVersion: 1}, }) - dist, err = defs.NewDistroYAML("fedora-40") + dist, err = defs.NewDistroYAML("", "fedora-40") require.NoError(t, err) assert.Equal(t, dist, &defs.DistroYAML{ Name: "fedora-40", @@ -1100,7 +1100,7 @@ distros: // this layer. XXX: consolidate it to the YAML level // already? - distro := generic.DistroFactory(tc.distroNameVer) + distro := generic.DistroFactory("", tc.distroNameVer) require.NotNil(t, distro) assert.Equal(t, tc.distroNameVer, distro.Name()) a, err := distro.GetArch("x86_64") @@ -1116,7 +1116,7 @@ func TestDistrosLoadingNotFound(t *testing.T) { restore := defs.MockDataFS(baseDir) defer restore() - distro, err := defs.NewDistroYAML("non-exiting") + distro, err := defs.NewDistroYAML("", "non-exiting") assert.Nil(t, err) assert.Nil(t, distro) } @@ -1177,7 +1177,7 @@ distros: {"test-distro-2", "some-uefi-vendor"}, } { - distro, err := defs.NewDistroYAML(tc.distroNameVer) + distro, err := defs.NewDistroYAML("", tc.distroNameVer) require.NoError(t, err) imgTypes := distro.ImageTypes() @@ -1219,7 +1219,7 @@ image_types: ` makeTestImageType(t, fakeImageTypesYaml) - distro, err := defs.NewDistroYAML("test-distro-1") + distro, err := defs.NewDistroYAML("", "test-distro-1") assert.NoError(t, err) require.NotNil(t, distro) imgTypes := distro.ImageTypes() @@ -1254,7 +1254,7 @@ distros: {"rhel-8.10", "rhel-8.10", "8.10"}, {"rhel-810", "rhel-8.10", "8.10"}, } { - dist, err := defs.NewDistroYAML(tc.nameVer) + dist, err := defs.NewDistroYAML("", tc.nameVer) require.NoError(t, err) assert.Equal(t, dist, &defs.DistroYAML{ Name: tc.expectedDistroNameVer, diff --git a/pkg/distro/distro_test.go b/pkg/distro/distro_test.go index ad63ce8bf3..a38f4e3a84 100644 --- a/pkg/distro/distro_test.go +++ b/pkg/distro/distro_test.go @@ -57,7 +57,7 @@ func TestImageTypePipelineNames(t *testing.T) { Pipelines []pipeline `json:"pipelines"` } - distroFactory := distrofactory.NewDefault() + distroFactory := distrofactory.NewDefault("") distros := listTestedDistros(t) for _, distroName := range distros { d := distroFactory.GetDistro(distroName) @@ -432,7 +432,7 @@ func TestPipelineRepositories(t *testing.T) { }, } - distroFactory := distrofactory.NewDefault() + distroFactory := distrofactory.NewDefault("") distros := listTestedDistros(t) for tName, tCase := range testCases { t.Run(tName, func(t *testing.T) { @@ -607,7 +607,7 @@ func TestDistro_ManifestFIPSWarning(t *testing.T) { "iot-qcow2", } - distroFactory := distrofactory.NewDefault() + distroFactory := distrofactory.NewDefault("") distros := listTestedDistros(t) for _, distroName := range distros { // FIPS blueprint customization is not supported for RHEL 7 images @@ -660,7 +660,7 @@ 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() + distroFactory := distrofactory.NewDefault("") assert.NotNil(distroFactory) distros := listTestedDistros(t) diff --git a/pkg/distro/generic/distro.go b/pkg/distro/generic/distro.go index 4b385e705e..f3bc399eb9 100644 --- a/pkg/distro/generic/distro.go +++ b/pkg/distro/generic/distro.go @@ -69,8 +69,8 @@ func (d *distribution) getISOLabelFunc(isoLabel string) isoLabelFunc { } } -func newDistro(nameVer string) (distro.Distro, error) { - distroYAML, err := defs.NewDistroYAML(nameVer) +func newDistro(defsDir, nameVer string) (distro.Distro, error) { + distroYAML, err := defs.NewDistroYAML(defsDir, nameVer) if err != nil { return nil, err } @@ -224,8 +224,8 @@ func (a *architecture) Distro() distro.Distro { return a.distro } -func DistroFactory(idStr string) distro.Distro { - distro, err := newDistro(idStr) +func DistroFactory(defsDir string, idStr string) distro.Distro { + distro, err := newDistro(defsDir, idStr) if errors.Is(err, ErrDistroNotFound) { return nil } diff --git a/pkg/distro/generic/distro_test.go b/pkg/distro/generic/distro_test.go index 903edbab70..161507721b 100644 --- a/pkg/distro/generic/distro_test.go +++ b/pkg/distro/generic/distro_test.go @@ -19,7 +19,7 @@ func TestBootstrapContainers(t *testing.T) { for _, distroName := range repos.ListDistros() { t.Run(distroName, func(t *testing.T) { - d := generic.DistroFactory(distroName) + d := generic.DistroFactory("", distroName) assert.NotNil(t, d) assert.NotEmpty(t, d.(*generic.Distribution).DistroYAML.BootstrapContainers) }) @@ -41,7 +41,7 @@ func TestManifestError(t *testing.T) { // use a single image type from each distro for _, distroName := range repos.ListDistros() { - df := generic.DistroFactory(distroName) + df := generic.DistroFactory("", distroName) require.NotNil(df) dist := fedoraFamilyDistros[len(fedoraFamilyDistros)-1] diff --git a/pkg/distro/generic/fedora_test.go b/pkg/distro/generic/fedora_test.go index 7d3042d58b..570ef8a712 100644 --- a/pkg/distro/generic/fedora_test.go +++ b/pkg/distro/generic/fedora_test.go @@ -16,9 +16,9 @@ import ( ) var fedoraFamilyDistros = []distro.Distro{ - generic.DistroFactory("fedora-40"), - generic.DistroFactory("fedora-41"), - generic.DistroFactory("fedora-42"), + generic.DistroFactory("", "fedora-40"), + generic.DistroFactory("", "fedora-41"), + generic.DistroFactory("", "fedora-42"), } func TestFedoraFilenameFromType(t *testing.T) { @@ -677,7 +677,7 @@ func TestFedoraDistroFactory(t *testing.T) { testCases := []testCase{ { strID: "fedora-40", - expected: generic.DistroFactory("fedora-40"), + expected: generic.DistroFactory("", "fedora-40"), }, { strID: "fedora-40.1", @@ -695,7 +695,7 @@ func TestFedoraDistroFactory(t *testing.T) { for _, tc := range testCases { t.Run(tc.strID, func(t *testing.T) { - d := generic.DistroFactory(tc.strID) + d := generic.DistroFactory("", tc.strID) if tc.expected == nil { assert.Nil(t, d) } else { diff --git a/pkg/distro/generic/options_test.go b/pkg/distro/generic/options_test.go index 311751f6c3..c6859ceb2f 100644 --- a/pkg/distro/generic/options_test.go +++ b/pkg/distro/generic/options_test.go @@ -1919,7 +1919,7 @@ func TestCheckOptions(t *testing.T) { t.Run(name, func(t *testing.T) { assert := assert.New(t) - d := generic.DistroFactory(tc.distro) + d := generic.DistroFactory("", tc.distro) archName := tc.arch if archName == "" { archName = "x86_64" diff --git a/pkg/distro/generic/rhel10_internal_test.go b/pkg/distro/generic/rhel10_internal_test.go index 369bcea278..a6b38d6bab 100644 --- a/pkg/distro/generic/rhel10_internal_test.go +++ b/pkg/distro/generic/rhel10_internal_test.go @@ -26,7 +26,7 @@ func TestRH10DistroFactory(t *testing.T) { }, { strID: "rhel-10.0", - expected: common.Must(newDistro("rhel-10.0")), + expected: common.Must(newDistro("", "rhel-10.0")), }, { strID: "rhel-103", @@ -34,7 +34,7 @@ func TestRH10DistroFactory(t *testing.T) { }, { strID: "rhel-10.3", - expected: common.Must(newDistro("rhel-10.3")), + expected: common.Must(newDistro("", "rhel-10.3")), }, { strID: "rhel-1010", @@ -42,17 +42,17 @@ func TestRH10DistroFactory(t *testing.T) { }, { strID: "rhel-10.10", - expected: common.Must(newDistro("rhel-10.10")), + expected: common.Must(newDistro("", "rhel-10.10")), }, { strID: "centos-10", - expected: common.Must(newDistro("centos-10")), + expected: common.Must(newDistro("", "centos-10")), }, } for _, tc := range testCases { t.Run(tc.strID, func(t *testing.T) { - d := DistroFactory(tc.strID) + d := DistroFactory("", tc.strID) if tc.expected == nil { assert.Nil(t, d) } else { @@ -65,7 +65,7 @@ func TestRH10DistroFactory(t *testing.T) { func TestRhel10_NoBootPartition(t *testing.T) { for _, distroName := range []string{"rhel-10.0", "centos-10"} { - dist := DistroFactory(distroName) + dist := DistroFactory("", distroName) require.NotNil(t, dist, distroName) for _, archName := range dist.ListArches() { arch, err := dist.GetArch(archName) @@ -94,7 +94,7 @@ func TestRhel10_NoBootPartition(t *testing.T) { func TestESP(t *testing.T) { var distros []distro.Distro for _, distroName := range []string{"rhel-10.0", "centos-10"} { - distros = append(distros, common.Must(newDistro(distroName))) + distros = append(distros, common.Must(newDistro("", distroName))) } distro_test_common.TestESP(t, distros, func(i distro.ImageType) (*disk.PartitionTable, error) { diff --git a/pkg/distro/generic/rhel10_test.go b/pkg/distro/generic/rhel10_test.go index 0456086056..c9d26407e5 100644 --- a/pkg/distro/generic/rhel10_test.go +++ b/pkg/distro/generic/rhel10_test.go @@ -22,7 +22,7 @@ type rhel10FamilyDistro struct { var rhel10FamilyDistros = []rhel10FamilyDistro{ { name: "rhel-10.0", - distro: generic.DistroFactory("rhel-10.0"), + distro: generic.DistroFactory("", "rhel-10.0"), }, } @@ -383,7 +383,7 @@ func TestRH10Rhel10_KernelOption(t *testing.T) { func TestRH10Rhel10_KernelOption_NoIfnames(t *testing.T) { for _, distroName := range []string{"rhel-10.0", "centos-10"} { - distro := generic.DistroFactory(distroName) + distro := generic.DistroFactory("", distroName) for _, archName := range distro.ListArches() { arch, err := distro.GetArch(archName) assert.NoError(t, err) diff --git a/pkg/distro/generic/rhel7_internal_test.go b/pkg/distro/generic/rhel7_internal_test.go index e34617ad30..1d4fbe8a01 100644 --- a/pkg/distro/generic/rhel7_internal_test.go +++ b/pkg/distro/generic/rhel7_internal_test.go @@ -13,7 +13,7 @@ import ( func TestRhel7_ESP(t *testing.T) { var distros []distro.Distro for _, distroName := range []string{"rhel-7.9"} { - distros = append(distros, common.Must(newDistro(distroName))) + distros = append(distros, common.Must(newDistro("", distroName))) } distro_test_common.TestESP(t, distros, func(i distro.ImageType) (*disk.PartitionTable, error) { diff --git a/pkg/distro/generic/rhel7_test.go b/pkg/distro/generic/rhel7_test.go index a1ff6808b0..d8e79539b7 100644 --- a/pkg/distro/generic/rhel7_test.go +++ b/pkg/distro/generic/rhel7_test.go @@ -20,7 +20,7 @@ type rhelFamilyDistro struct { var rhel7_FamilyDistros = []rhelFamilyDistro{ { name: "rhel-79", - distro: generic.DistroFactory("rhel-7.9"), + distro: generic.DistroFactory("", "rhel-7.9"), }, } @@ -281,13 +281,13 @@ func TestRhel7DistroFactory(t *testing.T) { }, { strID: "rhel-7.9", - expected: generic.DistroFactory("rhel-7.9"), + expected: generic.DistroFactory("", "rhel-7.9"), }, } for _, tc := range testCases { t.Run(tc.strID, func(t *testing.T) { - d := generic.DistroFactory(tc.strID) + d := generic.DistroFactory("", tc.strID) if tc.expected == nil { assert.Nil(t, d) } else { diff --git a/pkg/distro/generic/rhel8_internal_test.go b/pkg/distro/generic/rhel8_internal_test.go index 73fc20d87e..e3d5842a47 100644 --- a/pkg/distro/generic/rhel8_internal_test.go +++ b/pkg/distro/generic/rhel8_internal_test.go @@ -50,7 +50,7 @@ func TestRH8_EC2Partitioning(t *testing.T) { continue } t.Run(fmt.Sprintf("%s/%s/%s", tt.distro, arch, it), func(t *testing.T) { - d := DistroFactory(tt.distro) + d := DistroFactory("", tt.distro) require.NotNil(t, d) a, err := d.GetArch(arch) require.NoError(t, err) @@ -87,31 +87,31 @@ func TestRH8_DistroFactory(t *testing.T) { testCases := []testCase{ { strID: "rhel-8.0", - expected: common.Must(newDistro("rhel-8.0")), + expected: common.Must(newDistro("", "rhel-8.0")), }, { strID: "rhel-80", - expected: common.Must(newDistro("rhel-8.0")), + expected: common.Must(newDistro("", "rhel-8.0")), }, { strID: "rhel-8.4", - expected: common.Must(newDistro("rhel-8.4")), + expected: common.Must(newDistro("", "rhel-8.4")), }, { strID: "rhel-84", - expected: common.Must(newDistro("rhel-8.4")), + expected: common.Must(newDistro("", "rhel-8.4")), }, { strID: "rhel-8.10", - expected: common.Must(newDistro("rhel-8.10")), + expected: common.Must(newDistro("", "rhel-8.10")), }, { strID: "rhel-810", - expected: common.Must(newDistro("rhel-8.10")), + expected: common.Must(newDistro("", "rhel-8.10")), }, { strID: "centos-8", - expected: common.Must(newDistro("centos-8")), + expected: common.Must(newDistro("", "centos-8")), }, { strID: "centos-8.4", @@ -129,7 +129,7 @@ func TestRH8_DistroFactory(t *testing.T) { for _, tc := range testCases { t.Run(tc.strID, func(t *testing.T) { - d := DistroFactory(tc.strID) + d := DistroFactory("", tc.strID) if tc.expected == nil { assert.Nil(t, d) } else { @@ -143,7 +143,7 @@ func TestRH8_DistroFactory(t *testing.T) { func RH8_TestESP(t *testing.T) { var distros []distro.Distro for _, distroName := range []string{"rhel-8.8", "rhel-8.9", "rhel-8.10", "centos-8"} { - distros = append(distros, DistroFactory(distroName)) + distros = append(distros, DistroFactory("", distroName)) } distro_test_common.TestESP(t, distros, func(i distro.ImageType) (*disk.PartitionTable, error) { diff --git a/pkg/distro/generic/rhel8_test.go b/pkg/distro/generic/rhel8_test.go index 448a9cf83e..1a065ba5b0 100644 --- a/pkg/distro/generic/rhel8_test.go +++ b/pkg/distro/generic/rhel8_test.go @@ -17,7 +17,7 @@ import ( var rhel8_FamilyDistros = []rhelFamilyDistro{ { name: "rhel-810", - distro: generic.DistroFactory("rhel-810"), + distro: generic.DistroFactory("", "rhel-810"), }, } @@ -630,7 +630,7 @@ func TestRhel8_DistroFactory(t *testing.T) { testCases := []testCase{ { strID: "rhel-8.10", - expected: generic.DistroFactory("rhel-8.10"), + expected: generic.DistroFactory("", "rhel-8.10"), }, { strID: "rhel-8.4.1", @@ -644,7 +644,7 @@ func TestRhel8_DistroFactory(t *testing.T) { for _, tc := range testCases { t.Run(tc.strID, func(t *testing.T) { - d := generic.DistroFactory(tc.strID) + d := generic.DistroFactory("", tc.strID) if tc.expected == nil { assert.Nil(t, d) } else { diff --git a/pkg/distro/generic/rhel9_internal_test.go b/pkg/distro/generic/rhel9_internal_test.go index 1a285392e1..b52c832a16 100644 --- a/pkg/distro/generic/rhel9_internal_test.go +++ b/pkg/distro/generic/rhel9_internal_test.go @@ -49,7 +49,7 @@ func TestRhel9_EC2Partitioning(t *testing.T) { continue } t.Run(fmt.Sprintf("%s/%s/%s", tt.distro, arch, it), func(t *testing.T) { - a, err := DistroFactory(tt.distro).GetArch(arch) + a, err := DistroFactory("", tt.distro).GetArch(arch) require.NoError(t, err) i, err := a.GetImageType(it) require.NoError(t, err) @@ -78,31 +78,31 @@ func TestRhel9_DistroFactory(t *testing.T) { testCases := []testCase{ { strID: "rhel-90", - expected: common.Must(newDistro("rhel-9.0")), + expected: common.Must(newDistro("", "rhel-9.0")), }, { strID: "rhel-9.0", - expected: common.Must(newDistro("rhel-9.0")), + expected: common.Must(newDistro("", "rhel-9.0")), }, { strID: "rhel-93", - expected: common.Must(newDistro("rhel-9.3")), + expected: common.Must(newDistro("", "rhel-9.3")), }, { strID: "rhel-9.3", - expected: common.Must(newDistro("rhel-9.3")), + expected: common.Must(newDistro("", "rhel-9.3")), }, { strID: "rhel-910", - expected: common.Must(newDistro("rhel-9.10")), + expected: common.Must(newDistro("", "rhel-9.10")), }, { strID: "rhel-9.10", - expected: common.Must(newDistro("rhel-9.10")), + expected: common.Must(newDistro("", "rhel-9.10")), }, { strID: "centos-9", - expected: common.Must(newDistro("centos-9")), + expected: common.Must(newDistro("", "centos-9")), }, { strID: "centos-9.0", @@ -116,7 +116,7 @@ func TestRhel9_DistroFactory(t *testing.T) { for _, tc := range testCases { t.Run(tc.strID, func(t *testing.T) { - d := DistroFactory(tc.strID) + d := DistroFactory("", tc.strID) if tc.expected == nil { assert.Nil(t, d) } else { diff --git a/pkg/distro/generic/rhel9_test.go b/pkg/distro/generic/rhel9_test.go index 292fee835e..792ab0bd33 100644 --- a/pkg/distro/generic/rhel9_test.go +++ b/pkg/distro/generic/rhel9_test.go @@ -17,7 +17,7 @@ import ( var rhel9_FamilyDistros = []rhelFamilyDistro{ { name: "rhel-94", - distro: generic.DistroFactory("rhel-94"), + distro: generic.DistroFactory("", "rhel-94"), }, } @@ -640,7 +640,7 @@ func TestRhel9_DistroFactory(t *testing.T) { testCases := []testCase{ { strID: "rhel-9.6", - expected: generic.DistroFactory("rhel-9.6"), + expected: generic.DistroFactory("", "rhel-9.6"), }, { strID: "rhel-9.6.1", @@ -654,7 +654,7 @@ func TestRhel9_DistroFactory(t *testing.T) { for _, tc := range testCases { t.Run(tc.strID, func(t *testing.T) { - d := generic.DistroFactory(tc.strID) + d := generic.DistroFactory("", tc.strID) if tc.expected == nil { assert.Nil(t, d) } else { diff --git a/pkg/distro/imagetype_test.go b/pkg/distro/imagetype_test.go index 56e252b8af..107b602e09 100644 --- a/pkg/distro/imagetype_test.go +++ b/pkg/distro/imagetype_test.go @@ -16,7 +16,7 @@ func TestManifestRepositoryCustomization(t *testing.T) { var options distro.ImageOptions var repos []rpmmd.RepoConfig - distroFactory := distrofactory.NewDefault() + distroFactory := distrofactory.NewDefault("") for _, distroName := range []string{"fedora-42", "rhel-9.6"} { distro := distroFactory.GetDistro(distroName) arch, err := distro.GetArch("x86_64") diff --git a/pkg/distro/test_distro/distro.go b/pkg/distro/test_distro/distro.go index a9632b8e04..fd63291b1b 100644 --- a/pkg/distro/test_distro/distro.go +++ b/pkg/distro/test_distro/distro.go @@ -401,7 +401,7 @@ func newTestDistro(releasever string) *TestDistro { return &td } -func DistroFactory(idStr string) distro.Distro { +func DistroFactory(defsDir string, idStr string) distro.Distro { id, err := distro.ParseID(idStr) if err != nil { return nil diff --git a/pkg/distro/test_distro/distro_test.go b/pkg/distro/test_distro/distro_test.go index 0b44953bb0..69a67dfc9b 100644 --- a/pkg/distro/test_distro/distro_test.go +++ b/pkg/distro/test_distro/distro_test.go @@ -10,7 +10,7 @@ import ( ) func TestTestDistroGetPipelines(t *testing.T) { - testDistro := test_distro.DistroFactory(test_distro.TestDistro1Name) + testDistro := test_distro.DistroFactory("", test_distro.TestDistro1Name) for _, testArchName := range testDistro.ListArches() { testArch, err := testDistro.GetArch(testArchName) require.NoError(t, err) diff --git a/pkg/distrofactory/distrofactory.go b/pkg/distrofactory/distrofactory.go index d5cdfc586f..a30eeff533 100644 --- a/pkg/distrofactory/distrofactory.go +++ b/pkg/distrofactory/distrofactory.go @@ -13,10 +13,12 @@ import ( // FactoryFunc is a function that returns a distro.Distro for a given distro // represented as a string. If the string does not represent a distro, that can // be detected by the factory, it should return nil. -type FactoryFunc func(idStr string) distro.Distro +type FactoryFunc func(defsDir string, idStr string) distro.Distro // Factory is a list of distro.Distro factories. type Factory struct { + defsDir string + factories []FactoryFunc // distro ID string aliases @@ -28,8 +30,8 @@ type Factory struct { // factories match the given distro ID, it panics. func (f *Factory) getDistro(name string) distro.Distro { var match distro.Distro - for _, f := range f.factories { - if d := f(name); d != nil { + for _, ff := range f.factories { + if d := ff(f.defsDir, name); d != nil { if match != nil { panic(fmt.Sprintf("distro ID was matched by multiple distro factories: %v, %v", match, d)) } @@ -71,10 +73,10 @@ func (f *Factory) RegisterAliases(aliases map[string]string) error { for alias, target := range aliases { var targetExists bool for _, factory := range f.factories { - if factory(alias) != nil { + if factory(f.defsDir, alias) != nil { errors = append(errors, fmt.Sprintf("alias '%s' masks an existing distro", alias)) } - if factory(target) != nil { + if factory(f.defsDir, target) != nil { targetExists = true } } @@ -96,16 +98,18 @@ func (f *Factory) RegisterAliases(aliases map[string]string) error { } // New returns a Factory of distro.Distro factories for the given distros. -func New(factories ...FactoryFunc) *Factory { +func New(defsDir string, factories ...FactoryFunc) *Factory { return &Factory{ + defsDir: defsDir, factories: factories, } } // NewDefault returns a Factory of distro.Distro factories for all supported // distros. -func NewDefault() *Factory { +func NewDefault(defsDir string) *Factory { return New( + defsDir, generic.DistroFactory, bootc.DistroFactory, ) @@ -114,6 +118,7 @@ func NewDefault() *Factory { // NewTestDefault returns a Factory of distro.Distro factory for the test_distro. func NewTestDefault() *Factory { return New( + "", test_distro.DistroFactory, ) } diff --git a/pkg/distrofactory/distrofactory_test.go b/pkg/distrofactory/distrofactory_test.go index 2d031c3250..abf09478d4 100644 --- a/pkg/distrofactory/distrofactory_test.go +++ b/pkg/distrofactory/distrofactory_test.go @@ -63,7 +63,7 @@ func TestGetDistroDefaultList(t *testing.T) { }, } - df := NewDefault() + df := NewDefault("") for _, tc := range testCases { t.Run(tc.strID, func(t *testing.T) { @@ -117,7 +117,7 @@ func TestGetDistroDefaultListWithAliases(t *testing.T) { }, } - df := NewDefault() + df := NewDefault("") for _, tc := range testCases { t.Run(tc.strID, func(t *testing.T) { err := df.RegisterAliases(tc.aliases) diff --git a/pkg/imagefilter/imagefilter_test.go b/pkg/imagefilter/imagefilter_test.go index c2568fc663..c7c978ff78 100644 --- a/pkg/imagefilter/imagefilter_test.go +++ b/pkg/imagefilter/imagefilter_test.go @@ -14,7 +14,7 @@ import ( ) func TestImageFilterSmoke(t *testing.T) { - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") repos, err := testrepos.New() require.NoError(t, err) @@ -26,7 +26,7 @@ func TestImageFilterSmoke(t *testing.T) { } func TestImageFilterSpecificResult(t *testing.T) { - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") repos, err := testrepos.New() require.NoError(t, err) @@ -46,7 +46,7 @@ func TestImageFilterSpecificResult(t *testing.T) { } func TestImageFilterFilter(t *testing.T) { - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") repos, err := testrepos.New() require.NoError(t, err) diff --git a/pkg/manifestgen/manifestgen_test.go b/pkg/manifestgen/manifestgen_test.go index e94c30e461..ce8b73870e 100644 --- a/pkg/manifestgen/manifestgen_test.go +++ b/pkg/manifestgen/manifestgen_test.go @@ -39,7 +39,7 @@ func sha256For(s string) string { func TestManifestGeneratorDepsolve(t *testing.T) { repos, err := testrepos.New() assert.NoError(t, err) - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") filter, err := imagefilter.New(fac, repos) assert.NoError(t, err) @@ -88,7 +88,7 @@ func TestManifestGeneratorWithOstreeCommit(t *testing.T) { repos, err := testrepos.New() assert.NoError(t, err) - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") filter, err := imagefilter.New(fac, repos) assert.NoError(t, err) res, err := filter.Filter("distro:centos-9", "type:edge-ami", "arch:x86_64") @@ -209,7 +209,7 @@ func panicContainerResolver(containerSources map[string][]container.SourceSpec, func TestManifestGeneratorContainers(t *testing.T) { repos, err := testrepos.New() assert.NoError(t, err) - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") filter, err := imagefilter.New(fac, repos) assert.NoError(t, err) @@ -243,7 +243,7 @@ func TestManifestGeneratorContainers(t *testing.T) { func TestManifestGeneratorDepsolveWithSbomWriter(t *testing.T) { repos, err := testrepos.New() assert.NoError(t, err) - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") filter, err := imagefilter.New(fac, repos) assert.NoError(t, err) @@ -285,7 +285,7 @@ func TestManifestGeneratorDepsolveWithSbomWriter(t *testing.T) { func TestManifestGeneratorSeed(t *testing.T) { repos, err := testrepos.New() assert.NoError(t, err) - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") filter, err := imagefilter.New(fac, repos) assert.NoError(t, err) @@ -323,7 +323,7 @@ func TestManifestGeneratorSeed(t *testing.T) { func TestManifestGeneratorDepsolveOutput(t *testing.T) { repos, err := testrepos.New() assert.NoError(t, err) - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") filter, err := imagefilter.New(fac, repos) assert.NoError(t, err) @@ -350,7 +350,7 @@ func TestManifestGeneratorDepsolveOutput(t *testing.T) { func TestManifestGeneratorOverrideRepos(t *testing.T) { repos, err := testrepos.New() assert.NoError(t, err) - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") filter, err := imagefilter.New(fac, repos) assert.NoError(t, err) @@ -390,7 +390,7 @@ func TestManifestGeneratorOverrideRepos(t *testing.T) { func TestManifestGeneratorUseBootstrapContainer(t *testing.T) { repos, err := testrepos.New() assert.NoError(t, err) - fac := distrofactory.NewDefault() + fac := distrofactory.NewDefault("") filter, err := imagefilter.New(fac, repos) assert.NoError(t, err) diff --git a/pkg/reporegistry/reporegistry_test.go b/pkg/reporegistry/reporegistry_test.go index e05257d33f..9eb0b04a0a 100644 --- a/pkg/reporegistry/reporegistry_test.go +++ b/pkg/reporegistry/reporegistry_test.go @@ -13,7 +13,7 @@ import ( ) func getTestingRepoRegistry() *RepoRegistry { - testDistro := test_distro.DistroFactory(test_distro.TestDistro1Name) + testDistro := test_distro.DistroFactory("", test_distro.TestDistro1Name) return &RepoRegistry{ map[string]map[string][]rpmmd.RepoConfig{ testDistro.Name(): { @@ -50,7 +50,7 @@ func getTestingRepoRegistry() *RepoRegistry { func TestReposByImageType_reposByImageTypeName(t *testing.T) { rr := getTestingRepoRegistry() - testDistro := test_distro.DistroFactory(test_distro.TestDistro1Name) + testDistro := test_distro.DistroFactory("", test_distro.TestDistro1Name) ta, _ := testDistro.GetArch(test_distro.TestArchName) ta2, _ := testDistro.GetArch(test_distro.TestArch2Name) @@ -109,7 +109,7 @@ func TestReposByImageType_reposByImageTypeName(t *testing.T) { // TestInvalidreposByImageTypeName tests return values from reposByImageTypeName // for invalid distro name, arch and image type func TestInvalidreposByImageTypeName(t *testing.T) { - testDistro := test_distro.DistroFactory(test_distro.TestDistro1Name) + testDistro := test_distro.DistroFactory("", test_distro.TestDistro1Name) rr := getTestingRepoRegistry() type args struct { @@ -190,7 +190,7 @@ strconv.Atoi: parsing "name": invalid syntax`, func TestReposByArch(t *testing.T) { rr := getTestingRepoRegistry() - testDistro := test_distro.DistroFactory(test_distro.TestDistro1Name) + testDistro := test_distro.DistroFactory("", test_distro.TestDistro1Name) ta, _ := testDistro.GetArch(test_distro.TestArchName) ta2, _ := testDistro.GetArch(test_distro.TestArch2Name) @@ -258,7 +258,7 @@ func TestReposByArch(t *testing.T) { func TestInvalidReposByArch(t *testing.T) { rr := getTestingRepoRegistry() - td := test_distro.DistroFactory(test_distro.TestDistro1Name) + td := test_distro.DistroFactory("", test_distro.TestDistro1Name) repos, err := rr.ReposByArchName(td.Name(), "invalid-arch", false) assert.Nil(t, repos) @@ -272,7 +272,7 @@ func TestInvalidReposByArch(t *testing.T) { // TestInvalidReposByArchName tests return values from ReposByArchName // for invalid distro name and arch func TestInvalidReposByArchName(t *testing.T) { - testDistro := test_distro.DistroFactory(test_distro.TestDistro1Name) + testDistro := test_distro.DistroFactory("", test_distro.TestDistro1Name) rr := getTestingRepoRegistry() type args struct {