2013-07-29 19:42:35 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-03-24 15:41:29 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-07-29 19:42:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
2013-07-30 12:55:17 -04:00
|
|
|
return map[string]interface{}{
|
2013-07-31 01:33:41 -04:00
|
|
|
"ami_name": "foo",
|
2013-07-30 12:55:17 -04:00
|
|
|
"source_ami": "foo",
|
2017-11-07 17:57:10 -05:00
|
|
|
"region": "us-east-1",
|
2018-09-10 18:43:01 -04:00
|
|
|
// region validation logic is checked in ami_config_test
|
|
|
|
"skip_region_validation": true,
|
2013-07-30 12:55:17 -04:00
|
|
|
}
|
2013-07-29 19:42:35 -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-07-30 12:55:17 -04:00
|
|
|
|
2013-07-31 01:33:41 -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-07-31 01:33:41 -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-07-31 01:33:41 -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-07-31 01:33:41 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 01:29:06 -04:00
|
|
|
func TestBuilderPrepare_ChrootMounts(t *testing.T) {
|
|
|
|
b := &Builder{}
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["chroot_mounts"] = nil
|
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-31 01:29:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("err: %s", err)
|
|
|
|
}
|
2018-01-26 15:40:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ChrootMountsBadDefaults(t *testing.T) {
|
|
|
|
b := &Builder{}
|
|
|
|
config := testConfig()
|
2013-07-31 01:29:06 -04:00
|
|
|
|
|
|
|
config["chroot_mounts"] = [][]string{
|
2016-11-01 17:08:04 -04:00
|
|
|
{"bad"},
|
2013-07-31 01:29:06 -04:00
|
|
|
}
|
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-31 01:29:06 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 12:55:17 -04:00
|
|
|
func TestBuilderPrepare_SourceAmi(t *testing.T) {
|
|
|
|
b := &Builder{}
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["source_ami"] = ""
|
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-30 12:55:17 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
config["source_ami"] = "foo"
|
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-30 12:55:17 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-09-27 07:55:19 -04:00
|
|
|
|
|
|
|
func TestBuilderPrepare_CommandWrapper(t *testing.T) {
|
|
|
|
b := &Builder{}
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["command_wrapper"] = "echo hi; {{.Command}}"
|
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-09-27 07:55:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2017-03-24 15:41:29 -04:00
|
|
|
|
|
|
|
func TestBuilderPrepare_CopyFiles(t *testing.T) {
|
|
|
|
b := &Builder{}
|
|
|
|
config := testConfig()
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2017-03-24 15:41:29 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b.config.CopyFiles) != 1 && b.config.CopyFiles[0] != "/etc/resolv.conf" {
|
|
|
|
t.Errorf("Was expecting default value for copy_files.")
|
|
|
|
}
|
2018-01-26 15:40:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_CopyFilesNoDefault(t *testing.T) {
|
|
|
|
b := &Builder{}
|
|
|
|
config := testConfig()
|
2017-03-24 15:41:29 -04:00
|
|
|
|
|
|
|
config["copy_files"] = []string{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2017-03-24 15:41:29 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b.config.CopyFiles) > 0 {
|
2019-04-10 09:42:02 -04:00
|
|
|
t.Errorf("Was expecting no default value for copy_files. Found %v",
|
|
|
|
b.config.CopyFiles)
|
2017-03-24 15:41:29 -04:00
|
|
|
}
|
|
|
|
}
|
2019-02-25 13:55:10 -05:00
|
|
|
|
|
|
|
func TestBuilderPrepare_RootDeviceNameAndAMIMappings(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["root_device_name"] = "/dev/sda"
|
|
|
|
config["ami_block_device_mappings"] = []interface{}{map[string]string{}}
|
|
|
|
config["root_volume_size"] = 15
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2019-02-25 13:55:10 -05:00
|
|
|
if len(warnings) == 0 {
|
|
|
|
t.Fatal("Missing warning, stating block device mappings will be overwritten")
|
|
|
|
} else if len(warnings) > 1 {
|
|
|
|
t.Fatalf("excessive warnings: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_AMIMappingsNoRootDeviceName(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["ami_block_device_mappings"] = []interface{}{map[string]string{}}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2019-02-25 13:55:10 -05:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_RootDeviceNameNoAMIMappings(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["root_device_name"] = "/dev/sda"
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2019-02-25 13:55:10 -05:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should have error")
|
|
|
|
}
|
|
|
|
}
|
2020-01-20 10:29:38 -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")
|
|
|
|
}
|
|
|
|
if generatedData[1] != "Device" {
|
|
|
|
t.Fatalf("Generated data should contain Device")
|
|
|
|
}
|
|
|
|
if generatedData[2] != "MountPath" {
|
|
|
|
t.Fatalf("Generated data should contain MountPath")
|
|
|
|
}
|
|
|
|
}
|