diff --git a/builder/alicloud/ecs/run_config_test.go b/builder/alicloud/ecs/run_config_test.go index d62f60b38..fe499e21f 100644 --- a/builder/alicloud/ecs/run_config_test.go +++ b/builder/alicloud/ecs/run_config_test.go @@ -2,6 +2,7 @@ package ecs import ( "io/ioutil" + "os" "testing" "github.com/hashicorp/packer/helper/communicator" @@ -68,6 +69,7 @@ func TestRunConfigPrepare_UserData(t *testing.T) { if err != nil { t.Fatalf("err: %s", err) } + defer os.Remove(tf.Name()) defer tf.Close() c.UserData = "foo" @@ -92,6 +94,7 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) { if err != nil { t.Fatalf("err: %s", err) } + defer os.Remove(tf.Name()) defer tf.Close() c.UserDataFile = tf.Name() diff --git a/builder/amazon/common/run_config_test.go b/builder/amazon/common/run_config_test.go index 7cf42ded1..a88730e82 100644 --- a/builder/amazon/common/run_config_test.go +++ b/builder/amazon/common/run_config_test.go @@ -119,6 +119,7 @@ func TestRunConfigPrepare_UserData(t *testing.T) { if err != nil { t.Fatalf("err: %s", err) } + defer os.Remove(tf.Name()) defer tf.Close() c.UserData = "foo" @@ -143,6 +144,7 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) { if err != nil { t.Fatalf("err: %s", err) } + defer os.Remove(tf.Name()) defer tf.Close() c.UserDataFile = tf.Name() diff --git a/builder/amazon/instance/builder_test.go b/builder/amazon/instance/builder_test.go index 4136d57fe..1ad77d82b 100644 --- a/builder/amazon/instance/builder_test.go +++ b/builder/amazon/instance/builder_test.go @@ -8,13 +8,13 @@ import ( "github.com/hashicorp/packer/packer" ) -func testConfig() map[string]interface{} { +func testConfig() (config map[string]interface{}, tf *os.File) { tf, err := ioutil.TempFile("", "packer") if err != nil { panic(err) } - return map[string]interface{}{ + config = map[string]interface{}{ "account_id": "foo", "ami_name": "foo", "instance_type": "m1.small", @@ -26,6 +26,8 @@ func testConfig() map[string]interface{} { "x509_key_path": tf.Name(), "x509_upload_path": "/foo", } + + return config, tf } func TestBuilder_ImplementsBuilder(t *testing.T) { @@ -38,7 +40,9 @@ func TestBuilder_ImplementsBuilder(t *testing.T) { func TestBuilderPrepare_AccountId(t *testing.T) { b := &Builder{} - config := testConfig() + config, tempfile := testConfig() + defer os.Remove(tempfile.Name()) + defer tempfile.Close() config["account_id"] = "" warnings, err := b.Prepare(config) @@ -74,7 +78,9 @@ func TestBuilderPrepare_AccountId(t *testing.T) { func TestBuilderPrepare_AMIName(t *testing.T) { var b Builder - config := testConfig() + config, tempfile := testConfig() + defer os.Remove(tempfile.Name()) + defer tempfile.Close() // Test good config["ami_name"] = "foo" @@ -111,7 +117,9 @@ func TestBuilderPrepare_AMIName(t *testing.T) { func TestBuilderPrepare_BundleDestination(t *testing.T) { b := &Builder{} - config := testConfig() + config, tempfile := testConfig() + defer os.Remove(tempfile.Name()) + defer tempfile.Close() config["bundle_destination"] = "" warnings, err := b.Prepare(config) @@ -129,7 +137,9 @@ func TestBuilderPrepare_BundleDestination(t *testing.T) { func TestBuilderPrepare_BundlePrefix(t *testing.T) { b := &Builder{} - config := testConfig() + config, tempfile := testConfig() + defer os.Remove(tempfile.Name()) + defer tempfile.Close() warnings, err := b.Prepare(config) if len(warnings) > 0 { @@ -146,7 +156,9 @@ func TestBuilderPrepare_BundlePrefix(t *testing.T) { func TestBuilderPrepare_InvalidKey(t *testing.T) { var b Builder - config := testConfig() + config, tempfile := testConfig() + defer os.Remove(tempfile.Name()) + defer tempfile.Close() // Add a random key config["i_should_not_be_valid"] = true @@ -161,7 +173,9 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) { func TestBuilderPrepare_S3Bucket(t *testing.T) { b := &Builder{} - config := testConfig() + config, tempfile := testConfig() + defer os.Remove(tempfile.Name()) + defer tempfile.Close() config["s3_bucket"] = "" warnings, err := b.Prepare(config) @@ -184,7 +198,9 @@ func TestBuilderPrepare_S3Bucket(t *testing.T) { func TestBuilderPrepare_X509CertPath(t *testing.T) { b := &Builder{} - config := testConfig() + config, tempfile := testConfig() + defer os.Remove(tempfile.Name()) + defer tempfile.Close() config["x509_cert_path"] = "" warnings, err := b.Prepare(config) @@ -209,6 +225,7 @@ func TestBuilderPrepare_X509CertPath(t *testing.T) { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() config["x509_cert_path"] = tf.Name() warnings, err = b.Prepare(config) @@ -222,7 +239,9 @@ func TestBuilderPrepare_X509CertPath(t *testing.T) { func TestBuilderPrepare_X509KeyPath(t *testing.T) { b := &Builder{} - config := testConfig() + config, tempfile := testConfig() + defer os.Remove(tempfile.Name()) + defer tempfile.Close() config["x509_key_path"] = "" warnings, err := b.Prepare(config) @@ -247,6 +266,7 @@ func TestBuilderPrepare_X509KeyPath(t *testing.T) { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() config["x509_key_path"] = tf.Name() warnings, err = b.Prepare(config) @@ -260,7 +280,9 @@ func TestBuilderPrepare_X509KeyPath(t *testing.T) { func TestBuilderPrepare_X509UploadPath(t *testing.T) { b := &Builder{} - config := testConfig() + config, tempfile := testConfig() + defer os.Remove(tempfile.Name()) + defer tempfile.Close() config["x509_upload_path"] = "" warnings, err := b.Prepare(config) diff --git a/builder/googlecompute/config_test.go b/builder/googlecompute/config_test.go index d3ed84506..2da3faa59 100644 --- a/builder/googlecompute/config_test.go +++ b/builder/googlecompute/config_test.go @@ -3,6 +3,7 @@ package googlecompute import ( "fmt" "io/ioutil" + "os" "strings" "testing" ) @@ -199,7 +200,8 @@ func TestConfigPrepare(t *testing.T) { } for _, tc := range cases { - raw := testConfig(t) + raw, tempfile := testConfig(t) + defer os.Remove(tempfile) if tc.Value == nil { delete(raw, tc.Key) @@ -251,7 +253,8 @@ func TestConfigPrepareAccelerator(t *testing.T) { } for _, tc := range cases { - raw := testConfig(t) + raw, tempfile := testConfig(t) + defer os.Remove(tempfile) errStr := "" for k := range tc.Keys { @@ -300,7 +303,8 @@ func TestConfigPrepareServiceAccount(t *testing.T) { } for _, tc := range cases { - raw := testConfig(t) + raw, tempfile := testConfig(t) + defer os.Remove(tempfile) errStr := "" for k := range tc.Keys { @@ -342,7 +346,8 @@ func TestConfigDefaults(t *testing.T) { } for _, tc := range cases { - raw := testConfig(t) + raw, tempfile := testConfig(t) + defer os.Remove(tempfile) c, warns, errs := NewConfig(raw) testConfigOk(t, warns, errs) @@ -355,7 +360,10 @@ func TestConfigDefaults(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-") { 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) { - c, _, _ := NewConfig(testConfig(t)) + raw, tempfile := testConfig(t) + defer os.Remove(tempfile) + + c, _, _ := NewConfig(raw) if c.Region != "us-east1" { 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 -func testConfig(t *testing.T) map[string]interface{} { - return map[string]interface{}{ - "account_file": testAccountFile(t), +func testConfig(t *testing.T) (config map[string]interface{}, tempAccountFile string) { + tempAccountFile = testAccountFile(t) + + config = map[string]interface{}{ + "account_file": tempAccountFile, "project_id": "hashicorp", "source_image": "foo", "ssh_username": "root", @@ -389,10 +402,15 @@ func testConfig(t *testing.T) map[string]interface{} { }, "zone": "us-east1-a", } + + return config, tempAccountFile } 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 { t.Fatalf("bad: %#v", len(warns)) } diff --git a/builder/googlecompute/step_create_ssh_key_test.go b/builder/googlecompute/step_create_ssh_key_test.go index bc1b2e6b5..5e5150738 100644 --- a/builder/googlecompute/step_create_ssh_key_test.go +++ b/builder/googlecompute/step_create_ssh_key_test.go @@ -55,6 +55,7 @@ func TestStepCreateSSHKey_debug(t *testing.T) { if err != nil { t.Fatalf("err: %s", err) } + defer os.Remove(tf.Name()) tf.Close() state := testState(t) diff --git a/builder/googlecompute/step_create_windows_password_test.go b/builder/googlecompute/step_create_windows_password_test.go index 3a58d4816..6abb6243d 100644 --- a/builder/googlecompute/step_create_windows_password_test.go +++ b/builder/googlecompute/step_create_windows_password_test.go @@ -75,6 +75,7 @@ func TestStepCreateOrResetWindowsPassword_debug(t *testing.T) { if err != nil { t.Fatalf("err: %s", err) } + defer os.Remove(tf.Name()) tf.Close() state := testState(t) diff --git a/builder/parallels/common/driver_9_test.go b/builder/parallels/common/driver_9_test.go index 32d7c6662..bc9d3eb42 100644 --- a/builder/parallels/common/driver_9_test.go +++ b/builder/parallels/common/driver_9_test.go @@ -16,6 +16,7 @@ func TestIPAddress(t *testing.T) { t.Fatalf("err: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() d := Parallels9Driver{ dhcpLeaseFile: tf.Name(), @@ -62,9 +63,9 @@ func TestIPAddress(t *testing.T) { func TestXMLParseConfig(t *testing.T) { td, err := ioutil.TempDir("", "configpvs") 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(` diff --git a/builder/parallels/common/step_output_dir_test.go b/builder/parallels/common/step_output_dir_test.go index 17d441457..53c1d885c 100644 --- a/builder/parallels/common/step_output_dir_test.go +++ b/builder/parallels/common/step_output_dir_test.go @@ -28,6 +28,8 @@ func TestStepOutputDir_impl(t *testing.T) { func TestStepOutputDir(t *testing.T) { state := testState(t) step := testStepOutputDir(t) + // Delete the test output directory when done + defer os.RemoveAll(step.Path) // Test the run if action := step.Run(context.Background(), state); action != multistep.ActionContinue { diff --git a/builder/virtualbox/common/step_output_dir_test.go b/builder/virtualbox/common/step_output_dir_test.go index 1e2f05c84..766154ca9 100644 --- a/builder/virtualbox/common/step_output_dir_test.go +++ b/builder/virtualbox/common/step_output_dir_test.go @@ -28,6 +28,7 @@ func TestStepOutputDir_impl(t *testing.T) { func TestStepOutputDir(t *testing.T) { state := testState(t) step := testStepOutputDir(t) + defer os.RemoveAll(step.Path) // Test the run 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 { t.Fatalf("bad: %s", err) } + defer os.RemoveAll(step.Path) // Test the run if action := step.Run(context.Background(), state); action != multistep.ActionHalt { diff --git a/builder/vmware/common/step_output_dir_test.go b/builder/vmware/common/step_output_dir_test.go index fe71bbea8..3726a9354 100644 --- a/builder/vmware/common/step_output_dir_test.go +++ b/builder/vmware/common/step_output_dir_test.go @@ -30,6 +30,8 @@ func TestStepOutputDir(t *testing.T) { step := new(StepOutputDir) dir := testOutputDir(t) + // Delete the test output directory when done + defer os.RemoveAll(dir.dir) state.Put("dir", dir) // Test the run @@ -61,6 +63,7 @@ func TestStepOutputDir_existsNoForce(t *testing.T) { if err := os.MkdirAll(dir.dir, 0755); err != nil { t.Fatalf("err: %s", err) } + defer os.RemoveAll(dir.dir) // Test the run 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 { t.Fatalf("err: %s", err) } + defer os.RemoveAll(dir.dir) // Test the run if action := step.Run(context.Background(), state); action != multistep.ActionContinue { diff --git a/builder/vmware/common/step_shutdown_test.go b/builder/vmware/common/step_shutdown_test.go index ff775f2f2..0c3d98589 100644 --- a/builder/vmware/common/step_shutdown_test.go +++ b/builder/vmware/common/step_shutdown_test.go @@ -85,6 +85,12 @@ func TestStepShutdown_command(t *testing.T) { if comm.StartCmd.Command != "foo" { 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) { @@ -113,6 +119,12 @@ func TestStepShutdown_noCommand(t *testing.T) { if comm.StartCalled { 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) { diff --git a/common/download_test.go b/common/download_test.go index beac9cbab..ae22fbc09 100644 --- a/common/download_test.go +++ b/common/download_test.go @@ -60,6 +60,7 @@ func TestDownloadClient_basic(t *testing.T) { TargetPath: tf.Name(), CopyFile: true, }) + defer os.Remove(tf.Name()) path, err := client.Get() if err != nil { @@ -96,6 +97,7 @@ func TestDownloadClient_checksumBad(t *testing.T) { Checksum: checksum, CopyFile: true, }) + defer os.Remove(tf.Name()) if _, err := client.Get(); err == nil { t.Fatal("should error") } @@ -121,6 +123,7 @@ func TestDownloadClient_checksumGood(t *testing.T) { Checksum: checksum, CopyFile: true, }) + defer os.Remove(tf.Name()) path, err := client.Get() if err != nil { t.Fatalf("err: %s", err) @@ -191,6 +194,7 @@ func TestDownloadClient_resume(t *testing.T) { TargetPath: tf.Name(), CopyFile: true, }) + defer os.Remove(tf.Name()) path, err := client.Get() if err != nil { t.Fatalf("err: %s", err) @@ -211,7 +215,8 @@ func TestDownloadClient_usesDefaultUserAgent(t *testing.T) { if err != nil { t.Fatalf("tempfile error: %s", err) } - defer os.Remove(tf.Name()) + tf.Close() + os.Remove(tf.Name()) defaultUserAgent := "" asserted := false @@ -249,6 +254,7 @@ func TestDownloadClient_usesDefaultUserAgent(t *testing.T) { TargetPath: tf.Name(), CopyFile: true, } + defer os.Remove(tf.Name()) client := NewDownloadClient(config) _, err = client.Get() @@ -266,7 +272,8 @@ func TestDownloadClient_setsUserAgent(t *testing.T) { if err != nil { t.Fatalf("tempfile error: %s", err) } - defer os.Remove(tf.Name()) + tf.Close() + os.Remove(tf.Name()) asserted := false 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", CopyFile: true, } + defer os.Remove(tf.Name()) client := NewDownloadClient(config) _, err = client.Get() diff --git a/common/iso_config_test.go b/common/iso_config_test.go index f31c687b2..b465bead8 100644 --- a/common/iso_config_test.go +++ b/common/iso_config_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "os" "reflect" "runtime" "testing" @@ -90,6 +91,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { i = testISOConfig() i.ISOChecksum = "" 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) filePrefix := "file://" if runtime.GOOS == "windows" { @@ -112,6 +115,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { i = testISOConfig() i.ISOChecksum = "" 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) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) warns, err = i.Prepare(nil) @@ -130,6 +135,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { i = testISOConfig() i.ISOChecksum = "" 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) i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) warns, err = i.Prepare(nil) @@ -150,6 +157,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { cs_dir, _ := ioutil.TempDir("", "packer-testdir-") 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) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) 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.ISOChecksum = "" 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) i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) 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" 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) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) warns, err = i.Prepare(nil) @@ -208,6 +221,8 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { i.ISOChecksum = "" 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) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso") diff --git a/provisioner/powershell/provisioner_test.go b/provisioner/powershell/provisioner_test.go index 06fe39ef7..7e739016c 100644 --- a/provisioner/powershell/provisioner_test.go +++ b/provisioner/powershell/provisioner_test.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io/ioutil" - //"log" "os" "regexp" "strings" @@ -30,6 +29,7 @@ func TestProvisionerPrepare_extractScript(t *testing.T) { p := new(Provisioner) _ = p.Prepare(config) file, err := extractScript(p) + defer os.Remove(file) if err != nil { t.Fatalf("Should not be error: %s", err) } @@ -177,6 +177,7 @@ func TestProvisionerPrepare_Script(t *testing.T) { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() config["script"] = tf.Name() p = new(Provisioner) @@ -203,6 +204,7 @@ func TestProvisionerPrepare_ScriptAndInline(t *testing.T) { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() config["inline"] = []interface{}{"foo"} config["script"] = tf.Name() @@ -222,6 +224,7 @@ func TestProvisionerPrepare_ScriptAndScripts(t *testing.T) { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() config["inline"] = []interface{}{"foo"} config["scripts"] = []string{tf.Name()} @@ -248,6 +251,7 @@ func TestProvisionerPrepare_Scripts(t *testing.T) { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() config["scripts"] = []string{tf.Name()} p = new(Provisioner) @@ -444,6 +448,8 @@ func TestProvisionerProvision_Inline(t *testing.T) { func TestProvisionerProvision_Scripts(t *testing.T) { tempFile, _ := ioutil.TempFile("", "packer") defer os.Remove(tempFile.Name()) + defer tempFile.Close() + config := testConfig() delete(config, "inline") config["scripts"] = []string{tempFile.Name()} @@ -473,6 +479,8 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { config := testConfig() ui := testUi() defer os.Remove(tempFile.Name()) + defer tempFile.Close() + delete(config, "inline") config["scripts"] = []string{tempFile.Name()} diff --git a/provisioner/puppet-masterless/provisioner_test.go b/provisioner/puppet-masterless/provisioner_test.go index dd6725768..294534779 100644 --- a/provisioner/puppet-masterless/provisioner_test.go +++ b/provisioner/puppet-masterless/provisioner_test.go @@ -13,15 +13,17 @@ import ( "github.com/stretchr/testify/assert" ) -func testConfig() map[string]interface{} { +func testConfig() (config map[string]interface{}, tf *os.File) { tf, err := ioutil.TempFile("", "packer") if err != nil { panic(err) } - return map[string]interface{}{ + config = map[string]interface{}{ "manifest_file": tf.Name(), } + + return config, tf } func TestProvisioner_Impl(t *testing.T) { @@ -33,7 +35,10 @@ func TestProvisioner_Impl(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) err := p.Prepare(config) if err != nil { @@ -58,7 +63,10 @@ func TestGuestOSConfig_empty_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) err := p.Prepare(config) if err != nil { @@ -95,7 +103,10 @@ func TestGuestOSConfig_full_unix(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" p := new(Provisioner) err := p.Prepare(config) @@ -120,7 +131,10 @@ func TestGuestOSConfig_empty_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" p := new(Provisioner) err := p.Prepare(config) @@ -158,7 +172,9 @@ func TestGuestOSConfig_full_windows(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") p := new(Provisioner) @@ -183,7 +199,9 @@ func TestProvisionerPrepare_puppetBinDir(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") p := new(Provisioner) @@ -208,7 +226,9 @@ func TestProvisionerPrepare_hieraConfigPath(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") p := new(Provisioner) @@ -233,7 +253,9 @@ func TestProvisionerPrepare_manifestFile(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") p := new(Provisioner) @@ -258,7 +280,9 @@ func TestProvisionerPrepare_manifestDir(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") p := new(Provisioner) @@ -291,7 +315,9 @@ func TestProvisionerPrepare_modulePaths(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") p := new(Provisioner) @@ -346,7 +372,9 @@ func TestProvisionerPrepare_facterFacts(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 delete(config, "extra_arguments") @@ -377,7 +405,9 @@ func TestProvisionerPrepare_extraArguments(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") p := new(Provisioner) @@ -405,7 +435,9 @@ func TestProvisionerPrepare_stagingDir(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") p := new(Provisioner) @@ -438,7 +470,10 @@ func TestProvisionerPrepare_workingDir(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{ Writer: ioutil.Discard, } diff --git a/provisioner/puppet-server/provisioner_test.go b/provisioner/puppet-server/provisioner_test.go index 4c7746c78..68e9369b6 100644 --- a/provisioner/puppet-server/provisioner_test.go +++ b/provisioner/puppet-server/provisioner_test.go @@ -8,15 +8,17 @@ import ( "github.com/hashicorp/packer/packer" ) -func testConfig() map[string]interface{} { +func testConfig() (config map[string]interface{}, tf *os.File) { tf, err := ioutil.TempFile("", "packer") if err != nil { panic(err) } - return map[string]interface{}{ + config = map[string]interface{}{ "puppet_server": tf.Name(), } + + return config, tf } func TestProvisioner_Impl(t *testing.T) { @@ -28,7 +30,9 @@ func TestProvisioner_Impl(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") p := new(Provisioner) @@ -53,7 +57,9 @@ func TestProvisionerPrepare_puppetBinDir(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") p := new(Provisioner) @@ -86,7 +92,9 @@ func TestProvisionerPrepare_clientPrivateKeyPath(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") p := new(Provisioner) @@ -119,7 +127,9 @@ func TestProvisionerPrepare_clientCertPath(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") p := new(Provisioner) @@ -130,7 +140,9 @@ func TestProvisionerPrepare_executeCommand(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") p := new(Provisioner) @@ -185,7 +197,9 @@ func TestProvisionerPrepare_facterFacts(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") p := new(Provisioner) diff --git a/provisioner/windows-shell/provisioner_test.go b/provisioner/windows-shell/provisioner_test.go index e0731da10..5f4149478 100644 --- a/provisioner/windows-shell/provisioner_test.go +++ b/provisioner/windows-shell/provisioner_test.go @@ -25,6 +25,7 @@ func TestProvisionerPrepare_extractScript(t *testing.T) { p := new(Provisioner) _ = p.Prepare(config) file, err := extractScript(p) + defer os.Remove(file) if err != nil { t.Fatalf("Should not be error: %s", err) } @@ -104,6 +105,7 @@ func TestProvisionerPrepare_Script(t *testing.T) { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() config["script"] = tf.Name() p = new(Provisioner) @@ -130,6 +132,7 @@ func TestProvisionerPrepare_ScriptAndInline(t *testing.T) { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() config["inline"] = []interface{}{"foo"} config["script"] = tf.Name() @@ -149,6 +152,7 @@ func TestProvisionerPrepare_ScriptAndScripts(t *testing.T) { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() config["inline"] = []interface{}{"foo"} config["scripts"] = []string{tf.Name()} @@ -175,6 +179,7 @@ func TestProvisionerPrepare_Scripts(t *testing.T) { t.Fatalf("error tempfile: %s", err) } defer os.Remove(tf.Name()) + defer tf.Close() config["scripts"] = []string{tf.Name()} p = new(Provisioner) @@ -322,11 +327,16 @@ func TestProvisionerProvision_Inline(t *testing.T) { } func TestProvisionerProvision_Scripts(t *testing.T) { - tempFile, _ := ioutil.TempFile("", "packer") - defer os.Remove(tempFile.Name()) + tf, err := ioutil.TempFile("", "packer") + if err != nil { + t.Fatalf("error tempfile: %s", err) + } + defer os.Remove(tf.Name()) + defer tf.Close() + config := testConfig() delete(config, "inline") - config["scripts"] = []string{tempFile.Name()} + config["scripts"] = []string{tf.Name()} config["packer_build_name"] = "foobuild" config["packer_builder_type"] = "footype" ui := testUi() @@ -334,7 +344,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) { p := new(Provisioner) comm := new(packer.MockCommunicator) p.Prepare(config) - err := p.Provision(ui, comm) + err = p.Provision(ui, comm) if err != nil { t.Fatal("should not have error") } @@ -349,13 +359,18 @@ func TestProvisionerProvision_Scripts(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() ui := testUi() - defer os.Remove(tempFile.Name()) delete(config, "inline") - config["scripts"] = []string{tempFile.Name()} + config["scripts"] = []string{tf.Name()} config["packer_build_name"] = "foobuild" config["packer_builder_type"] = "footype" @@ -368,7 +383,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { p := new(Provisioner) comm := new(packer.MockCommunicator) p.Prepare(config) - err := p.Provision(ui, comm) + err = p.Provision(ui, comm) if err != nil { t.Fatal("should not have error") }