-
Notifications
You must be signed in to change notification settings - Fork 31
/
playbook_test.go
357 lines (268 loc) · 7.8 KB
/
playbook_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
package tachyon
import (
"path/filepath"
"testing"
"time"
)
func TestSimplePlaybook(t *testing.T) {
env := NewEnv(NewNestedScope(nil), DefaultConfig)
p, err := NewPlaybook(env, "test/playbook1.yml")
if err != nil {
panic(err)
}
if len(p.Plays) != 2 {
t.Fatalf("Didn't load 2 playbooks, loaded: %d", len(p.Plays))
}
x := p.Plays[1]
if x.Hosts != "all" {
t.Errorf("Hosts not all: was %s", x.Hosts)
}
vars := x.Vars
a, ok := vars.Get("answer")
if !ok {
t.Fatalf("No var 'answer'")
}
if a.Read() != "Wuh, I think so" {
t.Errorf("Unable to decode string var: %#v", a)
}
a, ok = vars.Get("port")
if !ok {
t.Fatalf("No var 'port'")
}
if a.Read() != 5150 {
t.Errorf("Unable to decode numeric var: %#v", a.Read())
}
if len(x.VarsFiles) != 2 {
t.Fatalf("Unable to decode varsfiles, got %d", len(x.VarsFiles))
}
f := x.VarsFiles[0]
if f != "common_vars.yml" {
t.Errorf("Unable to decode literal vars_files")
}
f2 := x.VarsFiles[1].([]interface{})
if f2[1].(string) != "default_os.yml" {
t.Errorf("Unable to decode list vars_files")
}
tasks := x.Tasks
if len(tasks) < 5 {
t.Errorf("Failed to decode the proper number of tasks: %d", len(tasks))
}
if tasks[3].Args() != "echo {{port}}" {
t.Errorf("Failed to decode templating in action: %#v", tasks[3].Args())
}
}
func totalRuntime(results []RunResult) time.Duration {
cur := time.Duration(0)
for _, res := range results {
cur += res.Runtime
}
return cur
}
func TestPlaybookFuturesRunInParallel(t *testing.T) {
run, _, err := RunCapture("test/future.yml")
if err != nil {
t.Fatalf("Unable to load test/future.yml")
}
total := run.Runtime.Seconds()
if total > 1.1 || total < 0.9 {
t.Errorf("Futures did not run in parallel: %f", total)
}
}
func TestPlaybookFuturesCanBeWaitedOn(t *testing.T) {
run, _, err := RunCapture("test/future.yml")
if err != nil {
t.Fatalf("Unable to load test/future.yml")
}
total := run.Runtime.Seconds()
if total > 1.1 || total < 0.9 {
t.Errorf("Futures did not run in parallel: %f", total)
}
}
func TestPlaybookTaskIncludes(t *testing.T) {
res, _, err := RunCapture("test/inc_parent.yml")
if err != nil {
t.Fatalf("Unable to run test/inc_parent.yml")
}
if filepath.Base(res.Results[0].Task.File) != "inc_child.yml" {
t.Fatalf("Did not include tasks from child")
}
}
func TestPlaybookTaskIncludesCanHaveVars(t *testing.T) {
res, _, err := RunCapture("test/inc_parent2.yml")
if err != nil {
t.Fatalf("Unable to run test/inc_parent2.yml: %s", err)
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "oscar" {
t.Fatalf("A variable was not passed into the included file")
}
d = res.Results[1].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "ellen" {
t.Fatalf("A variable was not passed into the included file")
}
d = res.Results[2].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "Los Angeles" {
t.Fatalf("A variable was not passed into the included file")
}
}
func TestPlaybookRoleTasksInclude(t *testing.T) {
res, _, err := RunCapture("test/site1.yml")
if err != nil {
t.Fatalf("Unable to run test/site1.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "in role" {
t.Fatalf("Task did not run from role")
}
}
func TestPlaybookRoleHandlersInclude(t *testing.T) {
res, _, err := RunCapture("test/site1.yml")
if err != nil {
t.Fatalf("Unable to run test/site1.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[1].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "in role handler" {
t.Fatalf("Task did not run from role")
}
}
func TestPlaybookRoleVarsInclude(t *testing.T) {
res, _, err := RunCapture("test/site2.yml")
if err != nil {
t.Fatalf("Unable to run test/site2.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "from role var" {
t.Fatalf("Task did not run from role")
}
}
func TestPlaybookRoleAcceptsVars(t *testing.T) {
res, _, err := RunCapture("test/site3.yml")
if err != nil {
t.Fatalf("Unable to run test/site3.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "from site3" {
t.Fatalf("Task did not run from role")
}
}
func TestPlaybookRoleAcceptsInlineVars(t *testing.T) {
res, _, err := RunCapture("test/site4.yml")
if err != nil {
t.Fatalf("Unable to run test/site4.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "from site4" {
t.Fatalf("Task did not run from role: %#v", d)
}
}
func TestPlaybookRoleIncludesSeeRoleFiles(t *testing.T) {
res, _, err := RunCapture("test/site5.yml")
if err != nil {
t.Fatalf("Unable to run test/site5.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "in special" {
t.Fatalf("Task did not run from role: %#v", d)
}
}
func TestPlaybookRoleFilesAreSeen(t *testing.T) {
res, _, err := RunCapture("test/site6.yml")
if err != nil {
t.Fatalf("Unable to run test/site6.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "in my script" {
t.Fatalf("Task did not run from role: %#v", d)
}
}
func TestPlaybookRoleDependenciesAreInvoked(t *testing.T) {
res, _, err := RunCapture("test/site7.yml")
if err != nil {
t.Fatalf("Unable to run test/site7.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "role7" {
t.Fatalf("Task did not run from role: %#v", d)
}
}
func TestPlaybookWithItems(t *testing.T) {
res, _, err := RunCapture("test/items.yml")
if err != nil {
t.Fatalf("Unable to run test/items.yml: %s", err)
}
if len(res.Results) != 3 {
t.Fatalf("tasks were not included from the role")
}
if v, ok := res.Results[0].Result.Get("stdout"); !ok || v.Read() != "a" {
t.Fatal("first isnt 'a'")
}
if v, ok := res.Results[1].Result.Get("stdout"); !ok || v.Read() != "b" {
t.Fatal("second isnt 'b'")
}
if v, ok := res.Results[2].Result.Get("stdout"); !ok || v.Read() != "c" {
t.Fatal("third isnt 'c'")
}
}
func TestPlaybookRoleModulesAreAvailable(t *testing.T) {
res, _, err := RunCapture("test/site8.yml")
if err != nil {
t.Fatalf("Unable to run test/site8.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "from module" {
t.Fatalf("Task did not run from role: %#v", d)
}
}
func TestPlaybookRoleModulesCanUseYAMLArgs(t *testing.T) {
res, _, err := RunCapture("test/site9.yml")
if err != nil {
t.Fatalf("Unable to run test/site9.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "from module" {
t.Fatalf("Task did not run from role: %#v", d)
}
}
func TestPlaybookRoleSubTasks(t *testing.T) {
res, _, err := RunCapture("test/site10.yml")
if err != nil {
t.Fatalf("Unable to run test/site10.yml: %s", err)
}
if len(res.Results) == 0 {
t.Fatalf("tasks were not included from the role")
}
d := res.Results[0].Result
if v, ok := d.Get("stdout"); !ok || v.Read() != "in get" {
t.Fatalf("Task did not run from role: %#v", d)
}
}