-
Notifications
You must be signed in to change notification settings - Fork 1
/
tfutils.go
124 lines (110 loc) · 3.38 KB
/
tfutils.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
package setuptest
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
)
type testExecutor interface {
Logger() logger.TestLogger
}
var _ testExecutor = executor{}
type executor struct{}
func (executor) Logger() logger.TestLogger {
l := NewMemoryLogger()
return l
}
// getDefaultTerraformOptions returns the default Terraform options for the
// given directory.
func getDefaultTerraformOptions(t *testing.T, dir string) *terraform.Options {
if !strings.HasSuffix(dir, "/") {
dir += "/"
}
o := terraform.Options{
Logger: logger.Default,
PlanFilePath: "tfplan",
TerraformDir: dir,
Lock: true,
NoColor: true,
Vars: make(map[string]any),
}
return terraform.WithDefaultRetryableErrors(t, &o)
}
// CopyTerraformFolderToTempAndCleanUp sets up a temporary copy of the supplied module folder
// It will return a three values which contains the temporary directory, a cleanup function and an error.
//
// The testdir input is the relative path to the test directory, it can be blank if testing the module directly with variables
// or it can be a relative path to the module directory if testing the module using a subdirectory.
//
// Note: This function will only work if the test directory is in a child subdirectory of the test directory.
// e.g. you cannot use parent paths of the moduleDir.
//
// The depth input is used to determine how many directories to go up to make sure we
// fully clean up.
func CopyTerraformFolderToTempAndCleanUp(t *testing.T, moduleDir string, testDir string) (string, func() error, error) {
tmp := test_structure.CopyTerraformFolderToTemp(t, moduleDir, testDir)
// We normalise, then work out the depth of the test directory relative
// to the test so we know how many/ directories to go up to get to the root.
// We can then delete the right directory when cleaning up.
absTestPath := filepath.Join(moduleDir, testDir)
relPath, err := filepath.Rel(moduleDir, absTestPath)
if err != nil {
err = fmt.Errorf("could not get relative path to test directory: %v", err)
return "", nil, err
}
list := strings.Split(relPath, string(os.PathSeparator))
depth := len(list)
if len(list) > 1 || list[0] != "." {
depth++
}
dir := tmp
for i := 0; i < depth; i++ {
dir = filepath.Dir(dir)
}
f := func() error {
err := os.RemoveAll(dir)
return err
}
return tmp, f, nil
}
// setup performs the copying of the module dirs to a tmp location
// and returns a Response struct and an error.
func setup(t *testing.T, moduleDir, testDir string, prep PrepFunc) (Response, error) {
resp := Response{}
subdir := filepath.Join(moduleDir, testDir)
_, err := os.Stat(subdir)
if os.IsNotExist(err) {
return resp, err
}
resp.t = t
tmp, cleanup, err := CopyTerraformFolderToTempAndCleanUp(t, moduleDir, testDir)
if err != nil {
return resp, err
}
resp.TmpDir = tmp
resp.Options = getDefaultTerraformOptions(t, tmp)
if prep != nil {
err = prep(resp)
if err != nil {
return resp, err
}
}
l := testExecutor(executor{}).Logger()
resp.Options.Logger = logger.New(l)
funcs := []func() error{cleanup}
c, ok := l.(io.Closer)
if ok {
funcs = append(funcs, c.Close)
}
resp.Cleanup = func() {
for _, fn := range funcs {
_ = fn()
}
}
return resp, nil
}