2013-07-15 02:56:28 -04:00
|
|
|
package instance
|
|
|
|
|
|
|
|
import (
|
2013-07-21 00:00:12 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-07-15 02:56:28 -04:00
|
|
|
"testing"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2013-07-15 02:56:28 -04:00
|
|
|
)
|
|
|
|
|
2018-04-25 08:23:36 -04:00
|
|
|
func testConfig() (config map[string]interface{}, tf *os.File) {
|
2013-07-21 00:00:12 -04:00
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-04-25 08:23:36 -04:00
|
|
|
config = map[string]interface{}{
|
2013-07-24 16:41:49 -04:00
|
|
|
"account_id": "foo",
|
2013-07-25 01:19:04 -04:00
|
|
|
"ami_name": "foo",
|
2013-07-21 00:00:12 -04:00
|
|
|
"instance_type": "m1.small",
|
|
|
|
"region": "us-east-1",
|
2013-07-25 00:22:16 -04:00
|
|
|
"s3_bucket": "foo",
|
2013-07-21 00:00:12 -04:00
|
|
|
"source_ami": "foo",
|
|
|
|
"ssh_username": "bob",
|
|
|
|
"x509_cert_path": tf.Name(),
|
|
|
|
"x509_key_path": tf.Name(),
|
|
|
|
"x509_upload_path": "/foo",
|
|
|
|
}
|
2018-04-25 08:23:36 -04:00
|
|
|
|
|
|
|
return config, tf
|
2013-07-20 22:40:45 -04:00
|
|
|
}
|
|
|
|
|
2013-07-15 02:56:28 -04:00
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Builder{}
|
2020-12-01 16:42:11 -05:00
|
|
|
if _, ok := raw.(packersdk.Builder); !ok {
|
2013-07-15 02:56:28 -04:00
|
|
|
t.Fatalf("Builder should be a builder")
|
|
|
|
}
|
|
|
|
}
|
2013-07-20 22:40:45 -04:00
|
|
|
|
2013-07-24 16:41:49 -04:00
|
|
|
func TestBuilderPrepare_AccountId(t *testing.T) {
|
|
|
|
b := &Builder{}
|
2018-04-25 08:23:36 -04:00
|
|
|
config, tempfile := testConfig()
|
2018-09-18 11:29:20 -04:00
|
|
|
config["skip_region_validation"] = true
|
|
|
|
|
2018-04-25 08:23:36 -04:00
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-07-24 16:41:49 -04:00
|
|
|
|
|
|
|
config["account_id"] = ""
|
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-24 16:41:49 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
config["account_id"] = "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-24 16:41:49 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["account_id"] = "0123-0456-7890"
|
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-24 16:41:49 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.AccountId != "012304567890" {
|
|
|
|
t.Errorf("should strip hyphens: %s", b.config.AccountId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:19:04 -04:00
|
|
|
func TestBuilderPrepare_AMIName(t *testing.T) {
|
|
|
|
var b Builder
|
2018-04-25 08:23:36 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-07-25 01:19:04 -04:00
|
|
|
|
|
|
|
// 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-25 01:19:04 -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-25 01:19:04 -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-25 01:19:04 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 00:22:16 -04:00
|
|
|
func TestBuilderPrepare_BundleDestination(t *testing.T) {
|
|
|
|
b := &Builder{}
|
2018-04-25 08:23:36 -04:00
|
|
|
config, tempfile := testConfig()
|
2018-09-18 11:29:20 -04:00
|
|
|
config["skip_region_validation"] = true
|
2018-04-25 08:23:36 -04:00
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-07-25 00:22:16 -04:00
|
|
|
|
|
|
|
config["bundle_destination"] = ""
|
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-25 00:22:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.BundleDestination != "/tmp" {
|
|
|
|
t.Fatalf("bad: %s", b.config.BundleDestination)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_BundlePrefix(t *testing.T) {
|
|
|
|
b := &Builder{}
|
2018-04-25 08:23:36 -04:00
|
|
|
config, tempfile := testConfig()
|
2018-09-18 11:29:20 -04:00
|
|
|
config["skip_region_validation"] = true
|
2018-04-25 08:23:36 -04:00
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-07-25 00:22:16 -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-25 00:22:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-08-08 20:04:39 -04:00
|
|
|
if b.config.BundlePrefix == "" {
|
2013-07-25 00:22:16 -04:00
|
|
|
t.Fatalf("bad: %s", b.config.BundlePrefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-20 22:40:45 -04:00
|
|
|
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var b Builder
|
2018-04-25 08:23:36 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-07-20 22:40:45 -04:00
|
|
|
|
|
|
|
// 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-20 22:40:45 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
2013-07-21 00:00:12 -04:00
|
|
|
|
2013-07-25 00:22:16 -04:00
|
|
|
func TestBuilderPrepare_S3Bucket(t *testing.T) {
|
|
|
|
b := &Builder{}
|
2018-04-25 08:23:36 -04:00
|
|
|
config, tempfile := testConfig()
|
2018-09-18 11:29:20 -04:00
|
|
|
config["skip_region_validation"] = true
|
2018-04-25 08:23:36 -04:00
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-07-25 00:22:16 -04:00
|
|
|
|
|
|
|
config["s3_bucket"] = ""
|
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-25 00:22:16 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
config["s3_bucket"] = "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-25 00:22:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 00:00:12 -04:00
|
|
|
func TestBuilderPrepare_X509CertPath(t *testing.T) {
|
|
|
|
b := &Builder{}
|
2018-04-25 08:23:36 -04:00
|
|
|
config, tempfile := testConfig()
|
2018-09-18 11:29:20 -04:00
|
|
|
config["skip_region_validation"] = true
|
2018-04-25 08:23:36 -04:00
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-07-21 00:00:12 -04:00
|
|
|
|
|
|
|
config["x509_cert_path"] = ""
|
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-21 00:00:12 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
config["x509_cert_path"] = "i/am/a/file/that/doesnt/exist"
|
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-21 00:00:12 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Error("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
2018-04-25 08:23:36 -04:00
|
|
|
defer tf.Close()
|
2013-07-21 00:00:12 -04:00
|
|
|
|
|
|
|
config["x509_cert_path"] = tf.Name()
|
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-21 00:00:12 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_X509KeyPath(t *testing.T) {
|
|
|
|
b := &Builder{}
|
2018-04-25 08:23:36 -04:00
|
|
|
config, tempfile := testConfig()
|
2018-09-18 11:29:20 -04:00
|
|
|
config["skip_region_validation"] = true
|
2018-04-25 08:23:36 -04:00
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-07-21 00:00:12 -04:00
|
|
|
|
|
|
|
config["x509_key_path"] = ""
|
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-21 00:00:12 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
config["x509_key_path"] = "i/am/a/file/that/doesnt/exist"
|
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-21 00:00:12 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Error("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
2018-04-25 08:23:36 -04:00
|
|
|
defer tf.Close()
|
2013-07-21 00:00:12 -04:00
|
|
|
|
|
|
|
config["x509_key_path"] = tf.Name()
|
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-21 00:00:12 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_X509UploadPath(t *testing.T) {
|
|
|
|
b := &Builder{}
|
2018-04-25 08:23:36 -04:00
|
|
|
config, tempfile := testConfig()
|
2018-09-18 11:29:20 -04:00
|
|
|
config["skip_region_validation"] = true
|
2018-04-25 08:23:36 -04:00
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-07-21 00:00:12 -04:00
|
|
|
|
|
|
|
config["x509_upload_path"] = ""
|
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-25 11:51:38 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
2013-07-21 00:00:12 -04:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 06:04:03 -05:00
|
|
|
|
|
|
|
func TestBuilderPrepare_ReturnGeneratedData(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2020-08-12 04:43:15 -04:00
|
|
|
if len(generatedData) == 0 {
|
|
|
|
t.Fatalf("Generated data should not be empty")
|
|
|
|
}
|
2020-01-16 06:04:03 -05:00
|
|
|
if generatedData[0] != "SourceAMIName" {
|
|
|
|
t.Fatalf("Generated data should contain SourceAMIName")
|
|
|
|
}
|
2020-08-12 04:43:15 -04:00
|
|
|
if generatedData[1] != "BuildRegion" {
|
|
|
|
t.Fatalf("Generated data should contain BuildRegion")
|
|
|
|
}
|
|
|
|
if generatedData[2] != "SourceAMI" {
|
|
|
|
t.Fatalf("Generated data should contain SourceAMI")
|
|
|
|
}
|
|
|
|
if generatedData[3] != "SourceAMICreationDate" {
|
|
|
|
t.Fatalf("Generated data should contain SourceAMICreationDate")
|
|
|
|
}
|
|
|
|
if generatedData[4] != "SourceAMIOwner" {
|
|
|
|
t.Fatalf("Generated data should contain SourceAMIOwner")
|
|
|
|
}
|
|
|
|
if generatedData[5] != "SourceAMIOwnerName" {
|
|
|
|
t.Fatalf("Generated data should contain SourceAMIOwnerName")
|
|
|
|
}
|
2020-01-16 06:04:03 -05:00
|
|
|
}
|