2013-12-12 22:46:21 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2014-09-05 12:47:20 -04:00
|
|
|
"io/ioutil"
|
2015-07-07 18:18:31 -04:00
|
|
|
"strings"
|
2013-12-12 22:46:21 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigPrepare(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Key string
|
|
|
|
Value interface{}
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"unknown_key",
|
|
|
|
"bad",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"private_key_file",
|
|
|
|
"/tmp/i/should/not/exist",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"project_id",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"project_id",
|
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"source_image",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"source_image",
|
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2016-11-13 10:53:45 -05:00
|
|
|
{
|
|
|
|
"source_image_family",
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"source_image_family",
|
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2013-12-12 22:46:21 -05:00
|
|
|
{
|
|
|
|
"zone",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"zone",
|
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"ssh_timeout",
|
|
|
|
"SO BAD",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ssh_timeout",
|
|
|
|
"5s",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"state_timeout",
|
|
|
|
"SO BAD",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"state_timeout",
|
|
|
|
"5s",
|
|
|
|
false,
|
|
|
|
},
|
2015-05-29 17:50:11 -04:00
|
|
|
{
|
|
|
|
"use_internal_ip",
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"use_internal_ip",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"use_internal_ip",
|
|
|
|
"SO VERY BAD",
|
|
|
|
true,
|
|
|
|
},
|
2015-12-05 00:17:42 -05:00
|
|
|
{
|
|
|
|
"preemptible",
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"preemptible",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"preemptible",
|
|
|
|
"SO VERY BAD",
|
|
|
|
true,
|
|
|
|
},
|
2016-05-06 07:43:39 -04:00
|
|
|
{
|
|
|
|
"image_family",
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"image_family",
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"image_family",
|
|
|
|
"foo-bar",
|
|
|
|
false,
|
|
|
|
},
|
2016-05-06 08:39:55 -04:00
|
|
|
{
|
|
|
|
"image_family",
|
|
|
|
"foo bar",
|
|
|
|
true,
|
|
|
|
},
|
2016-10-22 01:45:33 -04:00
|
|
|
{
|
|
|
|
"scopes",
|
|
|
|
[]string{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"scopes",
|
|
|
|
[]string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.full_control", "https://www.googleapis.com/auth/sqlservice.admin"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"scopes",
|
|
|
|
[]string{"https://www.googleapis.com/auth/cloud-platform"},
|
|
|
|
false,
|
|
|
|
},
|
2013-12-12 22:46:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
raw := testConfig(t)
|
|
|
|
|
|
|
|
if tc.Value == nil {
|
|
|
|
delete(raw, tc.Key)
|
|
|
|
} else {
|
|
|
|
raw[tc.Key] = tc.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
_, warns, errs := NewConfig(raw)
|
|
|
|
|
|
|
|
if tc.Err {
|
|
|
|
testConfigErr(t, warns, errs, tc.Key)
|
|
|
|
} else {
|
|
|
|
testConfigOk(t, warns, errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-05 12:47:20 -04:00
|
|
|
|
2015-06-29 12:56:33 -04:00
|
|
|
func TestConfigDefaults(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Read func(c *Config) interface{}
|
|
|
|
Value interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
func(c *Config) interface{} { return c.Comm.Type },
|
|
|
|
"ssh",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
func(c *Config) interface{} { return c.Comm.SSHPort },
|
|
|
|
22,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
raw := testConfig(t)
|
|
|
|
|
|
|
|
c, warns, errs := NewConfig(raw)
|
|
|
|
testConfigOk(t, warns, errs)
|
|
|
|
|
|
|
|
actual := tc.Read(c)
|
|
|
|
if actual != tc.Value {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 18:18:31 -04:00
|
|
|
func TestImageName(t *testing.T) {
|
|
|
|
c, _, _ := NewConfig(testConfig(t))
|
2016-01-19 14:12:19 -05:00
|
|
|
if !strings.HasPrefix(c.ImageName, "packer-") {
|
|
|
|
t.Fatalf("ImageName should have 'packer-' prefix, found %s", c.ImageName)
|
|
|
|
}
|
2015-07-07 18:18:31 -04:00
|
|
|
if strings.Contains(c.ImageName, "{{timestamp}}") {
|
|
|
|
t.Errorf("ImageName should be interpolated; found %s", c.ImageName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 03:54:48 -05:00
|
|
|
func TestRegion(t *testing.T) {
|
|
|
|
c, _, _ := NewConfig(testConfig(t))
|
|
|
|
if c.Region != "us-east1" {
|
|
|
|
t.Fatalf("Region should be 'us-east1' given Zone of 'us-east1-a', but is %s", c.Region)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 18:18:31 -04:00
|
|
|
// Helper stuff below
|
|
|
|
|
|
|
|
func testConfig(t *testing.T) map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"account_file": testAccountFile(t),
|
|
|
|
"project_id": "hashicorp",
|
|
|
|
"source_image": "foo",
|
2016-11-15 16:06:15 -05:00
|
|
|
"ssh_username": "root",
|
2016-05-06 07:43:39 -04:00
|
|
|
"image_family": "bar",
|
2016-02-11 03:54:48 -05:00
|
|
|
"zone": "us-east1-a",
|
2015-07-07 18:18:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConfigStruct(t *testing.T) *Config {
|
|
|
|
c, warns, errs := NewConfig(testConfig(t))
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", len(warns))
|
|
|
|
}
|
|
|
|
if errs != nil {
|
|
|
|
t.Fatalf("bad: %#v", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConfigErr(t *testing.T, warns []string, err error, extra string) {
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error: %s", extra)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConfigOk(t *testing.T, warns []string, err error) {
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 12:47:20 -04:00
|
|
|
func testAccountFile(t *testing.T) string {
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
if _, err := tf.Write([]byte(testAccountContent)); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tf.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is just some dummy data that doesn't actually work (it was revoked
|
|
|
|
// a long time ago).
|
|
|
|
const testAccountContent = `{}`
|