From 298a7cdbe47c29146f1f1f3839836bfbab4c1661 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 15 Apr 2013 15:48:42 -0700 Subject: [PATCH] Basic template parsing, tests, build command progress --- packer/build_command.go | 23 +++++++++++++++++++++ packer/build_command_test.go | 31 ++++++++++++++++++++++++++++ packer/template.go | 12 +++++++---- packer/template_test.go | 40 ++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 packer/build_command_test.go create mode 100644 packer/template_test.go diff --git a/packer/build_command.go b/packer/build_command.go index fb1fd90ba..8cdb00a8f 100644 --- a/packer/build_command.go +++ b/packer/build_command.go @@ -1,8 +1,31 @@ package packer +import ( + "io/ioutil" +) + type buildCommand byte func (buildCommand) Run(env *Environment, args []string) int { + if len(args) != 1 { + // TODO: Error message + return 1 + } + + // Read the file into a byte array so that we can parse the template + tplData, err := ioutil.ReadFile(args[0]) + if err != nil { + // TODO: Error message + return 1 + } + + // Parse the template into a machine-usable format + _, err = ParseTemplate(tplData) + if err != nil { + // TODO: error message + return 1 + } + return 0 } diff --git a/packer/build_command_test.go b/packer/build_command_test.go new file mode 100644 index 000000000..499d21230 --- /dev/null +++ b/packer/build_command_test.go @@ -0,0 +1,31 @@ +package packer + +import ( + "cgl.tideland.biz/asserts" + "testing" +) + +func TestBuildCommand_Run_NoArgs(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + command := new(buildCommand) + result := command.Run(testEnvironment(), make([]string, 0)) + assert.Equal(result, 1, "no args should error") +} + +func TestBuildCommand_Run_MoreThanOneArg(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + command := new(buildCommand) + + args := []string{"one", "two"} + result := command.Run(testEnvironment(), args) + assert.Equal(result, 1, "More than one arg should fail") +} + +func TestBuildCommand_Run_MissingFile(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + command := new(buildCommand) + + args := []string{"i-better-not-exist"} + result := command.Run(testEnvironment(), args) + assert.Equal(result, 1, "a non-existent file should error") +} diff --git a/packer/template.go b/packer/template.go index 0f2cf5327..6f0fd9776 100644 --- a/packer/template.go +++ b/packer/template.go @@ -27,12 +27,16 @@ type rawBuilderConfig struct { rawConfig interface{} } -func parseTemplate(data []byte) error { +func ParseTemplate(data []byte) (t *Template, err error) { var rawTpl rawTemplate - err := json.Unmarshal(data, &rawTpl) + err = json.Unmarshal(data, &rawTpl) if err != nil { - return err + return } - return nil + t = &Template{ + Name: rawTpl.Name, + } + + return } diff --git a/packer/template_test.go b/packer/template_test.go new file mode 100644 index 000000000..64420c866 --- /dev/null +++ b/packer/template_test.go @@ -0,0 +1,40 @@ +package packer + +import ( + "cgl.tideland.biz/asserts" + "testing" +) + +func TestParseTemplate_Basic(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + data := ` + { + "name": "my-image", + "builders": [] + } + ` + + result, err := ParseTemplate([]byte(data)) + assert.Nil(err, "should not error") + assert.NotNil(result, "template should not be nil") + assert.Equal(result.Name, "my-image", "name should be correct") + assert.Length(result.Builders, 0, "no builders") +} + +func TestParseTemplate_Invalid(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + // Note there is an extra comma below for a purposeful + // syntax error in the JSON. + data := ` + { + "name": "my-image",, + "builders": [] + } + ` + + result, err := ParseTemplate([]byte(data)) + assert.NotNil(err, "should have an error") + assert.Nil(result, "should have no result") +}