diff --git a/test/helper/core.go b/test/helper/core.go new file mode 100644 index 000000000..052610b0e --- /dev/null +++ b/test/helper/core.go @@ -0,0 +1,66 @@ +package helper + +import ( + "bytes" + amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" + "github.com/hashicorp/packer/command" + "github.com/hashicorp/packer/packer" + fileprovisioner "github.com/hashicorp/packer/provisioner/file" + "github.com/hashicorp/packer/provisioner/shell" + "os" + "testing" +) + +// fileExists returns true if the filename is found +func FileExists(filename string) bool { + if _, err := os.Stat(filename); err == nil { + return true + } + return false +} + +// testCoreConfigBuilder creates a packer CoreConfig that has a file builder +// available. This allows us to test a builder that writes files to disk. +func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { + components := packer.ComponentFinder{ + BuilderStore: packer.MapOfBuilder{ + "amazon-ebs": func() (packer.Builder, error) { return &amazonebsbuilder.Builder{}, nil }, + }, + ProvisionerStore: packer.MapOfProvisioner{ + "shell": func() (packer.Provisioner, error) { return &shell.Provisioner{}, nil }, + "file": func() (packer.Provisioner, error) { return &fileprovisioner.Provisioner{}, nil }, + }, + PostProcessorStore: packer.MapOfPostProcessor{}, + } + return &packer.CoreConfig{ + Components: components, + } +} + +// TestMetaFile creates a Meta object that includes a file builder +func TestMetaFile(t *testing.T) command.Meta { + var out, err bytes.Buffer + return command.Meta{ + CoreConfig: testCoreConfigBuilder(t), + Ui: &packer.BasicUi{ + Writer: &out, + ErrorWriter: &err, + }, + } +} + +func CleanupFiles(moreFiles ...string) { + for _, file := range moreFiles { + os.RemoveAll(file) + } +} + +func FatalCommand(t *testing.T, m command.Meta) { + ui := m.Ui.(*packer.BasicUi) + out := ui.Writer.(*bytes.Buffer) + err := ui.ErrorWriter.(*bytes.Buffer) + t.Fatalf( + "Bad exit code.\n\nStdout:\n\n%s\n\nStderr:\n\n%s", + out.String(), + err.String()) +} \ No newline at end of file diff --git a/test/provisioner/shell/provisioner_test.go b/test/provisioner/shell/provisioner_test.go index 419acc77d..8cb801b90 100644 --- a/test/provisioner/shell/provisioner_test.go +++ b/test/provisioner/shell/provisioner_test.go @@ -3,13 +3,8 @@ package shell_integration import ( - "bytes" "github.com/hashicorp/go-uuid" - amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" "github.com/hashicorp/packer/command" - "github.com/hashicorp/packer/packer" - fileprovisioner "github.com/hashicorp/packer/provisioner/file" - "github.com/hashicorp/packer/provisioner/shell" testshelper "github.com/hashicorp/packer/test/helper" "os" "path/filepath" @@ -20,26 +15,20 @@ func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) { UUID, _ := uuid.GenerateUUID() os.Setenv("PACKER_RUN_UUID", UUID) c := &command.BuildCommand{ - Meta: testMetaFile(t), + Meta: testshelper.TestMetaFile(t), } file := "provisioner.shell." + UUID + ".txt" - defer os.RemoveAll(file) + defer testshelper.CleanupFiles(file) args := []string{ filepath.Join("./test-fixtures", "shell-provisioner.json"), } if code := c.Run(args); code != 0 { - ui := c.Meta.Ui.(*packer.BasicUi) - out := ui.Writer.(*bytes.Buffer) - err := ui.ErrorWriter.(*bytes.Buffer) - t.Fatalf( - "Bad exit code.\n\nStdout:\n\n%s\n\nStderr:\n\n%s", - out.String(), - err.String()) + testshelper.FatalCommand(t, c.Meta) } - if _, err := os.Stat(file); err != nil { + if !testshelper.FileExists(file) { t.Errorf("Expected to find %s", file) } else { helper := testshelper.AWSHelper{ @@ -50,29 +39,3 @@ func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) { } } -func testMetaFile(t *testing.T) command.Meta { - var out, err bytes.Buffer - return command.Meta{ - CoreConfig: testCoreConfigBuilder(t), - Ui: &packer.BasicUi{ - Writer: &out, - ErrorWriter: &err, - }, - } -} - -func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { - components := packer.ComponentFinder{ - BuilderStore: packer.MapOfBuilder{ - "amazon-ebs": func() (packer.Builder, error) { return &amazonebsbuilder.Builder{}, nil }, - }, - ProvisionerStore: packer.MapOfProvisioner{ - "shell": func() (packer.Provisioner, error) { return &shell.Provisioner{}, nil }, - "file": func() (packer.Provisioner, error) { return &fileprovisioner.Provisioner{}, nil }, - }, - PostProcessorStore: packer.MapOfPostProcessor{}, - } - return &packer.CoreConfig{ - Components: components, - } -}