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
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/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{},
|
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(c)
|
2013-11-02 23:56:54 -04:00
|
|
|
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"
|
2018-09-18 11:29:20 -04:00
|
|
|
config["skip_region_validation"] = true
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2013-11-02 23:56:54 -04:00
|
|
|
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{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-02 23:56:54 -04:00
|
|
|
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{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-02 23:56:54 -04:00
|
|
|
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
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2013-11-02 23:56:54 -04:00
|
|
|
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
|
|
|
|
2016-12-14 15:50:26 -05:00
|
|
|
func TestBuilderPrepare_InvalidShutdownBehavior(t *testing.T) {
|
2016-05-20 06:21:48 -04:00
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test good
|
2016-12-14 15:50:26 -05:00
|
|
|
config["shutdown_behavior"] = "terminate"
|
2018-09-18 11:29:20 -04:00
|
|
|
config["skip_region_validation"] = true
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2016-05-20 06:21:48 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
2016-12-14 15:50:26 -05:00
|
|
|
config["shutdown_behavior"] = "stop"
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2016-05-20 06:21:48 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test bad
|
2016-12-14 15:50:26 -05:00
|
|
|
config["shutdown_behavior"] = "foobar"
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2016-05-20 06:21:48 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 06:04:03 -05:00
|
|
|
|
|
|
|
func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
generatedData, 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)
|
|
|
|
}
|
|
|
|
if len(generatedData) == 0 {
|
|
|
|
t.Fatalf("Generated data should not be empty")
|
|
|
|
}
|
|
|
|
if generatedData[0] != "SourceAMIName" {
|
|
|
|
t.Fatalf("Generated data should contain SourceAMIName")
|
|
|
|
}
|
|
|
|
}
|