2013-06-13 11:58:06 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-07-12 03:47:45 -04:00
|
|
|
"os"
|
2013-06-17 08:21:15 -04:00
|
|
|
"strconv"
|
2013-06-13 11:58:06 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-07-12 03:47:45 -04:00
|
|
|
func init() {
|
|
|
|
// Clear out the credential env vars
|
|
|
|
os.Setenv("DIGITALOCEAN_API_KEY", "")
|
|
|
|
os.Setenv("DIGITALOCEAN_CLIENT_ID", "")
|
|
|
|
}
|
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"client_id": "foo",
|
|
|
|
"api_key": "bar",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Builder{}
|
|
|
|
if _, ok := raw.(packer.Builder); !ok {
|
|
|
|
t.Fatalf("Builder should be a builder")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_Prepare_BadType(t *testing.T) {
|
|
|
|
b := &Builder{}
|
|
|
|
c := map[string]interface{}{
|
|
|
|
"api_key": []string{},
|
|
|
|
}
|
|
|
|
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(c)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_APIKey(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["api_key"] = "foo"
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.APIKey != "foo" {
|
|
|
|
t.Errorf("access key invalid: %s", b.config.APIKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
delete(config, "api_key")
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-07-12 03:47:45 -04:00
|
|
|
|
|
|
|
// Test env variable
|
|
|
|
delete(config, "api_key")
|
|
|
|
os.Setenv("DIGITALOCEAN_API_KEY", "foo")
|
|
|
|
defer os.Setenv("DIGITALOCEAN_API_KEY", "")
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-07-12 03:47:45 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ClientID(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["client_id"] = "foo"
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.ClientID != "foo" {
|
|
|
|
t.Errorf("invalid: %s", b.config.ClientID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
delete(config, "client_id")
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-07-12 03:47:45 -04:00
|
|
|
|
|
|
|
// Test env variable
|
|
|
|
delete(config, "client_id")
|
|
|
|
os.Setenv("DIGITALOCEAN_CLIENT_ID", "foo")
|
|
|
|
defer os.Setenv("DIGITALOCEAN_CLIENT_ID", "")
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-07-12 03:47:45 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
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
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
func TestBuilderPrepare_RegionID(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.RegionID != 1 {
|
|
|
|
t.Errorf("invalid: %d", b.config.RegionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["region_id"] = 2
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.RegionID != 2 {
|
|
|
|
t.Errorf("invalid: %d", b.config.RegionID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_SizeID(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SizeID != 66 {
|
|
|
|
t.Errorf("invalid: %d", b.config.SizeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["size_id"] = 67
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SizeID != 67 {
|
|
|
|
t.Errorf("invalid: %d", b.config.SizeID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ImageID(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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-06-15 14:11:03 -04:00
|
|
|
if b.config.SizeID != 66 {
|
2013-06-13 11:58:06 -04:00
|
|
|
t.Errorf("invalid: %d", b.config.SizeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["size_id"] = 2
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SizeID != 2 {
|
|
|
|
t.Errorf("invalid: %d", b.config.SizeID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_SSHUsername(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SSHUsername != "root" {
|
|
|
|
t.Errorf("invalid: %d", b.config.SSHUsername)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
2013-06-15 14:11:03 -04:00
|
|
|
config["ssh_username"] = "foo"
|
2013-06-13 11:58:06 -04:00
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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-06-15 14:11:03 -04:00
|
|
|
if b.config.SSHUsername != "foo" {
|
|
|
|
t.Errorf("invalid: %s", b.config.SSHUsername)
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_SSHTimeout(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.RawSSHTimeout != "1m" {
|
|
|
|
t.Errorf("invalid: %d", b.config.RawSSHTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["ssh_timeout"] = "30s"
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
config["ssh_timeout"] = "tubes"
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-23 06:51:51 -04:00
|
|
|
func TestBuilderPrepare_StateTimeout(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-06-24 12:25:00 -04:00
|
|
|
if b.config.RawStateTimeout != "6m" {
|
2013-06-23 06:51:51 -04:00
|
|
|
t.Errorf("invalid: %d", b.config.RawStateTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["state_timeout"] = "5m"
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-27 13:26:27 -05:00
|
|
|
func TestBuilderPrepare_PrivateNetworking(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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 {
|
|
|
|
t.Errorf("invalid: %s", b.config.PrivateNetworking)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["private_networking"] = true
|
|
|
|
b = Builder{}
|
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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 {
|
|
|
|
t.Errorf("invalid: %s", b.config.PrivateNetworking)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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
|
|
|
|
warnings, err := b.Prepare(config)
|
|
|
|
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{}
|
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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{}
|
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
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{}
|
|
|
|
warnings, err = b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|