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