Commit d2339b7c authored by Chris Bednarski's avatar Chris Bednarski

Merge pull request #2408 from mitchellh/b-google-image-name

Fix interpolation in google compute image name
parents 4128a49f 1c71eaaa
...@@ -59,6 +59,8 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { ...@@ -59,6 +59,8 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
return nil, nil, err return nil, nil, err
} }
var errs *packer.MultiError
// Set defaults. // Set defaults.
if c.Network == "" { if c.Network == "" {
c.Network = "default" c.Network = "default"
...@@ -73,7 +75,12 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { ...@@ -73,7 +75,12 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
} }
if c.ImageName == "" { if c.ImageName == "" {
c.ImageName = "packer-{{timestamp}}" img, err := interpolate.Render("packer-{{timestamp}}", nil)
if err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Unable to parse image name: %s ", err))
c.ImageName = img
}
} }
if c.InstanceName == "" { if c.InstanceName == "" {
...@@ -96,7 +103,6 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { ...@@ -96,7 +103,6 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
c.Comm.SSHUsername = "root" c.Comm.SSHUsername = "root"
} }
var errs *packer.MultiError
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 { if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
errs = packer.MultiErrorAppend(errs, es...) errs = packer.MultiErrorAppend(errs, es...)
} }
......
...@@ -2,48 +2,10 @@ package googlecompute ...@@ -2,48 +2,10 @@ package googlecompute
import ( import (
"io/ioutil" "io/ioutil"
"strings"
"testing" "testing"
) )
func testConfig(t *testing.T) map[string]interface{} {
return map[string]interface{}{
"account_file": testAccountFile(t),
"project_id": "hashicorp",
"source_image": "foo",
"zone": "us-east-1a",
}
}
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)
}
}
func TestConfigPrepare(t *testing.T) { func TestConfigPrepare(t *testing.T) {
cases := []struct { cases := []struct {
Key string Key string
...@@ -181,6 +143,54 @@ func TestConfigDefaults(t *testing.T) { ...@@ -181,6 +143,54 @@ func TestConfigDefaults(t *testing.T) {
} }
} }
func TestImageName(t *testing.T) {
c, _, _ := NewConfig(testConfig(t))
if strings.Contains(c.ImageName, "{{timestamp}}") {
t.Errorf("ImageName should be interpolated; found %s", c.ImageName)
}
}
// 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",
"zone": "us-east-1a",
}
}
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)
}
}
func testAccountFile(t *testing.T) string { func testAccountFile(t *testing.T) string {
tf, err := ioutil.TempFile("", "packer") tf, err := ioutil.TempFile("", "packer")
if err != nil { if err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment