2013-06-13 11:58:06 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2013-06-17 08:21:15 -04:00
|
|
|
"strconv"
|
2013-06-13 11:58:06 -04:00
|
|
|
"testing"
|
2015-06-13 18:26:13 -04:00
|
|
|
"time"
|
2013-06-13 11:58:06 -04:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2015-06-10 17:28:05 -04:00
|
|
|
)
|
2013-07-12 03:47:45 -04:00
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2016-11-15 16:06:15 -05:00
|
|
|
"api_token": "bar",
|
|
|
|
"region": "nyc2",
|
|
|
|
"size": "512mb",
|
|
|
|
"ssh_username": "root",
|
|
|
|
"image": "foo",
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Builder{}
|
2020-12-01 16:42:11 -05:00
|
|
|
if _, ok := raw.(packersdk.Builder); !ok {
|
2013-06-13 11:58:06 -04:00
|
|
|
t.Fatalf("Builder should be a builder")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_Prepare_BadType(t *testing.T) {
|
|
|
|
b := &Builder{}
|
|
|
|
c := map[string]interface{}{
|
|
|
|
"api_key": []string{},
|
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(c)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("prepare should fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 20:28:56 -04:00
|
|
|
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Add a random key
|
|
|
|
config["i_should_not_be_valid"] = true
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-07-13 20:28:56 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:33:31 -04:00
|
|
|
func TestBuilderPrepare_Region(t *testing.T) {
|
2013-06-13 11:58:06 -04:00
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2015-06-10 22:31:48 -04:00
|
|
|
delete(config, "region")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2015-06-10 21:54:51 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error")
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
2014-04-29 23:33:31 -04:00
|
|
|
expected := "sfo1"
|
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
// Test set
|
2014-04-29 23:33:31 -04:00
|
|
|
config["region"] = expected
|
2013-06-13 11:58:06 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:33:31 -04:00
|
|
|
if b.config.Region != expected {
|
|
|
|
t.Errorf("found %s, expected %s", b.config.Region, expected)
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:33:31 -04:00
|
|
|
func TestBuilderPrepare_Size(t *testing.T) {
|
2013-06-13 11:58:06 -04:00
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2015-06-10 22:31:48 -04:00
|
|
|
delete(config, "size")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2015-06-10 21:54:51 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error")
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
2014-04-29 23:33:31 -04:00
|
|
|
expected := "1024mb"
|
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
// Test set
|
2014-04-29 23:33:31 -04:00
|
|
|
config["size"] = expected
|
2013-06-13 11:58:06 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:33:31 -04:00
|
|
|
if b.config.Size != expected {
|
|
|
|
t.Errorf("found %s, expected %s", b.config.Size, expected)
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:33:31 -04:00
|
|
|
func TestBuilderPrepare_Image(t *testing.T) {
|
2013-06-13 11:58:06 -04:00
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2015-06-10 22:31:48 -04:00
|
|
|
delete(config, "image")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2015-06-10 21:54:51 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
2014-04-29 23:33:31 -04:00
|
|
|
expected := "ubuntu-14-04-x64"
|
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
// Test set
|
2014-04-29 23:33:31 -04:00
|
|
|
config["image"] = expected
|
2013-06-13 11:58:06 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:33:31 -04:00
|
|
|
if b.config.Image != expected {
|
|
|
|
t.Errorf("found %s, expected %s", b.config.Image, expected)
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 06:51:51 -04:00
|
|
|
func TestBuilderPrepare_StateTimeout(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-23 06:51:51 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:26:13 -04:00
|
|
|
if b.config.StateTimeout != 6*time.Minute {
|
|
|
|
t.Errorf("invalid: %s", b.config.StateTimeout)
|
2013-06-23 06:51:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["state_timeout"] = "5m"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-23 06:51:51 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
config["state_timeout"] = "tubes"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-23 06:51:51 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2019-07-04 10:28:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_SnapshotTimeout(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2019-07-04 10:28:09 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SnapshotTimeout != 60*time.Minute {
|
|
|
|
t.Errorf("invalid: %s", b.config.SnapshotTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["snapshot_timeout"] = "15m"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2019-07-04 10:28:09 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
2013-06-23 06:51:51 -04:00
|
|
|
|
2019-07-04 10:28:09 -04:00
|
|
|
// Test bad
|
|
|
|
config["snapshot_timeout"] = "badstring"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2019-07-04 10:28:09 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-06-23 06:51:51 -04:00
|
|
|
}
|
|
|
|
|
2013-12-27 13:26:27 -05:00
|
|
|
func TestBuilderPrepare_PrivateNetworking(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2013-12-27 13:26:27 -05:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.PrivateNetworking != false {
|
2014-12-15 12:54:51 -05:00
|
|
|
t.Errorf("invalid: %t", b.config.PrivateNetworking)
|
2013-12-27 13:26:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["private_networking"] = true
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-12-27 13:26:27 -05:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.PrivateNetworking != true {
|
2014-12-15 12:54:51 -05:00
|
|
|
t.Errorf("invalid: %t", b.config.PrivateNetworking)
|
2013-12-27 13:26:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
func TestBuilderPrepare_SnapshotName(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2013-06-17 08:21:15 -04:00
|
|
|
// Test default
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-08-08 18:42:19 -04:00
|
|
|
if b.config.SnapshotName == "" {
|
|
|
|
t.Errorf("invalid: %s", b.config.SnapshotName)
|
2013-06-17 08:21:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["snapshot_name"] = "foobarbaz"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-17 08:21:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
2013-06-17 08:21:15 -04:00
|
|
|
|
|
|
|
// Test set with template
|
2013-08-08 18:42:19 -04:00
|
|
|
config["snapshot_name"] = "{{timestamp}}"
|
2013-06-17 08:21:15 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-03 00:03:59 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-17 08:21:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = strconv.ParseInt(b.config.SnapshotName, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse int in template: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
2013-11-03 11:35:58 -05:00
|
|
|
|
|
|
|
func TestBuilderPrepare_DropletName(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2013-11-03 11:35:58 -05:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.DropletName == "" {
|
|
|
|
t.Errorf("invalid: %s", b.config.DropletName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test normal set
|
|
|
|
config["droplet_name"] = "foobar"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-03 11:35:58 -05:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with template
|
|
|
|
config["droplet_name"] = "foobar-{{timestamp}}"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-03 11:35:58 -05:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with bad template
|
|
|
|
config["droplet_name"] = "foobar-{{"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2013-11-03 11:35:58 -05:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-10-19 16:19:11 -04:00
|
|
|
|
|
|
|
func TestBuilderPrepare_VPCUUID(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test with the case vpc_uuid is defined but private_networking is not enabled
|
|
|
|
config["vpc_uuid"] = "554c41b3-425f-5403-8860-7f24fb108098"
|
|
|
|
_, warnings, err := b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should have error: 'private networking should be enabled to use vpc_uuid'")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with the case both vpc_uuid and private_networking are defined/enabled
|
|
|
|
config["private_networking"] = true
|
|
|
|
b = Builder{}
|
|
|
|
_, warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("should not have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ConnectWithPrivateIP(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test with the case connect_with_private_ip is defined but private_networking is not enabled
|
|
|
|
config["connect_with_private_ip"] = true
|
|
|
|
_, warnings, err := b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should have error: 'private networking should be enabled to use connect_with_private_ip'")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with the case both connect_with_private_ip and private_networking are enabled
|
|
|
|
config["private_networking"] = true
|
|
|
|
b = Builder{}
|
|
|
|
_, warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("should not have error")
|
|
|
|
}
|
|
|
|
}
|