/
testing_new.go
328 lines (274 loc) · 8.93 KB
/
testing_new.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package resource
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/davecgh/go-spew/spew"
tfjson "github.com/hashicorp/terraform-json"
testing "github.com/mitchellh/go-testing-interface"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func runPostTestDestroy(ctx context.Context, t testing.T, c TestCase, wd *plugintest.WorkingDir, factories map[string]func() (*schema.Provider, error), v5factories map[string]func() (tfprotov5.ProviderServer, error), v6factories map[string]func() (tfprotov6.ProviderServer, error), statePreDestroy *terraform.State) error {
t.Helper()
err := runProviderCommand(ctx, t, func() error {
return wd.Destroy(ctx)
}, wd, providerFactories{
legacy: factories,
protov5: v5factories,
protov6: v6factories})
if err != nil {
return err
}
if c.CheckDestroy != nil {
logging.HelperResourceTrace(ctx, "Using TestCase CheckDestroy")
logging.HelperResourceDebug(ctx, "Calling TestCase CheckDestroy")
if err := c.CheckDestroy(statePreDestroy); err != nil {
return err
}
logging.HelperResourceDebug(ctx, "Called TestCase CheckDestroy")
}
return nil
}
func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest.Helper) {
t.Helper()
spewConf := spew.NewDefaultConfig()
spewConf.SortKeys = true
wd := helper.RequireNewWorkingDir(ctx, t)
ctx = logging.TestTerraformPathContext(ctx, wd.GetHelper().TerraformExecPath())
ctx = logging.TestWorkingDirectoryContext(ctx, wd.GetHelper().WorkingDirectory())
defer func() {
var statePreDestroy *terraform.State
var err error
err = runProviderCommand(ctx, t, func() error {
statePreDestroy, err = getState(ctx, t, wd)
if err != nil {
return err
}
return nil
}, wd, providerFactories{
legacy: c.ProviderFactories,
protov5: c.ProtoV5ProviderFactories,
protov6: c.ProtoV6ProviderFactories})
if err != nil {
t.Fatalf("Error retrieving state, there may be dangling resources: %s", err.Error())
return
}
if !stateIsEmpty(statePreDestroy) {
err := runPostTestDestroy(ctx, t, c, wd, c.ProviderFactories, c.ProtoV5ProviderFactories, c.ProtoV6ProviderFactories, statePreDestroy)
if err != nil {
t.Fatalf("Error running post-test destroy, there may be dangling resources: %s", err.Error())
}
}
wd.Close()
}()
providerCfg, err := testProviderConfig(c)
if err != nil {
t.Fatal(err)
}
err = wd.SetConfig(ctx, providerCfg)
if err != nil {
t.Fatalf("Error setting test config: %s", err)
}
err = runProviderCommand(ctx, t, func() error {
return wd.Init(ctx)
}, wd, providerFactories{
legacy: c.ProviderFactories,
protov5: c.ProtoV5ProviderFactories,
protov6: c.ProtoV6ProviderFactories})
if err != nil {
t.Fatalf("Error running init: %s", err.Error())
return
}
logging.HelperResourceDebug(ctx, "Starting TestSteps")
// use this to track last step succesfully applied
// acts as default for import tests
var appliedCfg string
for i, step := range c.Steps {
ctx = logging.TestStepNumberContext(ctx, i+1)
logging.HelperResourceDebug(ctx, "Starting TestStep")
if step.PreConfig != nil {
logging.HelperResourceDebug(ctx, "Calling TestStep PreConfig")
step.PreConfig()
logging.HelperResourceDebug(ctx, "Called TestStep PreConfig")
}
if step.SkipFunc != nil {
logging.HelperResourceDebug(ctx, "Calling TestStep SkipFunc")
skip, err := step.SkipFunc()
if err != nil {
t.Fatal(err)
}
logging.HelperResourceDebug(ctx, "Called TestStep SkipFunc")
if skip {
t.Logf("Skipping step %d/%d due to SkipFunc", i+1, len(c.Steps))
logging.HelperResourceWarn(ctx, "Skipping TestStep due to SkipFunc")
continue
}
}
if step.ImportState {
logging.HelperResourceTrace(ctx, "TestStep is ImportState mode")
err := testStepNewImportState(ctx, t, c, helper, wd, step, appliedCfg)
if step.ExpectError != nil {
logging.HelperResourceDebug(ctx, "Checking TestStep ExpectError")
if err == nil {
t.Fatalf("Step %d/%d error running import: expected an error but got none", i+1, len(c.Steps))
}
if !step.ExpectError.MatchString(err.Error()) {
t.Fatalf("Step %d/%d error running import, expected an error with pattern (%s), no match on: %s", i+1, len(c.Steps), step.ExpectError.String(), err)
}
} else {
if err != nil && c.ErrorCheck != nil {
logging.HelperResourceDebug(ctx, "Calling TestCase ErrorCheck")
err = c.ErrorCheck(err)
}
if err != nil {
t.Fatalf("Step %d/%d error running import: %s", i+1, len(c.Steps), err)
}
}
logging.HelperResourceDebug(ctx, "Finished TestStep")
continue
}
if step.Config != "" {
logging.HelperResourceTrace(ctx, "TestStep is Config mode")
err := testStepNewConfig(ctx, t, c, wd, step)
if step.ExpectError != nil {
logging.HelperResourceDebug(ctx, "Checking TestStep ExpectError")
if err == nil {
t.Fatalf("Step %d/%d, expected an error but got none", i+1, len(c.Steps))
}
if !step.ExpectError.MatchString(err.Error()) {
t.Fatalf("Step %d/%d, expected an error with pattern, no match on: %s", i+1, len(c.Steps), err)
}
} else {
if err != nil && c.ErrorCheck != nil {
logging.HelperResourceDebug(ctx, "Calling TestCase ErrorCheck")
err = c.ErrorCheck(err)
logging.HelperResourceDebug(ctx, "Called TestCase ErrorCheck")
}
if err != nil {
t.Fatalf("Step %d/%d error: %s", i+1, len(c.Steps), err)
}
}
appliedCfg = step.Config
logging.HelperResourceDebug(ctx, "Finished TestStep")
continue
}
t.Fatalf("Step %d/%d, unsupported test mode", i+1, len(c.Steps))
}
}
func getState(ctx context.Context, t testing.T, wd *plugintest.WorkingDir) (*terraform.State, error) {
t.Helper()
jsonState, err := wd.State(ctx)
if err != nil {
return nil, err
}
state, err := shimStateFromJson(jsonState)
if err != nil {
t.Fatal(err)
}
return state, nil
}
func stateIsEmpty(state *terraform.State) bool {
return state.Empty() || !state.HasResources()
}
func planIsEmpty(plan *tfjson.Plan) bool {
for _, rc := range plan.ResourceChanges {
for _, a := range rc.Change.Actions {
if a != tfjson.ActionNoop {
return false
}
}
}
return true
}
func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest.WorkingDir, step TestStep, r *terraform.ResourceState) error {
t.Helper()
spewConf := spew.NewDefaultConfig()
spewConf.SortKeys = true
// Build the state. The state is just the resource with an ID. There
// are no attributes. We only set what is needed to perform a refresh.
state := terraform.NewState()
state.RootModule().Resources = make(map[string]*terraform.ResourceState)
state.RootModule().Resources[c.IDRefreshName] = &terraform.ResourceState{}
// Temporarily set the config to a minimal provider config for the refresh
// test. After the refresh we can reset it.
cfg, err := testProviderConfig(c)
if err != nil {
return err
}
err = wd.SetConfig(ctx, cfg)
if err != nil {
t.Fatalf("Error setting import test config: %s", err)
}
defer func() {
err = wd.SetConfig(ctx, step.Config)
if err != nil {
t.Fatalf("Error resetting test config: %s", err)
}
}()
// Refresh!
err = runProviderCommand(ctx, t, func() error {
err = wd.Refresh(ctx)
if err != nil {
t.Fatalf("Error running terraform refresh: %s", err)
}
state, err = getState(ctx, t, wd)
if err != nil {
return err
}
return nil
}, wd, providerFactories{
legacy: c.ProviderFactories,
protov5: c.ProtoV5ProviderFactories,
protov6: c.ProtoV6ProviderFactories})
if err != nil {
return err
}
// Verify attribute equivalence.
actualR := state.RootModule().Resources[c.IDRefreshName]
if actualR == nil {
return fmt.Errorf("Resource gone!")
}
if actualR.Primary == nil {
return fmt.Errorf("Resource has no primary instance")
}
actual := actualR.Primary.Attributes
expected := r.Primary.Attributes
if len(c.IDRefreshIgnore) > 0 {
logging.HelperResourceTrace(ctx, fmt.Sprintf("Using TestCase IDRefreshIgnore: %v", c.IDRefreshIgnore))
}
// Remove fields we're ignoring
for _, v := range c.IDRefreshIgnore {
for k := range actual {
if strings.HasPrefix(k, v) {
delete(actual, k)
}
}
for k := range expected {
if strings.HasPrefix(k, v) {
delete(expected, k)
}
}
}
if !reflect.DeepEqual(actual, expected) {
// Determine only the different attributes
for k, v := range expected {
if av, ok := actual[k]; ok && v == av {
delete(expected, k)
delete(actual, k)
}
}
spewConf := spew.NewDefaultConfig()
spewConf.SortKeys = true
return fmt.Errorf(
"Attributes not equivalent. Difference is shown below. Top is actual, bottom is expected."+
"\n\n%s\n\n%s",
spewConf.Sdump(actual), spewConf.Sdump(expected))
}
return nil
}