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 ( ...@@ -12,39 +12,6 @@ import (
"github.com/mitchellh/packer/template" "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) { func TestDetectFilename(t *testing.T) {
// Test default / fallback with no file extension // Test default / fallback with no file extension
nakedFilename := Config{OutputPath: "test"} nakedFilename := Config{OutputPath: "test"}
...@@ -87,35 +54,21 @@ func TestDetectFilename(t *testing.T) { ...@@ -87,35 +54,21 @@ func TestDetectFilename(t *testing.T) {
} }
} }
func TestSimpleCompress(t *testing.T) { const simpleTestCase = `
if os.Getenv(env.TestEnvVar) == "" { {
t.Skip(fmt.Sprintf( "post-processors": [
"Acceptance tests skipped unless env '%s' set", env.TestEnvVar)) {
"type": "compress",
"output": "package.tar.gz"
} }
]
}
`
ui, artifact, err := setup(t) func TestSimpleCompress(t *testing.T) {
if err != nil { artifact := testArchive(t, simpleTestCase)
t.Fatalf("Error bootstrapping test: %s", err)
}
if artifact != nil {
defer artifact.Destroy() defer artifact.Destroy()
}
tpl, err := template.Parse(strings.NewReader(simpleTestCase))
if err != nil {
t.Fatalf("Unable to parse test config: %s", err)
}
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()
// Verify things look good
fi, err := os.Stat("package.tar.gz") fi, err := os.Stat("package.tar.gz")
if err != nil { if err != nil {
t.Errorf("Unable to read archive: %s", err) t.Errorf("Unable to read archive: %s", err)
...@@ -125,77 +78,109 @@ func TestSimpleCompress(t *testing.T) { ...@@ -125,77 +78,109 @@ func TestSimpleCompress(t *testing.T) {
} }
} }
func TestZipArchive(t *testing.T) { const zipTestCase = `
if os.Getenv(env.TestEnvVar) == "" { {
t.Skip(fmt.Sprintf( "post-processors": [
"Acceptance tests skipped unless env '%s' set", env.TestEnvVar)) {
"type": "compress",
"output": "package.zip"
} }
]
}
`
ui, artifact, err := setup(t) func TestZipArchive(t *testing.T) {
if err != nil { artifact := testArchive(t, zipTestCase)
t.Fatalf("Error bootstrapping test: %s", err)
}
if artifact != nil {
defer artifact.Destroy() defer artifact.Destroy()
}
tpl, err := template.Parse(strings.NewReader(zipTestCase)) // Verify things look good
_, err := os.Stat("package.zip")
if err != nil { if err != nil {
t.Fatalf("Unable to parse test config: %s", err) t.Errorf("Unable to read archive: %s", err)
} }
}
compressor := PostProcessor{} const tarTestCase = `
compressor.Configure(tpl.PostProcessors[0][0].Config) {
artifactOut, _, err := compressor.PostProcess(ui, artifact) "post-processors": [
if err != nil { {
t.Fatalf("Failed to archive artifact: %s", err) "type": "compress",
"output": "package.tar"
} }
// Cleanup after the test completes ]
defer artifactOut.Destroy() }
`
func TestTarArchive(t *testing.T) {
artifact := testArchive(t, tarTestCase)
defer artifact.Destroy()
// Verify things look good // Verify things look good
_, err = os.Stat("package.zip") _, err := os.Stat("package.tar")
if err != nil { if err != nil {
t.Errorf("Unable to read archive: %s", err) t.Errorf("Unable to read archive: %s", err)
} }
} }
func TestTarArchive(t *testing.T) { const optionsTestCase = `
if os.Getenv(env.TestEnvVar) == "" { {
t.Skip(fmt.Sprintf( "post-processors": [
"Acceptance tests skipped unless env '%s' set", env.TestEnvVar)) {
"type": "compress",
"output": "package.gz",
"compression_level": 9
} }
]
}
`
ui, artifact, err := setup(t) func TestCompressOptions(t *testing.T) {
if err != nil { artifact := testArchive(t, optionsTestCase)
t.Fatalf("Error bootstrapping test: %s", err)
}
if artifact != nil {
defer artifact.Destroy() defer artifact.Destroy()
// Verify things look good
_, err := os.Stat("package.gz")
if err != nil {
t.Errorf("Unable to read archive: %s", err)
} }
}
// Test Helpers
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()}
tpl, err := template.Parse(strings.NewReader(tarTestCase)) // 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 { 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{} // Prepare the file builder
compressor.Configure(tpl.PostProcessors[0][0].Config) builder := file.Builder{}
artifactOut, _, err := compressor.PostProcess(ui, artifact) 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 { 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 // Run the file builder
_, err = os.Stat("package.tar") artifact, err := builder.Run(ui, nil, cache)
if err != nil { 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) == "" { if os.Getenv(env.TestEnvVar) == "" {
t.Skip(fmt.Sprintf( t.Skip(fmt.Sprintf(
"Acceptance tests skipped unless env '%s' set", env.TestEnvVar)) "Acceptance tests skipped unless env '%s' set", env.TestEnvVar))
...@@ -209,73 +194,17 @@ func TestCompressOptions(t *testing.T) { ...@@ -209,73 +194,17 @@ func TestCompressOptions(t *testing.T) {
defer artifact.Destroy() defer artifact.Destroy()
} }
tpl, err := template.Parse(strings.NewReader(optionsTestCase)) tpl, err := template.Parse(strings.NewReader(config))
if err != nil { if err != nil {
t.Fatalf("Unable to parse test config: %s", err) t.Fatalf("Unable to parse test config: %s", err)
} }
compressor := PostProcessor{} compressor := PostProcessor{}
compressor.Configure(tpl.PostProcessors[0][0].Config) 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) artifactOut, _, err := compressor.PostProcess(ui, artifact)
if err != nil { if err != nil {
t.Fatalf("Failed to archive artifact: %s", err) t.Fatalf("Failed to compress 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)
}
}
const simpleTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.tar.gz"
}
]
}
`
const zipTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.zip"
}
]
}
`
const tarTestCase = `
{
"post-processors": [
{
"type": "compress",
"output": "package.tar"
} }
]
}
`
const optionsTestCase = ` return artifactOut
{
"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