-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Expand file tree
/
Copy pathapply_test.go
More file actions
328 lines (286 loc) · 8.25 KB
/
apply_test.go
File metadata and controls
328 lines (286 loc) · 8.25 KB
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
// Copyright IBM Corp. 2014, 2026
// SPDX-License-Identifier: BUSL-1.1
package views
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform/internal/command/arguments"
"github.com/hashicorp/terraform/internal/lang/marks"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/terminal"
"github.com/zclconf/go-cty/cty"
)
// This test is mostly because I am paranoid about having two consecutive
// boolean arguments.
func TestApply_new(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
defer done(t)
v := NewApply(arguments.ViewHuman, false, NewView(streams).SetRunningInAutomation(true))
hv, ok := v.(*ApplyHuman)
if !ok {
t.Fatalf("unexpected return type %t", v)
}
if hv.destroy != false {
t.Fatalf("unexpected destroy value")
}
if hv.inAutomation != true {
t.Fatalf("unexpected inAutomation value")
}
}
// Basic test coverage of Outputs, since most of its functionality is tested
// elsewhere.
func TestApplyHuman_outputs(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
v := NewApply(arguments.ViewHuman, false, NewView(streams))
v.Outputs(map[string]*states.OutputValue{
"foo": {Value: cty.StringVal("secret")},
})
got := done(t).Stdout()
for _, want := range []string{"Outputs:", `foo = "secret"`} {
if !strings.Contains(got, want) {
t.Errorf("wrong result\ngot: %q\nwant: %q", got, want)
}
}
}
// Outputs should do nothing if there are no outputs to render.
func TestApplyHuman_outputsEmpty(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
v := NewApply(arguments.ViewHuman, false, NewView(streams))
v.Outputs(map[string]*states.OutputValue{})
got := done(t).Stdout()
if got != "" {
t.Errorf("output should be empty, but got: %q", got)
}
}
// Ensure that the correct view type and in-automation settings propagate to the
// Operation view.
func TestApplyHuman_operation(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
defer done(t)
v := NewApply(arguments.ViewHuman, false, NewView(streams).SetRunningInAutomation(true)).Operation()
if hv, ok := v.(*OperationHuman); !ok {
t.Fatalf("unexpected return type %t", v)
} else if hv.inAutomation != true {
t.Fatalf("unexpected inAutomation value on Operation view")
}
}
// This view is used for both apply and destroy commands, so the help output
// needs to cover both.
func TestApplyHuman_help(t *testing.T) {
testCases := map[string]bool{
"apply": false,
"destroy": true,
}
for name, destroy := range testCases {
t.Run(name, func(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
v := NewApply(arguments.ViewHuman, destroy, NewView(streams))
v.HelpPrompt()
got := done(t).Stderr()
if !strings.Contains(got, name) {
t.Errorf("wrong result\ngot: %q\nwant: %q", got, name)
}
})
}
}
// Hooks and ResourceCount are tangled up and easiest to test together.
func TestApply_resourceCount(t *testing.T) {
testCases := map[string]struct {
destroy bool
want string
importing bool
}{
"apply": {
false,
"Apply complete! Resources: 1 added, 2 changed, 3 destroyed.",
false,
},
"destroy": {
true,
"Destroy complete! Resources: 3 destroyed.",
false,
},
"import": {
false,
"Apply complete! Resources: 1 imported, 1 added, 2 changed, 3 destroyed.",
true,
},
}
// For compatibility reasons, these tests should hold true for both human
// and JSON output modes
views := []arguments.ViewType{arguments.ViewHuman, arguments.ViewJSON}
for name, tc := range testCases {
for _, viewType := range views {
t.Run(fmt.Sprintf("%s (%s view)", name, viewType), func(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
v := NewApply(viewType, tc.destroy, NewView(streams))
hooks := v.Hooks()
var count *countHook
for _, hook := range hooks {
if ch, ok := hook.(*countHook); ok {
count = ch
}
}
if count == nil {
t.Fatalf("expected Hooks to include a countHook: %#v", hooks)
}
count.Added = 1
count.Changed = 2
count.Removed = 3
if tc.importing {
count.Imported = 1
}
v.ResourceCount("", false)
got := done(t).Stdout()
if !strings.Contains(got, tc.want) {
t.Errorf("wrong result\ngot: %q\nwant: %q", got, tc.want)
}
})
}
}
}
func TestApplyHuman_resourceCountStatePath(t *testing.T) {
testCases := map[string]struct {
added int
changed int
removed int
statePath string
wantContains bool
}{
"default state path": {
added: 1,
changed: 2,
removed: 3,
statePath: "",
wantContains: false,
},
"only removed": {
added: 0,
changed: 0,
removed: 5,
statePath: "foo.tfstate",
wantContains: false,
},
"added": {
added: 5,
changed: 0,
removed: 0,
statePath: "foo.tfstate",
wantContains: true,
},
"changed": {
added: 0,
changed: 5,
removed: 0,
statePath: "foo.tfstate",
wantContains: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
v := NewApply(arguments.ViewHuman, false, NewView(streams))
hooks := v.Hooks()
var count *countHook
for _, hook := range hooks {
if ch, ok := hook.(*countHook); ok {
count = ch
}
}
if count == nil {
t.Fatalf("expected Hooks to include a countHook: %#v", hooks)
}
count.Added = tc.added
count.Changed = tc.changed
count.Removed = tc.removed
v.ResourceCount(tc.statePath, false)
got := done(t).Stdout()
want := "State path: " + tc.statePath
contains := strings.Contains(got, want)
if contains && !tc.wantContains {
t.Errorf("wrong result\ngot: %q\nshould not contain: %q", got, want)
} else if !contains && tc.wantContains {
t.Errorf("wrong result\ngot: %q\nshould contain: %q", got, want)
}
})
}
}
// Basic test coverage of Outputs, since most of its functionality is tested
// elsewhere.
func TestApplyJSON_outputs(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
v := NewApply(arguments.ViewJSON, false, NewView(streams))
v.Outputs(map[string]*states.OutputValue{
"boop_count": {Value: cty.NumberIntVal(92)},
"password": {Value: cty.StringVal("horse-battery").Mark(marks.Sensitive), Sensitive: true},
})
want := []map[string]interface{}{
{
"@level": "info",
"@message": "Outputs: 2",
"@module": "terraform.ui",
"type": "outputs",
"outputs": map[string]interface{}{
"boop_count": map[string]interface{}{
"sensitive": false,
"value": float64(92),
"type": "number",
},
"password": map[string]interface{}{
"sensitive": true,
"type": "string",
},
},
},
}
testJSONViewOutputEquals(t, done(t).Stdout(), want)
}
func TestApplyHuman_resourceCountErrored(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
view := NewView(streams)
view.Configure(&arguments.View{NoColor: false})
v := NewApply(arguments.ViewHuman, false, view)
hooks := v.Hooks()
var count *countHook
for _, hook := range hooks {
if ch, ok := hook.(*countHook); ok {
count = ch
}
}
if count == nil {
t.Fatalf("expected Hooks to include a countHook: %#v", hooks)
}
count.Added = 1
count.Changed = 2
count.Removed = 3
v.ResourceCount("", true)
got := done(t).Stdout()
want := "\x1b[0m\x1b[1m\x1b[31m\nApply incomplete with errors! Resources: 1 added, 2 changed, 3 destroyed."
if !strings.Contains(got, want) {
t.Fatalf("wrong result\ngot: %#q\nwant to contain: %#q", got, want)
}
}
func TestApplyJSON_resourceCountErrored(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
v := NewApply(arguments.ViewJSON, false, NewView(streams))
hooks := v.Hooks()
var count *countHook
for _, hook := range hooks {
if ch, ok := hook.(*countHook); ok {
count = ch
}
}
if count == nil {
t.Fatalf("expected Hooks to include a countHook: %#v", hooks)
}
count.Added = 1
v.ResourceCount("", true)
got := done(t).Stdout()
if !strings.Contains(got, `"@message":"Apply incomplete with errors! Resources: 1 added, 0 changed, 0 destroyed."`) {
t.Fatalf("wrong result\ngot: %q", got)
}
if !strings.Contains(got, `"errored":true`) {
t.Fatalf("expected errored field in json output\ngot: %q", got)
}
}