Skip to content
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
8 changes: 7 additions & 1 deletion acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions acceptance/bundle/invariant/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
31 changes: 29 additions & 2 deletions acceptance/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
38 changes: 33 additions & 5 deletions acceptance/internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
})
}
Expand Down