diff --git a/common/download_test.go b/common/download_test.go index beac9cbab84..ae22fbc09d5 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 f31c687b2a2..b465bead86a 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")