-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathintegration_windows_test.go
247 lines (201 loc) · 8.46 KB
/
integration_windows_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//go:build windows
// +build windows
package test_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/gruntwork-io/terragrunt/test/helpers"
"github.com/gruntwork-io/terragrunt/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testFixtureDownloadPath = "fixtures/download"
testFixtureLocalRelativeArgsWindowsDownloadPath = "fixtures/download/local-windows"
testFixtureManifestRemoval = "fixtures/manifest-removal"
testFixtureTflintNoIssuesFound = "fixtures/tflint/no-issues-found"
tempDir = `C:\tmp`
)
func TestMain(m *testing.M) {
// By default, t.TempDir() creates a temporary directory inside the user directory
// `C:/Users/circleci/AppData/Local/Temp/`, which ends up exceeding the maximum allowed length
// and causes the error: "fatal: '$GIT_DIR' too big". Example:
// "C:/Users/circleci/AppData/Local/Temp/TestWindowsLocalWithRelativeExtraArgsWindows1263358614/001/fixtures/download/local-windows/.terragrunt-cache/rviFlp3V5mrXldwi6Hbi8p2rDL0/U0tL3quoR7Yt-oR6jROJomrYpTs".
// Solution, shorten the TMP path.
envVars := map[string]string{"TMP": "", "TEMP": ""}
// Save current values to restore them at the end.
for name := range envVars {
envVars[name] = os.Getenv(name)
}
defer func() {
// Restore previous values.
for name, val := range envVars {
os.Setenv(name, val)
}
}()
if _, err := os.Stat(tempDir); os.IsNotExist(err) {
if err := os.Mkdir(tempDir, os.ModePerm); err != nil {
fmt.Printf("Failed to create temp dir due to error: %v", err)
os.Exit(1)
}
} else {
// Verify write permissions
testFile := filepath.Join(tempDir, ".write_test")
if err := os.WriteFile(testFile, []byte(""), 0666); err != nil {
fmt.Printf("Temp dir %s is not writable: %v", tempDir, err)
os.Exit(1)
}
os.Remove(testFile)
}
// Set temporary values.
for name := range envVars {
if err := os.Setenv(name, tempDir); err != nil {
fmt.Printf("Failed to set env var %s=%s due to error: %v", name, tempDir, err)
os.Exit(1)
}
}
os.Exit(m.Run())
}
func TestWindowsLocalWithRelativeExtraArgsWindows(t *testing.T) {
t.Parallel()
rootPath := helpers.CopyEnvironment(t, testFixtureDownloadPath)
modulePath := util.JoinPath(rootPath, testFixtureLocalRelativeArgsWindowsDownloadPath)
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s", modulePath))
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s", modulePath))
}
// TestWindowsTerragruntSourceMapDebug copies the test/fixtures/source-map directory to a new Windows path
// and then ensures that the TERRAGRUNT_SOURCE_MAP env var can be used to swap out git sources for local modules
func TestWindowsTerragruntSourceMapDebug(t *testing.T) {
testCases := []struct {
name string
}{
{
name: "multiple-match",
},
{
name: "multiple-with-dependency",
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
fixtureSourceMapPath := "fixtures/source-map"
helpers.CleanupTerraformFolder(t, fixtureSourceMapPath)
targetPath := "C:\\test\\infrastructure-modules/"
CopyEnvironmentToPath(t, fixtureSourceMapPath, targetPath)
rootPath := filepath.Join(targetPath, fixtureSourceMapPath)
t.Setenv(
"TERRAGRUNT_SOURCE_MAP",
strings.Join(
[]string{
fmt.Sprintf("git::ssh://[email protected]/gruntwork-io/i-dont-exist.git=%s", targetPath),
fmt.Sprintf("git::ssh://[email protected]/gruntwork-io/another-dont-exist.git=%s", targetPath),
},
",",
),
)
tgPath := filepath.Join(rootPath, testCase.name)
tgArgs := fmt.Sprintf("terragrunt run-all apply -auto-approve --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir %s", tgPath)
helpers.RunTerragrunt(t, tgArgs)
})
}
}
func TestWindowsTflintIsInvoked(t *testing.T) {
out := new(bytes.Buffer)
errOut := new(bytes.Buffer)
rootPath := CopyEnvironmentWithTflint(t, testFixtureTflintNoIssuesFound)
modulePath := util.JoinPath(rootPath, testFixtureTflintNoIssuesFound)
err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt plan --terragrunt-log-level trace --terragrunt-working-dir %s", modulePath), out, errOut)
assert.NoError(t, err)
assert.NotContains(t, errOut.String(), "Error while running tflint with args:")
assert.NotContains(t, errOut.String(), "Tflint found issues in the project. Check for the tflint logs above.")
found, err := regexp.MatchString(fmt.Sprintf("--config %s/.terragrunt-cache/.*/.tflint.hcl", modulePath), errOut.String())
assert.NoError(t, err)
assert.True(t, found)
}
func TestWindowsManifestFileIsRemoved(t *testing.T) {
out := new(bytes.Buffer)
errOut := new(bytes.Buffer)
rootPath := CopyEnvironmentWithTflint(t, testFixtureManifestRemoval)
modulePath := util.JoinPath(rootPath, testFixtureManifestRemoval, "app")
err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt plan --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s", modulePath), out, errOut)
assert.NoError(t, err)
info1, err := fileInfo(modulePath, ".terragrunt-module-manifest")
assert.NoError(t, err)
assert.NotNil(t, info1)
out = new(bytes.Buffer)
errOut = new(bytes.Buffer)
err = helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt plan --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s", modulePath), out, errOut)
assert.NoError(t, err)
info2, err := fileInfo(modulePath, ".terragrunt-module-manifest")
assert.NoError(t, err)
assert.NotNil(t, info2)
// ensure that .terragrunt-module-manifest was recreated
assert.True(t, (*info2).ModTime().After((*info1).ModTime()))
}
func fileInfo(path, fileName string) (*os.FileInfo, error) {
var fileInfo *os.FileInfo
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if fileInfo != nil {
return nil
}
if err != nil {
return err
}
if !info.IsDir() && info.Name() == fileName {
fileInfo = &info
return nil
}
return nil
})
return fileInfo, err
}
func TestWindowsFindParent(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureFindParent)
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt run-all plan --terragrunt-non-interactive --terragrunt-working-dir %s", testFixtureFindParent))
// second run shouldn't fail with find_in_parent_folders("root.hcl") issue
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s", testFixtureFindParent))
}
func TestWindowsScaffold(t *testing.T) {
t.Parallel()
// create temp dir
tmpDir, err := os.MkdirTemp("", "terragrunt-test")
assert.NoError(t, err)
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt scaffold github.com/gruntwork-io/terragrunt-infrastructure-modules-example//modules/mysql --terragrunt-working-dir %s", tmpDir))
// check that terragrunt.hcl was created
_, err = os.Stat(filepath.Join(tmpDir, "terragrunt.hcl"))
assert.NoError(t, err)
}
func TestWindowsScaffoldRef(t *testing.T) {
t.Parallel()
// create temp dir
tmpDir, err := os.MkdirTemp("", "terragrunt-test")
assert.NoError(t, err)
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt scaffold github.com/gruntwork-io/terragrunt-infrastructure-modules-example//modules/mysql?ref=v0.8.1 --terragrunt-working-dir %s", tmpDir))
// check that terragrunt.hcl was created
_, err = os.Stat(filepath.Join(tmpDir, "terragrunt.hcl"))
assert.NoError(t, err)
}
func CopyEnvironmentToPath(t *testing.T, environmentPath, targetPath string) {
if err := os.MkdirAll(targetPath, 0777); err != nil {
t.Fatalf("Failed to create temp dir %s due to error %v", targetPath, err)
}
copyErr := util.CopyFolderContents(createLogger(), environmentPath, util.JoinPath(targetPath, environmentPath), ".terragrunt-test", nil, nil)
require.NoError(t, copyErr)
}
func CopyEnvironmentWithTflint(t *testing.T, environmentPath string) string {
tmpDir, err := os.MkdirTemp("", "terragrunt-test")
if err != nil {
t.Fatalf("Failed to create temp dir due to error: %v", err)
}
t.Logf("Copying %s to %s", environmentPath, tmpDir)
require.NoError(t, util.CopyFolderContents(createLogger(), environmentPath, util.JoinPath(tmpDir, environmentPath), ".terragrunt-test", []string{".tflint.hcl"}, []string{}))
return tmpDir
}