Skip to content

Commit ef92c71

Browse files
authored
acc: add EnvVaryOutput (#3438)
## Changes Add new flag to acceptance test: EnvVaryOutput. This allows recording environment specific output in a single test. ## Why In some cases the output of a command is too different between terraform and direct (e.g. everything where an error happens). It is still desirable to have it a single test where common things (configs, recorded API requests) can be compared. Current workaround is to have parent directory with config and common script and 2 tests that copy the config and the script. That involves too much overhead when writing a test. This feature allows having single test but record different outputs as follows: `$CLI bundle deploy &> out.$DATABRICKS_CLI_DEPLOYMENT.txt` The test runner will compare only the variant applicable to current subtest. ## Tests New selftest. Existing tests that could benefit are converted to use this.
1 parent 50797ad commit ef92c71

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+203
-218
lines changed

acceptance/acceptance_test.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,11 @@ func testAccept(t *testing.T, inprocessMode bool, singleTest string) int {
299299
if testdiff.OverwriteMode && len(expanded) > 1 {
300300
// All variants of the test are producing the same output,
301301
// there is no need to run the concurrently when updating.
302-
expanded = expanded[0:1]
302+
// Exception: if EnvVaryOutput is configured with multiple values, we must
303+
// run all variants to record variant-specific outputs.
304+
if config.EnvVaryOutput == nil || len(config.EnvMatrix[*config.EnvVaryOutput]) <= 1 {
305+
expanded = expanded[0:1]
306+
}
303307
}
304308

305309
if len(expanded) == 1 {
@@ -635,8 +639,14 @@ func runTest(t *testing.T,
635639

636640
printedRepls := false
637641

642+
pathFilter := preparePathFilter(config, customEnv)
643+
638644
// Compare expected outputs
639645
for relPath := range outputs {
646+
if shouldSkip(pathFilter, relPath) {
647+
continue
648+
}
649+
640650
skipRepls := false
641651
if relPath == internal.MaterializedConfigFile {
642652
skipRepls = true
@@ -1336,3 +1346,53 @@ func loadUserReplacements(t *testing.T, repls *testdiff.ReplacementsContext, tmp
13361346
repls.SetWithOrder(old, "["+repl+"]", -100)
13371347
}
13381348
}
1349+
1350+
type pathFilter struct {
1351+
// contains substrings from the variants other than current.
1352+
// E.g. if EnvVaryOutput is DATABRICKS_CLI_DEPLOYMENT and current test running DATABRICKS_CLI_DEPLOYMENT="terraform" then
1353+
// notSelected contains ".direct-exp." meaning if filename contains that (e.g. out.deploy.direct-exp.txt) then we ignore it here.
1354+
notSelected []string
1355+
}
1356+
1357+
// preparePathFilter builds filter based on EnvVaryOutput and current variant env.
1358+
func preparePathFilter(config internal.TestConfig, customEnv []string) pathFilter {
1359+
if config.EnvVaryOutput == nil {
1360+
return pathFilter{}
1361+
}
1362+
key := *config.EnvVaryOutput
1363+
vals := config.EnvMatrix[key]
1364+
if len(vals) <= 1 {
1365+
return pathFilter{}
1366+
}
1367+
selected := ""
1368+
for _, kv := range customEnv {
1369+
items := strings.SplitN(kv, "=", 2)
1370+
if len(items) == 2 && items[0] == key {
1371+
selected = items[1]
1372+
break
1373+
}
1374+
}
1375+
if selected == "" {
1376+
return pathFilter{}
1377+
}
1378+
var others []string
1379+
for _, v := range vals {
1380+
if v == selected {
1381+
continue
1382+
}
1383+
others = append(others, "."+v+".")
1384+
}
1385+
return pathFilter{
1386+
notSelected: others,
1387+
}
1388+
}
1389+
1390+
// shouldSkip returns true if the given file belongs to a non-selected variant.
1391+
func shouldSkip(filter pathFilter, relPath string) bool {
1392+
for _, infix := range filter.notSelected {
1393+
if strings.Contains(relPath, infix) {
1394+
return true
1395+
}
1396+
}
1397+
return false
1398+
}
File renamed without changes.

acceptance/bundle/debug/direct/out.test.toml

Lines changed: 0 additions & 5 deletions
This file was deleted.

acceptance/bundle/debug/direct/output.txt

Lines changed: 0 additions & 25 deletions
This file was deleted.

acceptance/bundle/debug/direct/script

Lines changed: 0 additions & 2 deletions
This file was deleted.

acceptance/bundle/debug/direct/test.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

acceptance/bundle/debug/direct/out.stderr.txt renamed to acceptance/bundle/debug/out.stderr.direct-exp.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
>>> [CLI] bundle validate --debug
31
10:07:59 Info: start pid=12345 version=[DEV_VERSION] args="[CLI], bundle, validate, --debug"
42
10:07:59 Debug: Found bundle root at [TEST_TMP_DIR] (file [TEST_TMP_DIR]/databricks.yml) pid=12345
53
10:07:59 Info: Phase: load pid=12345
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Local = true
2+
Cloud = false
3+
4+
[EnvMatrix]
5+
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]

0 commit comments

Comments
 (0)