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"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() *RunConfig {
|
|
|
|
return &RunConfig{
|
|
|
|
AlicloudSourceImage: "alicloud_images",
|
|
|
|
InstanceType: "ecs.n1.tiny",
|
|
|
|
Comm: communicator.Config{
|
2019-06-06 10:44:48 -04:00
|
|
|
SSH: communicator.SSH{
|
|
|
|
SSHUsername: "alicloud",
|
|
|
|
},
|
2017-03-03 03:56:17 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2018-08-28 11:46:41 -04:00
|
|
|
c.Comm.SSHTemporaryKeyPairName = ""
|
2017-03-03 03:56:17 -05:00
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:46:41 -04:00
|
|
|
if c.Comm.SSHTemporaryKeyPairName == "" {
|
2017-03-03 03:56:17 -05:00
|
|
|
t.Fatal("keypair name is empty")
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:46:41 -04:00
|
|
|
c.Comm.SSHTemporaryKeyPairName = "ssh-key-123"
|
2017-03-03 03:56:17 -05:00
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:46:41 -04:00
|
|
|
if c.Comm.SSHTemporaryKeyPairName != "ssh-key-123" {
|
2017-03-03 03:56:17 -05:00
|
|
|
t.Fatal("keypair name does not match")
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 04:56:57 -04:00
|
|
|
|
|
|
|
func TestRunConfigPrepare_SSHPrivateIp(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if c.SSHPrivateIp != false {
|
|
|
|
t.Fatalf("invalid value, expected: %t, actul: %t", false, c.SSHPrivateIp)
|
|
|
|
}
|
|
|
|
c.SSHPrivateIp = true
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if c.SSHPrivateIp != true {
|
|
|
|
t.Fatalf("invalid value, expected: %t, actul: %t", true, c.SSHPrivateIp)
|
|
|
|
}
|
|
|
|
c.SSHPrivateIp = false
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if c.SSHPrivateIp != false {
|
|
|
|
t.Fatalf("invalid value, expected: %t, actul: %t", false, c.SSHPrivateIp)
|
|
|
|
}
|
|
|
|
}
|
2018-09-25 07:21:28 -04:00
|
|
|
|
|
|
|
func TestRunConfigPrepare_DisableStopInstance(t *testing.T) {
|
|
|
|
c := testConfig()
|
|
|
|
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if c.DisableStopInstance != false {
|
|
|
|
t.Fatalf("invalid value, expected: %t, actul: %t", false, c.DisableStopInstance)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.DisableStopInstance = true
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if c.DisableStopInstance != true {
|
|
|
|
t.Fatalf("invalid value, expected: %t, actul: %t", true, c.DisableStopInstance)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.DisableStopInstance = false
|
|
|
|
if err := c.Prepare(nil); len(err) != 0 {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if c.DisableStopInstance != false {
|
|
|
|
t.Fatalf("invalid value, expected: %t, actul: %t", false, c.DisableStopInstance)
|
|
|
|
}
|
|
|
|
}
|