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
|
|
|
|
|
|
|
"github.com/mitchellh/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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2013-07-16 01:23:45 -04:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_SourceAmi(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
c.SourceAmi = ""
|
2013-08-08 17:33:29 -04:00
|
|
|
if err := c.Prepare(nil); len(err) != 1 {
|
2013-07-16 01:23:45 -04:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) != 1 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SpotPriceAutoProduct = "foo"
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_SSHUsername(t *testing.T) {
|
|
|
|
c := testConfig()
|
2015-06-13 18:16:12 -04:00
|
|
|
c.Comm.SSHUsername = ""
|
2013-08-08 17:33:29 -04:00
|
|
|
if err := c.Prepare(nil); len(err) != 1 {
|
2013-07-16 01:23:45 -04:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
c.UserData = "foo"
|
|
|
|
c.UserDataFile = tf.Name()
|
|
|
|
if err := c.Prepare(nil); len(err) != 1 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
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()
|
2013-09-05 15:23:08 -04:00
|
|
|
c.TemporaryKeyPairName = ""
|
2013-09-05 08:28:31 -04:00
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-09-05 15:23:08 -04:00
|
|
|
if c.TemporaryKeyPairName == "" {
|
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`)
|
|
|
|
if !r.MatchString(c.TemporaryKeyPairName) {
|
|
|
|
t.Fatal("keypair name is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.TemporaryKeyPairName = "ssh-key-123"
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.TemporaryKeyPairName != "ssh-key-123" {
|
|
|
|
t.Fatal("keypair name does not match")
|
2013-09-05 08:28:31 -04:00
|
|
|
}
|
|
|
|
}
|