Commit 49e29d5a authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/testing: delete artifacts

parent 0e1fd516
...@@ -2,6 +2,7 @@ package testing ...@@ -2,6 +2,7 @@ package testing
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"strings" "strings"
...@@ -27,8 +28,7 @@ type TestCase struct { ...@@ -27,8 +28,7 @@ type TestCase struct {
// as the "test" builder in the template. // as the "test" builder in the template.
Builder packer.Builder Builder packer.Builder
// Template is a path to a text template. We use a text file // Template is the template contents to use.
// so we can use the entire machinery to test this builder.
Template string Template string
// Check is called after this step is executed in order to test that // Check is called after this step is executed in order to test that
...@@ -90,8 +90,8 @@ func Test(t TestT, c TestCase) { ...@@ -90,8 +90,8 @@ func Test(t TestT, c TestCase) {
} }
// Parse the template // Parse the template
log.Printf("[DEBUG] Parsing template: %s", c.Template) log.Printf("[DEBUG] Parsing template...")
tpl, err := template.ParseFile(c.Template) tpl, err := template.Parse(strings.NewReader(c.Template))
if err != nil { if err != nil {
t.Fatal(fmt.Sprintf("Failed to parse template: %s", err)) t.Fatal(fmt.Sprintf("Failed to parse template: %s", err))
return return
...@@ -138,18 +138,19 @@ func Test(t TestT, c TestCase) { ...@@ -138,18 +138,19 @@ func Test(t TestT, c TestCase) {
return return
} }
// Run it! // Run it! We use a temporary directory for caching and discard
// any UI output. We discard since it shows up in logs anyways.
log.Printf("[DEBUG] Running 'test' build") log.Printf("[DEBUG] Running 'test' build")
cache := &packer.FileCache{CacheDir: os.TempDir()} cache := &packer.FileCache{CacheDir: os.TempDir()}
ui := &packer.BasicUi{ ui := &packer.BasicUi{
Reader: os.Stdin, Reader: os.Stdin,
Writer: os.Stdout, Writer: ioutil.Discard,
ErrorWriter: os.Stdout, ErrorWriter: ioutil.Discard,
} }
artifacts, err := build.Run(ui, cache) artifacts, err := build.Run(ui, cache)
if err != nil { if err != nil {
t.Fatal(fmt.Sprintf("Run error:\n\n%s", err)) t.Fatal(fmt.Sprintf("Run error:\n\n%s", err))
return goto TEARDOWN
} }
// Check function // Check function
...@@ -157,7 +158,17 @@ func Test(t TestT, c TestCase) { ...@@ -157,7 +158,17 @@ func Test(t TestT, c TestCase) {
log.Printf("[DEBUG] Running check function") log.Printf("[DEBUG] Running check function")
if err := c.Check(artifacts); err != nil { if err := c.Check(artifacts); err != nil {
t.Fatal(fmt.Sprintf("Check error:\n\n%s", err)) t.Fatal(fmt.Sprintf("Check error:\n\n%s", err))
return goto TEARDOWN
}
}
TEARDOWN:
// Delete all artifacts
for _, a := range artifacts {
if err := a.Destroy(); err != nil {
t.Error(fmt.Sprintf(
"!!! ERROR REMOVING ARTIFACT '%s': %s !!!",
a.String(), err))
} }
} }
......
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