-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigator_run_resource_test.go
373 lines (341 loc) · 12.7 KB
/
navigator_run_resource_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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package provider_test
import (
"path/filepath"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)
const (
navigatorRunResource = "ansible_navigator_run.test"
)
func TestAccNavigatorRun_ansible_options(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "ansible_options")),
ConfigVariables: testDefaultConfigVariables(t),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(navigatorRunResource, "command", regexp.MustCompile("--force-handlers --skip-tags tag1,tag2 --start-at-task task name --limit host1,host2 --tags tag3,tag4")),
),
},
},
})
}
func TestAccNavigatorRun_artifact_queries(t *testing.T) {
t.Parallel()
var resourceCommand, resourceCommandUpdate string
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "artifact_queries")),
ConfigVariables: testDefaultConfigVariables(t),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(navigatorRunResource, "artifact_queries.stdout.result", regexp.MustCompile("ok=3")),
testExtractResourceAttr(navigatorRunResource, "command", &resourceCommand),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue("file_contents", knownvalue.StringExact("acc")),
},
},
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "artifact_queries_update")),
ConfigVariables: testDefaultConfigVariables(t),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectNonEmptyPlan(),
plancheck.ExpectUnknownValue(navigatorRunResource, tfjsonpath.New("artifact_queries").AtMapKey("file_contents").AtMapKey("result")),
plancheck.ExpectUnknownValue(navigatorRunResource, tfjsonpath.New("command")),
},
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(navigatorRunResource, "artifact_queries.stdout.result", regexp.MustCompile("ok=3")),
testExtractResourceAttr(navigatorRunResource, "command", &resourceCommandUpdate),
testCheckAttributeValuesDiffer(&resourceCommand, &resourceCommandUpdate),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue("file_contents", knownvalue.StringExact("acc_update")),
},
},
},
})
}
func TestAccNavigatorRun_basic_binary_path(t *testing.T) { //nolint:paralleltest
testPrependProgramsToPath(t)
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "basic_binary_path")),
ConfigVariables: testDefaultConfigVariables(t),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(navigatorRunResource, "id"),
resource.TestCheckResourceAttrSet(navigatorRunResource, "command"),
resource.TestCheckNoResourceAttr(navigatorRunResource, "ansible_navigator_binary"),
resource.TestCheckNoResourceAttr(navigatorRunResource, "ansible_options"),
resource.TestCheckNoResourceAttr(navigatorRunResource, "triggers"),
resource.TestCheckNoResourceAttr(navigatorRunResource, "replacement_triggers"),
resource.TestCheckNoResourceAttr(navigatorRunResource, "artifact_queries"),
),
},
},
})
}
func TestAccNavigatorRun_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "basic")),
ConfigVariables: testDefaultConfigVariables(t),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(navigatorRunResource, "id"),
resource.TestCheckResourceAttrSet(navigatorRunResource, "command"),
resource.TestCheckNoResourceAttr(navigatorRunResource, "ansible_options"),
resource.TestCheckNoResourceAttr(navigatorRunResource, "triggers"),
resource.TestCheckNoResourceAttr(navigatorRunResource, "replacement_triggers"),
resource.TestCheckNoResourceAttr(navigatorRunResource, "artifact_queries"),
),
},
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "basic")),
ConfigVariables: testDefaultConfigVariables(t),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
},
},
})
}
func TestAccNavigatorRun_env_vars(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "env_vars")),
ConfigVariables: testDefaultConfigVariables(t),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(navigatorRunResource, "id"),
resource.TestCheckResourceAttrSet(navigatorRunResource, "command"),
),
},
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "env_vars_update")),
ConfigVariables: testDefaultConfigVariables(t),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectNonEmptyPlan(),
},
},
},
},
})
}
func TestAccNavigatorRun_private_keys(t *testing.T) {
t.Parallel()
publicKey, privateKey := testSSHKeygen(t)
port := testSSHServer(t, publicKey)
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "private_keys")),
ConfigVariables: testConfigVariables(t, config.Variables{
"private_key_data": config.StringVariable(privateKey),
"ssh_port": config.IntegerVariable(port),
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(navigatorRunResource, "id"),
resource.TestCheckResourceAttrSet(navigatorRunResource, "command"),
),
},
},
})
}
func TestAccNavigatorRun_pull_args(t *testing.T) {
t.Parallel()
arg := "--tls-verify=true"
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "pull_args")),
ConfigVariables: testConfigVariables(t, config.Variables{
"pull_arguments": config.ListVariable(config.StringVariable(arg)),
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(navigatorRunResource, "artifact_queries.pull_args.result", regexp.MustCompile(arg)),
),
},
},
})
}
func TestAccNavigatorRun_relative_binary(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "relative_binary")),
ConfigVariables: testConfigVariables(t, config.Variables{
"working_directory": config.StringVariable(t.TempDir()),
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(navigatorRunResource, "id"),
resource.TestCheckResourceAttrSet(navigatorRunResource, "command"),
),
},
},
})
}
func TestAccNavigatorRun_role(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "role")),
ConfigVariables: testConfigVariables(t, config.Variables{
// https://github.com/hashicorp/terraform-plugin-testing/issues/277
"working_directory": config.StringVariable(filepath.Join("testdata", "navigator_run", "role-working-dir")),
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(navigatorRunResource, "id"),
resource.TestCheckResourceAttrSet(navigatorRunResource, "command"),
),
},
},
})
}
func TestAccNavigatorRun_skip_run(t *testing.T) {
t.Parallel()
var resourceCommand, resourceCommandUpdate string
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "skip_run")),
ConfigVariables: testDefaultConfigVariables(t),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(navigatorRunResource, "id"),
resource.TestCheckResourceAttrSet(navigatorRunResource, "command"),
testExtractResourceAttr(navigatorRunResource, "command", &resourceCommand),
),
},
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "skip_run_update")),
ConfigVariables: testConfigVariables(t, config.Variables{
"ansible_navigator_binary": config.StringVariable(acctest.RandString(8)),
}),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectNonEmptyPlan(),
},
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(navigatorRunResource, "id"),
resource.TestCheckResourceAttrSet(navigatorRunResource, "command"),
testExtractResourceAttr(navigatorRunResource, "command", &resourceCommandUpdate),
testCheckAttributeValuesEqual(&resourceCommand, &resourceCommandUpdate),
),
},
},
})
}
func TestAccNavigatorRun_errors(t *testing.T) {
t.Parallel()
tests := []struct {
name string
variables func(*testing.T) config.Variables
expected *regexp.Regexp
}{
{
name: "artifact_query",
expected: regexp.MustCompile("Not a valid JSONPath expression"),
},
{
name: "env_var_name",
expected: regexp.MustCompile("must not be empty|must consist only of printable ASCII characters"),
},
{
name: "navigator_preflight",
variables: func(t *testing.T) config.Variables { //nolint:thelper
return config.Variables{
"ansible_navigator_binary": config.StringVariable(testLookPath(t, "docker")),
}
},
expected: regexp.MustCompile("Ansible navigator preflight check"),
},
{
name: "playbook_yaml",
expected: regexp.MustCompile("Not valid YAML"),
},
{
name: "playbook",
expected: regexp.MustCompile("Ansible navigator run failed"),
},
{
name: "private_keys",
expected: regexp.MustCompile("Not a SSH private key|Not an unencrypted SSH private key"),
},
{
name: "timeout",
expected: regexp.MustCompile("Ansible navigator run timed out"),
},
{
name: "timezone",
expected: regexp.MustCompile("Not a valid IANA time zone"),
},
{
name: "working_directory",
variables: func(t *testing.T) config.Variables { //nolint:thelper
return config.Variables{
"working_directory": config.StringVariable(filepath.Join(t.TempDir(), "non-existent")),
}
},
expected: regexp.MustCompile("Working directory preflight check"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
variables := testDefaultConfigVariables(t)
if test.variables != nil {
variables = testConfigVariables(t, test.variables(t))
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "errors", test.name)),
ConfigVariables: variables,
ExpectError: test.expected,
},
},
})
})
}
}