Commit d02f6644 authored by Chris Bednarski's avatar Chris Bednarski

Refactored test so it's more DRY and also rearranged things so the test cases...

Refactored test so it's more DRY and also rearranged things so the test cases and configs are at the top of the file
parent 7497db67
......@@ -12,39 +12,6 @@ import (
"github.com/mitchellh/packer/template"
)
func setup(t *testing.T) (packer.Ui, packer.Artifact, error) {
// Create fake UI and Cache
ui := packer.TestUi(t)
cache := &packer.FileCache{CacheDir: os.TempDir()}
// Create config for file builder
const fileConfig = `{"builders":[{"type":"file","target":"package.txt","content":"Hello world!"}]}`
tpl, err := template.Parse(strings.NewReader(fileConfig))
if err != nil {
return nil, nil, fmt.Errorf("Unable to parse setup configuration: %s", err)
}
// Prepare the file builder
builder := file.Builder{}
warnings, err := builder.Prepare(tpl.Builders["file"].Config)
if len(warnings) > 0 {
for _, warn := range warnings {
return nil, nil, fmt.Errorf("Configuration warning: %s", warn)
}
}
if err != nil {
return nil, nil, fmt.Errorf("Invalid configuration: %s", err)
}
// Run the file builder
artifact, err := builder.Run(ui, nil, cache)
if err != nil {
return nil, nil, fmt.Errorf("Failed to build artifact: %s", err)
}
return ui, artifact, err
}
func TestDetectFilename(t *testing.T) {
// Test default / fallback with no file extension
nakedFilename := Config{OutputPath: "test"}
......@@ -87,35 +54,21 @@ func TestDetectFilename(t *testing.T) {
}
}
func TestSimpleCompress(t *testing.T) {
if os.Getenv(env.TestEnvVar) == "" {
t.Skip(fmt.Sprintf(
"Acceptance tests skipped unless env '%s' set", env.TestEnvVar))
}
ui, artifact, err := setup(t)
if err != nil {
t.Fatalf("Error bootstrapping test: %s", err)
}
if artifact != nil {
defer artifact.Destroy()
}
tpl, err := template.Parse(strings.NewReader(simpleTestCase))
if err != nil {
t.Fatalf("Unable to parse test config: %s", err)
}
const simpleTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.tar.gz"
}
]
}
`
compressor := PostProcessor{}
compressor.Configure(tpl.PostProcessors[0][0].Config)
artifactOut, _, err := compressor.PostProcess(ui, artifact)
if err != nil {
t.Fatalf("Failed to compress artifact: %s", err)
}
// Cleanup after the test completes
defer artifactOut.Destroy()
func TestSimpleCompress(t *testing.T) {
artifact := testArchive(t, simpleTestCase)
defer artifact.Destroy()
// Verify things look good
fi, err := os.Stat("package.tar.gz")
if err != nil {
t.Errorf("Unable to read archive: %s", err)
......@@ -125,77 +78,109 @@ func TestSimpleCompress(t *testing.T) {
}
}
const zipTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.zip"
}
]
}
`
func TestZipArchive(t *testing.T) {
if os.Getenv(env.TestEnvVar) == "" {
t.Skip(fmt.Sprintf(
"Acceptance tests skipped unless env '%s' set", env.TestEnvVar))
}
artifact := testArchive(t, zipTestCase)
defer artifact.Destroy()
ui, artifact, err := setup(t)
// Verify things look good
_, err := os.Stat("package.zip")
if err != nil {
t.Fatalf("Error bootstrapping test: %s", err)
}
if artifact != nil {
defer artifact.Destroy()
t.Errorf("Unable to read archive: %s", err)
}
}
tpl, err := template.Parse(strings.NewReader(zipTestCase))
if err != nil {
t.Fatalf("Unable to parse test config: %s", err)
}
const tarTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.tar"
}
]
}
`
compressor := PostProcessor{}
compressor.Configure(tpl.PostProcessors[0][0].Config)
artifactOut, _, err := compressor.PostProcess(ui, artifact)
if err != nil {
t.Fatalf("Failed to archive artifact: %s", err)
}
// Cleanup after the test completes
defer artifactOut.Destroy()
func TestTarArchive(t *testing.T) {
artifact := testArchive(t, tarTestCase)
defer artifact.Destroy()
// Verify things look good
_, err = os.Stat("package.zip")
_, err := os.Stat("package.tar")
if err != nil {
t.Errorf("Unable to read archive: %s", err)
}
}
func TestTarArchive(t *testing.T) {
if os.Getenv(env.TestEnvVar) == "" {
t.Skip(fmt.Sprintf(
"Acceptance tests skipped unless env '%s' set", env.TestEnvVar))
}
const optionsTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.gz",
"compression_level": 9
}
]
}
`
ui, artifact, err := setup(t)
func TestCompressOptions(t *testing.T) {
artifact := testArchive(t, optionsTestCase)
defer artifact.Destroy()
// Verify things look good
_, err := os.Stat("package.gz")
if err != nil {
t.Fatalf("Error bootstrapping test: %s", err)
}
if artifact != nil {
defer artifact.Destroy()
t.Errorf("Unable to read archive: %s", err)
}
}
// Test Helpers
tpl, err := template.Parse(strings.NewReader(tarTestCase))
func setup(t *testing.T) (packer.Ui, packer.Artifact, error) {
// Create fake UI and Cache
ui := packer.TestUi(t)
cache := &packer.FileCache{CacheDir: os.TempDir()}
// Create config for file builder
const fileConfig = `{"builders":[{"type":"file","target":"package.txt","content":"Hello world!"}]}`
tpl, err := template.Parse(strings.NewReader(fileConfig))
if err != nil {
t.Fatalf("Unable to parse test config: %s", err)
return nil, nil, fmt.Errorf("Unable to parse setup configuration: %s", err)
}
compressor := PostProcessor{}
compressor.Configure(tpl.PostProcessors[0][0].Config)
artifactOut, _, err := compressor.PostProcess(ui, artifact)
// Prepare the file builder
builder := file.Builder{}
warnings, err := builder.Prepare(tpl.Builders["file"].Config)
if len(warnings) > 0 {
for _, warn := range warnings {
return nil, nil, fmt.Errorf("Configuration warning: %s", warn)
}
}
if err != nil {
t.Fatalf("Failed to archive artifact: %s", err)
return nil, nil, fmt.Errorf("Invalid configuration: %s", err)
}
// Cleanup after the test completes
defer artifactOut.Destroy()
// Verify things look good
_, err = os.Stat("package.tar")
// Run the file builder
artifact, err := builder.Run(ui, nil, cache)
if err != nil {
t.Errorf("Unable to read archive: %s", err)
return nil, nil, fmt.Errorf("Failed to build artifact: %s", err)
}
return ui, artifact, err
}
func TestCompressOptions(t *testing.T) {
func testArchive(t *testing.T, config string) packer.Artifact {
if os.Getenv(env.TestEnvVar) == "" {
t.Skip(fmt.Sprintf(
"Acceptance tests skipped unless env '%s' set", env.TestEnvVar))
......@@ -209,73 +194,17 @@ func TestCompressOptions(t *testing.T) {
defer artifact.Destroy()
}
tpl, err := template.Parse(strings.NewReader(optionsTestCase))
tpl, err := template.Parse(strings.NewReader(config))
if err != nil {
t.Fatalf("Unable to parse test config: %s", err)
}
compressor := PostProcessor{}
compressor.Configure(tpl.PostProcessors[0][0].Config)
if compressor.config.CompressionLevel != 9 {
t.Errorf("Expected compression_level 9, got %d", compressor.config.CompressionLevel)
}
artifactOut, _, err := compressor.PostProcess(ui, artifact)
if err != nil {
t.Fatalf("Failed to archive artifact: %s", err)
}
// Cleanup after the test completes
defer artifactOut.Destroy()
// Verify things look good
_, err = os.Stat("package.gz")
if err != nil {
t.Errorf("Unable to read archive: %s", err)
t.Fatalf("Failed to compress artifact: %s", err)
}
}
const simpleTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.tar.gz"
}
]
return artifactOut
}
`
const zipTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.zip"
}
]
}
`
const tarTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.tar"
}
]
}
`
const optionsTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.gz",
"compression_level": 9
}
]
}
`
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment