Merge pull request #6200 from DanHam/cleanup-tmp-test-files

Clean up tmp files created by tests
This commit is contained in:
M. Marsh 2018-05-01 12:02:58 -07:00 committed by GitHub
commit a56f3ea538
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 221 additions and 58 deletions

View File

@ -2,6 +2,7 @@ package ecs
import ( import (
"io/ioutil" "io/ioutil"
"os"
"testing" "testing"
"github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/communicator"
@ -68,6 +69,7 @@ func TestRunConfigPrepare_UserData(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer os.Remove(tf.Name())
defer tf.Close() defer tf.Close()
c.UserData = "foo" c.UserData = "foo"
@ -92,6 +94,7 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer os.Remove(tf.Name())
defer tf.Close() defer tf.Close()
c.UserDataFile = tf.Name() c.UserDataFile = tf.Name()

View File

@ -119,6 +119,7 @@ func TestRunConfigPrepare_UserData(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer os.Remove(tf.Name())
defer tf.Close() defer tf.Close()
c.UserData = "foo" c.UserData = "foo"
@ -143,6 +144,7 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer os.Remove(tf.Name())
defer tf.Close() defer tf.Close()
c.UserDataFile = tf.Name() c.UserDataFile = tf.Name()

View File

@ -8,13 +8,13 @@ import (
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
func testConfig() map[string]interface{} { func testConfig() (config map[string]interface{}, tf *os.File) {
tf, err := ioutil.TempFile("", "packer") tf, err := ioutil.TempFile("", "packer")
if err != nil { if err != nil {
panic(err) panic(err)
} }
return map[string]interface{}{ config = map[string]interface{}{
"account_id": "foo", "account_id": "foo",
"ami_name": "foo", "ami_name": "foo",
"instance_type": "m1.small", "instance_type": "m1.small",
@ -26,6 +26,8 @@ func testConfig() map[string]interface{} {
"x509_key_path": tf.Name(), "x509_key_path": tf.Name(),
"x509_upload_path": "/foo", "x509_upload_path": "/foo",
} }
return config, tf
} }
func TestBuilder_ImplementsBuilder(t *testing.T) { func TestBuilder_ImplementsBuilder(t *testing.T) {
@ -38,7 +40,9 @@ func TestBuilder_ImplementsBuilder(t *testing.T) {
func TestBuilderPrepare_AccountId(t *testing.T) { func TestBuilderPrepare_AccountId(t *testing.T) {
b := &Builder{} b := &Builder{}
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
config["account_id"] = "" config["account_id"] = ""
warnings, err := b.Prepare(config) warnings, err := b.Prepare(config)
@ -74,7 +78,9 @@ func TestBuilderPrepare_AccountId(t *testing.T) {
func TestBuilderPrepare_AMIName(t *testing.T) { func TestBuilderPrepare_AMIName(t *testing.T) {
var b Builder var b Builder
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
// Test good // Test good
config["ami_name"] = "foo" config["ami_name"] = "foo"
@ -111,7 +117,9 @@ func TestBuilderPrepare_AMIName(t *testing.T) {
func TestBuilderPrepare_BundleDestination(t *testing.T) { func TestBuilderPrepare_BundleDestination(t *testing.T) {
b := &Builder{} b := &Builder{}
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
config["bundle_destination"] = "" config["bundle_destination"] = ""
warnings, err := b.Prepare(config) warnings, err := b.Prepare(config)
@ -129,7 +137,9 @@ func TestBuilderPrepare_BundleDestination(t *testing.T) {
func TestBuilderPrepare_BundlePrefix(t *testing.T) { func TestBuilderPrepare_BundlePrefix(t *testing.T) {
b := &Builder{} b := &Builder{}
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
warnings, err := b.Prepare(config) warnings, err := b.Prepare(config)
if len(warnings) > 0 { if len(warnings) > 0 {
@ -146,7 +156,9 @@ func TestBuilderPrepare_BundlePrefix(t *testing.T) {
func TestBuilderPrepare_InvalidKey(t *testing.T) { func TestBuilderPrepare_InvalidKey(t *testing.T) {
var b Builder var b Builder
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
// Add a random key // Add a random key
config["i_should_not_be_valid"] = true config["i_should_not_be_valid"] = true
@ -161,7 +173,9 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) {
func TestBuilderPrepare_S3Bucket(t *testing.T) { func TestBuilderPrepare_S3Bucket(t *testing.T) {
b := &Builder{} b := &Builder{}
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
config["s3_bucket"] = "" config["s3_bucket"] = ""
warnings, err := b.Prepare(config) warnings, err := b.Prepare(config)
@ -184,7 +198,9 @@ func TestBuilderPrepare_S3Bucket(t *testing.T) {
func TestBuilderPrepare_X509CertPath(t *testing.T) { func TestBuilderPrepare_X509CertPath(t *testing.T) {
b := &Builder{} b := &Builder{}
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
config["x509_cert_path"] = "" config["x509_cert_path"] = ""
warnings, err := b.Prepare(config) warnings, err := b.Prepare(config)
@ -209,6 +225,7 @@ func TestBuilderPrepare_X509CertPath(t *testing.T) {
t.Fatalf("error tempfile: %s", err) t.Fatalf("error tempfile: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
config["x509_cert_path"] = tf.Name() config["x509_cert_path"] = tf.Name()
warnings, err = b.Prepare(config) warnings, err = b.Prepare(config)
@ -222,7 +239,9 @@ func TestBuilderPrepare_X509CertPath(t *testing.T) {
func TestBuilderPrepare_X509KeyPath(t *testing.T) { func TestBuilderPrepare_X509KeyPath(t *testing.T) {
b := &Builder{} b := &Builder{}
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
config["x509_key_path"] = "" config["x509_key_path"] = ""
warnings, err := b.Prepare(config) warnings, err := b.Prepare(config)
@ -247,6 +266,7 @@ func TestBuilderPrepare_X509KeyPath(t *testing.T) {
t.Fatalf("error tempfile: %s", err) t.Fatalf("error tempfile: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
config["x509_key_path"] = tf.Name() config["x509_key_path"] = tf.Name()
warnings, err = b.Prepare(config) warnings, err = b.Prepare(config)
@ -260,7 +280,9 @@ func TestBuilderPrepare_X509KeyPath(t *testing.T) {
func TestBuilderPrepare_X509UploadPath(t *testing.T) { func TestBuilderPrepare_X509UploadPath(t *testing.T) {
b := &Builder{} b := &Builder{}
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
config["x509_upload_path"] = "" config["x509_upload_path"] = ""
warnings, err := b.Prepare(config) warnings, err := b.Prepare(config)

View File

@ -3,6 +3,7 @@ package googlecompute
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"strings" "strings"
"testing" "testing"
) )
@ -199,7 +200,8 @@ func TestConfigPrepare(t *testing.T) {
} }
for _, tc := range cases { for _, tc := range cases {
raw := testConfig(t) raw, tempfile := testConfig(t)
defer os.Remove(tempfile)
if tc.Value == nil { if tc.Value == nil {
delete(raw, tc.Key) delete(raw, tc.Key)
@ -251,7 +253,8 @@ func TestConfigPrepareAccelerator(t *testing.T) {
} }
for _, tc := range cases { for _, tc := range cases {
raw := testConfig(t) raw, tempfile := testConfig(t)
defer os.Remove(tempfile)
errStr := "" errStr := ""
for k := range tc.Keys { for k := range tc.Keys {
@ -300,7 +303,8 @@ func TestConfigPrepareServiceAccount(t *testing.T) {
} }
for _, tc := range cases { for _, tc := range cases {
raw := testConfig(t) raw, tempfile := testConfig(t)
defer os.Remove(tempfile)
errStr := "" errStr := ""
for k := range tc.Keys { for k := range tc.Keys {
@ -342,7 +346,8 @@ func TestConfigDefaults(t *testing.T) {
} }
for _, tc := range cases { for _, tc := range cases {
raw := testConfig(t) raw, tempfile := testConfig(t)
defer os.Remove(tempfile)
c, warns, errs := NewConfig(raw) c, warns, errs := NewConfig(raw)
testConfigOk(t, warns, errs) testConfigOk(t, warns, errs)
@ -355,7 +360,10 @@ func TestConfigDefaults(t *testing.T) {
} }
func TestImageName(t *testing.T) { func TestImageName(t *testing.T) {
c, _, _ := NewConfig(testConfig(t)) raw, tempfile := testConfig(t)
defer os.Remove(tempfile)
c, _, _ := NewConfig(raw)
if !strings.HasPrefix(c.ImageName, "packer-") { if !strings.HasPrefix(c.ImageName, "packer-") {
t.Fatalf("ImageName should have 'packer-' prefix, found %s", c.ImageName) t.Fatalf("ImageName should have 'packer-' prefix, found %s", c.ImageName)
} }
@ -365,7 +373,10 @@ func TestImageName(t *testing.T) {
} }
func TestRegion(t *testing.T) { func TestRegion(t *testing.T) {
c, _, _ := NewConfig(testConfig(t)) raw, tempfile := testConfig(t)
defer os.Remove(tempfile)
c, _, _ := NewConfig(raw)
if c.Region != "us-east1" { if c.Region != "us-east1" {
t.Fatalf("Region should be 'us-east1' given Zone of 'us-east1-a', but is %s", c.Region) t.Fatalf("Region should be 'us-east1' given Zone of 'us-east1-a', but is %s", c.Region)
} }
@ -373,9 +384,11 @@ func TestRegion(t *testing.T) {
// Helper stuff below // Helper stuff below
func testConfig(t *testing.T) map[string]interface{} { func testConfig(t *testing.T) (config map[string]interface{}, tempAccountFile string) {
return map[string]interface{}{ tempAccountFile = testAccountFile(t)
"account_file": testAccountFile(t),
config = map[string]interface{}{
"account_file": tempAccountFile,
"project_id": "hashicorp", "project_id": "hashicorp",
"source_image": "foo", "source_image": "foo",
"ssh_username": "root", "ssh_username": "root",
@ -389,10 +402,15 @@ func testConfig(t *testing.T) map[string]interface{} {
}, },
"zone": "us-east1-a", "zone": "us-east1-a",
} }
return config, tempAccountFile
} }
func testConfigStruct(t *testing.T) *Config { func testConfigStruct(t *testing.T) *Config {
c, warns, errs := NewConfig(testConfig(t)) raw, tempfile := testConfig(t)
defer os.Remove(tempfile)
c, warns, errs := NewConfig(raw)
if len(warns) > 0 { if len(warns) > 0 {
t.Fatalf("bad: %#v", len(warns)) t.Fatalf("bad: %#v", len(warns))
} }

View File

@ -55,6 +55,7 @@ func TestStepCreateSSHKey_debug(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer os.Remove(tf.Name())
tf.Close() tf.Close()
state := testState(t) state := testState(t)

View File

@ -75,6 +75,7 @@ func TestStepCreateOrResetWindowsPassword_debug(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer os.Remove(tf.Name())
tf.Close() tf.Close()
state := testState(t) state := testState(t)

View File

@ -16,6 +16,7 @@ func TestIPAddress(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
d := Parallels9Driver{ d := Parallels9Driver{
dhcpLeaseFile: tf.Name(), dhcpLeaseFile: tf.Name(),
@ -62,9 +63,9 @@ func TestIPAddress(t *testing.T) {
func TestXMLParseConfig(t *testing.T) { func TestXMLParseConfig(t *testing.T) {
td, err := ioutil.TempDir("", "configpvs") td, err := ioutil.TempDir("", "configpvs")
if err != nil { if err != nil {
t.Fatalf("Error creating temp file: %s", err) t.Fatalf("Error creating temp dir: %s", err)
} }
defer os.Remove(td) defer os.RemoveAll(td)
config := []byte(` config := []byte(`
<ExampleParallelsConfig> <ExampleParallelsConfig>

View File

@ -28,6 +28,8 @@ func TestStepOutputDir_impl(t *testing.T) {
func TestStepOutputDir(t *testing.T) { func TestStepOutputDir(t *testing.T) {
state := testState(t) state := testState(t)
step := testStepOutputDir(t) step := testStepOutputDir(t)
// Delete the test output directory when done
defer os.RemoveAll(step.Path)
// Test the run // Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionContinue { if action := step.Run(context.Background(), state); action != multistep.ActionContinue {

View File

@ -28,6 +28,7 @@ func TestStepOutputDir_impl(t *testing.T) {
func TestStepOutputDir(t *testing.T) { func TestStepOutputDir(t *testing.T) {
state := testState(t) state := testState(t)
step := testStepOutputDir(t) step := testStepOutputDir(t)
defer os.RemoveAll(step.Path)
// Test the run // Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionContinue { if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
@ -55,6 +56,7 @@ func TestStepOutputDir_exists(t *testing.T) {
if err := os.MkdirAll(step.Path, 0755); err != nil { if err := os.MkdirAll(step.Path, 0755); err != nil {
t.Fatalf("bad: %s", err) t.Fatalf("bad: %s", err)
} }
defer os.RemoveAll(step.Path)
// Test the run // Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionHalt { if action := step.Run(context.Background(), state); action != multistep.ActionHalt {

View File

@ -30,6 +30,8 @@ func TestStepOutputDir(t *testing.T) {
step := new(StepOutputDir) step := new(StepOutputDir)
dir := testOutputDir(t) dir := testOutputDir(t)
// Delete the test output directory when done
defer os.RemoveAll(dir.dir)
state.Put("dir", dir) state.Put("dir", dir)
// Test the run // Test the run
@ -61,6 +63,7 @@ func TestStepOutputDir_existsNoForce(t *testing.T) {
if err := os.MkdirAll(dir.dir, 0755); err != nil { if err := os.MkdirAll(dir.dir, 0755); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer os.RemoveAll(dir.dir)
// Test the run // Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionHalt { if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
@ -89,6 +92,7 @@ func TestStepOutputDir_existsForce(t *testing.T) {
if err := os.MkdirAll(dir.dir, 0755); err != nil { if err := os.MkdirAll(dir.dir, 0755); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer os.RemoveAll(dir.dir)
// Test the run // Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionContinue { if action := step.Run(context.Background(), state); action != multistep.ActionContinue {

View File

@ -85,6 +85,12 @@ func TestStepShutdown_command(t *testing.T) {
if comm.StartCmd.Command != "foo" { if comm.StartCmd.Command != "foo" {
t.Fatalf("bad: %#v", comm.StartCmd.Command) t.Fatalf("bad: %#v", comm.StartCmd.Command)
} }
// Clean up the created test output directory
dir := state.Get("dir").(*LocalOutputDir)
if err := dir.RemoveAll(); err != nil {
t.Fatalf("Error cleaning up directory: %s", err)
}
} }
func TestStepShutdown_noCommand(t *testing.T) { func TestStepShutdown_noCommand(t *testing.T) {
@ -113,6 +119,12 @@ func TestStepShutdown_noCommand(t *testing.T) {
if comm.StartCalled { if comm.StartCalled {
t.Fatal("start should not be called") t.Fatal("start should not be called")
} }
// Clean up the created test output directory
dir := state.Get("dir").(*LocalOutputDir)
if err := dir.RemoveAll(); err != nil {
t.Fatalf("Error cleaning up directory: %s", err)
}
} }
func TestStepShutdown_locks(t *testing.T) { func TestStepShutdown_locks(t *testing.T) {

View File

@ -60,6 +60,7 @@ func TestDownloadClient_basic(t *testing.T) {
TargetPath: tf.Name(), TargetPath: tf.Name(),
CopyFile: true, CopyFile: true,
}) })
defer os.Remove(tf.Name())
path, err := client.Get() path, err := client.Get()
if err != nil { if err != nil {
@ -96,6 +97,7 @@ func TestDownloadClient_checksumBad(t *testing.T) {
Checksum: checksum, Checksum: checksum,
CopyFile: true, CopyFile: true,
}) })
defer os.Remove(tf.Name())
if _, err := client.Get(); err == nil { if _, err := client.Get(); err == nil {
t.Fatal("should error") t.Fatal("should error")
} }
@ -121,6 +123,7 @@ func TestDownloadClient_checksumGood(t *testing.T) {
Checksum: checksum, Checksum: checksum,
CopyFile: true, CopyFile: true,
}) })
defer os.Remove(tf.Name())
path, err := client.Get() path, err := client.Get()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -191,6 +194,7 @@ func TestDownloadClient_resume(t *testing.T) {
TargetPath: tf.Name(), TargetPath: tf.Name(),
CopyFile: true, CopyFile: true,
}) })
defer os.Remove(tf.Name())
path, err := client.Get() path, err := client.Get()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -211,7 +215,8 @@ func TestDownloadClient_usesDefaultUserAgent(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("tempfile error: %s", err) t.Fatalf("tempfile error: %s", err)
} }
defer os.Remove(tf.Name()) tf.Close()
os.Remove(tf.Name())
defaultUserAgent := "" defaultUserAgent := ""
asserted := false asserted := false
@ -249,6 +254,7 @@ func TestDownloadClient_usesDefaultUserAgent(t *testing.T) {
TargetPath: tf.Name(), TargetPath: tf.Name(),
CopyFile: true, CopyFile: true,
} }
defer os.Remove(tf.Name())
client := NewDownloadClient(config) client := NewDownloadClient(config)
_, err = client.Get() _, err = client.Get()
@ -266,7 +272,8 @@ func TestDownloadClient_setsUserAgent(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("tempfile error: %s", err) t.Fatalf("tempfile error: %s", err)
} }
defer os.Remove(tf.Name()) tf.Close()
os.Remove(tf.Name())
asserted := false asserted := false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -281,6 +288,7 @@ func TestDownloadClient_setsUserAgent(t *testing.T) {
UserAgent: "fancy user agent", UserAgent: "fancy user agent",
CopyFile: true, CopyFile: true,
} }
defer os.Remove(tf.Name())
client := NewDownloadClient(config) client := NewDownloadClient(config)
_, err = client.Get() _, err = client.Get()

View File

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"reflect" "reflect"
"runtime" "runtime"
"testing" "testing"
@ -90,6 +91,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
i = testISOConfig() i = testISOConfig()
i.ISOChecksum = "" i.ISOChecksum = ""
cs_file, _ := ioutil.TempFile("", "packer-test-") cs_file, _ := ioutil.TempFile("", "packer-test-")
defer os.Remove(cs_file.Name())
defer cs_file.Close()
ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style), 0666) ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style), 0666)
filePrefix := "file://" filePrefix := "file://"
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
@ -112,6 +115,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
i = testISOConfig() i = testISOConfig()
i.ISOChecksum = "" i.ISOChecksum = ""
cs_file, _ = ioutil.TempFile("", "packer-test-") cs_file, _ = ioutil.TempFile("", "packer-test-")
defer os.Remove(cs_file.Name())
defer cs_file.Close()
ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666) ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666)
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
warns, err = i.Prepare(nil) warns, err = i.Prepare(nil)
@ -130,6 +135,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
i = testISOConfig() i = testISOConfig()
i.ISOChecksum = "" i.ISOChecksum = ""
cs_file, _ = ioutil.TempFile("", "packer-test-") cs_file, _ = ioutil.TempFile("", "packer-test-")
defer os.Remove(cs_file.Name())
defer cs_file.Close()
ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_no_newline), 0666) ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_no_newline), 0666)
i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name())
warns, err = i.Prepare(nil) warns, err = i.Prepare(nil)
@ -150,6 +157,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
cs_dir, _ := ioutil.TempDir("", "packer-testdir-") cs_dir, _ := ioutil.TempDir("", "packer-testdir-")
cs_file, _ = ioutil.TempFile(cs_dir, "packer-test-") cs_file, _ = ioutil.TempFile(cs_dir, "packer-test-")
defer os.RemoveAll(cs_dir) // Removes the file as well
defer cs_file.Close()
ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_subdir), 0666) ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_subdir), 0666)
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso") i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso")
@ -169,6 +178,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
i = testISOConfig() i = testISOConfig()
i.ISOChecksum = "" i.ISOChecksum = ""
cs_file, _ = ioutil.TempFile("", "packer-test-") cs_file, _ = ioutil.TempFile("", "packer-test-")
defer os.Remove(cs_file.Name())
defer cs_file.Close()
ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_no_newline), 0666) ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_no_newline), 0666)
i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name())
warns, err = i.Prepare(nil) warns, err = i.Prepare(nil)
@ -189,6 +200,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso?stuff=boo" i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso?stuff=boo"
cs_file, _ = ioutil.TempFile("", "packer-test-") cs_file, _ = ioutil.TempFile("", "packer-test-")
defer os.Remove(cs_file.Name())
defer cs_file.Close()
ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666) ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666)
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
warns, err = i.Prepare(nil) warns, err = i.Prepare(nil)
@ -208,6 +221,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
i.ISOChecksum = "" i.ISOChecksum = ""
cs_file, _ = ioutil.TempFile(cs_dir, "packer-test-") cs_file, _ = ioutil.TempFile(cs_dir, "packer-test-")
defer os.Remove(cs_file.Name())
defer cs_file.Close()
ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_subdir), 0666) ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_subdir), 0666)
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso") i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso")

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
//"log"
"os" "os"
"regexp" "regexp"
"strings" "strings"
@ -30,6 +29,7 @@ func TestProvisionerPrepare_extractScript(t *testing.T) {
p := new(Provisioner) p := new(Provisioner)
_ = p.Prepare(config) _ = p.Prepare(config)
file, err := extractScript(p) file, err := extractScript(p)
defer os.Remove(file)
if err != nil { if err != nil {
t.Fatalf("Should not be error: %s", err) t.Fatalf("Should not be error: %s", err)
} }
@ -177,6 +177,7 @@ func TestProvisionerPrepare_Script(t *testing.T) {
t.Fatalf("error tempfile: %s", err) t.Fatalf("error tempfile: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
config["script"] = tf.Name() config["script"] = tf.Name()
p = new(Provisioner) p = new(Provisioner)
@ -203,6 +204,7 @@ func TestProvisionerPrepare_ScriptAndInline(t *testing.T) {
t.Fatalf("error tempfile: %s", err) t.Fatalf("error tempfile: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
config["inline"] = []interface{}{"foo"} config["inline"] = []interface{}{"foo"}
config["script"] = tf.Name() config["script"] = tf.Name()
@ -222,6 +224,7 @@ func TestProvisionerPrepare_ScriptAndScripts(t *testing.T) {
t.Fatalf("error tempfile: %s", err) t.Fatalf("error tempfile: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
config["inline"] = []interface{}{"foo"} config["inline"] = []interface{}{"foo"}
config["scripts"] = []string{tf.Name()} config["scripts"] = []string{tf.Name()}
@ -248,6 +251,7 @@ func TestProvisionerPrepare_Scripts(t *testing.T) {
t.Fatalf("error tempfile: %s", err) t.Fatalf("error tempfile: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
config["scripts"] = []string{tf.Name()} config["scripts"] = []string{tf.Name()}
p = new(Provisioner) p = new(Provisioner)
@ -444,6 +448,8 @@ func TestProvisionerProvision_Inline(t *testing.T) {
func TestProvisionerProvision_Scripts(t *testing.T) { func TestProvisionerProvision_Scripts(t *testing.T) {
tempFile, _ := ioutil.TempFile("", "packer") tempFile, _ := ioutil.TempFile("", "packer")
defer os.Remove(tempFile.Name()) defer os.Remove(tempFile.Name())
defer tempFile.Close()
config := testConfig() config := testConfig()
delete(config, "inline") delete(config, "inline")
config["scripts"] = []string{tempFile.Name()} config["scripts"] = []string{tempFile.Name()}
@ -473,6 +479,8 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
config := testConfig() config := testConfig()
ui := testUi() ui := testUi()
defer os.Remove(tempFile.Name()) defer os.Remove(tempFile.Name())
defer tempFile.Close()
delete(config, "inline") delete(config, "inline")
config["scripts"] = []string{tempFile.Name()} config["scripts"] = []string{tempFile.Name()}

View File

@ -13,15 +13,17 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func testConfig() map[string]interface{} { func testConfig() (config map[string]interface{}, tf *os.File) {
tf, err := ioutil.TempFile("", "packer") tf, err := ioutil.TempFile("", "packer")
if err != nil { if err != nil {
panic(err) panic(err)
} }
return map[string]interface{}{ config = map[string]interface{}{
"manifest_file": tf.Name(), "manifest_file": tf.Name(),
} }
return config, tf
} }
func TestProvisioner_Impl(t *testing.T) { func TestProvisioner_Impl(t *testing.T) {
@ -33,7 +35,10 @@ func TestProvisioner_Impl(t *testing.T) {
} }
func TestGuestOSConfig_empty_unix(t *testing.T) { func TestGuestOSConfig_empty_unix(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
p := new(Provisioner) p := new(Provisioner)
err := p.Prepare(config) err := p.Prepare(config)
if err != nil { if err != nil {
@ -58,7 +63,10 @@ func TestGuestOSConfig_empty_unix(t *testing.T) {
} }
func TestGuestOSConfig_full_unix(t *testing.T) { func TestGuestOSConfig_full_unix(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
p := new(Provisioner) p := new(Provisioner)
err := p.Prepare(config) err := p.Prepare(config)
if err != nil { if err != nil {
@ -95,7 +103,10 @@ func TestGuestOSConfig_full_unix(t *testing.T) {
} }
func TestGuestOSConfig_empty_windows(t *testing.T) { func TestGuestOSConfig_empty_windows(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
config["guest_os_type"] = "windows" config["guest_os_type"] = "windows"
p := new(Provisioner) p := new(Provisioner)
err := p.Prepare(config) err := p.Prepare(config)
@ -120,7 +131,10 @@ func TestGuestOSConfig_empty_windows(t *testing.T) {
} }
func TestGuestOSConfig_full_windows(t *testing.T) { func TestGuestOSConfig_full_windows(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
config["guest_os_type"] = "windows" config["guest_os_type"] = "windows"
p := new(Provisioner) p := new(Provisioner)
err := p.Prepare(config) err := p.Prepare(config)
@ -158,7 +172,9 @@ func TestGuestOSConfig_full_windows(t *testing.T) {
} }
func TestProvisionerPrepare_puppetBinDir(t *testing.T) { func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "puppet_bin_dir") delete(config, "puppet_bin_dir")
p := new(Provisioner) p := new(Provisioner)
@ -183,7 +199,9 @@ func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
} }
func TestProvisionerPrepare_hieraConfigPath(t *testing.T) { func TestProvisionerPrepare_hieraConfigPath(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "hiera_config_path") delete(config, "hiera_config_path")
p := new(Provisioner) p := new(Provisioner)
@ -208,7 +226,9 @@ func TestProvisionerPrepare_hieraConfigPath(t *testing.T) {
} }
func TestProvisionerPrepare_manifestFile(t *testing.T) { func TestProvisionerPrepare_manifestFile(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "manifest_file") delete(config, "manifest_file")
p := new(Provisioner) p := new(Provisioner)
@ -233,7 +253,9 @@ func TestProvisionerPrepare_manifestFile(t *testing.T) {
} }
func TestProvisionerPrepare_manifestDir(t *testing.T) { func TestProvisionerPrepare_manifestDir(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "manifestdir") delete(config, "manifestdir")
p := new(Provisioner) p := new(Provisioner)
@ -258,7 +280,9 @@ func TestProvisionerPrepare_manifestDir(t *testing.T) {
} }
func TestProvisionerPrepare_modulePaths(t *testing.T) { func TestProvisionerPrepare_modulePaths(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "module_paths") delete(config, "module_paths")
p := new(Provisioner) p := new(Provisioner)
@ -291,7 +315,9 @@ func TestProvisionerPrepare_modulePaths(t *testing.T) {
} }
func TestProvisionerPrepare_facterFacts(t *testing.T) { func TestProvisionerPrepare_facterFacts(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "facter") delete(config, "facter")
p := new(Provisioner) p := new(Provisioner)
@ -346,7 +372,9 @@ func TestProvisionerPrepare_facterFacts(t *testing.T) {
} }
func TestProvisionerPrepare_extraArguments(t *testing.T) { func TestProvisionerPrepare_extraArguments(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
// Test with missing parameter // Test with missing parameter
delete(config, "extra_arguments") delete(config, "extra_arguments")
@ -377,7 +405,9 @@ func TestProvisionerPrepare_extraArguments(t *testing.T) {
} }
func TestProvisionerPrepare_stagingDir(t *testing.T) { func TestProvisionerPrepare_stagingDir(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "staging_directory") delete(config, "staging_directory")
p := new(Provisioner) p := new(Provisioner)
@ -405,7 +435,9 @@ func TestProvisionerPrepare_stagingDir(t *testing.T) {
} }
func TestProvisionerPrepare_workingDir(t *testing.T) { func TestProvisionerPrepare_workingDir(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "working_directory") delete(config, "working_directory")
p := new(Provisioner) p := new(Provisioner)
@ -438,7 +470,10 @@ func TestProvisionerPrepare_workingDir(t *testing.T) {
} }
func TestProvisionerProvision_extraArguments(t *testing.T) { func TestProvisionerProvision_extraArguments(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
ui := &packer.MachineReadableUi{ ui := &packer.MachineReadableUi{
Writer: ioutil.Discard, Writer: ioutil.Discard,
} }

View File

@ -8,15 +8,17 @@ import (
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
func testConfig() map[string]interface{} { func testConfig() (config map[string]interface{}, tf *os.File) {
tf, err := ioutil.TempFile("", "packer") tf, err := ioutil.TempFile("", "packer")
if err != nil { if err != nil {
panic(err) panic(err)
} }
return map[string]interface{}{ config = map[string]interface{}{
"puppet_server": tf.Name(), "puppet_server": tf.Name(),
} }
return config, tf
} }
func TestProvisioner_Impl(t *testing.T) { func TestProvisioner_Impl(t *testing.T) {
@ -28,7 +30,9 @@ func TestProvisioner_Impl(t *testing.T) {
} }
func TestProvisionerPrepare_puppetBinDir(t *testing.T) { func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "puppet_bin_dir") delete(config, "puppet_bin_dir")
p := new(Provisioner) p := new(Provisioner)
@ -53,7 +57,9 @@ func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
} }
func TestProvisionerPrepare_clientPrivateKeyPath(t *testing.T) { func TestProvisionerPrepare_clientPrivateKeyPath(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "client_private_key_path") delete(config, "client_private_key_path")
p := new(Provisioner) p := new(Provisioner)
@ -86,7 +92,9 @@ func TestProvisionerPrepare_clientPrivateKeyPath(t *testing.T) {
} }
func TestProvisionerPrepare_clientCertPath(t *testing.T) { func TestProvisionerPrepare_clientCertPath(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "client_cert_path") delete(config, "client_cert_path")
p := new(Provisioner) p := new(Provisioner)
@ -119,7 +127,9 @@ func TestProvisionerPrepare_clientCertPath(t *testing.T) {
} }
func TestProvisionerPrepare_executeCommand(t *testing.T) { func TestProvisionerPrepare_executeCommand(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "execute_command") delete(config, "execute_command")
p := new(Provisioner) p := new(Provisioner)
@ -130,7 +140,9 @@ func TestProvisionerPrepare_executeCommand(t *testing.T) {
} }
func TestProvisionerPrepare_facterFacts(t *testing.T) { func TestProvisionerPrepare_facterFacts(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "facter") delete(config, "facter")
p := new(Provisioner) p := new(Provisioner)
@ -185,7 +197,9 @@ func TestProvisionerPrepare_facterFacts(t *testing.T) {
} }
func TestProvisionerPrepare_stagingDir(t *testing.T) { func TestProvisionerPrepare_stagingDir(t *testing.T) {
config := testConfig() config, tempfile := testConfig()
defer os.Remove(tempfile.Name())
defer tempfile.Close()
delete(config, "staging_dir") delete(config, "staging_dir")
p := new(Provisioner) p := new(Provisioner)

View File

@ -25,6 +25,7 @@ func TestProvisionerPrepare_extractScript(t *testing.T) {
p := new(Provisioner) p := new(Provisioner)
_ = p.Prepare(config) _ = p.Prepare(config)
file, err := extractScript(p) file, err := extractScript(p)
defer os.Remove(file)
if err != nil { if err != nil {
t.Fatalf("Should not be error: %s", err) t.Fatalf("Should not be error: %s", err)
} }
@ -104,6 +105,7 @@ func TestProvisionerPrepare_Script(t *testing.T) {
t.Fatalf("error tempfile: %s", err) t.Fatalf("error tempfile: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
config["script"] = tf.Name() config["script"] = tf.Name()
p = new(Provisioner) p = new(Provisioner)
@ -130,6 +132,7 @@ func TestProvisionerPrepare_ScriptAndInline(t *testing.T) {
t.Fatalf("error tempfile: %s", err) t.Fatalf("error tempfile: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
config["inline"] = []interface{}{"foo"} config["inline"] = []interface{}{"foo"}
config["script"] = tf.Name() config["script"] = tf.Name()
@ -149,6 +152,7 @@ func TestProvisionerPrepare_ScriptAndScripts(t *testing.T) {
t.Fatalf("error tempfile: %s", err) t.Fatalf("error tempfile: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
config["inline"] = []interface{}{"foo"} config["inline"] = []interface{}{"foo"}
config["scripts"] = []string{tf.Name()} config["scripts"] = []string{tf.Name()}
@ -175,6 +179,7 @@ func TestProvisionerPrepare_Scripts(t *testing.T) {
t.Fatalf("error tempfile: %s", err) t.Fatalf("error tempfile: %s", err)
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
defer tf.Close()
config["scripts"] = []string{tf.Name()} config["scripts"] = []string{tf.Name()}
p = new(Provisioner) p = new(Provisioner)
@ -322,11 +327,16 @@ func TestProvisionerProvision_Inline(t *testing.T) {
} }
func TestProvisionerProvision_Scripts(t *testing.T) { func TestProvisionerProvision_Scripts(t *testing.T) {
tempFile, _ := ioutil.TempFile("", "packer") tf, err := ioutil.TempFile("", "packer")
defer os.Remove(tempFile.Name()) if err != nil {
t.Fatalf("error tempfile: %s", err)
}
defer os.Remove(tf.Name())
defer tf.Close()
config := testConfig() config := testConfig()
delete(config, "inline") delete(config, "inline")
config["scripts"] = []string{tempFile.Name()} config["scripts"] = []string{tf.Name()}
config["packer_build_name"] = "foobuild" config["packer_build_name"] = "foobuild"
config["packer_builder_type"] = "footype" config["packer_builder_type"] = "footype"
ui := testUi() ui := testUi()
@ -334,7 +344,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
p := new(Provisioner) p := new(Provisioner)
comm := new(packer.MockCommunicator) comm := new(packer.MockCommunicator)
p.Prepare(config) p.Prepare(config)
err := p.Provision(ui, comm) err = p.Provision(ui, comm)
if err != nil { if err != nil {
t.Fatal("should not have error") t.Fatal("should not have error")
} }
@ -349,13 +359,18 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
} }
func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
tempFile, _ := ioutil.TempFile("", "packer") tf, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("error tempfile: %s", err)
}
defer os.Remove(tf.Name())
defer tf.Close()
config := testConfig() config := testConfig()
ui := testUi() ui := testUi()
defer os.Remove(tempFile.Name())
delete(config, "inline") delete(config, "inline")
config["scripts"] = []string{tempFile.Name()} config["scripts"] = []string{tf.Name()}
config["packer_build_name"] = "foobuild" config["packer_build_name"] = "foobuild"
config["packer_builder_type"] = "footype" config["packer_builder_type"] = "footype"
@ -368,7 +383,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
p := new(Provisioner) p := new(Provisioner)
comm := new(packer.MockCommunicator) comm := new(packer.MockCommunicator)
p.Prepare(config) p.Prepare(config)
err := p.Provision(ui, comm) err = p.Provision(ui, comm)
if err != nil { if err != nil {
t.Fatal("should not have error") t.Fatal("should not have error")
} }