diff --git a/docs/user/reference/config/config-file.md b/docs/user/reference/config/config-file.md index 258e9cf2b..433291b50 100644 --- a/docs/user/reference/config/config-file.md +++ b/docs/user/reference/config/config-file.md @@ -15,6 +15,7 @@ All config files share the same schema — there is no distinction between a "ro | `component-groups` | map of objects | Named groups of components with shared defaults | [Component Groups](component-groups.md) | | `images` | map of objects | Image definitions (VMs, containers) | [Images](images.md) | | `tests` | map of objects | Named test definitions (new-shape, parse-only) | [Tests and Test Groups](tests.md) | +| `tmt` | object | Project-wide TMT metadata, such as components with dist-git tests enabled | [Tests — Dist-git TMT Tests](tests.md#dist-git-tmt-tests) | | `test-groups` | map of objects | Named bundles of test references (new-shape, parse-only) | [Tests and Test Groups](tests.md) | | `sku-groups` | map of objects | Named Azure VM size lists for image test fan-out | [Tests — SKU Groups](tests.md#sku-groups) | | `vm-skus` | map of objects | Per-VM-size metadata for external test orchestration | [Tests — VM SKUs](tests.md#vm-skus) | diff --git a/docs/user/reference/config/tests.md b/docs/user/reference/config/tests.md index 6ca7d0d64..641c0fb46 100644 --- a/docs/user/reference/config/tests.md +++ b/docs/user/reference/config/tests.md @@ -78,6 +78,23 @@ plan = "/plans/example" After associating `example-tmt` with a component, run it locally with `azldev component test --image-path --rpm `. +### Dist-git TMT Tests + +The top-level `[tmt.dist-git]` section lists components whose own dist-git TMT +tests are enabled. Listed components need no `[tests.]` entry: the test +source is the component's prepared sources (see +[`azldev component prepare-sources`](../cli/azldev_component_prepare-sources.md)), +and consumers read the list from `azldev config dump`. + +```toml +[tmt.dist-git] +enabled-components = ["bash", "util-linux"] +``` + +| Field | TOML Key | Type | Description | +|-------|----------|------|-------------| +| Enabled components | `enabled-components` | string array | Names of defined components. Entries must be unique across all config files; an undefined component is an error. | + ### Pytest Fields The `[tests..pytest]` subtable configures a pytest run. azldev validates diff --git a/internal/app/azldev/cmds/config/schema_test.go b/internal/app/azldev/cmds/config/schema_test.go index ccdd1a18b..bb34d8247 100644 --- a/internal/app/azldev/cmds/config/schema_test.go +++ b/internal/app/azldev/cmds/config/schema_test.go @@ -103,6 +103,31 @@ func TestSchema_TestGroupMembersAreClosed(t *testing.T) { assert.NotContains(t, props, "group", "test-group members must not expose group") } +func TestSchema_TmtDistGitEnabledComponents(t *testing.T) { + schemaText, err := config.GenerateAzlDevTOMLJSONSchema() + require.NoError(t, err) + + var schema map[string]any + require.NoError(t, json.Unmarshal([]byte(schemaText), &schema)) + defs := mustSchemaMap(t, schema["$defs"], "$defs") + root := mustSchemaMap(t, defs["ConfigFile"], "ConfigFile") + rootProps := mustSchemaMap(t, root["properties"], "ConfigFile properties") + tmt := mustSchemaMap(t, rootProps["tmt"], "tmt") + assert.Equal(t, "#/$defs/TmtDiscoveryConfig", tmt["$ref"]) + discovery := mustSchemaMap(t, defs["TmtDiscoveryConfig"], "TmtDiscoveryConfig") + discoveryProps := mustSchemaMap(t, discovery["properties"], "TmtDiscoveryConfig properties") + distGit := mustSchemaMap(t, discoveryProps["dist-git"], "dist-git") + assert.Equal(t, "#/$defs/TmtDistGitConfig", distGit["$ref"]) + distGitConfig := mustSchemaMap(t, defs["TmtDistGitConfig"], "TmtDistGitConfig") + props := mustSchemaMap(t, distGitConfig["properties"], "TmtDistGitConfig properties") + components := mustSchemaMap(t, props["enabled-components"], "enabled-components") + assert.Equal(t, "array", components["type"]) + assert.Equal(t, true, components["uniqueItems"]) + items := mustSchemaMap(t, components["items"], "enabled-components items") + assert.Equal(t, "string", items["type"]) + assert.Equal(t, false, distGitConfig["additionalProperties"]) +} + // mustSchemaMap asserts that value is a JSON object and returns it. func mustSchemaMap(t *testing.T, value any, name string) map[string]any { t.Helper() diff --git a/internal/projectconfig/configfile.go b/internal/projectconfig/configfile.go index ee6f07c73..2ee186fd8 100644 --- a/internal/projectconfig/configfile.go +++ b/internal/projectconfig/configfile.go @@ -71,6 +71,8 @@ type ConfigFile struct { // Definitions of individual tests (new schema, [tests.X]). Tests map[string]TestDefinition `toml:"tests,omitempty" validate:"dive" jsonschema:"title=Tests,description=Definitions of individual tests"` + Tmt *TmtDiscoveryConfig `toml:"tmt,omitempty" jsonschema:"title=TMT discovery,description=Project-wide TMT discovery metadata"` + // Definitions of test groups (new schema, [test-groups.X]). TestGroups map[string]TestGroup `toml:"test-groups,omitempty" validate:"dive" jsonschema:"title=Test Groups,description=Definitions of named bundles of tests"` @@ -86,6 +88,31 @@ type ConfigFile struct { dir string `toml:"-" validate:"dir"` } +// TmtDiscoveryConfig holds project-wide TMT discovery metadata ([tmt]). +type TmtDiscoveryConfig struct { + DistGit *TmtDistGitConfig `toml:"dist-git,omitempty" json:"distGit,omitempty" jsonschema:"title=Dist-git TMT discovery,description=Opt-in metadata for component-owned dist-git TMT tests"` +} + +// TmtDistGitConfig lists the components whose dist-git TMT tests are enabled ([tmt.dist-git]). +type TmtDistGitConfig struct { + EnabledComponents []string `toml:"enabled-components,omitempty" json:"enabledComponents,omitempty" validate:"unique,dive,required" jsonschema:"title=Enabled components,description=Source component names opted into dist-git TMT discovery,uniqueItems=true"` +} + +// validateTmtDistGit rejects enabled components that are not defined in the project. +func validateTmtDistGit(tmt *TmtDiscoveryConfig, components map[string]ComponentConfig) error { + if tmt == nil || tmt.DistGit == nil { + return nil + } + + for _, name := range tmt.DistGit.EnabledComponents { + if _, ok := components[name]; !ok { + return fmt.Errorf("%w: %#q", ErrUndefinedTmtDistGitComponent, name) + } + } + + return nil +} + // SourcePath returns the absolute path to the config file on disk. func (f ConfigFile) SourcePath() string { return f.sourcePath diff --git a/internal/projectconfig/configfile_test.go b/internal/projectconfig/configfile_test.go index b10216d17..4719f9535 100644 --- a/internal/projectconfig/configfile_test.go +++ b/internal/projectconfig/configfile_test.go @@ -10,6 +10,7 @@ import ( "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils" + "github.com/pelletier/go-toml/v2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -19,6 +20,27 @@ func TestProjectConfigFileValidation_EmptyFile(t *testing.T) { assert.NoError(t, file.Validate()) } +func TestProjectConfigFileValidation_TmtDistGit(t *testing.T) { + var file projectconfig.ConfigFile + + decoder := toml.NewDecoder(bytes.NewBufferString(`[tmt.dist-git] +enabled-components = ["bash", "hostname", "selinux-policy", "util-linux"] +`)).DisallowUnknownFields() + require.NoError(t, decoder.Decode(&file)) + require.NoError(t, file.Validate()) + require.NotNil(t, file.Tmt) + require.NotNil(t, file.Tmt.DistGit) + assert.Equal(t, []string{"bash", "hostname", "selinux-policy", "util-linux"}, file.Tmt.DistGit.EnabledComponents) + + for _, names := range [][]string{{"bash", "bash"}, {""}} { + file.Tmt.DistGit.EnabledComponents = names + require.Error(t, file.Validate()) + } + + file.Tmt.DistGit.EnabledComponents = nil + assert.NoError(t, file.Validate()) +} + func TestProjectConfigFileValidation_TestDefinitionMismatchedSubtable(t *testing.T) { file := projectconfig.ConfigFile{ Tests: map[string]projectconfig.TestDefinition{ diff --git a/internal/projectconfig/loader.go b/internal/projectconfig/loader.go index 79af6ca08..4f95548c8 100644 --- a/internal/projectconfig/loader.go +++ b/internal/projectconfig/loader.go @@ -154,6 +154,8 @@ func mergeConfigFile(resolvedCfg *ProjectConfig, loadedCfg *ConfigFile, options return err } + mergeTmt(resolvedCfg, loadedCfg) + if err := mergeTestGroups(resolvedCfg, loadedCfg); err != nil { return err } @@ -394,6 +396,21 @@ func mergeTests(resolvedCfg *ProjectConfig, loadedCfg *ConfigFile) error { return nil } +// mergeTmt appends the dist-git enabled components from a loaded config file; the +// merged list is checked for duplicates and unknown components during validation. +func mergeTmt(resolvedCfg *ProjectConfig, loadedCfg *ConfigFile) { + if loadedCfg.Tmt == nil || loadedCfg.Tmt.DistGit == nil { + return + } + + if resolvedCfg.Tmt == nil { + resolvedCfg.Tmt = &TmtDiscoveryConfig{DistGit: &TmtDistGitConfig{}} + } + + distGit := resolvedCfg.Tmt.DistGit + distGit.EnabledComponents = append(distGit.EnabledComponents, loadedCfg.Tmt.DistGit.EnabledComponents...) +} + // mergeTestGroups merges named test groups from a loaded config file into the // resolved config. Duplicate group names are not allowed. func mergeTestGroups(resolvedCfg *ProjectConfig, loadedCfg *ConfigFile) error { diff --git a/internal/projectconfig/loader_test.go b/internal/projectconfig/loader_test.go index ec9f4168d..d2fcbfd3b 100644 --- a/internal/projectconfig/loader_test.go +++ b/internal/projectconfig/loader_test.go @@ -1733,3 +1733,60 @@ upstream-distro = { name = "fedora", version = "rawhide" } require.NotNil(t, component.UpstreamCommitConfigFile()) assert.Equal(t, "/project/component.toml", component.UpstreamCommitConfigFile().sourcePath) } + +func TestLoadAndResolveProjectConfig_TmtDistGit(t *testing.T) { + const mainFile = "includes = [\"include.toml\"]\n[components.a]\n[components.b]\n" + + tests := []struct { + name string + main, extra string + wantEnabled []string + wantErr error + wantAnyError bool + }{ + { + name: "merged across files", + main: mainFile + "[tmt.dist-git]\nenabled-components = [\"a\"]\n", + extra: "[tmt.dist-git]\nenabled-components = [\"b\"]\n", + wantEnabled: []string{"a", "b"}, + }, + { + name: "undefined component", + main: mainFile + "[tmt.dist-git]\nenabled-components = [\"missing\"]\n", + extra: "", + wantErr: ErrUndefinedTmtDistGitComponent, + }, + { + name: "duplicate across files", + main: mainFile + "[tmt.dist-git]\nenabled-components = [\"a\"]\n", + extra: "[tmt.dist-git]\nenabled-components = [\"a\"]\n", + wantAnyError: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ctx := testctx.NewCtx() + require.NoError(t, fileutils.MkdirAll(ctx.FS(), "/project")) + require.NoError(t, fileutils.WriteFile(ctx.FS(), testConfigPath, []byte(test.main), fileperms.PrivateFile)) + require.NoError(t, fileutils.WriteFile(ctx.FS(), "/project/include.toml", []byte(test.extra), fileperms.PrivateFile)) + + config, err := loadAndResolveProjectConfig(ctx.FS(), loadOptions{}, testConfigPath) + + if test.wantErr != nil { + require.ErrorIs(t, err, test.wantErr) + + return + } + + if test.wantAnyError { + require.Error(t, err) + + return + } + + require.NoError(t, err) + assert.Equal(t, test.wantEnabled, config.Tmt.DistGit.EnabledComponents) + }) + } +} diff --git a/internal/projectconfig/project.go b/internal/projectconfig/project.go index 605066b80..984a7531c 100644 --- a/internal/projectconfig/project.go +++ b/internal/projectconfig/project.go @@ -48,6 +48,9 @@ type ProjectConfig struct { // Definitions of individual tests. Tests map[string]TestDefinition `toml:"tests,omitempty" json:"tests,omitempty" jsonschema:"title=Tests,description=Mapping of test names to configurations"` + // Project-wide TMT discovery metadata. + Tmt *TmtDiscoveryConfig `toml:"tmt,omitempty" json:"tmt,omitempty" jsonschema:"title=TMT discovery,description=Project-wide TMT discovery metadata"` + // Definitions of named test groups. TestGroups map[string]TestGroup `toml:"test-groups,omitempty" json:"testGroups,omitempty" jsonschema:"title=Test Groups,description=Mapping of test group names to configurations"` @@ -121,6 +124,10 @@ func (cfg *ProjectConfig) validate(withoutLockfile bool) error { return err } + if err := validateTmtDistGit(cfg.Tmt, cfg.Components); err != nil { + return err + } + if err := validateRpmRepos(cfg.Resources.RpmRepos); err != nil { return err } diff --git a/internal/projectconfig/testsuite.go b/internal/projectconfig/testsuite.go index 1d8696070..6b0b77456 100644 --- a/internal/projectconfig/testsuite.go +++ b/internal/projectconfig/testsuite.go @@ -49,6 +49,8 @@ var ( ErrSKUGroupNotAllowed = errors.New("sku-groups not allowed outside image test references") // ErrDuplicateVMSKU is returned when duplicate conflicting [vm-skus] entries are found. ErrDuplicateVMSKU = errors.New("duplicate VM SKU") + // ErrUndefinedTmtDistGitComponent is returned when [tmt.dist-git] enables a component that is not defined. + ErrUndefinedTmtDistGitComponent = errors.New("undefined component in tmt.dist-git.enabled-components") // ErrUnknownTestType is returned for unrecognized test types. ErrUnknownTestType = errors.New("unknown test type") // ErrMissingTestField is returned when a required test config field is missing. diff --git a/scenario/__snapshots__/TestSnapshotsContainer_config_generate-schema_stdout_1.snap b/scenario/__snapshots__/TestSnapshotsContainer_config_generate-schema_stdout_1.snap index 9cf47bd35..aef9dace9 100755 --- a/scenario/__snapshots__/TestSnapshotsContainer_config_generate-schema_stdout_1.snap +++ b/scenario/__snapshots__/TestSnapshotsContainer_config_generate-schema_stdout_1.snap @@ -488,6 +488,11 @@ "title": "Tests", "description": "Definitions of individual tests" }, + "tmt": { + "$ref": "#/$defs/TmtDiscoveryConfig", + "title": "TMT discovery", + "description": "Project-wide TMT discovery metadata" + }, "test-groups": { "additionalProperties": { "$ref": "#/$defs/TestGroup" @@ -1684,6 +1689,32 @@ "additionalProperties": false, "type": "object" }, + "TmtDiscoveryConfig": { + "properties": { + "dist-git": { + "$ref": "#/$defs/TmtDistGitConfig", + "title": "Dist-git TMT discovery", + "description": "Opt-in metadata for component-owned dist-git TMT tests" + } + }, + "additionalProperties": false, + "type": "object" + }, + "TmtDistGitConfig": { + "properties": { + "enabled-components": { + "items": { + "type": "string" + }, + "type": "array", + "uniqueItems": true, + "title": "Enabled components", + "description": "Source component names opted into dist-git TMT discovery" + } + }, + "additionalProperties": false, + "type": "object" + }, "ToolsConfig": { "properties": { "imageCustomizer": { diff --git a/scenario/__snapshots__/TestSnapshots_config_generate-schema_stdout_1.snap b/scenario/__snapshots__/TestSnapshots_config_generate-schema_stdout_1.snap index 9cf47bd35..aef9dace9 100755 --- a/scenario/__snapshots__/TestSnapshots_config_generate-schema_stdout_1.snap +++ b/scenario/__snapshots__/TestSnapshots_config_generate-schema_stdout_1.snap @@ -488,6 +488,11 @@ "title": "Tests", "description": "Definitions of individual tests" }, + "tmt": { + "$ref": "#/$defs/TmtDiscoveryConfig", + "title": "TMT discovery", + "description": "Project-wide TMT discovery metadata" + }, "test-groups": { "additionalProperties": { "$ref": "#/$defs/TestGroup" @@ -1684,6 +1689,32 @@ "additionalProperties": false, "type": "object" }, + "TmtDiscoveryConfig": { + "properties": { + "dist-git": { + "$ref": "#/$defs/TmtDistGitConfig", + "title": "Dist-git TMT discovery", + "description": "Opt-in metadata for component-owned dist-git TMT tests" + } + }, + "additionalProperties": false, + "type": "object" + }, + "TmtDistGitConfig": { + "properties": { + "enabled-components": { + "items": { + "type": "string" + }, + "type": "array", + "uniqueItems": true, + "title": "Enabled components", + "description": "Source component names opted into dist-git TMT discovery" + } + }, + "additionalProperties": false, + "type": "object" + }, "ToolsConfig": { "properties": { "imageCustomizer": { diff --git a/schemas/azldev.schema.json b/schemas/azldev.schema.json index 9cf47bd35..aef9dace9 100644 --- a/schemas/azldev.schema.json +++ b/schemas/azldev.schema.json @@ -488,6 +488,11 @@ "title": "Tests", "description": "Definitions of individual tests" }, + "tmt": { + "$ref": "#/$defs/TmtDiscoveryConfig", + "title": "TMT discovery", + "description": "Project-wide TMT discovery metadata" + }, "test-groups": { "additionalProperties": { "$ref": "#/$defs/TestGroup" @@ -1684,6 +1689,32 @@ "additionalProperties": false, "type": "object" }, + "TmtDiscoveryConfig": { + "properties": { + "dist-git": { + "$ref": "#/$defs/TmtDistGitConfig", + "title": "Dist-git TMT discovery", + "description": "Opt-in metadata for component-owned dist-git TMT tests" + } + }, + "additionalProperties": false, + "type": "object" + }, + "TmtDistGitConfig": { + "properties": { + "enabled-components": { + "items": { + "type": "string" + }, + "type": "array", + "uniqueItems": true, + "title": "Enabled components", + "description": "Source component names opted into dist-git TMT discovery" + } + }, + "additionalProperties": false, + "type": "object" + }, "ToolsConfig": { "properties": { "imageCustomizer": {