github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/environment_test.go (about) 1 package packer 2 3 import ( 4 "bytes" 5 "errors" 6 "io/ioutil" 7 "log" 8 "os" 9 "reflect" 10 "strings" 11 "testing" 12 ) 13 14 func init() { 15 // Disable log output for tests 16 log.SetOutput(ioutil.Discard) 17 } 18 19 func testComponentFinder() *ComponentFinder { 20 builderFactory := func(n string) (Builder, error) { return new(MockBuilder), nil } 21 ppFactory := func(n string) (PostProcessor, error) { return new(TestPostProcessor), nil } 22 provFactory := func(n string) (Provisioner, error) { return new(MockProvisioner), nil } 23 return &ComponentFinder{ 24 Builder: builderFactory, 25 PostProcessor: ppFactory, 26 Provisioner: provFactory, 27 } 28 } 29 30 func testEnvironment() Environment { 31 config := DefaultEnvironmentConfig() 32 config.Ui = &BasicUi{ 33 Reader: new(bytes.Buffer), 34 Writer: new(bytes.Buffer), 35 ErrorWriter: new(bytes.Buffer), 36 } 37 38 env, err := NewEnvironment(config) 39 if err != nil { 40 panic(err) 41 } 42 43 return env 44 } 45 46 func TestEnvironment_DefaultConfig_Commands(t *testing.T) { 47 config := DefaultEnvironmentConfig() 48 if len(config.Commands) != 0 { 49 t.Fatalf("bad: %#v", config.Commands) 50 } 51 } 52 53 func TestEnvironment_DefaultConfig_Ui(t *testing.T) { 54 config := DefaultEnvironmentConfig() 55 if config.Ui == nil { 56 t.Fatal("config.Ui should not be nil") 57 } 58 59 rwUi, ok := config.Ui.(*BasicUi) 60 if !ok { 61 t.Fatal("default UI should be BasicUi") 62 } 63 if rwUi.Writer != os.Stdout { 64 t.Fatal("default UI should go to stdout") 65 } 66 if rwUi.Reader != os.Stdin { 67 t.Fatal("default UI reader should go to stdin") 68 } 69 } 70 71 func TestNewEnvironment_NoConfig(t *testing.T) { 72 env, err := NewEnvironment(nil) 73 if env != nil { 74 t.Fatal("env should be nil") 75 } 76 if err == nil { 77 t.Fatal("should have error") 78 } 79 } 80 81 func TestEnvironment_NilComponents(t *testing.T) { 82 config := DefaultEnvironmentConfig() 83 config.Components = *new(ComponentFinder) 84 85 env, err := NewEnvironment(config) 86 if err != nil { 87 t.Fatalf("err: %s", err) 88 } 89 90 // All of these should not cause panics... so we don't assert 91 // anything but if there is a panic in the test then yeah, something 92 // went wrong. 93 env.Builder("foo") 94 env.Cli([]string{"foo"}) 95 env.Hook("foo") 96 env.PostProcessor("foo") 97 env.Provisioner("foo") 98 } 99 100 func TestEnvironment_Builder(t *testing.T) { 101 builder := &MockBuilder{} 102 builders := make(map[string]Builder) 103 builders["foo"] = builder 104 105 config := DefaultEnvironmentConfig() 106 config.Components.Builder = func(n string) (Builder, error) { return builders[n], nil } 107 108 env, _ := NewEnvironment(config) 109 returnedBuilder, err := env.Builder("foo") 110 if err != nil { 111 t.Fatalf("err: %s", err) 112 } 113 if returnedBuilder != builder { 114 t.Fatalf("bad: %#v", returnedBuilder) 115 } 116 } 117 118 func TestEnvironment_Builder_NilError(t *testing.T) { 119 config := DefaultEnvironmentConfig() 120 config.Components.Builder = func(n string) (Builder, error) { return nil, nil } 121 122 env, _ := NewEnvironment(config) 123 returnedBuilder, err := env.Builder("foo") 124 if err == nil { 125 t.Fatal("should have error") 126 } 127 if returnedBuilder != nil { 128 t.Fatalf("bad: %#v", returnedBuilder) 129 } 130 } 131 132 func TestEnvironment_Builder_Error(t *testing.T) { 133 config := DefaultEnvironmentConfig() 134 config.Components.Builder = func(n string) (Builder, error) { return nil, errors.New("foo") } 135 136 env, _ := NewEnvironment(config) 137 returnedBuilder, err := env.Builder("foo") 138 if err == nil { 139 t.Fatal("should have error") 140 } 141 if err.Error() != "foo" { 142 t.Fatalf("bad err: %s", err) 143 } 144 if returnedBuilder != nil { 145 t.Fatalf("should be nil: %#v", returnedBuilder) 146 } 147 } 148 149 func TestEnvironment_Cache(t *testing.T) { 150 config := DefaultEnvironmentConfig() 151 env, _ := NewEnvironment(config) 152 if env.Cache() == nil { 153 t.Fatal("cache should not be nil") 154 } 155 } 156 157 func TestEnvironment_Cli_Error(t *testing.T) { 158 config := DefaultEnvironmentConfig() 159 config.Components.Command = func(n string) (Command, error) { return nil, errors.New("foo") } 160 161 env, _ := NewEnvironment(config) 162 _, err := env.Cli([]string{"foo"}) 163 if err == nil { 164 t.Fatal("should have error") 165 } 166 if err.Error() != "foo" { 167 t.Fatalf("bad: %s", err) 168 } 169 } 170 171 func TestEnvironment_Cli_CallsRun(t *testing.T) { 172 command := &TestCommand{} 173 commands := make(map[string]Command) 174 commands["foo"] = command 175 176 config := &EnvironmentConfig{} 177 config.Commands = []string{"foo"} 178 config.Components.Command = func(n string) (Command, error) { return commands[n], nil } 179 180 env, _ := NewEnvironment(config) 181 exitCode, err := env.Cli([]string{"foo", "bar", "baz"}) 182 if err != nil { 183 t.Fatalf("err: %s", err) 184 } 185 if exitCode != 0 { 186 t.Fatalf("bad: %d", exitCode) 187 } 188 if !command.runCalled { 189 t.Fatal("command should be run") 190 } 191 if command.runEnv != env { 192 t.Fatalf("bad env: %#v", command.runEnv) 193 } 194 if !reflect.DeepEqual(command.runArgs, []string{"bar", "baz"}) { 195 t.Fatalf("bad: %#v", command.runArgs) 196 } 197 } 198 199 func TestEnvironment_DefaultCli_Empty(t *testing.T) { 200 defaultEnv := testEnvironment() 201 202 // Test with no args 203 exitCode, _ := defaultEnv.Cli([]string{}) 204 if exitCode != 1 { 205 t.Fatalf("bad: %d", exitCode) 206 } 207 208 // Test with only blank args 209 exitCode, _ = defaultEnv.Cli([]string{""}) 210 if exitCode != 1 { 211 t.Fatalf("bad: %d", exitCode) 212 } 213 } 214 215 func TestEnvironment_DefaultCli_Help(t *testing.T) { 216 defaultEnv := testEnvironment() 217 218 // A little lambda to help us test the output actually contains help 219 testOutput := func() { 220 buffer := defaultEnv.Ui().(*BasicUi).Writer.(*bytes.Buffer) 221 output := buffer.String() 222 buffer.Reset() 223 if !strings.Contains(output, "usage: packer") { 224 t.Fatalf("should contain help: %#v", output) 225 } 226 } 227 228 // Test "--help" 229 exitCode, _ := defaultEnv.Cli([]string{"--help"}) 230 if exitCode != 1 { 231 t.Fatalf("bad: %d", exitCode) 232 } 233 testOutput() 234 235 // Test "-h" 236 exitCode, _ = defaultEnv.Cli([]string{"--help"}) 237 if exitCode != 1 { 238 t.Fatalf("bad: %d", exitCode) 239 } 240 testOutput() 241 } 242 243 func TestEnvironment_DefaultCli_Version(t *testing.T) { 244 defaultEnv := testEnvironment() 245 246 versionCommands := []string{"version", "--version", "-v"} 247 for _, command := range versionCommands { 248 exitCode, _ := defaultEnv.Cli([]string{command}) 249 if exitCode != 0 { 250 t.Fatalf("bad: %d", exitCode) 251 } 252 253 // Test the --version and -v can appear anywhere 254 exitCode, _ = defaultEnv.Cli([]string{"bad", command}) 255 256 if command != "version" { 257 if exitCode != 0 { 258 t.Fatalf("bad: %d", exitCode) 259 } 260 } else { 261 if exitCode != 1 { 262 t.Fatalf("bad: %d", exitCode) 263 } 264 } 265 } 266 } 267 268 func TestEnvironment_Hook(t *testing.T) { 269 hook := &MockHook{} 270 hooks := make(map[string]Hook) 271 hooks["foo"] = hook 272 273 config := DefaultEnvironmentConfig() 274 config.Components.Hook = func(n string) (Hook, error) { return hooks[n], nil } 275 276 env, _ := NewEnvironment(config) 277 returned, err := env.Hook("foo") 278 if err != nil { 279 t.Fatalf("err: %s", err) 280 } 281 if returned != hook { 282 t.Fatalf("bad: %#v", returned) 283 } 284 } 285 286 func TestEnvironment_Hook_NilError(t *testing.T) { 287 config := DefaultEnvironmentConfig() 288 config.Components.Hook = func(n string) (Hook, error) { return nil, nil } 289 290 env, _ := NewEnvironment(config) 291 returned, err := env.Hook("foo") 292 if err == nil { 293 t.Fatal("should have error") 294 } 295 if returned != nil { 296 t.Fatalf("bad: %#v", returned) 297 } 298 } 299 300 func TestEnvironment_Hook_Error(t *testing.T) { 301 config := DefaultEnvironmentConfig() 302 config.Components.Hook = func(n string) (Hook, error) { return nil, errors.New("foo") } 303 304 env, _ := NewEnvironment(config) 305 returned, err := env.Hook("foo") 306 if err == nil { 307 t.Fatal("should have error") 308 } 309 if err.Error() != "foo" { 310 t.Fatalf("err: %s", err) 311 } 312 if returned != nil { 313 t.Fatalf("bad: %#v", returned) 314 } 315 } 316 317 func TestEnvironment_PostProcessor(t *testing.T) { 318 pp := &TestPostProcessor{} 319 pps := make(map[string]PostProcessor) 320 pps["foo"] = pp 321 322 config := DefaultEnvironmentConfig() 323 config.Components.PostProcessor = func(n string) (PostProcessor, error) { return pps[n], nil } 324 325 env, _ := NewEnvironment(config) 326 returned, err := env.PostProcessor("foo") 327 if err != nil { 328 t.Fatalf("err: %s", err) 329 } 330 if returned != pp { 331 t.Fatalf("bad: %#v", returned) 332 } 333 } 334 335 func TestEnvironment_PostProcessor_NilError(t *testing.T) { 336 config := DefaultEnvironmentConfig() 337 config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, nil } 338 339 env, _ := NewEnvironment(config) 340 returned, err := env.PostProcessor("foo") 341 if err == nil { 342 t.Fatal("should have error") 343 } 344 if returned != nil { 345 t.Fatalf("bad: %#v", returned) 346 } 347 } 348 349 func TestEnvironment_PostProcessor_Error(t *testing.T) { 350 config := DefaultEnvironmentConfig() 351 config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, errors.New("foo") } 352 353 env, _ := NewEnvironment(config) 354 returned, err := env.PostProcessor("foo") 355 if err == nil { 356 t.Fatal("should be an error") 357 } 358 if err.Error() != "foo" { 359 t.Fatalf("bad err: %s", err) 360 } 361 if returned != nil { 362 t.Fatalf("bad: %#v", returned) 363 } 364 } 365 366 func TestEnvironmentProvisioner(t *testing.T) { 367 p := &MockProvisioner{} 368 ps := make(map[string]Provisioner) 369 ps["foo"] = p 370 371 config := DefaultEnvironmentConfig() 372 config.Components.Provisioner = func(n string) (Provisioner, error) { return ps[n], nil } 373 374 env, _ := NewEnvironment(config) 375 returned, err := env.Provisioner("foo") 376 if err != nil { 377 t.Fatalf("err: %s", err) 378 } 379 if returned != p { 380 t.Fatalf("bad: %#v", returned) 381 } 382 } 383 384 func TestEnvironmentProvisioner_NilError(t *testing.T) { 385 config := DefaultEnvironmentConfig() 386 config.Components.Provisioner = func(n string) (Provisioner, error) { return nil, nil } 387 388 env, _ := NewEnvironment(config) 389 returned, err := env.Provisioner("foo") 390 if err == nil { 391 t.Fatal("should have error") 392 } 393 if returned != nil { 394 t.Fatalf("bad: %#v", returned) 395 } 396 } 397 398 func TestEnvironmentProvisioner_Error(t *testing.T) { 399 config := DefaultEnvironmentConfig() 400 config.Components.Provisioner = func(n string) (Provisioner, error) { 401 return nil, errors.New("foo") 402 } 403 404 env, _ := NewEnvironment(config) 405 returned, err := env.Provisioner("foo") 406 if err == nil { 407 t.Fatal("should have error") 408 } 409 if err.Error() != "foo" { 410 t.Fatalf("err: %s", err) 411 } 412 if returned != nil { 413 t.Fatalf("bad: %#v", returned) 414 } 415 } 416 417 func TestEnvironment_SettingUi(t *testing.T) { 418 ui := &BasicUi{ 419 Reader: new(bytes.Buffer), 420 Writer: new(bytes.Buffer), 421 } 422 423 config := &EnvironmentConfig{} 424 config.Ui = ui 425 426 env, _ := NewEnvironment(config) 427 428 if env.Ui() != ui { 429 t.Fatalf("UI should be equal: %#v", env.Ui()) 430 } 431 }