-
Notifications
You must be signed in to change notification settings - Fork 6
/
e2etest.go
144 lines (121 loc) · 4.47 KB
/
e2etest.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
package terraform_module_test_helper
import (
"fmt"
"io"
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/gruntwork-io/terratest/modules/files"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
terratest "github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)
type TestOptions struct {
TerraformOptions terraform.Options
Assertion func(*testing.T, TerraformOutput)
SkipIdempotentCheck bool
SkipDestroy bool
}
var copyLock = &KeyedMutex{}
var initLock = &sync.Mutex{}
type TerraformOutput = map[string]interface{}
type testExecutor interface {
TearDown(t *testing.T, rootDir string, modulePath string)
Logger() logger.TestLogger
}
var _ testExecutor = e2eTestExecutor{}
type e2eTestExecutor struct{}
func (e2eTestExecutor) TearDown(t *testing.T, rootDir string, modulePath string) {
s := SuccessTestVersionSnapshot(rootDir, modulePath)
if t.Failed() {
s = FailedTestVersionSnapshot(rootDir, modulePath, "")
}
require.NoError(t, s.Save(t))
}
func (e2eTestExecutor) Logger() logger.TestLogger {
l := NewMemoryLogger()
return l
}
func RunE2ETest(t *testing.T, moduleRootPath, exampleRelativePath string, option terraform.Options, assertion func(*testing.T, TerraformOutput)) {
initAndApplyAndIdempotentTest(t, moduleRootPath, exampleRelativePath, option, false, false, assertion, e2eTestExecutor{})
}
func RunE2ETestWithOption(t *testing.T, moduleRootPath, exampleRelativePath string, testOption TestOptions) {
initAndApplyAndIdempotentTest(t, moduleRootPath, exampleRelativePath, testOption.TerraformOptions, false, testOption.SkipIdempotentCheck, testOption.Assertion, e2eTestExecutor{})
}
func initAndApplyAndIdempotentTest(t *testing.T, moduleRootPath string, exampleRelativePath string, option terraform.Options, skipDestroy bool, skipCheckIdempotent bool, assertion func(*testing.T, TerraformOutput), executor testExecutor) {
tryParallel(t)
defer executor.TearDown(t, moduleRootPath, exampleRelativePath)
testDir := filepath.Join(moduleRootPath, exampleRelativePath)
logger.Log(t, fmt.Sprintf("===> Starting test for %s, since we're running tests in parallel, the test log will be buffered and output to stdout after the test was finished.", testDir))
tmpDir := copyTerraformFolderToTemp(t, moduleRootPath, exampleRelativePath)
defer func() {
_ = os.RemoveAll(filepath.Clean(tmpDir))
}()
option.TerraformDir = tmpDir
l := executor.Logger()
c, ok := l.(io.Closer)
if ok {
defer func() {
_ = c.Close()
}()
}
option.Logger = logger.New(l)
option = setupRetryLogic(option)
if !skipDestroy {
defer destroy(t, option)
}
initAndApply(t, &option)
var err error
if !skipCheckIdempotent {
err = initAndPlanAndIdempotentAtEasyMode(t, option)
}
require.NoError(t, err)
if assertion != nil {
assertion(t, terraform.OutputAll(t, removeLogger(option)))
}
}
func copyTerraformFolderToTemp(t *testing.T, moduleRootPath string, exampleRelativePath string) string {
unlock := copyLock.Lock(exampleRelativePath)
defer unlock()
tmpDir := test_structure.CopyTerraformFolderToTemp(t, moduleRootPath, exampleRelativePath)
return tmpDir
}
func initAndApply(t terratest.TestingT, options *terraform.Options) string {
tfInit(t, options)
return terraform.Apply(t, options)
}
func tfInit(t terratest.TestingT, options *terraform.Options) {
initLock.Lock()
defer initLock.Unlock()
terraform.Init(t, options)
}
func destroy(t *testing.T, option terraform.Options) {
path := option.TerraformDir
if !files.IsExistingDir(path) || !files.FileExists(filepath.Join(path, "terraform.tfstate")) {
return
}
option.MaxRetries = 5
option.TimeBetweenRetries = time.Minute
option.RetryableTerraformErrors = map[string]string{
".*": "Retry destroy on any error",
}
_, err := terraform.RunTerraformCommandE(t, &option, terraform.FormatArgs(&option, "destroy", "-auto-approve", "-input=false", "-refresh=false")...)
if err != nil {
_, err = terraform.DestroyE(t, &option)
}
require.NoError(t, err)
}
func removeLogger(option terraform.Options) *terraform.Options {
// default logger might leak sensitive data
option.Logger = logger.Discard
return &option
}
func retryableOptions(t *testing.T, options terraform.Options) terraform.Options {
result := terraform.WithDefaultRetryableErrors(t, &options)
result.RetryableTerraformErrors[".*Please try again.*"] = "Service side suggest retry."
return *result
}