forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack_test.go
80 lines (65 loc) · 2.3 KB
/
stack_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package configstack
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/coveooss/terragrunt/v2/config"
"github.com/coveooss/terragrunt/v2/options"
"github.com/coveooss/terragrunt/v2/util"
"github.com/stretchr/testify/assert"
)
func TestFindStackInSubfolders(t *testing.T) {
t.Parallel()
filePaths := []string{
"/stage/data-stores/redis/" + config.DefaultConfigName,
"/stage/data-stores/postgres/" + config.DefaultConfigName,
"/stage/ecs-cluster/" + config.DefaultConfigName,
"/stage/kms-master-key/" + config.DefaultConfigName,
"/stage/vpc/" + config.DefaultConfigName,
}
tempFolder := createTempFolder()
writeDummyTerragruntConfigs(t, tempFolder, filePaths)
envFolder := filepath.ToSlash(util.JoinPath(tempFolder + "/stage"))
terragruntOptions := options.NewTerragruntOptions(envFolder)
terragruntOptions.WorkingDir = envFolder
stack, err := FindStackInSubfolders(terragruntOptions)
if err != nil {
t.Fatalf("Failed when calling method under test: %s\n", err.Error())
}
var modulePaths []string
tempFolder = strings.TrimSuffix(tempFolder, "/")
for _, module := range stack.Modules {
relPath := strings.Replace(module.Path, tempFolder, "", 1)
relPath = filepath.ToSlash(util.JoinPath(relPath, config.DefaultConfigName))
modulePaths = append(modulePaths, relPath)
}
for _, filePath := range filePaths {
filePathFound := util.ListContainsElement(modulePaths, filePath)
assert.True(t, filePathFound, "The filePath %s was not found by Terragrunt.\n", filePath)
}
}
func createTempFolder() string {
return filepath.ToSlash(os.TempDir())
}
// Create a dummy Terragrunt config file at each of the given paths
func writeDummyTerragruntConfigs(t *testing.T, tmpFolder string, paths []string) {
contents := []byte("terraform {\nsource = \"test\"\n}")
for _, path := range paths {
absPath := util.JoinPath(tmpFolder, path)
containingDir := filepath.Dir(absPath)
createDirIfNotExist(t, containingDir)
err := os.WriteFile(absPath, contents, os.ModePerm)
if err != nil {
t.Fatalf("Failed to write file at path %s: %s\n", path, err.Error())
}
}
}
func createDirIfNotExist(t *testing.T, path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
t.Fatalf("Failed to create directory: %s\n", err.Error())
}
}
}