github.com/sneal/packer@v0.5.2/builder/openstack/builder_test.go (about) 1 package openstack 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "testing" 6 ) 7 8 func testConfig() map[string]interface{} { 9 return map[string]interface{}{ 10 "username": "foo", 11 "password": "bar", 12 "provider": "foo", 13 "region": "DFW", 14 "image_name": "foo", 15 "source_image": "foo", 16 "flavor": "foo", 17 "ssh_username": "root", 18 } 19 } 20 21 func TestBuilder_ImplementsBuilder(t *testing.T) { 22 var raw interface{} 23 raw = &Builder{} 24 if _, ok := raw.(packer.Builder); !ok { 25 t.Fatalf("Builder should be a builder") 26 } 27 } 28 29 func TestBuilder_Prepare_BadType(t *testing.T) { 30 b := &Builder{} 31 c := map[string]interface{}{ 32 "password": []string{}, 33 } 34 35 warns, err := b.Prepare(c) 36 if len(warns) > 0 { 37 t.Fatalf("bad: %#v", warns) 38 } 39 if err == nil { 40 t.Fatalf("prepare should fail") 41 } 42 } 43 44 func TestBuilderPrepare_ImageName(t *testing.T) { 45 var b Builder 46 config := testConfig() 47 48 // Test good 49 config["image_name"] = "foo" 50 warns, err := b.Prepare(config) 51 if len(warns) > 0 { 52 t.Fatalf("bad: %#v", warns) 53 } 54 if err != nil { 55 t.Fatalf("should not have error: %s", err) 56 } 57 58 // Test bad 59 config["image_name"] = "foo {{" 60 b = Builder{} 61 warns, err = b.Prepare(config) 62 if len(warns) > 0 { 63 t.Fatalf("bad: %#v", warns) 64 } 65 if err == nil { 66 t.Fatal("should have error") 67 } 68 69 // Test bad 70 delete(config, "image_name") 71 b = Builder{} 72 warns, err = b.Prepare(config) 73 if len(warns) > 0 { 74 t.Fatalf("bad: %#v", warns) 75 } 76 if err == nil { 77 t.Fatal("should have error") 78 } 79 } 80 81 func TestBuilderPrepare_InvalidKey(t *testing.T) { 82 var b Builder 83 config := testConfig() 84 85 // Add a random key 86 config["i_should_not_be_valid"] = true 87 warns, err := b.Prepare(config) 88 if len(warns) > 0 { 89 t.Fatalf("bad: %#v", warns) 90 } 91 if err == nil { 92 t.Fatal("should have error") 93 } 94 }