Skip to content

Commit

Permalink
Remove tmp file created by common/ test. Avoid possible race condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanHam committed May 9, 2018
1 parent 3944088 commit d9d8b93
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions common/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestDownloadClientVerifyChecksum(t *testing.T) {
func TestDownloadClient_basic(t *testing.T) {
tf, _ := ioutil.TempFile("", "packer")
tf.Close()
os.Remove(tf.Name())
defer os.Remove(tf.Name())

ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root")))
defer ts.Close()
Expand All @@ -60,7 +60,6 @@ func TestDownloadClient_basic(t *testing.T) {
TargetPath: tf.Name(),
CopyFile: true,
})
defer os.Remove(tf.Name())

path, err := client.Get()
if err != nil {
Expand All @@ -85,7 +84,7 @@ func TestDownloadClient_checksumBad(t *testing.T) {

tf, _ := ioutil.TempFile("", "packer")
tf.Close()
os.Remove(tf.Name())
defer os.Remove(tf.Name())

ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root")))
defer ts.Close()
Expand All @@ -97,7 +96,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 @@ -111,7 +110,7 @@ func TestDownloadClient_checksumGood(t *testing.T) {

tf, _ := ioutil.TempFile("", "packer")
tf.Close()
os.Remove(tf.Name())
defer os.Remove(tf.Name())

ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root")))
defer ts.Close()
Expand All @@ -123,7 +122,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 @@ -176,7 +175,7 @@ func TestDownloadClient_checksumNoDownload(t *testing.T) {
func TestDownloadClient_notFound(t *testing.T) {
tf, _ := ioutil.TempFile("", "packer")
tf.Close()
os.Remove(tf.Name())
defer os.Remove(tf.Name())

ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root")))
defer ts.Close()
Expand All @@ -195,6 +194,7 @@ func TestDownloadClient_resume(t *testing.T) {
tf, _ := ioutil.TempFile("", "packer")
tf.Write([]byte("w"))
tf.Close()
defer os.Remove(tf.Name())

ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.Method == "HEAD" {
Expand All @@ -212,7 +212,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 @@ -234,7 +234,7 @@ func TestDownloadClient_usesDefaultUserAgent(t *testing.T) {
t.Fatalf("tempfile error: %s", err)
}
tf.Close()
os.Remove(tf.Name())
defer os.Remove(tf.Name())

defaultUserAgent := ""
asserted := false
Expand Down Expand Up @@ -272,7 +272,6 @@ func TestDownloadClient_usesDefaultUserAgent(t *testing.T) {
TargetPath: tf.Name(),
CopyFile: true,
}
defer os.Remove(tf.Name())

client := NewDownloadClient(config)
_, err = client.Get()
Expand All @@ -291,7 +290,7 @@ func TestDownloadClient_setsUserAgent(t *testing.T) {
t.Fatalf("tempfile error: %s", err)
}
tf.Close()
os.Remove(tf.Name())
defer os.Remove(tf.Name())

asserted := false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -306,7 +305,6 @@ 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

0 comments on commit d9d8b93

Please sign in to comment.