2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2018-04-25 07:30:54 -04:00
|
|
|
"os"
|
2017-03-03 03:56:17 -05:00
|
|
|
"testing"
|
|
|
|
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() *RunConfig {
|
|
|
|
return &RunConfig{
|
|
|
|
AlicloudSourceImage: "alicloud_images",
|
|
|
|
InstanceType: "ecs.n1.tiny",
|
|
|
|
Comm: communicator.Config{
|
|
|
|
SSHUsername: "alicloud",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
err := c.Prepare(nil)
|
|
|
|
if len(err) > 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_InstanceType(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
c.InstanceType = ""
|
|
|
|
if err := c.Prepare(nil); len(err) != 1 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_SourceECSImage(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
c.AlicloudSourceImage = ""
|
|
|
|
if err := c.Prepare(nil); len(err) != 1 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_SSHPort(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
c.Comm.SSHPort = 0
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Comm.SSHPort != 22 {
|
|
|
|
t.Fatalf("invalid value: %d", c.Comm.SSHPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Comm.SSHPort = 44
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Comm.SSHPort != 44 {
|
|
|
|
t.Fatalf("invalid value: %d", c.Comm.SSHPort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:30:54 -04:00
|
|
|
defer os.Remove(tf.Name())
|
2017-03-03 03:56:17 -05:00
|
|
|
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)
|
|
|
|
}
|
2018-04-25 07:30:54 -04:00
|
|
|
defer os.Remove(tf.Name())
|
2017-03-03 03:56:17 -05:00
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
c.UserDataFile = tf.Name()
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
c.TemporaryKeyPairName = ""
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.TemporaryKeyPairName == "" {
|
|
|
|
t.Fatal("keypair name is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|