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"
|
2013-07-16 01:23:45 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
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",
|
2013-07-16 02:34:07 -04:00
|
|
|
SSHUsername: "root",
|
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()
|
|
|
|
c.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHPort != 22 {
|
|
|
|
t.Fatalf("invalid value: %d", c.SSHPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHPort != 44 {
|
|
|
|
t.Fatalf("invalid value: %d", c.SSHPort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_SSHTimeout(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
c.RawSSHTimeout = ""
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.RawSSHTimeout = "bad"
|
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_SSHUsername(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
c.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 == "" {
|
|
|
|
t.Fatal("keypair empty")
|
2013-09-05 08:28:31 -04:00
|
|
|
}
|
|
|
|
}
|