builder/amazonebs: Tests for invalid AMI name

This commit is contained in:
Mitchell Hashimoto 2013-06-17 15:24:33 -07:00
parent 72bb2d68dd
commit 64f9173e8b
3 changed files with 40 additions and 5 deletions

View File

@ -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}
}

View File

@ -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()

View File

@ -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()