2013-07-16 01:23:45 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-08-12 14:52:43 -04:00
|
|
|
"io/ioutil"
|
2013-07-16 02:33:52 -04:00
|
|
|
"os"
|
2016-08-25 03:17:57 -04:00
|
|
|
"regexp"
|
2013-07-16 01:23:45 -04:00
|
|
|
"testing"
|
2015-06-13 18:16:12 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2013-07-16 01:23:45 -04:00
|
|
|
)
|
|
|
|
|
2013-07-16 02:33:52 -04:00
|
|
|
func init() {
|
|
|
|
// Clear out the AWS access key env vars so they don't
|
|
|
|
// affect our tests.
|
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "")
|
|
|
|
os.Setenv("AWS_ACCESS_KEY", "")
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "")
|
|
|
|
os.Setenv("AWS_SECRET_KEY", "")
|
|
|
|
}
|
|
|
|
|
2013-07-16 01:23:45 -04:00
|
|
|
func testConfig() *RunConfig {
|
|
|
|
return &RunConfig{
|
2013-07-16 02:34:07 -04:00
|
|
|
SourceAmi: "abcd",
|
2013-07-16 01:23:45 -04:00
|
|
|
InstanceType: "m1.small",
|
2015-06-13 18:16:12 -04:00
|
|
|
|
|
|
|
Comm: communicator.Config{
|
|
|
|
SSHUsername: "foo",
|
|
|
|
},
|
2013-07-16 01:23:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-20 20:29:51 -04:00
|
|
|
func testConfigFilter() *RunConfig {
|
|
|
|
config := testConfig()
|
|
|
|
config.SourceAmi = ""
|
|
|
|
config.SourceAmiFilter = AmiFilterOptions{}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2013-07-16 01:23:45 -04:00
|
|
|
func TestRunConfigPrepare(t *testing.T) {
|
|
|
|
c := testConfig()
|
2013-08-08 17:33:29 -04:00
|
|
|
err := c.Prepare(nil)
|
2013-07-16 01:23:45 -04:00
|
|
|
if len(err) > 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_InstanceType(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
c.InstanceType = ""
|
2013-08-08 17:33:29 -04:00
|
|
|
if err := c.Prepare(nil); len(err) != 1 {
|
2018-05-15 06:44:58 -04:00
|
|
|
t.Fatalf("Should error if an instance_type is not specified")
|
2013-07-16 01:23:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_SourceAmi(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
c.SourceAmi = ""
|
2018-08-14 15:30:05 -04:00
|
|
|
if err := c.Prepare(nil); len(err) != 2 {
|
2018-05-15 06:44:58 -04:00
|
|
|
t.Fatalf("Should error if a source_ami (or source_ami_filter) is not specified")
|
2013-07-16 01:23:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-20 20:29:51 -04:00
|
|
|
func TestRunConfigPrepare_SourceAmiFilterBlank(t *testing.T) {
|
|
|
|
c := testConfigFilter()
|
2018-08-14 15:30:05 -04:00
|
|
|
if err := c.Prepare(nil); len(err) != 2 {
|
2018-05-15 06:44:58 -04:00
|
|
|
t.Fatalf("Should error if source_ami_filter is empty or not specified (and source_ami is not specified)")
|
2018-08-14 15:30:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_SourceAmiFilterOwnersBlank(t *testing.T) {
|
|
|
|
c := testConfigFilter()
|
|
|
|
filter_key := "name"
|
|
|
|
filter_value := "foo"
|
|
|
|
c.SourceAmiFilter = AmiFilterOptions{Filters: map[*string]*string{&filter_key: &filter_value}}
|
|
|
|
if err := c.Prepare(nil); len(err) != 1 {
|
|
|
|
t.Fatalf("Should error if Owners is not specified)")
|
2016-08-20 20:29:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_SourceAmiFilterGood(t *testing.T) {
|
|
|
|
c := testConfigFilter()
|
|
|
|
owner := "123"
|
|
|
|
filter_key := "name"
|
|
|
|
filter_value := "foo"
|
|
|
|
goodFilter := AmiFilterOptions{Owners: []*string{&owner}, Filters: map[*string]*string{&filter_key: &filter_value}}
|
|
|
|
c.SourceAmiFilter = goodFilter
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 11:13:20 -04:00
|
|
|
func TestRunConfigPrepare_EnableT2UnlimitedGood(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
// Must have a T2 instance type if T2 Unlimited is enabled
|
|
|
|
c.InstanceType = "t2.micro"
|
|
|
|
c.EnableT2Unlimited = true
|
|
|
|
err := c.Prepare(nil)
|
|
|
|
if len(err) > 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_EnableT2UnlimitedBadInstanceType(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
// T2 Unlimited cannot be used with instance types other than T2
|
|
|
|
c.InstanceType = "m5.large"
|
|
|
|
c.EnableT2Unlimited = true
|
|
|
|
err := c.Prepare(nil)
|
|
|
|
if len(err) != 1 {
|
2018-05-15 06:44:58 -04:00
|
|
|
t.Fatalf("Should error if T2 Unlimited is enabled with non-T2 instance_type")
|
2018-05-13 11:13:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_EnableT2UnlimitedBadWithSpotInstanceRequest(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
// T2 Unlimited cannot be used with Spot Instances
|
|
|
|
c.InstanceType = "t2.micro"
|
|
|
|
c.EnableT2Unlimited = true
|
|
|
|
c.SpotPrice = "auto"
|
|
|
|
err := c.Prepare(nil)
|
|
|
|
if len(err) != 1 {
|
2018-05-15 06:44:58 -04:00
|
|
|
t.Fatalf("Should error if T2 Unlimited has been used in conjuntion with a Spot Price request")
|
2018-05-13 11:13:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 13:44:12 -04:00
|
|
|
func TestRunConfigPrepare_SpotAuto(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
c.SpotPrice = "auto"
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2019-06-28 19:34:38 -04:00
|
|
|
|
|
|
|
// Shouldn't error (YET) even though SpotPriceAutoProduct is deprecated
|
|
|
|
c.SpotPriceAutoProduct = "Linux/Unix"
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-09-06 13:44:12 -04:00
|
|
|
}
|
|
|
|
|
2013-07-16 01:23:45 -04:00
|
|
|
func TestRunConfigPrepare_SSHPort(t *testing.T) {
|
|
|
|
c := testConfig()
|
2015-06-13 18:16:12 -04:00
|
|
|
c.Comm.SSHPort = 0
|
2013-08-08 17:33:29 -04:00
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
2013-07-16 01:23:45 -04:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:16:12 -04:00
|
|
|
if c.Comm.SSHPort != 22 {
|
|
|
|
t.Fatalf("invalid value: %d", c.Comm.SSHPort)
|
2013-07-16 01:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-06-13 18:16:12 -04:00
|
|
|
c.Comm.SSHPort = 44
|
2013-08-08 17:33:29 -04:00
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
2013-07-16 01:23:45 -04:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:16:12 -04:00
|
|
|
if c.Comm.SSHPort != 44 {
|
|
|
|
t.Fatalf("invalid value: %d", c.Comm.SSHPort)
|
2013-07-16 01:23:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 14:52:43 -04:00
|
|
|
func TestRunConfigPrepare_UserData(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-04-25 07:39:55 -04:00
|
|
|
defer os.Remove(tf.Name())
|
2013-08-12 14:52:43 -04:00
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
c.UserData = "foo"
|
|
|
|
c.UserDataFile = tf.Name()
|
|
|
|
if err := c.Prepare(nil); len(err) != 1 {
|
2018-05-15 06:44:58 -04:00
|
|
|
t.Fatalf("Should error if user_data string and user_data_file have both been specified")
|
2013-08-12 14:52:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_UserDataFile(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UserDataFile = "idontexistidontthink"
|
|
|
|
if err := c.Prepare(nil); len(err) != 1 {
|
2018-05-15 06:44:58 -04:00
|
|
|
t.Fatalf("Should error if the file specified by user_data_file does not exist")
|
2013-08-12 14:52:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-04-25 07:39:55 -04:00
|
|
|
defer os.Remove(tf.Name())
|
2013-08-12 14:52:43 -04:00
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
c.UserDataFile = tf.Name()
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-09-05 08:28:31 -04:00
|
|
|
|
2013-09-05 15:23:08 -04:00
|
|
|
func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) {
|
2013-09-05 08:28:31 -04:00
|
|
|
c := testConfig()
|
2018-08-28 11:47:02 -04:00
|
|
|
c.Comm.SSHTemporaryKeyPairName = ""
|
2013-09-05 08:28:31 -04:00
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:47:02 -04:00
|
|
|
if c.Comm.SSHTemporaryKeyPairName == "" {
|
2016-08-25 03:17:57 -04:00
|
|
|
t.Fatal("keypair name is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match prefix and UUID, e.g. "packer_5790d491-a0b8-c84c-c9d2-2aea55086550".
|
|
|
|
r := regexp.MustCompile(`\Apacker_(?:(?i)[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}?)\z`)
|
2018-08-28 11:47:02 -04:00
|
|
|
if !r.MatchString(c.Comm.SSHTemporaryKeyPairName) {
|
2016-08-25 03:17:57 -04:00
|
|
|
t.Fatal("keypair name is not valid")
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:47:02 -04:00
|
|
|
c.Comm.SSHTemporaryKeyPairName = "ssh-key-123"
|
2016-08-25 03:17:57 -04:00
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:47:02 -04:00
|
|
|
if c.Comm.SSHTemporaryKeyPairName != "ssh-key-123" {
|
2016-08-25 03:17:57 -04:00
|
|
|
t.Fatal("keypair name does not match")
|
2013-09-05 08:28:31 -04:00
|
|
|
}
|
|
|
|
}
|