From 5ac06e116e581b27bad310f98908fbda65c4bb2f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 10 May 2013 22:47:20 -0700 Subject: [PATCH] packer: Parse "hooks" configuration into the Template --- packer/template.go | 3 +++ packer/template_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/packer/template.go b/packer/template.go index 3312259e3..1db86166a 100644 --- a/packer/template.go +++ b/packer/template.go @@ -12,6 +12,7 @@ import ( type rawTemplate struct { Name string Builders []map[string]interface{} + Hooks map[string][]string Provisioners []map[string]interface{} Outputs []map[string]interface{} } @@ -21,6 +22,7 @@ type rawTemplate struct { type Template struct { Name string Builders map[string]rawBuilderConfig + Hooks map[string][]string } // The rawBuilderConfig struct represents a raw, unprocessed builder @@ -44,6 +46,7 @@ func ParseTemplate(data []byte) (t *Template, err error) { t = &Template{} t.Name = rawTpl.Name t.Builders = make(map[string]rawBuilderConfig) + t.Hooks = rawTpl.Hooks for _, v := range rawTpl.Builders { rawType, ok := v["type"] diff --git a/packer/template_test.go b/packer/template_test.go index bd0e708b3..58e950804 100644 --- a/packer/template_test.go +++ b/packer/template_test.go @@ -89,6 +89,29 @@ func TestParseTemplate_BuilderWithName(t *testing.T) { assert.Equal(builder.builderName, "amazon-ebs", "builder should be amazon-ebs") } +func TestParseTemplate_Hooks(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + data := ` + { + "name": "my-image", + + "hooks": { + "event": ["foo", "bar"] + } + } + ` + + result, err := ParseTemplate([]byte(data)) + assert.Nil(err, "should not error") + assert.NotNil(result, "template should not be nil") + assert.Length(result.Hooks, 1, "should have one hook") + + hooks, ok := result.Hooks["event"] + assert.True(ok, "should have hook") + assert.Equal(hooks, []string{"foo", "bar"}, "hooks should be correct") +} + func TestTemplate_BuildNames(t *testing.T) { assert := asserts.NewTestingAsserts(t, true)