Add integration tests helper on test package

This commit is contained in:
Moss 2020-03-13 16:35:31 +01:00
parent 7fbbbffd5c
commit 2a7847a2ab
2 changed files with 70 additions and 41 deletions

66
test/helper/core.go Normal file
View File

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

View File

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