From 5642f8cb012a04cb70aa94b08c8b4e49e2605aa1 Mon Sep 17 00:00:00 2001 From: DanHam Date: Mon, 30 Apr 2018 20:36:48 +0100 Subject: [PATCH] Remove tmp files created by builder/googlecompute tests --- builder/googlecompute/config_test.go | 38 ++++++++++++++----- .../googlecompute/step_create_ssh_key_test.go | 1 + .../step_create_windows_password_test.go | 1 + 3 files changed, 30 insertions(+), 10 deletions(-) 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)