2013-07-15 02:02:18 -04:00
|
|
|
package ebs
|
2013-05-09 01:34:20 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-05-09 01:34:20 -04:00
|
|
|
)
|
|
|
|
|
2013-06-08 20:41:56 -04:00
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2013-06-08 22:26:07 -04:00
|
|
|
"access_key": "foo",
|
|
|
|
"secret_key": "bar",
|
|
|
|
"source_ami": "foo",
|
2013-06-08 20:46:34 -04:00
|
|
|
"instance_type": "foo",
|
2013-06-08 22:26:07 -04:00
|
|
|
"region": "us-east-1",
|
|
|
|
"ssh_username": "root",
|
2013-06-17 18:24:33 -04:00
|
|
|
"ami_name": "foo",
|
2013-06-08 20:41:56 -04:00
|
|
|
}
|
|
|
|
}
|
2013-05-09 01:34:20 -04:00
|
|
|
|
2013-06-08 20:41:56 -04:00
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Builder{}
|
|
|
|
if _, ok := raw.(packer.Builder); !ok {
|
|
|
|
t.Fatalf("Builder should be a builder")
|
|
|
|
}
|
2013-05-09 01:34:20 -04:00
|
|
|
}
|
2013-05-09 16:19:38 -04:00
|
|
|
|
|
|
|
func TestBuilder_Prepare_BadType(t *testing.T) {
|
|
|
|
b := &Builder{}
|
2013-05-10 20:01:24 -04:00
|
|
|
c := map[string]interface{}{
|
2013-05-09 16:19:38 -04:00
|
|
|
"access_key": []string{},
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:56:54 -04:00
|
|
|
warnings, err := b.Prepare(c)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-05-20 19:39:43 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("prepare should fail")
|
|
|
|
}
|
2013-05-09 16:19:38 -04:00
|
|
|
}
|
|
|
|
|
2013-06-17 18:24:33 -04:00
|
|
|
func TestBuilderPrepare_AMIName(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["ami_name"] = "foo"
|
2013-11-02 23:56:54 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-17 18:24:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
config["ami_name"] = "foo {{"
|
|
|
|
b = Builder{}
|
2013-11-02 23:56:54 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-17 18:24:33 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
delete(config, "ami_name")
|
|
|
|
b = Builder{}
|
2013-11-02 23:56:54 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-17 18:24:33 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 20:28:56 -04:00
|
|
|
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Add a random key
|
|
|
|
config["i_should_not_be_valid"] = true
|
2013-11-02 23:56:54 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-07-13 20:28:56 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 06:21:48 -04:00
|
|
|
|
|
|
|
func TestBuilderPrepare_InvalidShutdownBehaviour(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["shutdown_behaviour"] = "terminate"
|
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["shutdown_behaviour"] = "stop"
|
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
config["shutdown_behaviour"] = "foobar"
|
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|