github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/amazon/ebsvolume/builder_test.go (about)

     1  package ebsvolume
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/mitchellh/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_InvalidShutdownBehaviour(t *testing.T) {
    59  	var b Builder
    60  	config := testConfig()
    61  
    62  	// Test good
    63  	config["shutdown_behaviour"] = "terminate"
    64  	warnings, err := b.Prepare(config)
    65  	if len(warnings) > 0 {
    66  		t.Fatalf("bad: %#v", warnings)
    67  	}
    68  	if err != nil {
    69  		t.Fatalf("should not have error: %s", err)
    70  	}
    71  
    72  	// Test good
    73  	config["shutdown_behaviour"] = "stop"
    74  	warnings, err = b.Prepare(config)
    75  	if len(warnings) > 0 {
    76  		t.Fatalf("bad: %#v", warnings)
    77  	}
    78  	if err != nil {
    79  		t.Fatalf("should not have error: %s", err)
    80  	}
    81  
    82  	// Test bad
    83  	config["shutdown_behaviour"] = "foobar"
    84  	warnings, err = b.Prepare(config)
    85  	if len(warnings) > 0 {
    86  		t.Fatalf("bad: %#v", warnings)
    87  	}
    88  	if err == nil {
    89  		t.Fatal("should have error")
    90  	}
    91  }