Commit 64f9173e authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazonebs: Tests for invalid AMI name

parent 72bb2d68
......@@ -15,6 +15,7 @@ import (
"github.com/mitchellh/packer/builder/common"
"github.com/mitchellh/packer/packer"
"log"
"text/template"
"time"
)
......@@ -98,6 +99,15 @@ func (b *Builder) Prepare(raws ...interface{}) error {
errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
}
if b.config.AMIName == "" {
errs = append(errs, errors.New("ami_name must be specified"))
} else {
_, err = template.New("ami").Parse(b.config.AMIName)
if err != nil {
errs = append(errs, fmt.Errorf("Failed parsing ami_name: %s", err))
}
}
if len(errs) > 0 {
return &packer.MultiError{errs}
}
......
......@@ -13,6 +13,7 @@ func testConfig() map[string]interface{} {
"instance_type": "foo",
"region": "us-east-1",
"ssh_username": "root",
"ami_name": "foo",
}
}
......@@ -60,6 +61,34 @@ func TestBuilderPrepare_AccessKey(t *testing.T) {
}
}
func TestBuilderPrepare_AMIName(t *testing.T) {
var b Builder
config := testConfig()
// Test good
config["ami_name"] = "foo"
err := b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
// Test bad
config["ami_name"] = "foo {{"
b = Builder{}
err = b.Prepare(config)
if err == nil {
t.Fatal("should have error")
}
// Test bad
delete(config, "ami_name")
b = Builder{}
err = b.Prepare(config)
if err == nil {
t.Fatal("should have error")
}
}
func TestBuilderPrepare_InstanceType(t *testing.T) {
var b Builder
config := testConfig()
......
......@@ -29,11 +29,7 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction {
strconv.FormatInt(time.Now().UTC().Unix(), 10),
}
t, err := template.New("ami").Parse(config.AMIName)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
t := template.Must(template.New("ami").Parse(config.AMIName))
t.Execute(amiNameBuf, tData)
amiName := amiNameBuf.String()
......
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