From fe0c548619b2288c3a196369ed0b27ff21eb27e0 Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Tue, 16 Jun 2015 11:30:49 -0700 Subject: [PATCH] Added acceptance test for file builder --- builder/file/builder.go | 16 +---- builder/file/builder_test.go | 67 +++++++++++++++++++ builder/file/config_test.go | 4 +- builder/file/test-fixtures/artifact.txt | 1 + .../compress/post-processor_test.go | 21 +++++- 5 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 builder/file/test-fixtures/artifact.txt diff --git a/builder/file/builder.go b/builder/file/builder.go index 9297b456d..9a2c2cc7f 100644 --- a/builder/file/builder.go +++ b/builder/file/builder.go @@ -15,27 +15,13 @@ import ( "github.com/mitchellh/packer/packer" ) -const BuilderId = "cbednarski.file" +const BuilderId = "packer.file" type Builder struct { config *Config runner multistep.Runner } -// Prepare is responsible for configuring the builder and validating -// that configuration. Any setup should be done in this method. Note that -// NO side effects should take place in prepare, it is meant as a state -// setup only. Calling Prepare is not necessarilly followed by a Run. -// -// The parameters to Prepare are a set of interface{} values of the -// configuration. These are almost always `map[string]interface{}` -// parsed from a template, but no guarantee is made. -// -// Each of the configuration values should merge into the final -// configuration. -// -// Prepare should return a list of warnings along with any errors -// that occured while preparing. func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { c, warnings, errs := NewConfig(raws...) if errs != nil { diff --git a/builder/file/builder_test.go b/builder/file/builder_test.go index 63d36a0a5..3ce9e77ae 100644 --- a/builder/file/builder_test.go +++ b/builder/file/builder_test.go @@ -1,11 +1,78 @@ package file import ( + "fmt" + "io/ioutil" "testing" + builderT "github.com/mitchellh/packer/helper/builder/testing" "github.com/mitchellh/packer/packer" ) func TestBuilder_implBuilder(t *testing.T) { var _ packer.Builder = new(Builder) } + +func TestBuilderFileAcc_content(t *testing.T) { + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: fileContentTest, + Check: checkContent, + }) +} + +func TestBuilderFileAcc_copy(t *testing.T) { + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: fileCopyTest, + Check: checkCopy, + }) +} + +func checkContent(artifacts []packer.Artifact) error { + content, err := ioutil.ReadFile("contentTest.txt") + if err != nil { + return err + } + contentString := string(content) + if contentString != "hello world!" { + return fmt.Errorf("Unexpected file contents: %s", contentString) + } + return nil +} + +func checkCopy(artifacts []packer.Artifact) error { + content, err := ioutil.ReadFile("copyTest.txt") + if err != nil { + return err + } + contentString := string(content) + if contentString != "Hello world.\n" { + return fmt.Errorf("Unexpected file contents: %s", contentString) + } + return nil +} + +const fileContentTest = ` +{ + "builders": [ + { + "type":"test", + "target":"contentTest.txt", + "content":"hello world!" + } + ] +} +` + +const fileCopyTest = ` +{ + "builders": [ + { + "type":"test", + "target":"copyTest.txt", + "source":"test-fixtures/artifact.txt" + } + ] +} +` diff --git a/builder/file/config_test.go b/builder/file/config_test.go index 6d8039558..9d8f346fc 100644 --- a/builder/file/config_test.go +++ b/builder/file/config_test.go @@ -1,7 +1,6 @@ package file import ( - "fmt" "strings" "testing" ) @@ -39,8 +38,7 @@ func TestNoContent(t *testing.T) { delete(raw, "content") delete(raw, "source") _, warns, _ := NewConfig(raw) - fmt.Println(len(warns)) - fmt.Printf("%#v\n", warns) + if len(warns) == 0 { t.Error("Expected config warning without any content") } diff --git a/builder/file/test-fixtures/artifact.txt b/builder/file/test-fixtures/artifact.txt new file mode 100644 index 000000000..18249f335 --- /dev/null +++ b/builder/file/test-fixtures/artifact.txt @@ -0,0 +1 @@ +Hello world. diff --git a/post-processor/compress/post-processor_test.go b/post-processor/compress/post-processor_test.go index 92cbfc4b3..5f4d6b9ca 100644 --- a/post-processor/compress/post-processor_test.go +++ b/post-processor/compress/post-processor_test.go @@ -1,3 +1,22 @@ package compress -import () +// import ( +// "testing" +// +// builderT "github.com/mitchellh/packer/helper/builder/testing" +// ) +// +// func TestBuilderTagsAcc_basic(t *testing.T) { +// builderT.Test(t, builderT.TestCase{ +// Builder: &Builder{}, +// Template: simpleTestCase, +// Check: checkTags(), +// }) +// } + +const simpleTestCase = ` +{ + "type": "compress", + "output": "foo.tar.gz" +} +`