github.com/masahide/goansible@v0.0.0-20160116054156-01eac649e9f2/playbook_test.go (about) 1 package goansible 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestSimplePlaybook(t *testing.T) { 9 env := NewEnv(NewNestedScope(nil), DefaultConfig) 10 p, err := NewPlaybook(env, "test/playbook1.yml") 11 12 if err != nil { 13 panic(err) 14 } 15 16 if len(p.Plays) != 2 { 17 t.Fatalf("Didn't load 2 playbooks, loaded: %d", len(p.Plays)) 18 } 19 20 x := p.Plays[1] 21 22 if x.Hosts != "all" { 23 t.Errorf("Hosts not all: was %s", x.Hosts) 24 } 25 26 vars := x.Vars 27 28 a, ok := vars.Get("answer") 29 30 if !ok { 31 t.Fatalf("No var 'answer'") 32 } 33 34 if a.Read() != "Wuh, I think so" { 35 t.Errorf("Unable to decode string var: %#v", a) 36 } 37 38 a, ok = vars.Get("port") 39 40 if !ok { 41 t.Fatalf("No var 'port'") 42 } 43 44 if a.Read() != 5150 { 45 t.Errorf("Unable to decode numeric var: %#v", a.Read()) 46 } 47 48 if len(x.VarsFiles) != 2 { 49 t.Fatalf("Unable to decode varsfiles, got %d", len(x.VarsFiles)) 50 } 51 52 f := x.VarsFiles[0] 53 54 if f != "common_vars.yml" { 55 t.Errorf("Unable to decode literal vars_files") 56 } 57 58 f2 := x.VarsFiles[1].([]interface{}) 59 60 if f2[1].(string) != "default_os.yml" { 61 t.Errorf("Unable to decode list vars_files") 62 } 63 64 tasks := x.Tasks 65 66 if len(tasks) < 5 { 67 t.Errorf("Failed to decode the proper number of tasks: %d", len(tasks)) 68 } 69 70 if tasks[3].Args() != "echo {{port}}" { 71 t.Errorf("Failed to decode templating in action: %#v", tasks[3].Args()) 72 } 73 } 74 75 func totalRuntime(results []RunResult) time.Duration { 76 cur := time.Duration(0) 77 78 for _, res := range results { 79 cur += res.Runtime 80 } 81 82 return cur 83 } 84 85 func TestPlaybookFuturesRunInParallel(t *testing.T) { 86 run, _, err := RunCapture("test/future.yml") 87 if err != nil { 88 t.Fatalf("Unable to load test/future.yml") 89 } 90 91 total := run.Runtime.Seconds() 92 93 if total > 1.1 || total < 0.9 { 94 t.Errorf("Futures did not run in parallel: %f", total) 95 } 96 } 97 98 func TestPlaybookFuturesCanBeWaitedOn(t *testing.T) { 99 run, _, err := RunCapture("test/future.yml") 100 if err != nil { 101 t.Fatalf("Unable to load test/future.yml") 102 } 103 104 total := run.Runtime.Seconds() 105 106 if total > 1.1 || total < 0.9 { 107 t.Errorf("Futures did not run in parallel: %f", total) 108 } 109 } 110 111 /* 112 func TestPlaybookTaskIncludes(t *testing.T) { 113 res, _, err := RunCapture("test/inc_parent.yml") 114 if err != nil { 115 t.Fatalf("Unable to run test/inc_parent.yml") 116 } 117 118 if filepath.Base(res.Results[0].Task.File) != "inc_child.yml" { 119 t.Fatalf("Did not include tasks from child") 120 } 121 } 122 */ 123 124 func TestPlaybookTaskIncludesCanHaveVars(t *testing.T) { 125 res, _, err := RunCapture("test/inc_parent2.yml") 126 if err != nil { 127 t.Fatalf("Unable to run test/inc_parent2.yml: %s", err) 128 } 129 130 d := res.Results[0].Result 131 132 if v, ok := d.Get("stdout"); !ok || v.Read() != "oscar" { 133 t.Fatalf("A variable was not passed into the included file") 134 } 135 136 d = res.Results[1].Result 137 138 if v, ok := d.Get("stdout"); !ok || v.Read() != "ellen" { 139 t.Fatalf("A variable was not passed into the included file") 140 } 141 142 d = res.Results[2].Result 143 144 if v, ok := d.Get("stdout"); !ok || v.Read() != "Los Angeles" { 145 t.Fatalf("A variable was not passed into the included file") 146 } 147 } 148 149 func TestPlaybookRoleTasksInclude(t *testing.T) { 150 res, _, err := RunCapture("test/site1.yml") 151 if err != nil { 152 t.Fatalf("Unable to run test/site1.yml: %s", err) 153 } 154 155 if len(res.Results) == 0 { 156 t.Fatalf("tasks were not included from the role") 157 } 158 159 d := res.Results[0].Result 160 161 if v, ok := d.Get("stdout"); !ok || v.Read() != "in role" { 162 t.Fatalf("Task did not run from role") 163 } 164 } 165 166 func TestPlaybookRoleHandlersInclude(t *testing.T) { 167 res, _, err := RunCapture("test/site1.yml") 168 if err != nil { 169 t.Fatalf("Unable to run test/site1.yml: %s", err) 170 } 171 172 if len(res.Results) == 0 { 173 t.Fatalf("tasks were not included from the role") 174 } 175 176 d := res.Results[1].Result 177 178 if v, ok := d.Get("stdout"); !ok || v.Read() != "in role handler" { 179 t.Fatalf("Task did not run from role") 180 } 181 } 182 183 func TestPlaybookRoleVarsInclude(t *testing.T) { 184 res, _, err := RunCapture("test/site2.yml") 185 if err != nil { 186 t.Fatalf("Unable to run test/site2.yml: %s", err) 187 } 188 189 if len(res.Results) == 0 { 190 t.Fatalf("tasks were not included from the role") 191 } 192 193 d := res.Results[0].Result 194 195 if v, ok := d.Get("stdout"); !ok || v.Read() != "from role var" { 196 t.Fatalf("Task did not run from role") 197 } 198 } 199 200 func TestPlaybookRoleAcceptsVars(t *testing.T) { 201 res, _, err := RunCapture("test/site3.yml") 202 if err != nil { 203 t.Fatalf("Unable to run test/site3.yml: %s", err) 204 } 205 206 if len(res.Results) == 0 { 207 t.Fatalf("tasks were not included from the role") 208 } 209 210 d := res.Results[0].Result 211 212 if v, ok := d.Get("stdout"); !ok || v.Read() != "from site3" { 213 t.Fatalf("Task did not run from role") 214 } 215 } 216 217 func TestPlaybookRoleAcceptsInlineVars(t *testing.T) { 218 res, _, err := RunCapture("test/site4.yml") 219 if err != nil { 220 t.Fatalf("Unable to run test/site4.yml: %s", err) 221 } 222 223 if len(res.Results) == 0 { 224 t.Fatalf("tasks were not included from the role") 225 } 226 227 d := res.Results[0].Result 228 229 if v, ok := d.Get("stdout"); !ok || v.Read() != "from site4" { 230 t.Fatalf("Task did not run from role: %#v", d) 231 } 232 } 233 234 func TestPlaybookRoleIncludesSeeRoleFiles(t *testing.T) { 235 res, _, err := RunCapture("test/site5.yml") 236 if err != nil { 237 t.Fatalf("Unable to run test/site5.yml: %s", err) 238 } 239 240 if len(res.Results) == 0 { 241 t.Fatalf("tasks were not included from the role") 242 } 243 244 d := res.Results[0].Result 245 246 if v, ok := d.Get("stdout"); !ok || v.Read() != "in special" { 247 t.Fatalf("Task did not run from role: %#v", d) 248 } 249 } 250 251 func TestPlaybookRoleFilesAreSeen(t *testing.T) { 252 res, _, err := RunCapture("test/site6.yml") 253 if err != nil { 254 t.Fatalf("Unable to run test/site6.yml: %s", err) 255 } 256 257 if len(res.Results) == 0 { 258 t.Fatalf("tasks were not included from the role") 259 } 260 261 d := res.Results[0].Result 262 263 if v, ok := d.Get("stdout"); !ok || v.Read() != "in my script" { 264 t.Fatalf("Task did not run from role: %#v", d) 265 } 266 } 267 268 func TestPlaybookRoleDependenciesAreInvoked(t *testing.T) { 269 res, _, err := RunCapture("test/site7.yml") 270 if err != nil { 271 t.Fatalf("Unable to run test/site7.yml: %s", err) 272 } 273 274 if len(res.Results) == 0 { 275 t.Fatalf("tasks were not included from the role") 276 } 277 278 d := res.Results[0].Result 279 280 if v, ok := d.Get("stdout"); !ok || v.Read() != "role7" { 281 t.Fatalf("Task did not run from role: %#v", d) 282 } 283 } 284 285 func TestPlaybookWithItems(t *testing.T) { 286 res, _, err := RunCapture("test/items.yml") 287 if err != nil { 288 t.Fatalf("Unable to run test/items.yml: %s", err) 289 } 290 291 if len(res.Results) != 3 { 292 t.Fatalf("tasks were not included from the role") 293 } 294 295 if v, ok := res.Results[0].Result.Get("stdout"); !ok || v.Read() != "a" { 296 t.Fatal("first isnt 'a'") 297 } 298 299 if v, ok := res.Results[1].Result.Get("stdout"); !ok || v.Read() != "b" { 300 t.Fatal("second isnt 'b'") 301 } 302 303 if v, ok := res.Results[2].Result.Get("stdout"); !ok || v.Read() != "c" { 304 t.Fatal("third isnt 'c'") 305 } 306 307 } 308 309 func TestPlaybookRoleModulesAreAvailable(t *testing.T) { 310 res, _, err := RunCapture("test/site8.yml") 311 if err != nil { 312 t.Fatalf("Unable to run test/site8.yml: %s", err) 313 } 314 315 if len(res.Results) == 0 { 316 t.Fatalf("tasks were not included from the role") 317 } 318 319 d := res.Results[0].Result 320 321 if v, ok := d.Get("stdout"); !ok || v.Read() != "from module" { 322 t.Fatalf("Task did not run from role: %#v", d) 323 } 324 } 325 326 func TestPlaybookRoleModulesCanUseYAMLArgs(t *testing.T) { 327 res, _, err := RunCapture("test/site9.yml") 328 if err != nil { 329 t.Fatalf("Unable to run test/site9.yml: %s", err) 330 } 331 332 if len(res.Results) == 0 { 333 t.Fatalf("tasks were not included from the role") 334 } 335 336 d := res.Results[0].Result 337 338 if v, ok := d.Get("stdout"); !ok || v.Read() != "from module" { 339 t.Fatalf("Task did not run from role: %#v", d) 340 } 341 } 342 343 func TestPlaybookRoleSubTasks(t *testing.T) { 344 res, _, err := RunCapture("test/site10.yml") 345 if err != nil { 346 t.Fatalf("Unable to run test/site10.yml: %s", err) 347 } 348 349 if len(res.Results) == 0 { 350 t.Fatalf("tasks were not included from the role") 351 } 352 353 d := res.Results[0].Result 354 355 if v, ok := d.Get("stdout"); !ok || v.Read() != "in get" { 356 t.Fatalf("Task did not run from role: %#v", d) 357 } 358 }