diff --git a/command/agent/config_parse_test.go b/command/agent/config_parse_test.go index 6e1aed5a6..c54b7873d 100644 --- a/command/agent/config_parse_test.go +++ b/command/agent/config_parse_test.go @@ -1,7 +1,9 @@ package agent import ( + "fmt" "path/filepath" + "sort" "testing" "time" @@ -670,3 +672,68 @@ func TestConfig_ParseDir(t *testing.T) { require.EqualValues(t, sample1, c) } + +// TestConfig_ParseDir_Matches_IndividualParsing asserts +// that parsing a directory config is the equivalent of +// parsing individual files in any order +func TestConfig_ParseDir_Matches_IndividualParsing(t *testing.T) { + dirConfig, err := LoadConfig("./testdata/sample1") + require.NoError(t, err) + + dirConfig = DefaultConfig().Merge(dirConfig) + + files := []string{ + "testdata/sample1/sample0.json", + "testdata/sample1/sample1.json", + "testdata/sample1/sample2.hcl", + } + + for _, perm := range permutations(files) { + t.Run(fmt.Sprintf("permutation %v", perm), func(t *testing.T) { + config := DefaultConfig() + + for _, f := range perm { + fc, err := LoadConfig(f) + require.NoError(t, err) + + config = config.Merge(fc) + } + + // sort files to get stable view + sort.Strings(config.Files) + sort.Strings(dirConfig.Files) + + require.EqualValues(t, dirConfig, config) + }) + } + +} + +// https://stackoverflow.com/a/30226442 +func permutations(arr []string) [][]string { + var helper func([]string, int) + res := [][]string{} + + helper = func(arr []string, n int) { + if n == 1 { + tmp := make([]string, len(arr)) + copy(tmp, arr) + res = append(res, tmp) + } else { + for i := 0; i < n; i++ { + helper(arr, n-1) + if n%2 == 1 { + tmp := arr[i] + arr[i] = arr[n-1] + arr[n-1] = tmp + } else { + tmp := arr[0] + arr[0] = arr[n-1] + arr[n-1] = tmp + } + } + } + } + helper(arr, len(arr)) + return res +}