2017-04-06 05:19:17 -04:00
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2017-04-06 05:19:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2020-09-09 04:27:48 -04:00
|
|
|
"project_id": "00000000-1111-2222-3333-444444444444",
|
|
|
|
"access_key": "SCWABCXXXXXXXXXXXXXX",
|
|
|
|
"secret_key": "00000000-1111-2222-3333-444444444444",
|
|
|
|
"zone": "fr-par-1",
|
2018-05-28 10:18:59 -04:00
|
|
|
"commercial_type": "START1-S",
|
2017-04-11 06:19:28 -04:00
|
|
|
"ssh_username": "root",
|
|
|
|
"image": "image-uuid",
|
2017-04-06 05:19:17 -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 {
|
2017-04-06 05:19:17 -04:00
|
|
|
t.Fatalf("Builder should be a builder")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_Prepare_BadType(t *testing.T) {
|
|
|
|
b := &Builder{}
|
|
|
|
c := map[string]interface{}{
|
|
|
|
"api_token": []string{},
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
_, _, err := b.Prepare(c)
|
2017-04-06 05:19:17 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("prepare should fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 11:22:55 -04:00
|
|
|
func TestBuilderPrepare(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
_, warnings, err := b.Prepare(config)
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("should not have errors")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 05:19:17 -04:00
|
|
|
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["i_should_not_be_valid"] = true
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2017-04-06 05:19:17 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
func TestBuilderPrepare_Zone(t *testing.T) {
|
2017-04-06 05:19:17 -04:00
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
delete(config, "zone")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2017-04-06 05:19:17 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error")
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
expected := "fr-par-1"
|
2017-04-06 05:19:17 -04:00
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
config["zone"] = expected
|
2017-04-06 05:19:17 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2017-04-06 05:19:17 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
if b.config.Zone != expected {
|
|
|
|
t.Errorf("found %s, expected %s", b.config.Zone, expected)
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_CommercialType(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "commercial_type")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2017-04-06 05:19:17 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error")
|
|
|
|
}
|
|
|
|
|
2018-05-28 10:18:59 -04:00
|
|
|
expected := "START1-S"
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
config["commercial_type"] = expected
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2017-04-06 05:19:17 -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.CommercialType != expected {
|
|
|
|
t.Errorf("found %s, expected %s", b.config.CommercialType, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_Image(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "image")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2017-04-06 05:19:17 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "cc586e45-5156-4f71-b223-cf406b10dd1c"
|
|
|
|
|
|
|
|
config["image"] = expected
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2017-04-06 05:19:17 -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_SnapshotName(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2017-04-06 05:19:17 -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.SnapshotName == "" {
|
|
|
|
t.Errorf("invalid: %s", b.config.SnapshotName)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["snapshot_name"] = "foobarbaz"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2017-04-06 05:19:17 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["snapshot_name"] = "{{timestamp}}"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2017-04-06 05:19:17 -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.SnapshotName, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse int in template: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ServerName(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := b.Prepare(config)
|
2017-04-06 05:19:17 -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.ServerName == "" {
|
|
|
|
t.Errorf("invalid: %s", b.config.ServerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["server_name"] = "foobar"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2017-04-06 05:19:17 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["server_name"] = "foobar-{{timestamp}}"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2017-04-06 05:19:17 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["server_name"] = "foobar-{{"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err = b.Prepare(config)
|
2017-04-06 05:19:17 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|