diff --git a/acceptance/acceptance_test.go b/acceptance/acceptance_test.go index 07a4fc1763..0c5b43ed66 100644 --- a/acceptance/acceptance_test.go +++ b/acceptance/acceptance_test.go @@ -337,7 +337,13 @@ func testAccept(t *testing.T, inprocessMode bool, singleTest string) int { t.Parallel() } - expanded := internal.ExpandEnvMatrix(config.EnvMatrix, config.EnvMatrixExclude) + // Build extra vars for exclusion matching (config state as env vars) + var extraVars []string + if cloudEnv != "" { + extraVars = append(extraVars, "CONFIG_Cloud=true") + } + + expanded := internal.ExpandEnvMatrix(config.EnvMatrix, config.EnvMatrixExclude, extraVars) if len(expanded) == 1 { // env vars aren't part of the test case name, so log them for debugging diff --git a/acceptance/bundle/invariant/test.toml b/acceptance/bundle/invariant/test.toml index 01c885b02e..93282bda60 100644 --- a/acceptance/bundle/invariant/test.toml +++ b/acceptance/bundle/invariant/test.toml @@ -36,6 +36,9 @@ EnvMatrix.INPUT_CONFIG = [ "volume.yml.tmpl", ] +[EnvMatrixExclude] +no_alert_on_cloud = ["CONFIG_Cloud=true", "INPUT_CONFIG=alert.yml.tmpl"] + # Fake SQL endpoint for local tests [[Server]] Pattern = "POST /api/2.0/sql/statements/" diff --git a/acceptance/internal/config.go b/acceptance/internal/config.go index afa13a0d8e..fbf1ce3807 100644 --- a/acceptance/internal/config.go +++ b/acceptance/internal/config.go @@ -102,9 +102,10 @@ type TestConfig struct { // Environment variables matrix exclusion list. // Exclude certain configuration from the grid generated by EnvMatrix. - // They key is arbitrary string and the value is a list of KEY=value pairs to exclude. + // The key is arbitrary string and the value is a list of KEY=value pairs to exclude. // For example: // EnvMatrixExclude.noplantf = ["READPLAN=1", "DATABRICKS_BUNDLE_ENGINE=terraform"] + // Special pseudo-var CONFIG_Cloud=true is available for excluding combinations on cloud runs. EnvMatrixExclude map[string][]string // List of keys for which to do string replacement value -> [KEY]. If not set, defaults to true. @@ -262,7 +263,9 @@ func (t mapTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Valu // // If any entries is an empty list, that variable is dropped from the matrix before processing. // The exclude parameter specifies combinations to exclude from the result. -func ExpandEnvMatrix(matrix, exclude map[string][]string) [][]string { +// The extraVars parameter specifies additional env vars to include for exclusion matching only; +// they are stripped from the final result. +func ExpandEnvMatrix(matrix, exclude map[string][]string, extraVars []string) [][]string { result := [][]string{{}} if len(matrix) == 0 { @@ -307,11 +310,35 @@ func ExpandEnvMatrix(matrix, exclude map[string][]string) [][]string { result = newResult } + // Add extraVars to each envset for exclusion matching + if len(extraVars) > 0 { + for i := range result { + result[i] = append(result[i], extraVars...) + } + } + // Filter out excluded combinations if len(exclude) > 0 { result = filterExcludedEnvSets(result, exclude) } + // Strip extraVars from the final result + if len(extraVars) > 0 { + extraSet := make(map[string]bool, len(extraVars)) + for _, v := range extraVars { + extraSet[v] = true + } + for i, envset := range result { + filtered := envset[:0] + for _, kv := range envset { + if !extraSet[kv] { + filtered = append(filtered, kv) + } + } + result[i] = filtered + } + } + return result } diff --git a/acceptance/internal/config_test.go b/acceptance/internal/config_test.go index ea06335c5d..c2701337f5 100644 --- a/acceptance/internal/config_test.go +++ b/acceptance/internal/config_test.go @@ -8,10 +8,11 @@ import ( func TestExpandEnvMatrix(t *testing.T) { tests := []struct { - name string - matrix map[string][]string - exclude map[string][]string - expected [][]string + name string + matrix map[string][]string + exclude map[string][]string + extraVars []string + expected [][]string }{ { name: "empty matrix", @@ -160,11 +161,38 @@ func TestExpandEnvMatrix(t *testing.T) { {"KEY1=A"}, }, }, + { + name: "extraVars used for exclusion matching but stripped from result", + matrix: map[string][]string{ + "KEY": {"A", "B"}, + }, + exclude: map[string][]string{ + "rule1": {"KEY=A", "CONFIG_Cloud=true"}, + }, + extraVars: []string{"CONFIG_Cloud=true"}, + expected: [][]string{ + {"KEY=B"}, + }, + }, + { + name: "extraVars not matching exclusion rule", + matrix: map[string][]string{ + "KEY": {"A", "B"}, + }, + exclude: map[string][]string{ + "rule1": {"KEY=A", "CONFIG_Cloud=true"}, + }, + extraVars: nil, + expected: [][]string{ + {"KEY=A"}, + {"KEY=B"}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result := ExpandEnvMatrix(tt.matrix, tt.exclude) + result := ExpandEnvMatrix(tt.matrix, tt.exclude, tt.extraVars) assert.Equal(t, tt.expected, result) }) }