/
testing_new_import_state.go
256 lines (215 loc) · 6.51 KB
/
testing_new_import_state.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
package resource
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/davecgh/go-spew/spew"
testing "github.com/mitchellh/go-testing-interface"
"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 testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfg string, providers *providerFactories) error {
t.Helper()
spewConf := spew.NewDefaultConfig()
spewConf.SortKeys = true
if step.ResourceName == "" {
t.Fatal("ResourceName is required for an import state test")
}
// get state from check sequence
var state *terraform.State
var err error
err = runProviderCommand(ctx, t, func() error {
state, err = getState(ctx, t, wd)
if err != nil {
return err
}
return nil
}, wd, providers)
if err != nil {
t.Fatalf("Error getting state: %s", err)
}
// Determine the ID to import
var importId string
switch {
case step.ImportStateIdFunc != nil:
logging.HelperResourceTrace(ctx, "Using TestStep ImportStateIdFunc for import identifier")
var err error
logging.HelperResourceDebug(ctx, "Calling TestStep ImportStateIdFunc")
importId, err = step.ImportStateIdFunc(state)
if err != nil {
t.Fatal(err)
}
logging.HelperResourceDebug(ctx, "Called TestStep ImportStateIdFunc")
case step.ImportStateId != "":
logging.HelperResourceTrace(ctx, "Using TestStep ImportStateId for import identifier")
importId = step.ImportStateId
default:
logging.HelperResourceTrace(ctx, "Using resource identifier for import identifier")
resource, err := testResource(step, state)
if err != nil {
t.Fatal(err)
}
importId = resource.Primary.ID
}
if step.ImportStateIdPrefix != "" {
logging.HelperResourceTrace(ctx, "Prepending TestStep ImportStateIdPrefix for import identifier")
importId = step.ImportStateIdPrefix + importId
}
logging.HelperResourceTrace(ctx, fmt.Sprintf("Using import identifier: %s", importId))
// Create working directory for import tests
if step.Config == "" {
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
step.Config = cfg
if step.Config == "" {
t.Fatal("Cannot import state with no specified config")
}
}
importWd := helper.RequireNewWorkingDir(ctx, t)
defer importWd.Close()
err = importWd.SetConfig(ctx, step.Config)
if err != nil {
t.Fatalf("Error setting test config: %s", err)
}
logging.HelperResourceDebug(ctx, "Running Terraform CLI init and import")
err = runProviderCommand(ctx, t, func() error {
return importWd.Init(ctx)
}, importWd, providers)
if err != nil {
t.Fatalf("Error running init: %s", err)
}
err = runProviderCommand(ctx, t, func() error {
return importWd.Import(ctx, step.ResourceName, importId)
}, importWd, providers)
if err != nil {
return err
}
var importState *terraform.State
err = runProviderCommand(ctx, t, func() error {
importState, err = getState(ctx, t, importWd)
if err != nil {
return err
}
return nil
}, importWd, providers)
if err != nil {
t.Fatalf("Error getting state: %s", err)
}
// Go through the imported state and verify
if step.ImportStateCheck != nil {
logging.HelperResourceTrace(ctx, "Using TestStep ImportStateCheck")
var states []*terraform.InstanceState
for _, r := range importState.RootModule().Resources {
if r.Primary != nil {
is := r.Primary.DeepCopy()
is.Ephemeral.Type = r.Type // otherwise the check function cannot see the type
states = append(states, is)
}
}
logging.HelperResourceDebug(ctx, "Calling TestStep ImportStateCheck")
if err := step.ImportStateCheck(states); err != nil {
t.Fatal(err)
}
logging.HelperResourceDebug(ctx, "Called TestStep ImportStateCheck")
}
// Verify that all the states match
if step.ImportStateVerify {
logging.HelperResourceTrace(ctx, "Using TestStep ImportStateVerify")
newResources := importState.RootModule().Resources
oldResources := state.RootModule().Resources
for _, r := range newResources {
// Find the existing resource
var oldR *terraform.ResourceState
for r2Key, r2 := range oldResources {
// Ensure that we do not match against data sources as they
// cannot be imported and are not what we want to verify.
// Mode is not present in ResourceState so we use the
// stringified ResourceStateKey for comparison.
if strings.HasPrefix(r2Key, "data.") {
continue
}
if r2.Primary != nil && r2.Primary.ID == r.Primary.ID && r2.Type == r.Type && r2.Provider == r.Provider {
oldR = r2
break
}
}
if oldR == nil || oldR.Primary == nil {
t.Fatalf(
"Failed state verification, resource with ID %s not found",
r.Primary.ID)
}
// don't add empty flatmapped containers, so we can more easily
// compare the attributes
skipEmpty := func(k, v string) bool {
if strings.HasSuffix(k, ".#") || strings.HasSuffix(k, ".%") {
if v == "0" {
return true
}
}
return false
}
// Compare their attributes
actual := make(map[string]string)
for k, v := range r.Primary.Attributes {
if skipEmpty(k, v) {
continue
}
actual[k] = v
}
expected := make(map[string]string)
for k, v := range oldR.Primary.Attributes {
if skipEmpty(k, v) {
continue
}
expected[k] = v
}
// Remove fields we're ignoring
for _, v := range step.ImportStateVerifyIgnore {
for k := range actual {
if strings.HasPrefix(k, v) {
delete(actual, k)
}
}
for k := range expected {
if strings.HasPrefix(k, v) {
delete(expected, k)
}
}
}
// timeouts are only _sometimes_ added to state. To
// account for this, just don't compare timeouts at
// all.
for k := range actual {
if strings.HasPrefix(k, "timeouts.") {
delete(actual, k)
}
if k == "timeouts" {
delete(actual, k)
}
}
for k := range expected {
if strings.HasPrefix(k, "timeouts.") {
delete(expected, k)
}
if k == "timeouts" {
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)
}
}
t.Fatalf(
"ImportStateVerify 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
}