Skip to content

Commit

Permalink
Remove tmp files created by common/ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanHam committed Apr 30, 2018
1 parent fefaf0f commit 7e478f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
12 changes: 10 additions & 2 deletions common/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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) {
Expand All @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions common/iso_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"reflect"
"runtime"
"testing"
Expand Down Expand Up @@ -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" {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit 7e478f6

Please sign in to comment.