-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathintegration_engine_test.go
269 lines (207 loc) · 11 KB
/
integration_engine_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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//go:build engine
package test_test
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/gruntwork-io/terragrunt/test/helpers"
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testFixtureLocalEngine = "fixtures/engine/local-engine"
testFixtureRemoteEngine = "fixtures/engine/remote-engine"
testFixtureOpenTofuEngine = "fixtures/engine/opentofu-engine"
testFixtureOpenTofuRunAll = "fixtures/engine/opentofu-run-all"
testFixtureOpenTofuLatestRunAll = "fixtures/engine/opentofu-latest-run-all"
envVarExperimental = "TG_EXPERIMENTAL_ENGINE"
)
var LocalEngineBinaryPath = "terragrunt-iac-engine-opentofu_rpc_" + testEngineVersion() + "_" + runtime.GOOS + "_" + runtime.GOARCH
func TestEngineLocalPlan(t *testing.T) {
rootPath := setupLocalEngine(t)
stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt plan --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir %s --terragrunt-log-level trace", rootPath))
require.NoError(t, err)
assert.Contains(t, stderr, LocalEngineBinaryPath)
assert.Contains(t, stderr, "[INFO] plugin process exited:")
assert.Contains(t, stderr, "plugin process exited:")
assert.Contains(t, stdout, "1 to add, 0 to change, 0 to destroy.")
}
func TestEngineLocalApply(t *testing.T) {
rootPath := setupLocalEngine(t)
stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir %s", rootPath))
require.NoError(t, err)
assert.Contains(t, stderr, LocalEngineBinaryPath)
assert.Contains(t, stderr, "[INFO] plugin process exited:")
assert.Contains(t, stderr, "plugin process exited:")
assert.Contains(t, stdout, "Apply complete! Resources: 1 added, 0 changed, 0 destroyed.")
}
func TestEngineOpentofu(t *testing.T) {
t.Setenv(envVarExperimental, "1")
helpers.CleanupTerraformFolder(t, testFixtureOpenTofuEngine)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureOpenTofuEngine)
rootPath := util.JoinPath(tmpEnvPath, testFixtureOpenTofuEngine)
stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir %s", rootPath))
require.NoError(t, err)
assert.Contains(t, stderr, "[INFO] plugin process exited:")
assert.Contains(t, stderr, "plugin process exited:")
assert.Contains(t, stdout, "OpenTofu has been successfully initialized")
assert.Contains(t, stdout, "Apply complete! Resources: 1 added, 0 changed, 0 destroyed.")
}
func TestEngineRunAllOpentofu(t *testing.T) {
t.Setenv(envVarExperimental, "1")
helpers.CleanupTerraformFolder(t, testFixtureOpenTofuRunAll)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureOpenTofuRunAll)
rootPath := util.JoinPath(tmpEnvPath, testFixtureOpenTofuRunAll)
stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir %s", rootPath))
require.NoError(t, err)
assert.Contains(t, stderr, "[INFO] plugin process exited")
assert.Contains(t, stderr, "plugin process exited:")
assert.Contains(t, stdout, "resource \"local_file\" \"test\"")
assert.Contains(t, stdout, "filename = \"./test.txt\"\n")
assert.Contains(t, stdout, "OpenTofu has been successful")
assert.Contains(t, stderr, "Tofu Shutdown completed")
assert.Contains(t, stdout, "Apply complete!")
}
func TestEngineRunAllOpentofuCustomPath(t *testing.T) {
t.Setenv(envVarExperimental, "1")
cacheDir, rootPath := setupEngineCache(t)
stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir %s", rootPath))
require.NoError(t, err)
assert.Contains(t, stderr, "[INFO] plugin process exited:")
assert.Contains(t, stderr, "plugin process exited:")
assert.Contains(t, stdout, "OpenTofu has been successful")
assert.Contains(t, stderr, "Tofu Shutdown completed")
assert.Contains(t, stdout, "Apply complete!")
// check if cache folder is not empty
files, err := os.ReadDir(cacheDir)
require.NoError(t, err)
assert.NotEmpty(t, files)
}
func TestEngineDownloadOverHttp(t *testing.T) {
t.Setenv(envVarExperimental, "1")
helpers.CleanupTerraformFolder(t, testFixtureRemoteEngine)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureRemoteEngine)
rootPath := util.JoinPath(tmpEnvPath, testFixtureRemoteEngine)
platform := runtime.GOOS
arch := runtime.GOARCH
helpers.CopyAndFillMapPlaceholders(t, util.JoinPath(testFixtureRemoteEngine, "terragrunt.hcl"), util.JoinPath(rootPath, config.DefaultTerragruntConfigPath), map[string]string{
"__hardcoded_url__": fmt.Sprintf("https://github.com/gruntwork-io/terragrunt-engine-opentofu/releases/download/v0.0.4/terragrunt-iac-engine-opentofu_rpc_v0.0.4_%s_%s.zip", platform, arch),
})
stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir %s", rootPath))
require.NoError(t, err)
assert.Contains(t, stderr, "[INFO] plugin process exited:")
assert.Contains(t, stderr, "plugin process exited:")
assert.Contains(t, stdout, "OpenTofu has been successfully initialized")
assert.Contains(t, stdout, "Apply complete! Resources: 1 added, 0 changed, 0 destroyed.")
}
func TestEngineChecksumVerification(t *testing.T) {
t.Setenv(envVarExperimental, "1")
cachePath, rootPath := setupEngineCache(t)
_, _, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s", rootPath))
require.NoError(t, err)
// change the checksum of the package file
version := "v0.0.9"
platform := runtime.GOOS
arch := runtime.GOARCH
executablePath := fmt.Sprintf("terragrunt/plugins/iac-engine/rpc/%s/%s/%s/terragrunt-iac-engine-opentofu_rpc_%s_%s_%s", version, platform, arch, version, platform, arch)
fullPath := util.JoinPath(cachePath, executablePath)
// open the file and write some data
file, err := os.OpenFile(fullPath, os.O_APPEND|os.O_WRONLY, 0600)
assert.NoError(t, err)
nonExecutableData := []byte{0x00}
if _, err := file.Write(nonExecutableData); err != nil {
assert.NoError(t, err)
}
assert.NoError(t, file.Close())
_, _, err = helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s", rootPath))
require.Error(t, err)
require.Contains(t, err.Error(), "checksum list has unexpected SHA-256 hash")
}
func TestEngineDisableChecksumCheck(t *testing.T) {
t.Setenv(envVarExperimental, "1")
cachePath, rootPath := setupEngineCache(t)
_, _, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s", rootPath))
require.NoError(t, err)
err = filepath.Walk(cachePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(filepath.Base(path), "_SHA256SUMS") {
// clean checksum list
if err := os.Truncate(path, 0); err != nil {
return err
}
}
return nil
})
require.NoError(t, err)
// create separated directory for new tests
helpers.CleanupTerraformFolder(t, testFixtureOpenTofuRunAll)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureOpenTofuRunAll)
rootPath = util.JoinPath(tmpEnvPath, testFixtureOpenTofuRunAll)
_, _, err = helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s", rootPath))
require.Error(t, err)
require.Contains(t, err.Error(), "verification failure")
// disable checksum check
t.Setenv("TERRAGRUNT_ENGINE_SKIP_CHECK", "1")
_, _, err = helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s", rootPath))
require.NoError(t, err)
}
func TestEngineOpentofuLatestRunAll(t *testing.T) {
t.Setenv(envVarExperimental, "1")
helpers.CleanupTerraformFolder(t, testFixtureOpenTofuLatestRunAll)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureOpenTofuLatestRunAll)
rootPath := util.JoinPath(tmpEnvPath, testFixtureOpenTofuLatestRunAll)
stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir %s", rootPath))
require.NoError(t, err)
assert.Contains(t, stdout, "resource \"local_file\" \"test\"")
assert.Contains(t, stdout, "filename = \"./test.txt\"\n")
assert.Contains(t, stderr, "Tofu Shutdown completed")
assert.Contains(t, stdout, "Apply complete!")
}
func TestEngineLogLevel(t *testing.T) {
t.Setenv(envVarExperimental, "1")
helpers.CleanupTerraformFolder(t, testFixtureOpenTofuLatestRunAll)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureOpenTofuLatestRunAll)
rootPath := util.JoinPath(tmpEnvPath, testFixtureOpenTofuLatestRunAll)
_, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply -no-color -auto-approve --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir %s --terragrunt-log-level trace", rootPath))
require.NoError(t, err)
assert.Contains(t, stderr, "level=debug")
assert.Contains(t, stderr, "[DEBUG] terragrunt-iac-engine-opentofu_rpc")
assert.Contains(t, stderr, "[DEBUG] plugin exited")
}
func setupEngineCache(t *testing.T) (string, string) {
// create temporary folder
cacheDir := t.TempDir()
t.Setenv("TG_ENGINE_CACHE_PATH", cacheDir)
helpers.CleanupTerraformFolder(t, testFixtureOpenTofuRunAll)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureOpenTofuRunAll)
rootPath := util.JoinPath(tmpEnvPath, testFixtureOpenTofuRunAll)
return cacheDir, rootPath
}
func setupLocalEngine(t *testing.T) string {
t.Setenv(envVarExperimental, "1")
helpers.CleanupTerraformFolder(t, testFixtureLocalEngine)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureLocalEngine)
rootPath := util.JoinPath(tmpEnvPath, testFixtureLocalEngine)
// get pwd
pwd, err := os.Getwd()
require.NoError(t, err)
helpers.CopyAndFillMapPlaceholders(t, util.JoinPath(testFixtureLocalEngine, "terragrunt.hcl"), util.JoinPath(rootPath, config.DefaultTerragruntConfigPath), map[string]string{
"__engine_source__": pwd + "/../" + LocalEngineBinaryPath,
})
return rootPath
}
// testEngineVersion returns the version of the engine to be used in the test
func testEngineVersion() string {
value, found := os.LookupEnv("TOFU_ENGINE_VERSION")
if !found {
return "v0.0.1"
}
return value
}