github.com/rothwerx/packer@v0.9.0/builder/amazon/ebs/builder_test.go (about)

     1  package ebs
     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  		"ami_name":      "foo",
    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  		"access_key": []string{},
    33  	}
    34  
    35  	warnings, err := b.Prepare(c)
    36  	if len(warnings) > 0 {
    37  		t.Fatalf("bad: %#v", warnings)
    38  	}
    39  	if err == nil {
    40  		t.Fatalf("prepare should fail")
    41  	}
    42  }
    43  
    44  func TestBuilderPrepare_AMIName(t *testing.T) {
    45  	var b Builder
    46  	config := testConfig()
    47  
    48  	// Test good
    49  	config["ami_name"] = "foo"
    50  	warnings, err := b.Prepare(config)
    51  	if len(warnings) > 0 {
    52  		t.Fatalf("bad: %#v", warnings)
    53  	}
    54  	if err != nil {
    55  		t.Fatalf("should not have error: %s", err)
    56  	}
    57  
    58  	// Test bad
    59  	config["ami_name"] = "foo {{"
    60  	b = Builder{}
    61  	warnings, err = b.Prepare(config)
    62  	if len(warnings) > 0 {
    63  		t.Fatalf("bad: %#v", warnings)
    64  	}
    65  	if err == nil {
    66  		t.Fatal("should have error")
    67  	}
    68  
    69  	// Test bad
    70  	delete(config, "ami_name")
    71  	b = Builder{}
    72  	warnings, err = b.Prepare(config)
    73  	if len(warnings) > 0 {
    74  		t.Fatalf("bad: %#v", warnings)
    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  	warnings, err := b.Prepare(config)
    88  	if len(warnings) > 0 {
    89  		t.Fatalf("bad: %#v", warnings)
    90  	}
    91  	if err == nil {
    92  		t.Fatal("should have error")
    93  	}
    94  }