2019-04-15 17:22:25 -04:00
|
|
|
package linode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"linode_token": "bar",
|
|
|
|
"region": "us-east",
|
|
|
|
"instance_type": "g6-nanode-1",
|
|
|
|
"ssh_username": "root",
|
|
|
|
"image": "linode/alpine3.9",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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{}{
|
|
|
|
"linode_token": []string{},
|
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(c)
|
2019-04-15 17:22:25 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("prepare should fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-04-15 17:22:25 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_Region(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
|
|
|
delete(config, "region")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2019-04-15 17:22:25 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "us-east"
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["region"] = expected
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2019-04-15 17:22:25 -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.Region != expected {
|
|
|
|
t.Errorf("found %s, expected %s", b.config.Region, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_Size(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
|
|
|
delete(config, "instance_type")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2019-04-15 17:22:25 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "g6-nanode-1"
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["instance_type"] = expected
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2019-04-15 17:22:25 -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.InstanceType != expected {
|
|
|
|
t.Errorf("found %s, expected %s", b.config.InstanceType, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_Image(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
|
|
|
delete(config, "image")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2019-04-15 17:22:25 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "linode/alpine3.9"
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["image"] = expected
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2019-04-15 17:22:25 -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.Image != expected {
|
|
|
|
t.Errorf("found %s, expected %s", b.config.Image, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ImageLabel(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2019-04-15 17:22:25 -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.ImageLabel == "" {
|
|
|
|
t.Errorf("invalid: %s", b.config.ImageLabel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config["image_label"] = "foobarbaz"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2019-04-15 17:22:25 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set with template
|
|
|
|
config["image_label"] = "{{timestamp}}"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2019-04-15 17:22:25 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = strconv.ParseInt(b.config.ImageLabel, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse int in template: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_Label(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test default
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2019-04-15 17:22:25 -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.Label == "" {
|
|
|
|
t.Errorf("invalid: %s", b.config.Label)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test normal set
|
|
|
|
config["instance_label"] = "foobar"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2019-04-15 17:22:25 -04: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["instance_label"] = "foobar-{{timestamp}}"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2019-04-15 17:22:25 -04: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["instance_label"] = "foobar-{{"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2019-04-15 17:22:25 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|