-
Notifications
You must be signed in to change notification settings - Fork 108
/
util_test.go
214 lines (176 loc) · 4.46 KB
/
util_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
package e2etest
import (
"context"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-exec/tfexec"
"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)
const testFixtureDir = "testdata"
var (
showMinVersion = version.Must(version.NewVersion("0.12.0"))
providerAddressMinVersion = version.Must(version.NewVersion("0.13.0"))
)
func runTest(t *testing.T, fixtureName string, cb func(t *testing.T, tfVersion *version.Version, tf *tfexec.Terraform)) {
t.Helper()
versions := []string{
testutil.Latest011,
testutil.Latest012,
testutil.Latest013,
testutil.Latest014,
testutil.Latest015,
testutil.Latest_v1,
testutil.Latest_v1_1,
}
if override := os.Getenv("TFEXEC_E2ETEST_VERSIONS"); override != "" {
versions = strings.Split(override, ",")
}
runTestVersions(t, versions, fixtureName, cb)
}
func runTestVersions(t *testing.T, versions []string, fixtureName string, cb func(t *testing.T, tfVersion *version.Version, tf *tfexec.Terraform)) {
t.Helper()
alreadyRunVersions := map[string]bool{}
for _, tfv := range versions {
t.Run(fmt.Sprintf("%s-%s", fixtureName, tfv), func(t *testing.T) {
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
v, err := version.NewVersion(tfv)
if err != nil {
t.Fatal(err)
}
if v.LessThan(version.Must(version.NewVersion("1.0.2"))) {
t.Skipf("Terraform not available for darwin/arm64 < 1.0.2 (%s)", v)
}
}
if alreadyRunVersions[tfv] {
t.Skipf("already run version %q", tfv)
}
alreadyRunVersions[tfv] = true
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("error creating temporary test directory: %s", err)
}
t.Cleanup(func() {
os.RemoveAll(td)
})
// TODO: do this in a cleaner way than string comparison?
var execPath string
switch {
case strings.HasPrefix(tfv, "refs/"):
execPath = tfcache.GitRef(t, tfv)
default:
execPath = tfcache.Version(t, tfv)
}
tf, err := tfexec.NewTerraform(td, execPath)
if err != nil {
t.Fatal(err)
}
tf.SetAppendUserAgent("tfexec-e2etest")
runningVersion, _, err := tf.Version(context.Background(), false)
if err != nil {
t.Fatalf("unable to determine running version (expected %q): %s", tfv, err)
}
if fixtureName != "" {
err = copyFiles(filepath.Join(testFixtureDir, fixtureName), td)
if err != nil {
t.Fatalf("error copying config file into test dir: %s", err)
}
}
var stdouterr strings.Builder
tf.SetStdout(&stdouterr)
tf.SetStderr(&stdouterr)
tf.SetLogger(&testingPrintfer{t})
// TODO: capture panics here?
cb(t, runningVersion, tf)
t.Logf("CLI Output:\n%s", stdouterr.String())
})
}
}
type testingPrintfer struct {
t *testing.T
}
func (t *testingPrintfer) Printf(format string, v ...interface{}) {
t.t.Logf(format, v...)
}
func copyFiles(path string, dstPath string) error {
infos, err := ioutil.ReadDir(path)
if err != nil {
return err
}
for _, info := range infos {
srcPath := filepath.Join(path, info.Name())
if info.IsDir() {
newDir := filepath.Join(dstPath, info.Name())
err = os.MkdirAll(newDir, info.Mode())
if err != nil {
return err
}
err = copyFiles(srcPath, newDir)
if err != nil {
return err
}
} else {
err = copyFile(srcPath, dstPath)
if err != nil {
return err
}
}
}
return nil
}
func copyFile(path string, dstPath string) error {
srcF, err := os.Open(path)
if err != nil {
return err
}
defer srcF.Close()
di, err := os.Stat(dstPath)
if err != nil {
return err
}
if di.IsDir() {
_, file := filepath.Split(path)
dstPath = filepath.Join(dstPath, file)
}
dstF, err := os.Create(dstPath)
if err != nil {
return err
}
defer dstF.Close()
if _, err := io.Copy(dstF, srcF); err != nil {
return err
}
return nil
}
// filesEqual asserts that two files have the same contents.
func textFilesEqual(t *testing.T, expected, actual string) {
eb, err := ioutil.ReadFile(expected)
if err != nil {
t.Fatal(err)
}
ab, err := ioutil.ReadFile(actual)
if err != nil {
t.Fatal(err)
}
es := string(eb)
es = strings.ReplaceAll(es, "\r\n", "\n")
as := string(ab)
as = strings.ReplaceAll(as, "\r\n", "\n")
if as != es {
t.Fatalf("expected:\n%s\n\ngot:\n%s\n", es, as)
}
}
func checkSum(t *testing.T, filename string) uint32 {
b, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
return crc32.ChecksumIEEE(b)
}