From e573fde668dcb418561e61535c1d68b2888f5b0f Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Fri, 29 Dec 2017 23:53:43 -0500 Subject: [PATCH 01/12] replace invalid TMPDIR variable wth PACKER_TMP_DIR. update ConfigTmpDir() to try common temporary paths first and only write to configDir() as a last resort. --- packer/config_file.go | 21 ++++++++++++------- packer/config_file_unix.go | 2 +- website/source/docs/other/debugging.html.md | 2 +- .../docs/other/environment-variables.html.md | 13 ++++++------ 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/packer/config_file.go b/packer/config_file.go index edd10edee..3bd9cfe8d 100644 --- a/packer/config_file.go +++ b/packer/config_file.go @@ -20,17 +20,24 @@ func ConfigDir() (string, error) { // ConfigTmpDir returns the configuration tmp directory for Packer func ConfigTmpDir() (string, error) { - if tmpdir := os.Getenv("PACKER_TMP_DIR"); tmpdir != "" { - return filepath.Abs(tmpdir) + var tmpdir, td string + var found bool + + if tmpdir = os.Getenv("PACKER_TMP_DIR"); tmpdir == "" { + for e := range []string{"TEMP", "TMP", "LOCALAPPDATA"} { + if tmpdir, found := os.LookupEnv(e); found { + td = filepath.Join(tmpdir, "packer") + break + } + } } - configdir, err := configDir() - if err != nil { - return "", err + if tmpdir == "" { + td = filepath.Join(configDir(), "tmp") } - td := filepath.Join(configdir, "tmp") + _, err = os.Stat(td) if os.IsNotExist(err) { - if err = os.MkdirAll(td, 0755); err != nil { + if err = os.MkdirAll(td, 0700); err != nil { return "", err } } else if err != nil { diff --git a/packer/config_file_unix.go b/packer/config_file_unix.go index 512ac42ed..9b55cd9b6 100644 --- a/packer/config_file_unix.go +++ b/packer/config_file_unix.go @@ -18,7 +18,7 @@ func configFile() (string, error) { return "", err } - return filepath.Join(dir, ".packerconfig"), nil + return filepath.Join(dir, "packer.config"), nil } func configDir() (string, error) { diff --git a/website/source/docs/other/debugging.html.md b/website/source/docs/other/debugging.html.md index 367e81061..8f45df932 100644 --- a/website/source/docs/other/debugging.html.md +++ b/website/source/docs/other/debugging.html.md @@ -139,4 +139,4 @@ Failed to initialize build 'docker': error initializing builder 'docker': plugin ``` you should try setting your temp directory to something shorter. This can be -done through the `TMPDIR` environment variable. +done through the `PACKER_TMP_DIR` environment variable. diff --git a/website/source/docs/other/environment-variables.html.md b/website/source/docs/other/environment-variables.html.md index cd36ef1ad..a5e86dec6 100644 --- a/website/source/docs/other/environment-variables.html.md +++ b/website/source/docs/other/environment-variables.html.md @@ -42,9 +42,10 @@ each can be found below: new versions of Packer. If you want to disable this for security or privacy reasons, you can set this environment variable to `1`. -- `TMPDIR` (Unix) / `TMP` (Windows) - The location of the directory used for - temporary files (defaults to `/tmp` on Linux/Unix and - `%USERPROFILE%\AppData\Local\Temp` on Windows Vista and above). It might be - necessary to customize it when working with large files since `/tmp` is a - memory-backed filesystem in some Linux distributions in which case - `/var/tmp` might be preferred. +- `PACKER_TMP_DIR` - The directory used for temporary files during marshalling. + If unset, appends 'packer' to environment variables TEMP, TMP, or LOCALAPPDATA + (Windows) before falling back to the value of `configDir()/tmp` which resolves + to `$HOME/.packer.d/` (Unix) or `%APPDAT%\packer.d` (Windows). + This is not to be confused with the provisionee's temporary directory which + is often defined (hard-coded) as '/tmp' or '%SYSTEMROOT%\Temp' per each + provisioner module. From acaa2b31ed463219c4ef099f351eec72406e2989 Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Tue, 30 Oct 2018 19:08:39 -0400 Subject: [PATCH 02/12] revert dangling config file change --- packer/config_file_unix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packer/config_file_unix.go b/packer/config_file_unix.go index 9b55cd9b6..512ac42ed 100644 --- a/packer/config_file_unix.go +++ b/packer/config_file_unix.go @@ -18,7 +18,7 @@ func configFile() (string, error) { return "", err } - return filepath.Join(dir, "packer.config"), nil + return filepath.Join(dir, ".packerconfig"), nil } func configDir() (string, error) { From eb65416619437e4a3dec90277770803dd5b2281c Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Tue, 30 Oct 2018 19:30:44 -0400 Subject: [PATCH 03/12] match styistic convension with rest of docs --- website/source/docs/other/environment-variables.html.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/website/source/docs/other/environment-variables.html.md b/website/source/docs/other/environment-variables.html.md index a5e86dec6..9e295de17 100644 --- a/website/source/docs/other/environment-variables.html.md +++ b/website/source/docs/other/environment-variables.html.md @@ -43,9 +43,9 @@ each can be found below: reasons, you can set this environment variable to `1`. - `PACKER_TMP_DIR` - The directory used for temporary files during marshalling. - If unset, appends 'packer' to environment variables TEMP, TMP, or LOCALAPPDATA - (Windows) before falling back to the value of `configDir()/tmp` which resolves - to `$HOME/.packer.d/` (Unix) or `%APPDAT%\packer.d` (Windows). + If unset, appends 'packer' to environment variables TEMP, TMP, or (windows: + LOCALAPPDATA) before falling back to the value of `configDir()/tmp` which + resolves to (unix: `$HOME/.packer.d`, windows: `%APPDAT%/packer.d`). This is not to be confused with the provisionee's temporary directory which - is often defined (hard-coded) as '/tmp' or '%SYSTEMROOT%\Temp' per each + is often defined (hard-coded) as '/tmp' or '%SYSTEMROOT%/Temp' per each provisioner module. From d74839ede05dacf712b55a7bb48aec19fe6b007f Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Tue, 30 Oct 2018 20:40:58 -0400 Subject: [PATCH 04/12] remove extraneous variable declaration, fix FOR loop --- packer/config_file.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/packer/config_file.go b/packer/config_file.go index 3bd9cfe8d..0b38e0ec2 100644 --- a/packer/config_file.go +++ b/packer/config_file.go @@ -20,22 +20,19 @@ func ConfigDir() (string, error) { // ConfigTmpDir returns the configuration tmp directory for Packer func ConfigTmpDir() (string, error) { - var tmpdir, td string - var found bool - - if tmpdir = os.Getenv("PACKER_TMP_DIR"); tmpdir == "" { - for e := range []string{"TEMP", "TMP", "LOCALAPPDATA"} { + if tmpdir := os.Getenv("PACKER_TMP_DIR"); tmpdir == "" { + for _, e := range []string{"TEMP", "TMP", "LOCALAPPDATA"} { if tmpdir, found := os.LookupEnv(e); found { - td = filepath.Join(tmpdir, "packer") + td := filepath.Join(tmpdir, "packer") break } } } if tmpdir == "" { - td = filepath.Join(configDir(), "tmp") + td := filepath.Join(ConfigDir(), "tmp") } - _, err = os.Stat(td) + _, err := os.Stat(td) if os.IsNotExist(err) { if err = os.MkdirAll(td, 0700); err != nil { return "", err From 0f2933adb6f6922dfeab78a95371a444ec8918ab Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Wed, 31 Oct 2018 00:54:35 -0400 Subject: [PATCH 05/12] use mktemp() equivalent to create temporary directory --- packer/config_file.go | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/packer/config_file.go b/packer/config_file.go index 0b38e0ec2..63e195c43 100644 --- a/packer/config_file.go +++ b/packer/config_file.go @@ -1,8 +1,9 @@ package packer import ( + "io/ioutil" + "log" "os" - "path/filepath" ) // ConfigFile returns the default path to the configuration file. On @@ -20,25 +21,20 @@ func ConfigDir() (string, error) { // ConfigTmpDir returns the configuration tmp directory for Packer func ConfigTmpDir() (string, error) { - if tmpdir := os.Getenv("PACKER_TMP_DIR"); tmpdir == "" { - for _, e := range []string{"TEMP", "TMP", "LOCALAPPDATA"} { - if tmpdir, found := os.LookupEnv(e); found { - td := filepath.Join(tmpdir, "packer") - break - } + var tmpdir, td, cd string + + cd, err := ConfigDir() + for _, tmpdir := range []string{os.Getenv("PACKER_TMP_DIR"), os.TempDir(), cd} { + if tmpdir != "" { + break } } - if tmpdir == "" { - td := filepath.Join(ConfigDir(), "tmp") - } - _, err := os.Stat(td) - if os.IsNotExist(err) { - if err = os.MkdirAll(td, 0700); err != nil { - return "", err - } - } else if err != nil { - return "", err + if td, err := ioutil.TempDir(tmpdir, "packer"); err != nil { + log.Fatal(err) } + + defer os.RemoveAll(td) + return td, nil } From 7a730450916bf8e5dbc2a741ec233a49466ab7cc Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Wed, 31 Oct 2018 01:06:00 -0400 Subject: [PATCH 06/12] adjust var declaration scope --- packer/config_file.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packer/config_file.go b/packer/config_file.go index 63e195c43..2b68074cf 100644 --- a/packer/config_file.go +++ b/packer/config_file.go @@ -22,15 +22,16 @@ func ConfigDir() (string, error) { // ConfigTmpDir returns the configuration tmp directory for Packer func ConfigTmpDir() (string, error) { var tmpdir, td, cd string + var err error - cd, err := ConfigDir() - for _, tmpdir := range []string{os.Getenv("PACKER_TMP_DIR"), os.TempDir(), cd} { + cd, _ = ConfigDir() + for _, tmpdir = range []string{os.Getenv("PACKER_TMP_DIR"), os.TempDir(), cd} { if tmpdir != "" { break } } - if td, err := ioutil.TempDir(tmpdir, "packer"); err != nil { + if td, err = ioutil.TempDir(tmpdir, "packer"); err != nil { log.Fatal(err) } From c22092c601c33484327674f322c7379fa41506d7 Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Wed, 31 Oct 2018 20:11:36 -0400 Subject: [PATCH 07/12] close massive file overwrite hole with TempDir --- helper/common/shared_state.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/helper/common/shared_state.go b/helper/common/shared_state.go index 87e111cfb..88ddf0aaa 100644 --- a/helper/common/shared_state.go +++ b/helper/common/shared_state.go @@ -7,11 +7,16 @@ import ( "path/filepath" ) +var sharedStateDir := ioutil.TempDir(packer.ConfigTmpDir(), "state") + // Used to set variables which we need to access later in the build, where // state bag and config information won't work func sharedStateFilename(suffix string, buildName string) string { uuid := os.Getenv("PACKER_RUN_UUID") - return filepath.Join(os.TempDir(), fmt.Sprintf("packer-%s-%s-%s", uuid, suffix, buildName)) + if uuid == "" { + uuid="none" + } + return filepath.Join(sharedStateDir, fmt.Sprintf("%s-%s-%s", uuid, suffix, buildName)) } func SetSharedState(key string, value string, buildName string) error { From 6b5b8f6d4edad0e187ca13d64ac4118f34eee643 Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Thu, 1 Nov 2018 07:21:59 -0400 Subject: [PATCH 08/12] refacter config_file. replace all hard-coded os.TempDir with wrapper --- builder/docker/step_temp_dir.go | 7 +- builder/docker/step_temp_dir_test.go | 22 +++---- .../hyperv/common/step_create_build_dir.go | 5 +- .../common/step_create_build_dir_test.go | 6 +- .../hyperv/common/step_mount_floppydrive.go | 4 +- .../virtualbox/common/step_attach_floppy.go | 4 +- builder/vmware/iso/step_create_vmx.go | 4 +- checkpoint.go | 4 +- common/powershell/powershell.go | 5 +- config.go | 3 +- helper/common/shared_state.go | 19 +++++- main.go | 3 +- packer/config_file.go | 41 ------------ packer/configfile/configfile.go | 47 ++++++++++++++ packer/configfile/configfile_test.go | 65 +++++++++++++++++++ .../unix.go} | 4 +- .../windows.go} | 2 +- packer/telemetry.go | 3 +- post-processor/vagrant/post-processor.go | 4 +- provisioner/ansible/scp.go | 7 +- provisioner/powershell/provisioner.go | 4 +- provisioner/windows-shell/provisioner.go | 5 +- provisioner/windows-shell/provisioner_test.go | 6 +- template/parse.go | 4 +- 24 files changed, 192 insertions(+), 86 deletions(-) delete mode 100644 packer/config_file.go create mode 100644 packer/configfile/configfile.go create mode 100644 packer/configfile/configfile_test.go rename packer/{config_file_unix.go => configfile/unix.go} (91%) rename packer/{config_file_windows.go => configfile/windows.go} (97%) diff --git a/builder/docker/step_temp_dir.go b/builder/docker/step_temp_dir.go index a5b1d6ade..622a2150c 100644 --- a/builder/docker/step_temp_dir.go +++ b/builder/docker/step_temp_dir.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) // StepTempDir creates a temporary directory that we use in order to @@ -24,10 +25,8 @@ func (s *StepTempDir) Run(_ context.Context, state multistep.StateBag) multistep var err error var tempdir string - configTmpDir, err := packer.ConfigTmpDir() - if err == nil { - tempdir, err = ioutil.TempDir(configTmpDir, "packer-docker") - } + prefix, _ := configfile.ConfigTmpDir() + tempdir, err = ioutil.TempDir(prefix, "docker") if err != nil { err := fmt.Errorf("Error making temp dir: %s", err) state.Put("error", err) diff --git a/builder/docker/step_temp_dir_test.go b/builder/docker/step_temp_dir_test.go index e36d4fcff..b15154353 100644 --- a/builder/docker/step_temp_dir_test.go +++ b/builder/docker/step_temp_dir_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/hashicorp/packer/helper/multistep" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func TestStepTempDir_impl(t *testing.T) { @@ -54,20 +54,18 @@ func TestStepTempDir(t *testing.T) { } func TestStepTempDir_notmpdir(t *testing.T) { - tempenv := "PACKER_TMP_DIR" - - oldenv := os.Getenv(tempenv) - defer os.Setenv(tempenv, oldenv) - os.Setenv(tempenv, "") + oldenv := os.Getenv(configfile.EnvPackerTmpDir) + defer os.Setenv(configfile.EnvPackerTmpDir, oldenv) + os.Setenv(configfile.EnvPackerTmpDir, "") dir1 := testStepTempDir_impl(t) - cd, err := packer.ConfigDir() + cd, err := configfile.ConfigDir() if err != nil { t.Fatalf("bad ConfigDir") } td := filepath.Join(cd, "tmp") - os.Setenv(tempenv, td) + os.Setenv(configfile.EnvPackerTmpDir, td) dir2 := testStepTempDir_impl(t) @@ -77,11 +75,9 @@ func TestStepTempDir_notmpdir(t *testing.T) { } func TestStepTempDir_packertmpdir(t *testing.T) { - tempenv := "PACKER_TMP_DIR" - - oldenv := os.Getenv(tempenv) - defer os.Setenv(tempenv, oldenv) - os.Setenv(tempenv, ".") + oldenv := os.Getenv(configfile.EnvPackerTmpDir) + defer os.Setenv(configfile.EnvPackerTmpDir, oldenv) + os.Setenv(configfile.EnvPackerTmpDir, ".") dir1 := testStepTempDir_impl(t) diff --git a/builder/hyperv/common/step_create_build_dir.go b/builder/hyperv/common/step_create_build_dir.go index 405a3407c..53c04b492 100644 --- a/builder/hyperv/common/step_create_build_dir.go +++ b/builder/hyperv/common/step_create_build_dir.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) type StepCreateBuildDir struct { @@ -30,11 +31,11 @@ func (s *StepCreateBuildDir) Run(_ context.Context, state multistep.StateBag) mu ui.Say("Creating build directory...") if s.TempPath == "" { - s.TempPath = os.TempDir() + s.TempPath, _ = configfile.ConfigTmpDir() } var err error - s.buildDir, err = ioutil.TempDir(s.TempPath, "packerhv") + s.buildDir, err = ioutil.TempDir(s.TempPath, "hyperv") if err != nil { err = fmt.Errorf("Error creating build directory: %s", err) state.Put("error", err) diff --git a/builder/hyperv/common/step_create_build_dir_test.go b/builder/hyperv/common/step_create_build_dir_test.go index 3f0fe57cc..5b12b16b6 100644 --- a/builder/hyperv/common/step_create_build_dir_test.go +++ b/builder/hyperv/common/step_create_build_dir_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer/configfile" ) func TestStepCreateBuildDir_imp(t *testing.T) { @@ -37,8 +38,9 @@ func TestStepCreateBuildDir_Defaults(t *testing.T) { // On windows convert everything to forward slash separated paths // This prevents the regexp interpreting backslashes as escape sequences stateBuildDir := filepath.ToSlash(v.(string)) + configTmpDir, _ := configfile.ConfigTmpDir() expectedBuildDirRe := regexp.MustCompile( - filepath.ToSlash(filepath.Join(os.TempDir(), "packerhv") + `[[:digit:]]{9}$`)) + filepath.ToSlash(filepath.Join(configTmpDir, "hyperv") + `[[:digit:]]{9}$`)) match := expectedBuildDirRe.MatchString(stateBuildDir) if !match { t.Fatalf("Got path that doesn't match expected format in 'build_dir': %s", stateBuildDir) @@ -79,7 +81,7 @@ func TestStepCreateBuildDir_UserDefinedTempPath(t *testing.T) { // This prevents the regexp interpreting backslashes as escape sequences stateBuildDir := filepath.ToSlash(v.(string)) expectedBuildDirRe := regexp.MustCompile( - filepath.ToSlash(filepath.Join(step.TempPath, "packerhv") + `[[:digit:]]{9}$`)) + filepath.ToSlash(filepath.Join(step.TempPath, "hyperv") + `[[:digit:]]{9}$`)) match := expectedBuildDirRe.MatchString(stateBuildDir) if !match { t.Fatalf("Got path that doesn't match expected format in 'build_dir': %s", stateBuildDir) diff --git a/builder/hyperv/common/step_mount_floppydrive.go b/builder/hyperv/common/step_mount_floppydrive.go index 1ae03d1db..483938c9b 100644 --- a/builder/hyperv/common/step_mount_floppydrive.go +++ b/builder/hyperv/common/step_mount_floppydrive.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) const ( @@ -93,7 +94,8 @@ func (s *StepMountFloppydrive) Cleanup(state multistep.StateBag) { } func (s *StepMountFloppydrive) copyFloppy(path string) (string, error) { - tempdir, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tempdir, err := ioutil.TempDir(prefix, "hyperv") if err != nil { return "", err } diff --git a/builder/virtualbox/common/step_attach_floppy.go b/builder/virtualbox/common/step_attach_floppy.go index 6c73aa4b4..7cf1534a7 100644 --- a/builder/virtualbox/common/step_attach_floppy.go +++ b/builder/virtualbox/common/step_attach_floppy.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) // This step attaches the ISO to the virtual machine. @@ -106,7 +107,8 @@ func (s *StepAttachFloppy) Cleanup(state multistep.StateBag) { } func (s *StepAttachFloppy) copyFloppy(path string) (string, error) { - tempdir, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tempdir, err := ioutil.TempDir(prefix, "virtualbox") if err != nil { return "", err } diff --git a/builder/vmware/iso/step_create_vmx.go b/builder/vmware/iso/step_create_vmx.go index 432d543fe..529af33a7 100644 --- a/builder/vmware/iso/step_create_vmx.go +++ b/builder/vmware/iso/step_create_vmx.go @@ -12,6 +12,7 @@ import ( vmwcommon "github.com/hashicorp/packer/builder/vmware/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template/interpolate" ) @@ -614,7 +615,8 @@ func (s *stepCreateVMX) Run(_ context.Context, state multistep.StateBag) multist if config.RemoteType != "" { // For remote builds, we just put the VMX in a temporary // directory since it just gets uploaded anyways. - vmxDir, err = ioutil.TempDir("", "packer-vmx") + prefix, _ := configfile.ConfigTmpDir() + vmxDir, err = ioutil.TempDir(prefix, "vmware") if err != nil { err := fmt.Errorf("Error preparing VMX template: %s", err) state.Put("error", err) diff --git a/checkpoint.go b/checkpoint.go index bbefeef5e..570a18f43 100644 --- a/checkpoint.go +++ b/checkpoint.go @@ -7,7 +7,7 @@ import ( "github.com/hashicorp/go-checkpoint" "github.com/hashicorp/packer/command" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" packerVersion "github.com/hashicorp/packer/version" ) @@ -27,7 +27,7 @@ func runCheckpoint(c *config) { return } - configDir, err := packer.ConfigDir() + configDir, err := configfile.ConfigDir() if err != nil { log.Printf("[ERR] Checkpoint setup error: %s", err) checkpointResult <- nil diff --git a/common/powershell/powershell.go b/common/powershell/powershell.go index 4c3dd80d1..5765aec3c 100644 --- a/common/powershell/powershell.go +++ b/common/powershell/powershell.go @@ -10,6 +10,8 @@ import ( "os/exec" "strconv" "strings" + + "github.com/hashicorp/packer/packer/configfile" ) const ( @@ -121,7 +123,8 @@ func (ps *PowerShellCmd) getPowerShellPath() (string, error) { } func saveScript(fileContents string) (string, error) { - file, err := ioutil.TempFile(os.TempDir(), "ps") + prefix, _ := configfile.ConfigTmpDir() + file, err := ioutil.TempFile(prefix, "powershell") if err != nil { return "", err } diff --git a/config.go b/config.go index 409b8f12d..c5866e2a1 100644 --- a/config.go +++ b/config.go @@ -13,6 +13,7 @@ import ( "github.com/hashicorp/packer/command" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/packer/plugin" "github.com/kardianos/osext" ) @@ -64,7 +65,7 @@ func (c *config) Discover() error { } // Next, look in the plugins directory. - dir, err := packer.ConfigDir() + dir, err := configfile.ConfigDir() if err != nil { log.Printf("[ERR] Error loading config directory: %s", err) } else { diff --git a/helper/common/shared_state.go b/helper/common/shared_state.go index 88ddf0aaa..c1316e9f4 100644 --- a/helper/common/shared_state.go +++ b/helper/common/shared_state.go @@ -5,16 +5,29 @@ import ( "io/ioutil" "os" "path/filepath" + + "github.com/hashicorp/packer/packer/configfile" ) -var sharedStateDir := ioutil.TempDir(packer.ConfigTmpDir(), "state") +var sharedStateDir string // Used to set variables which we need to access later in the build, where // state bag and config information won't work func sharedStateFilename(suffix string, buildName string) string { - uuid := os.Getenv("PACKER_RUN_UUID") + var uuid string + + if sharedStateDir == "" { + prefix, _ := configfile.ConfigTmpDir() + sharedStateDir, err := ioutil.TempDir(prefix, "state") + if err != nil { + return "" + } + defer os.RemoveAll(sharedStateDir) + } + + uuid = os.Getenv("PACKER_RUN_UUID") if uuid == "" { - uuid="none" + uuid = "none" } return filepath.Join(sharedStateDir, fmt.Sprintf("%s-%s-%s", uuid, suffix, buildName)) } diff --git a/main.go b/main.go index 16ec61b41..0ad7fe94e 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "github.com/hashicorp/go-uuid" "github.com/hashicorp/packer/command" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/packer/plugin" "github.com/hashicorp/packer/version" "github.com/mitchellh/cli" @@ -294,7 +295,7 @@ func loadConfig() (*config, error) { log.Printf("'PACKER_CONFIG' set, loading config from environment.") } else { var err error - configFilePath, err = packer.ConfigFile() + configFilePath, err = configfile.ConfigFile() if err != nil { log.Printf("Error detecting default config file path: %s", err) diff --git a/packer/config_file.go b/packer/config_file.go deleted file mode 100644 index 2b68074cf..000000000 --- a/packer/config_file.go +++ /dev/null @@ -1,41 +0,0 @@ -package packer - -import ( - "io/ioutil" - "log" - "os" -) - -// ConfigFile returns the default path to the configuration file. On -// Unix-like systems this is the ".packerconfig" file in the home directory. -// On Windows, this is the "packer.config" file in the application data -// directory. -func ConfigFile() (string, error) { - return configFile() -} - -// ConfigDir returns the configuration directory for Packer. -func ConfigDir() (string, error) { - return configDir() -} - -// ConfigTmpDir returns the configuration tmp directory for Packer -func ConfigTmpDir() (string, error) { - var tmpdir, td, cd string - var err error - - cd, _ = ConfigDir() - for _, tmpdir = range []string{os.Getenv("PACKER_TMP_DIR"), os.TempDir(), cd} { - if tmpdir != "" { - break - } - } - - if td, err = ioutil.TempDir(tmpdir, "packer"); err != nil { - log.Fatal(err) - } - - defer os.RemoveAll(td) - - return td, nil -} diff --git a/packer/configfile/configfile.go b/packer/configfile/configfile.go new file mode 100644 index 000000000..33c641602 --- /dev/null +++ b/packer/configfile/configfile.go @@ -0,0 +1,47 @@ +package configfile + +import ( + "os" + "path/filepath" +) + +const EnvPackerTmpDir = "PACKER_TMP_DIR" + +// ConfigFile returns the default path to the configuration file. On +// Unix-like systems this is the ".packerconfig" file in the home directory. +// On Windows, this is the "packer.config" file in the application data +// directory. +func ConfigFile() (string, error) { + return configFile() +} + +// ConfigDir returns the configuration directory for Packer. +func ConfigDir() (string, error) { + return configDir() +} + +// ConfigTmpDir returns a "deterministic" (based on environment or Packer config) +// path intended as the root of subsequent temporary items, to minimize scatter. +// +// The caller must ensure safe tempfile practice via ioutil.TempDir() and friends. +func ConfigTmpDir() (string, error) { + var tmpdir, td string + var err error + + if tmpdir = os.Getenv(EnvPackerTmpDir); tmpdir != "" { + td, err = filepath.Abs(tmpdir) + } else if tmpdir, err = configDir(); err == nil { + td = filepath.Join(tmpdir, "tmp") + } else if tmpdir = os.TempDir(); tmpdir != "" { + td = filepath.Join(tmpdir, "packer") + } + + if _, err = os.Stat(td); os.IsNotExist(err) { + err = os.MkdirAll(td, 0700) + } + if err != nil { + return "", err + } + + return td, nil +} diff --git a/packer/configfile/configfile_test.go b/packer/configfile/configfile_test.go new file mode 100644 index 000000000..137627040 --- /dev/null +++ b/packer/configfile/configfile_test.go @@ -0,0 +1,65 @@ +package configfile + +import ( + "os" + "path/filepath" + "io/ioutil" + "fmt" + "testing" +) + +func testConfigTmpDir_impl(t *testing.T) string { + var dir string + + prefix, _ := ConfigTmpDir() + if dir, err := ioutil.TempDir(prefix, ""); err == nil { + defer os.RemoveAll(dir) + } else { + _ := fmt.Errorf("Error making directory: %s", err) + } + + return dir +} + +func TestConfigTmpDir(t *testing.T) { + testConfigTmpDir_impl(t) +} + +func TestConfigTmpDir_noenv_PackerTmpDir(t *testing.T) { + oldenv := os.Getenv(EnvPackerTmpDir) + defer os.Setenv(EnvPackerTmpDir, oldenv) + os.Setenv(EnvPackerTmpDir, "") + + dir1 := testConfigTmpDir_impl(t) + + cd, err := ConfigDir() + if err != nil { + t.Fatalf("bad ConfigDir") + } + td := filepath.Join(cd, "tmp") + os.Setenv(EnvPackerTmpDir, td) + + dir2 := testConfigTmpDir_impl(t) + + if filepath.Dir(dir1) != filepath.Dir(dir2) { + t.Fatalf("base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) + } +} + +func TestConfigTmpDir_PackerTmpDir(t *testing.T) { + oldenv := os.Getenv(EnvPackerTmpDir) + defer os.Setenv(EnvPackerTmpDir, oldenv) + os.Setenv(EnvPackerTmpDir, ".") + + dir1 := testConfigTmpDir_impl(t) + + abspath, err := filepath.Abs(".") + if err != nil { + t.Fatalf("bad absolute path") + } + dir2 := filepath.Join(abspath, "tmp") + + if filepath.Dir(dir1) != filepath.Dir(dir2) { + t.Fatalf("base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) + } +} diff --git a/packer/config_file_unix.go b/packer/configfile/unix.go similarity index 91% rename from packer/config_file_unix.go rename to packer/configfile/unix.go index 512ac42ed..fd3e95147 100644 --- a/packer/config_file_unix.go +++ b/packer/configfile/unix.go @@ -1,11 +1,10 @@ // +build darwin freebsd linux netbsd openbsd solaris -package packer +package configfile import ( "bytes" "errors" - "log" "os" "os/exec" "path/filepath" @@ -33,7 +32,6 @@ func configDir() (string, error) { func homeDir() (string, error) { // First prefer the HOME environmental variable if home := os.Getenv("HOME"); home != "" { - log.Printf("Detected home directory from env var: %s", home) return home, nil } diff --git a/packer/config_file_windows.go b/packer/configfile/windows.go similarity index 97% rename from packer/config_file_windows.go rename to packer/configfile/windows.go index d0bcc1c50..37370c1f6 100644 --- a/packer/config_file_windows.go +++ b/packer/configfile/windows.go @@ -1,6 +1,6 @@ // +build windows -package packer +package configfile import ( "path/filepath" diff --git a/packer/telemetry.go b/packer/telemetry.go index 0f493a83a..9f69ff9a9 100644 --- a/packer/telemetry.go +++ b/packer/telemetry.go @@ -9,6 +9,7 @@ import ( "time" checkpoint "github.com/hashicorp/go-checkpoint" + "github.com/hashicorp/packer/packer/configfile" packerVersion "github.com/hashicorp/packer/version" ) @@ -35,7 +36,7 @@ func NewCheckpointReporter(disableSignature bool) *CheckpointTelemetry { return nil } - configDir, err := ConfigDir() + configDir, err := configfile.ConfigDir() if err != nil { log.Printf("[WARN] (telemetry) setup error: %s", err) return nil diff --git a/post-processor/vagrant/post-processor.go b/post-processor/vagrant/post-processor.go index 5ea07f3a3..39cab9681 100644 --- a/post-processor/vagrant/post-processor.go +++ b/post-processor/vagrant/post-processor.go @@ -14,6 +14,7 @@ import ( "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template/interpolate" "github.com/mitchellh/mapstructure" ) @@ -95,7 +96,8 @@ func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui p } // Create a temporary directory for us to build the contents of the box in - dir, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + dir, err := ioutil.TempDir(prefix, "vagrant") if err != nil { return nil, false, err } diff --git a/provisioner/ansible/scp.go b/provisioner/ansible/scp.go index 0d951c728..037273b9f 100644 --- a/provisioner/ansible/scp.go +++ b/provisioner/ansible/scp.go @@ -14,6 +14,7 @@ import ( "time" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) const ( @@ -43,7 +44,8 @@ func scpUploadSession(opts []byte, rest string, in io.Reader, out io.Writer, com return errors.New("no scp target specified") } - d, err := ioutil.TempDir("", "packer-ansible-upload") + prefix, _ := configfile.ConfigTmpDir() + d, err := ioutil.TempDir(prefix, "ansible-upload") if err != nil { fmt.Fprintf(out, scpEmptyError) return err @@ -68,7 +70,8 @@ func scpDownloadSession(opts []byte, rest string, in io.Reader, out io.Writer, c return errors.New("no scp source specified") } - d, err := ioutil.TempDir("", "packer-ansible-download") + prefix, _ := configfile.ConfigTmpDir() + d, err := ioutil.TempDir(prefix, "ansible-download") if err != nil { fmt.Fprintf(out, scpEmptyError) return err diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index c5724021b..f6aade45e 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -20,6 +20,7 @@ import ( commonhelper "github.com/hashicorp/packer/helper/common" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template/interpolate" ) @@ -238,7 +239,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { // Takes the inline scripts, concatenates them into a temporary file and // returns a string containing the location of said file. func extractScript(p *Provisioner) (string, error) { - temp, err := ioutil.TempFile(os.TempDir(), "packer-powershell-provisioner") + prefix, _ := configfile.ConfigTmpDir() + temp, err := ioutil.TempFile(prefix, "powershell-provisioner") if err != nil { return "", err } diff --git a/provisioner/windows-shell/provisioner.go b/provisioner/windows-shell/provisioner.go index f9b105121..bd02b98de 100644 --- a/provisioner/windows-shell/provisioner.go +++ b/provisioner/windows-shell/provisioner.go @@ -16,9 +16,11 @@ import ( "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template/interpolate" ) +//FIXME query remote host or use %SYSTEMROOT%, %TEMP% and more creative filename const DefaultRemotePath = "c:/Windows/Temp/script.bat" var retryableSleep = 2 * time.Second @@ -157,7 +159,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { // into a temporary file and returns a string containing the location // of said file. func extractScript(p *Provisioner) (string, error) { - temp, err := ioutil.TempFile(os.TempDir(), "packer-windows-shell-provisioner") + prefix, _ := configfile.ConfigTmpDir() + temp, err := ioutil.TempFile(prefix, "windows-shell-provisioner") if err != nil { log.Printf("Unable to create temporary file for inline scripts: %s", err) return "", err diff --git a/provisioner/windows-shell/provisioner_test.go b/provisioner/windows-shell/provisioner_test.go index 5f4149478..21595fa8d 100644 --- a/provisioner/windows-shell/provisioner_test.go +++ b/provisioner/windows-shell/provisioner_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { @@ -30,8 +31,9 @@ func TestProvisionerPrepare_extractScript(t *testing.T) { t.Fatalf("Should not be error: %s", err) } log.Printf("File: %s", file) - if strings.Index(file, os.TempDir()) != 0 { - t.Fatalf("Temp file should reside in %s. File location: %s", os.TempDir(), file) + tmpdir, _ := configfile.ConfigTmpDir() + if strings.Index(file, tmpdir) != 0 { + t.Fatalf("Temp file should reside in %s. File location: %s", tmpdir, file) } // File contents should contain 2 lines concatenated by newlines: foo\nbar diff --git a/template/parse.go b/template/parse.go index e0d06a2b4..0b5880d90 100644 --- a/template/parse.go +++ b/template/parse.go @@ -13,6 +13,7 @@ import ( "strings" "github.com/hashicorp/go-multierror" + "github.com/hashicorp/packer/packer/configfile" "github.com/mitchellh/mapstructure" ) @@ -328,7 +329,8 @@ func ParseFile(path string) (*Template, error) { var err error if path == "-" { // Create a temp file for stdin in case of errors - f, err = ioutil.TempFile(os.TempDir(), "packer") + prefix, _ := configfile.ConfigTmpDir() + f, err = ioutil.TempFile(prefix, "parse") if err != nil { return nil, err } From 54add38d1d1c6e283cd444b367ed8bd49a5f3699 Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Thu, 1 Nov 2018 07:41:29 -0400 Subject: [PATCH 09/12] revise documentation to match code --- .../source/docs/other/environment-variables.html.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/website/source/docs/other/environment-variables.html.md b/website/source/docs/other/environment-variables.html.md index 9e295de17..8a854781c 100644 --- a/website/source/docs/other/environment-variables.html.md +++ b/website/source/docs/other/environment-variables.html.md @@ -43,9 +43,9 @@ each can be found below: reasons, you can set this environment variable to `1`. - `PACKER_TMP_DIR` - The directory used for temporary files during marshalling. - If unset, appends 'packer' to environment variables TEMP, TMP, or (windows: - LOCALAPPDATA) before falling back to the value of `configDir()/tmp` which - resolves to (unix: `$HOME/.packer.d`, windows: `%APPDAT%/packer.d`). - This is not to be confused with the provisionee's temporary directory which - is often defined (hard-coded) as '/tmp' or '%SYSTEMROOT%/Temp' per each - provisioner module. + If unset, resolves to (unix: `$HOME/.packer.d`, windows: `%USERPROFILE%/packer.d`) + or finally appending 'packer' to the value of os.TempDir() typically + (unix: `/tmp`, windows: `%TEMP%` or `%LOCALAPPDATA%`). + This is not to be confused with the provision target's temporary directory which + is often hard-coded as `/tmp` or `%SYSTEMROOT%/Temp` per Provisioner module or + user-supplied template. From 852113ed076e2d14e5dca6815ea680da1e2896bb Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Thu, 1 Nov 2018 15:39:46 -0400 Subject: [PATCH 10/12] insert URL to GoLang API for os.TempDir() --- website/source/docs/other/environment-variables.html.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/source/docs/other/environment-variables.html.md b/website/source/docs/other/environment-variables.html.md index 8a854781c..8d042e78f 100644 --- a/website/source/docs/other/environment-variables.html.md +++ b/website/source/docs/other/environment-variables.html.md @@ -44,8 +44,8 @@ each can be found below: - `PACKER_TMP_DIR` - The directory used for temporary files during marshalling. If unset, resolves to (unix: `$HOME/.packer.d`, windows: `%USERPROFILE%/packer.d`) - or finally appending 'packer' to the value of os.TempDir() typically - (unix: `/tmp`, windows: `%TEMP%` or `%LOCALAPPDATA%`). + or finally appending 'packer' to the value of [os.TempDir()](https://golang.org/pkg/os/#TempDir) + typically (unix: `/tmp`, windows: `%TEMP%` or `%LOCALAPPDATA%`). This is not to be confused with the provision target's temporary directory which - is often hard-coded as `/tmp` or `%SYSTEMROOT%/Temp` per Provisioner module or + is often hard-coded as `/tmp` or `%SYSTEMROOT%/Temp` in Provisioner modules or user-supplied template. From e9b6adefeae8c65eb8aa47fef38cbf0aa424338c Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Thu, 1 Nov 2018 18:52:38 -0400 Subject: [PATCH 11/12] sync straggler to refactor. implement tests at packer/configfile instead of buried in docker. --- builder/docker/step_temp_dir_test.go | 41 ----------------- packer/configfile/configfile_test.go | 51 +++++++++++----------- provisioner/powershell/provisioner_test.go | 6 ++- 3 files changed, 30 insertions(+), 68 deletions(-) diff --git a/builder/docker/step_temp_dir_test.go b/builder/docker/step_temp_dir_test.go index b15154353..a573726ec 100644 --- a/builder/docker/step_temp_dir_test.go +++ b/builder/docker/step_temp_dir_test.go @@ -3,11 +3,9 @@ package docker import ( "context" "os" - "path/filepath" "testing" "github.com/hashicorp/packer/helper/multistep" - "github.com/hashicorp/packer/packer/configfile" ) func TestStepTempDir_impl(t *testing.T) { @@ -52,42 +50,3 @@ func testStepTempDir_impl(t *testing.T) string { func TestStepTempDir(t *testing.T) { testStepTempDir_impl(t) } - -func TestStepTempDir_notmpdir(t *testing.T) { - oldenv := os.Getenv(configfile.EnvPackerTmpDir) - defer os.Setenv(configfile.EnvPackerTmpDir, oldenv) - os.Setenv(configfile.EnvPackerTmpDir, "") - - dir1 := testStepTempDir_impl(t) - - cd, err := configfile.ConfigDir() - if err != nil { - t.Fatalf("bad ConfigDir") - } - td := filepath.Join(cd, "tmp") - os.Setenv(configfile.EnvPackerTmpDir, td) - - dir2 := testStepTempDir_impl(t) - - if filepath.Dir(dir1) != filepath.Dir(dir2) { - t.Fatalf("temp base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) - } -} - -func TestStepTempDir_packertmpdir(t *testing.T) { - oldenv := os.Getenv(configfile.EnvPackerTmpDir) - defer os.Setenv(configfile.EnvPackerTmpDir, oldenv) - os.Setenv(configfile.EnvPackerTmpDir, ".") - - dir1 := testStepTempDir_impl(t) - - abspath, err := filepath.Abs(".") - if err != nil { - t.Fatalf("bad absolute path") - } - dir2 := filepath.Join(abspath, "tmp") - - if filepath.Dir(dir1) != filepath.Dir(dir2) { - t.Fatalf("temp base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) - } -} diff --git a/packer/configfile/configfile_test.go b/packer/configfile/configfile_test.go index 137627040..912295705 100644 --- a/packer/configfile/configfile_test.go +++ b/packer/configfile/configfile_test.go @@ -1,65 +1,66 @@ package configfile import ( + "io/ioutil" "os" "path/filepath" - "io/ioutil" - "fmt" - "testing" + "testing" ) func testConfigTmpDir_impl(t *testing.T) string { var dir string + var err error prefix, _ := ConfigTmpDir() - if dir, err := ioutil.TempDir(prefix, ""); err == nil { + if dir, err = ioutil.TempDir(prefix, ""); err == nil { defer os.RemoveAll(dir) } else { - _ := fmt.Errorf("Error making directory: %s", err) + t.Fatalf("Error making directory: %s", err) } return dir } -func TestConfigTmpDir(t *testing.T) { - testConfigTmpDir_impl(t) -} - func TestConfigTmpDir_noenv_PackerTmpDir(t *testing.T) { - oldenv := os.Getenv(EnvPackerTmpDir) + var oldenv, cd, dir1, dir2 string + var err error + + oldenv = os.Getenv(EnvPackerTmpDir) defer os.Setenv(EnvPackerTmpDir, oldenv) os.Setenv(EnvPackerTmpDir, "") - dir1 := testConfigTmpDir_impl(t) + dir1 = testConfigTmpDir_impl(t) - cd, err := ConfigDir() - if err != nil { - t.Fatalf("bad ConfigDir") + if cd, err = ConfigDir(); err != nil { + t.Fatalf("Error during ConfigDir()") } - td := filepath.Join(cd, "tmp") - os.Setenv(EnvPackerTmpDir, td) - dir2 := testConfigTmpDir_impl(t) + os.Setenv(EnvPackerTmpDir, filepath.Join(cd, "tmp")) + + dir2 = testConfigTmpDir_impl(t) if filepath.Dir(dir1) != filepath.Dir(dir2) { - t.Fatalf("base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) + t.Fatalf("base directories do not match: '%s' vs '%s'", filepath.Dir(dir1), filepath.Dir(dir2)) } } func TestConfigTmpDir_PackerTmpDir(t *testing.T) { - oldenv := os.Getenv(EnvPackerTmpDir) + var oldenv, abspath, dir1, dir2 string + var err error + + oldenv = os.Getenv(EnvPackerTmpDir) defer os.Setenv(EnvPackerTmpDir, oldenv) os.Setenv(EnvPackerTmpDir, ".") - dir1 := testConfigTmpDir_impl(t) + dir1 = testConfigTmpDir_impl(t) - abspath, err := filepath.Abs(".") - if err != nil { - t.Fatalf("bad absolute path") + if abspath, err = filepath.Abs("."); err != nil { + t.Fatalf("Error during filepath.ABS()") } - dir2 := filepath.Join(abspath, "tmp") + + dir2 = filepath.Join(abspath, "tmp") if filepath.Dir(dir1) != filepath.Dir(dir2) { - t.Fatalf("base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) + t.Fatalf("Base directories do not match: '%s' vs '%s'", filepath.Dir(dir1), filepath.Dir(dir2)) } } diff --git a/provisioner/powershell/provisioner_test.go b/provisioner/powershell/provisioner_test.go index 23b4c372a..211624841 100644 --- a/provisioner/powershell/provisioner_test.go +++ b/provisioner/powershell/provisioner_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { @@ -34,8 +35,9 @@ func TestProvisionerPrepare_extractScript(t *testing.T) { t.Fatalf("Should not be error: %s", err) } t.Logf("File: %s", file) - if strings.Index(file, os.TempDir()) != 0 { - t.Fatalf("Temp file should reside in %s. File location: %s", os.TempDir(), file) + tmpdir, _ := configfile.ConfigTmpDir() + if strings.Index(file, tmpdir) != 0 { + t.Fatalf("Temp file should reside in %s. File location: %s", tmpdir, file) } // File contents should contain 2 lines concatenated by newlines: foo\nbar From 66001525d72de56a4cf9339b900c46f59bc6e91a Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Fri, 2 Nov 2018 01:42:38 -0400 Subject: [PATCH 12/12] update tests to honor configured temporary directory --- builder/docker/communicator_test.go | 25 +++++- builder/docker/config_test.go | 5 +- builder/hyperv/common/artifact_test.go | 4 +- builder/hyperv/common/output_config_test.go | 4 +- builder/hyperv/common/step_test.go | 22 +++++- builder/hyperv/iso/builder_test.go | 12 ++- builder/hyperv/vmcx/builder_test.go | 48 ++++++++---- builder/oracle/oci/config_test.go | 4 +- builder/parallels/common/artifact_test.go | 4 +- builder/parallels/common/driver_9_test.go | 8 +- .../parallels/common/output_config_test.go | 7 +- .../parallels/common/step_output_dir_test.go | 4 +- builder/qemu/builder_test.go | 4 +- builder/virtualbox/common/artifact_test.go | 4 +- .../virtualbox/common/output_config_test.go | 4 +- .../virtualbox/common/step_output_dir_test.go | 4 +- builder/vmware/common/artifact_test.go | 4 +- builder/vmware/common/step_output_dir_test.go | 4 +- builder/vmware/iso/builder_test.go | 4 +- builder/vmware/iso/step_create_vmx.go | 2 +- builder/vmware/iso/step_create_vmx_test.go | 19 ++++- builder/vmware/vmx/step_clone_vmx_test.go | 4 +- common/iso_config_test.go | 30 ++++---- common/step_create_floppy_test.go | 18 +++-- helper/builder/testing/testing.go | 9 ++- packer/cache_test.go | 5 +- .../checksum/post-processor_test.go | 9 ++- post-processor/vagrant/virtualbox_test.go | 7 +- provisioner/ansible-local/provisioner_test.go | 76 +++++++++++++++---- provisioner/ansible/provisioner_test.go | 75 +++++++++++++----- provisioner/chef-client/provisioner_test.go | 11 ++- provisioner/chef-solo/provisioner_test.go | 29 ++++--- provisioner/file/provisioner_test.go | 13 +++- .../puppet-masterless/provisioner_test.go | 24 ++++-- provisioner/puppet-server/provisioner_test.go | 16 ++-- .../salt-masterless/provisioner_test.go | 17 +++-- 36 files changed, 399 insertions(+), 140 deletions(-) diff --git a/builder/docker/communicator_test.go b/builder/docker/communicator_test.go index bf4cf76d7..5bd2a43dc 100644 --- a/builder/docker/communicator_test.go +++ b/builder/docker/communicator_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/provisioner/file" "github.com/hashicorp/packer/provisioner/shell" "github.com/hashicorp/packer/template" @@ -21,7 +22,13 @@ func TestCommunicator_impl(t *testing.T) { // TestUploadDownload verifies that basic upload / download functionality works func TestUploadDownload(t *testing.T) { ui := packer.TestUi(t) - cache := &packer.FileCache{CacheDir: os.TempDir()} + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "docker") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + cache := &packer.FileCache{CacheDir: td} tpl, err := template.Parse(strings.NewReader(dockerBuilderConfig)) if err != nil { @@ -105,7 +112,13 @@ func TestUploadDownload(t *testing.T) { // only intermittently. func TestLargeDownload(t *testing.T) { ui := packer.TestUi(t) - cache := &packer.FileCache{CacheDir: os.TempDir()} + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "docker") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + cache := &packer.FileCache{CacheDir: td} tpl, err := template.Parse(strings.NewReader(dockerLargeBuilderConfig)) if err != nil { @@ -210,7 +223,13 @@ func TestLargeDownload(t *testing.T) { // TestFixUploadOwner verifies that owner of uploaded files is the user the container is running as. func TestFixUploadOwner(t *testing.T) { ui := packer.TestUi(t) - cache := &packer.FileCache{CacheDir: os.TempDir()} + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "docker") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + cache := &packer.FileCache{CacheDir: td} tpl, err := template.Parse(strings.NewReader(testFixUploadOwnerTemplate)) if err != nil { diff --git a/builder/docker/config_test.go b/builder/docker/config_test.go index 442c6ffe2..9bb04f7a1 100644 --- a/builder/docker/config_test.go +++ b/builder/docker/config_test.go @@ -4,6 +4,8 @@ import ( "io/ioutil" "os" "testing" + + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { @@ -44,7 +46,8 @@ func testConfigOk(t *testing.T, warns []string, err error) { } func TestConfigPrepare_exportPath(t *testing.T) { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "docker") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/hyperv/common/artifact_test.go b/builder/hyperv/common/artifact_test.go index 023d2dd44..6a46075e3 100644 --- a/builder/hyperv/common/artifact_test.go +++ b/builder/hyperv/common/artifact_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func TestArtifact_impl(t *testing.T) { @@ -14,7 +15,8 @@ func TestArtifact_impl(t *testing.T) { } func TestNewArtifact(t *testing.T) { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/hyperv/common/output_config_test.go b/builder/hyperv/common/output_config_test.go index 3a87dc18f..2afacbec9 100644 --- a/builder/hyperv/common/output_config_test.go +++ b/builder/hyperv/common/output_config_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/packer/configfile" ) func TestOutputConfigPrepare(t *testing.T) { @@ -26,7 +27,8 @@ func TestOutputConfigPrepare(t *testing.T) { } func TestOutputConfigPrepare_exists(t *testing.T) { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/hyperv/common/step_test.go b/builder/hyperv/common/step_test.go index e5f9f3998..263d74be1 100644 --- a/builder/hyperv/common/step_test.go +++ b/builder/hyperv/common/step_test.go @@ -2,13 +2,14 @@ package common import ( "bytes" - "os" + "io/ioutil" "path/filepath" "testing" "github.com/hashicorp/packer/common/uuid" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testState(t *testing.T) multistep.StateBag { @@ -21,8 +22,23 @@ func testState(t *testing.T) multistep.StateBag { return state } -// Generates an absolute path to a directory under OS temp with a name +// Generates an absolute path to a directory with a name // beginning with prefix and a UUID appended to the end func genTestDirPath(prefix string) string { - return filepath.Join(os.TempDir(), prefix+"-"+uuid.TimeOrderedUUID()) + var suffix string + + if prefix == "" { + suffix = uuid.TimeOrderedUUID() + } else { + suffix = prefix+"-"+uuid.TimeOrderedUUID() + } + + tdprefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(tdprefix, "hyperv") + if err != nil { + // use CWD as last-ditch + td, err = filepath.Abs(".") + } + + return filepath.Join(td, suffix) } diff --git a/builder/hyperv/iso/builder_test.go b/builder/hyperv/iso/builder_test.go index 0369c0df7..fbc6a84c8 100644 --- a/builder/hyperv/iso/builder_test.go +++ b/builder/hyperv/iso/builder_test.go @@ -5,15 +5,15 @@ package iso import ( "context" "fmt" + "io/ioutil" "reflect" "strconv" "testing" - "os" - hypervcommon "github.com/hashicorp/packer/builder/hyperv/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { @@ -607,7 +607,13 @@ func TestUserVariablesInBootCommand(t *testing.T) { } ui := packer.TestUi(t) - cache := &packer.FileCache{CacheDir: os.TempDir()} + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-iso") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + cache := &packer.FileCache{CacheDir: td} hook := &packer.MockHook{} driver := &hypervcommon.DriverMock{} diff --git a/builder/hyperv/vmcx/builder_test.go b/builder/hyperv/vmcx/builder_test.go index 23d64458c..e3d5691d9 100644 --- a/builder/hyperv/vmcx/builder_test.go +++ b/builder/hyperv/vmcx/builder_test.go @@ -12,6 +12,7 @@ import ( hypervcommon "github.com/hashicorp/packer/builder/hyperv/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { @@ -42,7 +43,8 @@ func TestBuilderPrepare_Defaults(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -67,7 +69,8 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -104,7 +107,8 @@ func TestBuilderPrepare_ExportedMachinePathDoesNotExist(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -128,7 +132,8 @@ func TestBuilderPrepare_ExportedMachinePathExists(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -175,7 +180,8 @@ func TestBuilderPrepare_ISOChecksum(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -213,7 +219,8 @@ func TestBuilderPrepare_ISOChecksumType(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -277,7 +284,8 @@ func TestBuilderPrepare_ISOUrl(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -356,7 +364,8 @@ func TestBuilderPrepare_FloppyFiles(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -398,7 +407,8 @@ func TestBuilderPrepare_InvalidFloppies(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -423,7 +433,8 @@ func TestBuilderPrepare_CommConfig(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -460,7 +471,8 @@ func TestBuilderPrepare_CommConfig(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { t.Fatalf("err: %s", err) } @@ -498,10 +510,12 @@ func TestUserVariablesInBootCommand(t *testing.T) { config := testConfig() //Create vmcx folder - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "hyperv-vmcx") if err != nil { - t.Fatalf("err: %s", err) + t.Fatalf("Mkdir failed: %s", err) } + defer os.RemoveAll(td) config["clone_from_vmcx_path"] = td @@ -517,7 +531,13 @@ func TestUserVariablesInBootCommand(t *testing.T) { } ui := packer.TestUi(t) - cache := &packer.FileCache{CacheDir: os.TempDir()} + prefix, _ = configfile.ConfigTmpDir() + td, err = ioutil.TempDir(prefix, "hyperv-vmcx") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + cache := &packer.FileCache{CacheDir: td} hook := &packer.MockHook{} driver := &hypervcommon.DriverMock{} diff --git a/builder/oracle/oci/config_test.go b/builder/oracle/oci/config_test.go index a5b67d384..783e8dcc0 100644 --- a/builder/oracle/oci/config_test.go +++ b/builder/oracle/oci/config_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/go-ini/ini" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig(accessConfFile *os.File) map[string]interface{} { @@ -53,7 +54,8 @@ func TestConfig(t *testing.T) { // Temporarily set $HOME to temp directory to bypass default // access config loading. - tmpHome, err := ioutil.TempDir("", "packer_config_test") + prefix, _ := configfile.ConfigTmpDir() + tmpHome, err := ioutil.TempDir(prefix, "oracle") if err != nil { t.Fatalf("Unexpected error when creating temporary directory: %+v", err) } diff --git a/builder/parallels/common/artifact_test.go b/builder/parallels/common/artifact_test.go index cb2da6d39..a2762a6e0 100644 --- a/builder/parallels/common/artifact_test.go +++ b/builder/parallels/common/artifact_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func TestArtifact_impl(t *testing.T) { @@ -14,7 +15,8 @@ func TestArtifact_impl(t *testing.T) { } func TestNewArtifact(t *testing.T) { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "parallels") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/parallels/common/driver_9_test.go b/builder/parallels/common/driver_9_test.go index bc9d3eb42..3c09aaeeb 100644 --- a/builder/parallels/common/driver_9_test.go +++ b/builder/parallels/common/driver_9_test.go @@ -4,6 +4,8 @@ import ( "io/ioutil" "os" "testing" + + "github.com/hashicorp/packer/packer/configfile" ) func TestParallels9Driver_impl(t *testing.T) { @@ -11,7 +13,8 @@ func TestParallels9Driver_impl(t *testing.T) { } func TestIPAddress(t *testing.T) { - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "parallels") if err != nil { t.Fatalf("err: %s", err) } @@ -61,7 +64,8 @@ func TestIPAddress(t *testing.T) { } func TestXMLParseConfig(t *testing.T) { - td, err := ioutil.TempDir("", "configpvs") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "parallels") if err != nil { t.Fatalf("Error creating temp dir: %s", err) } diff --git a/builder/parallels/common/output_config_test.go b/builder/parallels/common/output_config_test.go index 6feb54a88..55358070b 100644 --- a/builder/parallels/common/output_config_test.go +++ b/builder/parallels/common/output_config_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/packer/configfile" ) func TestOutputConfigPrepare(t *testing.T) { @@ -28,7 +29,8 @@ func TestOutputConfigPrepare(t *testing.T) { } func TestOutputConfigPrepare_exists(t *testing.T) { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "parallels") if err != nil { t.Fatalf("err: %s", err) } @@ -48,7 +50,8 @@ func TestOutputConfigPrepare_exists(t *testing.T) { } func TestOutputConfigPrepare_forceExists(t *testing.T) { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "parallels") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/parallels/common/step_output_dir_test.go b/builder/parallels/common/step_output_dir_test.go index 53c1d885c..91b3af052 100644 --- a/builder/parallels/common/step_output_dir_test.go +++ b/builder/parallels/common/step_output_dir_test.go @@ -7,10 +7,12 @@ import ( "testing" "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer/configfile" ) func testStepOutputDir(t *testing.T) *StepOutputDir { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "parallels") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/qemu/builder_test.go b/builder/qemu/builder_test.go index d0d403666..854a3071a 100644 --- a/builder/qemu/builder_test.go +++ b/builder/qemu/builder_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) var testPem = ` @@ -336,7 +337,8 @@ func TestBuilderPrepare_OutputDir(t *testing.T) { config := testConfig() // Test with existing dir - dir, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + dir, err := ioutil.TempDir(prefix, "qemu") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/virtualbox/common/artifact_test.go b/builder/virtualbox/common/artifact_test.go index 023d2dd44..c856be10c 100644 --- a/builder/virtualbox/common/artifact_test.go +++ b/builder/virtualbox/common/artifact_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func TestArtifact_impl(t *testing.T) { @@ -14,7 +15,8 @@ func TestArtifact_impl(t *testing.T) { } func TestNewArtifact(t *testing.T) { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "virtualbox") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/virtualbox/common/output_config_test.go b/builder/virtualbox/common/output_config_test.go index 3a87dc18f..c0690c850 100644 --- a/builder/virtualbox/common/output_config_test.go +++ b/builder/virtualbox/common/output_config_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/packer/configfile" ) func TestOutputConfigPrepare(t *testing.T) { @@ -26,7 +27,8 @@ func TestOutputConfigPrepare(t *testing.T) { } func TestOutputConfigPrepare_exists(t *testing.T) { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "virtualbox") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/virtualbox/common/step_output_dir_test.go b/builder/virtualbox/common/step_output_dir_test.go index 766154ca9..dd99c6d10 100644 --- a/builder/virtualbox/common/step_output_dir_test.go +++ b/builder/virtualbox/common/step_output_dir_test.go @@ -7,10 +7,12 @@ import ( "testing" "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer/configfile" ) func testStepOutputDir(t *testing.T) *StepOutputDir { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "virtualbox") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/vmware/common/artifact_test.go b/builder/vmware/common/artifact_test.go index 53b8364f1..531d92bbe 100644 --- a/builder/vmware/common/artifact_test.go +++ b/builder/vmware/common/artifact_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func TestLocalArtifact_impl(t *testing.T) { @@ -14,7 +15,8 @@ func TestLocalArtifact_impl(t *testing.T) { } func TestNewLocalArtifact(t *testing.T) { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "vmware") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/vmware/common/step_output_dir_test.go b/builder/vmware/common/step_output_dir_test.go index 3726a9354..860586817 100644 --- a/builder/vmware/common/step_output_dir_test.go +++ b/builder/vmware/common/step_output_dir_test.go @@ -7,10 +7,12 @@ import ( "testing" "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer/configfile" ) func testOutputDir(t *testing.T) *LocalOutputDir { - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "vmware") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/vmware/iso/builder_test.go b/builder/vmware/iso/builder_test.go index 59e18907d..2ee284d67 100644 --- a/builder/vmware/iso/builder_test.go +++ b/builder/vmware/iso/builder_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { @@ -274,7 +275,8 @@ func TestBuilderPrepare_OutputDir(t *testing.T) { config := testConfig() // Test with existing dir - dir, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + dir, err := ioutil.TempDir(prefix, "vmw-iso") if err != nil { t.Fatalf("err: %s", err) } diff --git a/builder/vmware/iso/step_create_vmx.go b/builder/vmware/iso/step_create_vmx.go index 529af33a7..60bd83add 100644 --- a/builder/vmware/iso/step_create_vmx.go +++ b/builder/vmware/iso/step_create_vmx.go @@ -616,7 +616,7 @@ func (s *stepCreateVMX) Run(_ context.Context, state multistep.StateBag) multist // For remote builds, we just put the VMX in a temporary // directory since it just gets uploaded anyways. prefix, _ := configfile.ConfigTmpDir() - vmxDir, err = ioutil.TempDir(prefix, "vmware") + vmxDir, err = ioutil.TempDir(prefix, "vmw-iso") if err != nil { err := fmt.Errorf("Error preparing VMX template: %s", err) state.Put("error", err) diff --git a/builder/vmware/iso/step_create_vmx_test.go b/builder/vmware/iso/step_create_vmx_test.go index 26605a1f5..f3fee11ec 100644 --- a/builder/vmware/iso/step_create_vmx_test.go +++ b/builder/vmware/iso/step_create_vmx_test.go @@ -15,6 +15,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/provisioner/shell" "github.com/hashicorp/packer/template" ) @@ -40,13 +41,19 @@ const vmxTestTemplate string = `{"builders":[{%s}],"provisioners":[{%s}]}` func tmpnam(prefix string) string { var path string - var err error const length = 16 - dir := os.TempDir() + tdprefix, _ := configfile.ConfigTmpDir() + dir, err := ioutil.TempDir(tdprefix, "vmw-iso") + if err != nil { + // use CWD as last-ditch + dir, err = filepath.Abs(".") + } + max := int(math.Pow(2, float64(length))) + // FIXME use ioutil.TempFile() or at least mimic implementation, this could loop forever n, err := rand.Intn(max), nil for path = filepath.Join(dir, prefix+strconv.Itoa(n)); err == nil; _, err = os.Stat(path) { n = rand.Intn(max) @@ -173,7 +180,13 @@ func setupVMwareBuild(t *testing.T, builderConfig map[string]string, provisioner } // and then finally build it - cache := &packer.FileCache{CacheDir: os.TempDir()} + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "vmw-iso") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + cache := &packer.FileCache{CacheDir: td} artifacts, err := b.Run(ui, cache) if err != nil { t.Fatalf("Failed to build artifact: %s", err) diff --git a/builder/vmware/vmx/step_clone_vmx_test.go b/builder/vmware/vmx/step_clone_vmx_test.go index d781df687..19a209f3c 100644 --- a/builder/vmware/vmx/step_clone_vmx_test.go +++ b/builder/vmware/vmx/step_clone_vmx_test.go @@ -10,6 +10,7 @@ import ( vmwcommon "github.com/hashicorp/packer/builder/vmware/common" "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer/configfile" "github.com/stretchr/testify/assert" ) @@ -26,7 +27,8 @@ func TestStepCloneVMX_impl(t *testing.T) { func TestStepCloneVMX(t *testing.T) { // Setup some state - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "vmw-vmx") if err != nil { t.Fatalf("err: %s", err) } diff --git a/common/iso_config_test.go b/common/iso_config_test.go index 0a054f700..2a76feeed 100644 --- a/common/iso_config_test.go +++ b/common/iso_config_test.go @@ -11,6 +11,8 @@ import ( "reflect" "runtime" "testing" + + "github.com/hashicorp/packer/packer/configfile" ) func testISOConfig() ISOConfig { @@ -89,13 +91,15 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { t.Fatalf("bad: %#v, %#v", warns, err) } + tdprefix, _ := configfile.ConfigTmpDir() + // Test good - ISOChecksumURL BSD style i = testISOConfig() i.ISOChecksum = "" - cs_file, _ := ioutil.TempFile("", "packer-test-") + cs_file, _ := ioutil.TempFile(tdprefix, "packer-test-") defer os.Remove(cs_file.Name()) defer cs_file.Close() - ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style), 0666) + ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style), 0600) filePrefix := "file://" if runtime.GOOS == "windows" { filePrefix += "/" @@ -116,10 +120,10 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { // Test good - ISOChecksumURL GNU style i = testISOConfig() i.ISOChecksum = "" - cs_file, _ = ioutil.TempFile("", "packer-test-") + cs_file, _ = ioutil.TempFile(tdprefix, "packer-test-") defer os.Remove(cs_file.Name()) defer cs_file.Close() - ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666) + ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0600) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) warns, err = i.Prepare(nil) if len(warns) > 0 { @@ -136,10 +140,10 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { // Test good - ISOChecksumURL BSD style no newline i = testISOConfig() i.ISOChecksum = "" - cs_file, _ = ioutil.TempFile("", "packer-test-") + cs_file, _ = ioutil.TempFile(tdprefix, "packer-test-") defer os.Remove(cs_file.Name()) defer cs_file.Close() - ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_no_newline), 0666) + ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_no_newline), 0600) i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) warns, err = i.Prepare(nil) if len(warns) > 0 { @@ -157,11 +161,11 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { i = testISOConfig() i.ISOChecksum = "" - cs_dir, _ := ioutil.TempDir("", "packer-testdir-") + cs_dir, _ := ioutil.TempDir(tdprefix, "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) + ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_subdir), 0600) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso") warns, err = i.Prepare(nil) @@ -179,10 +183,10 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { // Test good - ISOChecksumURL GNU style no newline i = testISOConfig() i.ISOChecksum = "" - cs_file, _ = ioutil.TempFile("", "packer-test-") + cs_file, _ = ioutil.TempFile(tdprefix, "packer-test-") defer os.Remove(cs_file.Name()) defer cs_file.Close() - ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_no_newline), 0666) + ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_no_newline), 0600) i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) warns, err = i.Prepare(nil) if len(warns) > 0 { @@ -201,10 +205,10 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { i.ISOChecksum = "" i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso?stuff=boo" - cs_file, _ = ioutil.TempFile("", "packer-test-") + cs_file, _ = ioutil.TempFile(tdprefix, "packer-test-") defer os.Remove(cs_file.Name()) defer cs_file.Close() - ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666) + ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0600) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) warns, err = i.Prepare(nil) if len(warns) > 0 { @@ -225,7 +229,7 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { 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) + ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_subdir), 0600) i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso") warns, err = i.Prepare(nil) diff --git a/common/step_create_floppy_test.go b/common/step_create_floppy_test.go index 4c43a6839..86e9ae996 100644 --- a/common/step_create_floppy_test.go +++ b/common/step_create_floppy_test.go @@ -15,6 +15,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) const TestFixtures = "test-fixtures" @@ -57,9 +58,10 @@ func TestStepCreateFloppy(t *testing.T) { state := testStepCreateFloppyState(t) step := new(StepCreateFloppy) - dir, err := ioutil.TempDir("", "packer") + tdprefix, _ := configfile.ConfigTmpDir() + dir, err := ioutil.TempDir(tdprefix, "common") if err != nil { - t.Fatalf("err: %s", err) + t.Fatalf("Mkdir failed: %s", err) } defer os.RemoveAll(dir) @@ -73,7 +75,7 @@ func TestStepCreateFloppy(t *testing.T) { for i := 0; i < expected; i++ { files[i] = path.Join(dir, prefix+strconv.Itoa(i)+ext) - _, err := os.Create(files[i]) + _, err = os.Create(files[i]) if err != nil { t.Fatalf("err: %s", err) } @@ -120,9 +122,10 @@ func xxxTestStepCreateFloppy_missing(t *testing.T) { state := testStepCreateFloppyState(t) step := new(StepCreateFloppy) - dir, err := ioutil.TempDir("", "packer") + tdprefix, _ := configfile.ConfigTmpDir() + dir, err := ioutil.TempDir(tdprefix, "common") if err != nil { - t.Fatalf("err: %s", err) + t.Fatalf("Mkdir failed: %s", err) } defer os.RemoveAll(dir) @@ -165,9 +168,10 @@ func xxxTestStepCreateFloppy_notfound(t *testing.T) { state := testStepCreateFloppyState(t) step := new(StepCreateFloppy) - dir, err := ioutil.TempDir("", "packer") + tdprefix, _ := configfile.ConfigTmpDir() + dir, err := ioutil.TempDir(tdprefix, "common") if err != nil { - t.Fatalf("err: %s", err) + t.Fatalf("Mkdir failed: %s", err) } defer os.RemoveAll(dir) diff --git a/helper/builder/testing/testing.go b/helper/builder/testing/testing.go index 7e862af9d..5d4ef1ddc 100644 --- a/helper/builder/testing/testing.go +++ b/helper/builder/testing/testing.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template" ) @@ -145,7 +146,13 @@ func Test(t TestT, c TestCase) { // Run it! We use a temporary directory for caching and discard // any UI output. We discard since it shows up in logs anyways. log.Printf("[DEBUG] Running 'test' build") - cache := &packer.FileCache{CacheDir: os.TempDir()} + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "packer") + if err != nil { + t.Fatal("Mkdir failed: %s", err) + } + + cache := &packer.FileCache{CacheDir: td} ui := &packer.BasicUi{ Reader: os.Stdin, Writer: ioutil.Discard, diff --git a/packer/cache_test.go b/packer/cache_test.go index e4463e61d..fc7b24c8d 100644 --- a/packer/cache_test.go +++ b/packer/cache_test.go @@ -5,6 +5,8 @@ import ( "os" "strings" "testing" + + "github.com/hashicorp/packer/packer/configfile" ) type TestCache struct{} @@ -30,7 +32,8 @@ func TestFileCache_Implements(t *testing.T) { } func TestFileCache(t *testing.T) { - cacheDir, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + cacheDir, err := ioutil.TempDir(prefix, "packer") if err != nil { t.Fatalf("error creating temporary dir: %s", err) } diff --git a/post-processor/checksum/post-processor_test.go b/post-processor/checksum/post-processor_test.go index 6b4420d39..86e2bfd70 100644 --- a/post-processor/checksum/post-processor_test.go +++ b/post-processor/checksum/post-processor_test.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/packer/builder/file" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template" ) @@ -46,7 +47,13 @@ func TestChecksumSHA1(t *testing.T) { func setup(t *testing.T) (packer.Ui, packer.Artifact, error) { // Create fake UI and Cache ui := packer.TestUi(t) - cache := &packer.FileCache{CacheDir: os.TempDir()} + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "checksum") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + cache := &packer.FileCache{CacheDir: td} // Create config for file builder const fileConfig = `{"builders":[{"type":"file","target":"package.txt","content":"Hello world!"}]}` diff --git a/post-processor/vagrant/virtualbox_test.go b/post-processor/vagrant/virtualbox_test.go index 5082c9388..e61049e5c 100644 --- a/post-processor/vagrant/virtualbox_test.go +++ b/post-processor/vagrant/virtualbox_test.go @@ -6,6 +6,7 @@ import ( "path/filepath" "testing" + "github.com/hashicorp/packer/packer/configfile" "github.com/stretchr/testify/assert" ) @@ -14,8 +15,11 @@ func TestVBoxProvider_impl(t *testing.T) { } func TestDecomressOVA(t *testing.T) { - td, err := ioutil.TempDir("", "pp-vagrant-virtualbox") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "pp-vagrant-virtualbox") assert.NoError(t, err) + defer os.RemoveAll(td) + fixture := "../../common/test-fixtures/decompress-tar/outside_parent.tar" err = DecompressOva(td, fixture) assert.NoError(t, err) @@ -23,5 +27,4 @@ func TestDecomressOVA(t *testing.T) { assert.Error(t, err) _, err = os.Stat(filepath.Join(td, "demo.poc")) assert.NoError(t, err) - os.RemoveAll(td) } diff --git a/provisioner/ansible-local/provisioner_test.go b/provisioner/ansible-local/provisioner_test.go index 741bba235..6da910bc8 100644 --- a/provisioner/ansible-local/provisioner_test.go +++ b/provisioner/ansible-local/provisioner_test.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/packer/builder/docker" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/provisioner/file" "github.com/hashicorp/packer/template" ) @@ -28,7 +29,13 @@ func TestProvisionerPrepare_Defaults(t *testing.T) { var p Provisioner config := testConfig() - playbook_file, err := ioutil.TempFile("", "playbook") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -61,7 +68,13 @@ func TestProvisionerPrepare_PlaybookFile(t *testing.T) { t.Fatal("should have error") } - playbook_file, err := ioutil.TempFile("", "playbook") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -90,7 +103,13 @@ func TestProvisionerPrepare_PlaybookFiles(t *testing.T) { t.Fatal("should have error") } - playbook_file, err := ioutil.TempFile("", "playbook") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -123,7 +142,8 @@ func TestProvisionerProvision_PlaybookFiles(t *testing.T) { var p Provisioner config := testConfig() - playbooks := createTempFiles("", 3) + prefix, _ := configfile.ConfigTmpDir() + playbooks := createTempFiles(prefix, 3) defer removeFiles(playbooks...) config["playbook_files"] = playbooks @@ -145,7 +165,8 @@ func TestProvisionerProvision_PlaybookFilesWithPlaybookDir(t *testing.T) { var p Provisioner config := testConfig() - playbook_dir, err := ioutil.TempDir("", "") + prefix, _ := configfile.ConfigTmpDir() + playbook_dir, err := ioutil.TempDir(prefix, "playbook") if err != nil { t.Fatalf("Failed to create playbook_dir: %s", err) } @@ -190,7 +211,13 @@ func TestProvisionerPrepare_InventoryFile(t *testing.T) { t.Fatal("should have error") } - playbook_file, err := ioutil.TempFile("", "playbook") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -202,7 +229,7 @@ func TestProvisionerPrepare_InventoryFile(t *testing.T) { t.Fatalf("err: %s", err) } - inventory_file, err := ioutil.TempFile("", "inventory") + inventory_file, err := ioutil.TempFile(td, "inventory") if err != nil { t.Fatalf("err: %s", err) } @@ -230,7 +257,13 @@ func TestProvisionerPrepare_Dirs(t *testing.T) { t.Fatal("should have error") } - playbook_file, err := ioutil.TempFile("", "playbook") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -248,7 +281,8 @@ func TestProvisionerPrepare_Dirs(t *testing.T) { t.Fatal("should error if playbook paths is not a dir") } - config["playbook_paths"] = []string{os.TempDir()} +// XXX was os.TempDir() + config["playbook_paths"] = []string{td} err = p.Prepare(config) if err != nil { t.Fatalf("err: %s", err) @@ -259,8 +293,8 @@ func TestProvisionerPrepare_Dirs(t *testing.T) { if err == nil { t.Fatal("should error if role paths is not a dir") } - - config["role_paths"] = []string{os.TempDir()} +// XXX was os.TempDir() + config["role_paths"] = []string{td} err = p.Prepare(config) if err != nil { t.Fatalf("err: %s", err) @@ -272,7 +306,7 @@ func TestProvisionerPrepare_Dirs(t *testing.T) { t.Fatalf("should error if group_vars path is not a dir") } - config["group_vars"] = os.TempDir() + config["group_vars"] = td err = p.Prepare(config) if err != nil { t.Fatalf("err: %s", err) @@ -284,7 +318,7 @@ func TestProvisionerPrepare_Dirs(t *testing.T) { t.Fatalf("should error if host_vars path is not a dir") } - config["host_vars"] = os.TempDir() + config["host_vars"] = td err = p.Prepare(config) if err != nil { t.Fatalf("err: %s", err) @@ -295,7 +329,13 @@ func TestProvisionerPrepare_CleanStagingDir(t *testing.T) { var p Provisioner config := testConfig() - playbook_file, err := ioutil.TempFile("", "playbook") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -328,7 +368,13 @@ func testProvisionerProvisionDockerWithPlaybookFiles(t *testing.T, templateStrin } ui := packer.TestUi(t) - cache := &packer.FileCache{CacheDir: os.TempDir()} + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + cache := &packer.FileCache{CacheDir: td} tpl, err := template.Parse(strings.NewReader(templateString)) if err != nil { diff --git a/provisioner/ansible/provisioner_test.go b/provisioner/ansible/provisioner_test.go index 3386b74cb..6ce875067 100644 --- a/provisioner/ansible/provisioner_test.go +++ b/provisioner/ansible/provisioner_test.go @@ -14,6 +14,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) // Be sure to remove the Ansible stub file in each test with: @@ -53,19 +54,25 @@ func TestProvisionerPrepare_Defaults(t *testing.T) { t.Fatalf("should have error") } - hostkey_file, err := ioutil.TempFile("", "hostkey") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + hostkey_file, err := ioutil.TempFile(td, "hostkey") if err != nil { t.Fatalf("err: %s", err) } defer os.Remove(hostkey_file.Name()) - publickey_file, err := ioutil.TempFile("", "publickey") + publickey_file, err := ioutil.TempFile(td, "publickey") if err != nil { t.Fatalf("err: %s", err) } defer os.Remove(publickey_file.Name()) - playbook_file, err := ioutil.TempFile("", "playbook") + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -95,13 +102,19 @@ func TestProvisionerPrepare_PlaybookFile(t *testing.T) { config := testConfig(t) defer os.Remove(config["command"].(string)) - hostkey_file, err := ioutil.TempFile("", "hostkey") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + hostkey_file, err := ioutil.TempFile(td, "hostkey") if err != nil { t.Fatalf("err: %s", err) } defer os.Remove(hostkey_file.Name()) - publickey_file, err := ioutil.TempFile("", "publickey") + publickey_file, err := ioutil.TempFile(td, "publickey") if err != nil { t.Fatalf("err: %s", err) } @@ -115,7 +128,7 @@ func TestProvisionerPrepare_PlaybookFile(t *testing.T) { t.Fatal("should have error") } - playbook_file, err := ioutil.TempFile("", "playbook") + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -133,13 +146,19 @@ func TestProvisionerPrepare_HostKeyFile(t *testing.T) { config := testConfig(t) defer os.Remove(config["command"].(string)) - publickey_file, err := ioutil.TempFile("", "publickey") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + publickey_file, err := ioutil.TempFile(td, "publickey") if err != nil { t.Fatalf("err: %s", err) } defer os.Remove(publickey_file.Name()) - playbook_file, err := ioutil.TempFile("", "playbook") + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -160,7 +179,7 @@ func TestProvisionerPrepare_HostKeyFile(t *testing.T) { t.Fatal("should error if ssh_host_key_file does not exist") } - hostkey_file, err := ioutil.TempFile("", "hostkey") + hostkey_file, err := ioutil.TempFile(td, "hostkey") if err != nil { t.Fatalf("err: %s", err) } @@ -178,13 +197,19 @@ func TestProvisionerPrepare_AuthorizedKeyFile(t *testing.T) { config := testConfig(t) defer os.Remove(config["command"].(string)) - hostkey_file, err := ioutil.TempFile("", "hostkey") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + hostkey_file, err := ioutil.TempFile(td, "hostkey") if err != nil { t.Fatalf("err: %s", err) } defer os.Remove(hostkey_file.Name()) - playbook_file, err := ioutil.TempFile("", "playbook") + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -205,7 +230,7 @@ func TestProvisionerPrepare_AuthorizedKeyFile(t *testing.T) { t.Errorf("should error if ssh_authorized_key_file does not exist") } - publickey_file, err := ioutil.TempFile("", "publickey") + publickey_file, err := ioutil.TempFile(td, "publickey") if err != nil { t.Fatalf("err: %s", err) } @@ -223,19 +248,25 @@ func TestProvisionerPrepare_LocalPort(t *testing.T) { config := testConfig(t) defer os.Remove(config["command"].(string)) - hostkey_file, err := ioutil.TempFile("", "hostkey") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + hostkey_file, err := ioutil.TempFile(td, "hostkey") if err != nil { t.Fatalf("err: %s", err) } defer os.Remove(hostkey_file.Name()) - publickey_file, err := ioutil.TempFile("", "publickey") + publickey_file, err := ioutil.TempFile(td, "publickey") if err != nil { t.Fatalf("err: %s", err) } defer os.Remove(publickey_file.Name()) - playbook_file, err := ioutil.TempFile("", "playbook") + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -263,19 +294,25 @@ func TestProvisionerPrepare_InventoryDirectory(t *testing.T) { config := testConfig(t) defer os.Remove(config["command"].(string)) - hostkey_file, err := ioutil.TempFile("", "hostkey") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "ansible") + if err != nil { + t.Fatalf("Mkdir failed: %s", err) + } + + hostkey_file, err := ioutil.TempFile(td, "hostkey") if err != nil { t.Fatalf("err: %s", err) } defer os.Remove(hostkey_file.Name()) - publickey_file, err := ioutil.TempFile("", "publickey") + publickey_file, err := ioutil.TempFile(td, "publickey") if err != nil { t.Fatalf("err: %s", err) } defer os.Remove(publickey_file.Name()) - playbook_file, err := ioutil.TempFile("", "playbook") + playbook_file, err := ioutil.TempFile(td, "playbook") if err != nil { t.Fatalf("err: %s", err) } @@ -291,7 +328,7 @@ func TestProvisionerPrepare_InventoryDirectory(t *testing.T) { t.Errorf("should error if inventory_directory does not exist") } - inventoryDirectory, err := ioutil.TempDir("", "some_inventory_dir") + inventoryDirectory, err := ioutil.TempDir(prefix, "ansible_inventory") if err != nil { t.Fatalf("err: %s", err) } diff --git a/provisioner/chef-client/provisioner_test.go b/provisioner/chef-client/provisioner_test.go index acb674b53..00d91e812 100644 --- a/provisioner/chef-client/provisioner_test.go +++ b/provisioner/chef-client/provisioner_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { @@ -53,7 +54,8 @@ func TestProvisionerPrepare_configTemplate(t *testing.T) { } // Test with a file - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "chefclient") if err != nil { t.Fatalf("err: %s", err) } @@ -68,7 +70,7 @@ func TestProvisionerPrepare_configTemplate(t *testing.T) { } // Test with a directory - td, err := ioutil.TempDir("", "packer") + td, err := ioutil.TempDir(prefix, "chefclient") if err != nil { t.Fatalf("err: %s", err) } @@ -152,7 +154,8 @@ func TestProvisionerPrepare_encryptedDataBagSecretPath(t *testing.T) { } // Test with a file - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "chefclient") if err != nil { t.Fatalf("err: %s", err) } @@ -167,7 +170,7 @@ func TestProvisionerPrepare_encryptedDataBagSecretPath(t *testing.T) { } // Test with a directory - td, err := ioutil.TempDir("", "packer") + td, err := ioutil.TempDir(prefix, "chefclient") if err != nil { t.Fatalf("err: %s", err) } diff --git a/provisioner/chef-solo/provisioner_test.go b/provisioner/chef-solo/provisioner_test.go index 01d237330..24ea08f11 100644 --- a/provisioner/chef-solo/provisioner_test.go +++ b/provisioner/chef-solo/provisioner_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { @@ -49,7 +50,8 @@ func TestProvisionerPrepare_configTemplate(t *testing.T) { } // Test with a file - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "chefsolo") if err != nil { t.Fatalf("err: %s", err) } @@ -64,7 +66,7 @@ func TestProvisionerPrepare_configTemplate(t *testing.T) { } // Test with a directory - td, err := ioutil.TempDir("", "packer") + td, err := ioutil.TempDir(prefix, "chefsolo") if err != nil { t.Fatalf("err: %s", err) } @@ -82,22 +84,23 @@ func TestProvisionerPrepare_configTemplate(t *testing.T) { func TestProvisionerPrepare_cookbookPaths(t *testing.T) { var p Provisioner - path1, err := ioutil.TempDir("", "cookbooks_one") + prefix, _ := configfile.ConfigTmpDir() + path1, err := ioutil.TempDir(prefix, "chefsolo-cookbooks_one") if err != nil { t.Fatalf("err: %s", err) } - path2, err := ioutil.TempDir("", "cookbooks_two") + path2, err := ioutil.TempDir(prefix, "chefsolo-cookbooks_two") if err != nil { t.Fatalf("err: %s", err) } - rolesPath, err := ioutil.TempDir("", "roles") + rolesPath, err := ioutil.TempDir(prefix, "chefsolo-roles") if err != nil { t.Fatalf("err: %s", err) } - dataBagsPath, err := ioutil.TempDir("", "data_bags") + dataBagsPath, err := ioutil.TempDir(prefix, "chefsolo-data_bags") if err != nil { t.Fatalf("err: %s", err) } @@ -137,7 +140,8 @@ func TestProvisionerPrepare_cookbookPaths(t *testing.T) { func TestProvisionerPrepare_dataBagsPath(t *testing.T) { var p Provisioner - dataBagsPath, err := ioutil.TempDir("", "data_bags") + prefix, _ := configfile.ConfigTmpDir() + dataBagsPath, err := ioutil.TempDir(prefix, "chefsolo-data_bags") if err != nil { t.Fatalf("err: %s", err) } @@ -169,7 +173,8 @@ func TestProvisionerPrepare_encryptedDataBagSecretPath(t *testing.T) { } // Test with a file - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "chefsolo") if err != nil { t.Fatalf("err: %s", err) } @@ -184,7 +189,7 @@ func TestProvisionerPrepare_encryptedDataBagSecretPath(t *testing.T) { } // Test with a directory - td, err := ioutil.TempDir("", "packer") + td, err := ioutil.TempDir(prefix, "chefsolo") if err != nil { t.Fatalf("err: %s", err) } @@ -202,7 +207,8 @@ func TestProvisionerPrepare_encryptedDataBagSecretPath(t *testing.T) { func TestProvisionerPrepare_environmentsPath(t *testing.T) { var p Provisioner - environmentsPath, err := ioutil.TempDir("", "environments") + prefix, _ := configfile.ConfigTmpDir() + environmentsPath, err := ioutil.TempDir(prefix, "chefsolo-environments") if err != nil { t.Fatalf("err: %s", err) } @@ -224,7 +230,8 @@ func TestProvisionerPrepare_environmentsPath(t *testing.T) { func TestProvisionerPrepare_rolesPath(t *testing.T) { var p Provisioner - rolesPath, err := ioutil.TempDir("", "roles") + prefix, _ := configfile.ConfigTmpDir() + rolesPath, err := ioutil.TempDir(prefix, "chefsolo-roles") if err != nil { t.Fatalf("err: %s", err) } diff --git a/provisioner/file/provisioner_test.go b/provisioner/file/provisioner_test.go index 732bba3ff..f6ed97871 100644 --- a/provisioner/file/provisioner_test.go +++ b/provisioner/file/provisioner_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { @@ -57,7 +58,8 @@ func TestProvisionerPrepare_InvalidSource(t *testing.T) { func TestProvisionerPrepare_ValidSource(t *testing.T) { var p Provisioner - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "file") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -102,7 +104,9 @@ func TestProvisionerPrepare_EmptyDestination(t *testing.T) { func TestProvisionerProvision_SendsFile(t *testing.T) { var p Provisioner - tf, err := ioutil.TempFile("", "packer") + + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "file") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -159,11 +163,14 @@ func TestProvisionDownloadMkdirAll(t *testing.T) { {"path/to/dir"}, {"path/to/dir/"}, } - tmpDir, err := ioutil.TempDir("", "packer-file") + + prefix, _ := configfile.ConfigTmpDir() + tmpDir, err := ioutil.TempDir(prefix, "file") if err != nil { t.Fatalf("error tempdir: %s", err) } defer os.RemoveAll(tmpDir) + tf, err := ioutil.TempFile(tmpDir, "packer") if err != nil { t.Fatalf("error tempfile: %s", err) diff --git a/provisioner/puppet-masterless/provisioner_test.go b/provisioner/puppet-masterless/provisioner_test.go index c8adce89e..27779fb09 100644 --- a/provisioner/puppet-masterless/provisioner_test.go +++ b/provisioner/puppet-masterless/provisioner_test.go @@ -10,12 +10,14 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template/interpolate" "github.com/stretchr/testify/assert" ) func testConfig() (config map[string]interface{}, tf *os.File) { - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "puppetmasterless") if err != nil { panic(err) } @@ -185,7 +187,8 @@ func TestProvisionerPrepare_puppetBinDir(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "puppetmasterless") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -212,7 +215,8 @@ func TestProvisionerPrepare_hieraConfigPath(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "puppetmasterless") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -239,7 +243,8 @@ func TestProvisionerPrepare_manifestFile(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "puppetmasterless") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -266,9 +271,10 @@ func TestProvisionerPrepare_manifestDir(t *testing.T) { } // Test with a good one - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "puppetmasterless") if err != nil { - t.Fatalf("error: %s", err) + t.Fatalf("err: %s", err) } defer os.RemoveAll(td) @@ -301,7 +307,8 @@ func TestProvisionerPrepare_modulePaths(t *testing.T) { } // Test with a good one - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "puppetmasterless") if err != nil { t.Fatalf("error: %s", err) } @@ -336,7 +343,8 @@ func TestProvisionerPrepare_facterFacts(t *testing.T) { } // Test with a good one - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "puppetmasterless") if err != nil { t.Fatalf("error: %s", err) } diff --git a/provisioner/puppet-server/provisioner_test.go b/provisioner/puppet-server/provisioner_test.go index 68e9369b6..5a2185624 100644 --- a/provisioner/puppet-server/provisioner_test.go +++ b/provisioner/puppet-server/provisioner_test.go @@ -6,10 +6,12 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() (config map[string]interface{}, tf *os.File) { - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "puppetserver") if err != nil { panic(err) } @@ -42,7 +44,8 @@ func TestProvisionerPrepare_puppetBinDir(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "puppetserver") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -77,7 +80,8 @@ func TestProvisionerPrepare_clientPrivateKeyPath(t *testing.T) { } // Test with a good one - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "puppetserver") if err != nil { t.Fatalf("error: %s", err) } @@ -112,7 +116,8 @@ func TestProvisionerPrepare_clientCertPath(t *testing.T) { } // Test with a good one - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "puppetserver") if err != nil { t.Fatalf("error: %s", err) } @@ -160,7 +165,8 @@ func TestProvisionerPrepare_facterFacts(t *testing.T) { } // Test with a good one - td, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + td, err := ioutil.TempDir(prefix, "puppetserver") if err != nil { t.Fatalf("error: %s", err) } diff --git a/provisioner/salt-masterless/provisioner_test.go b/provisioner/salt-masterless/provisioner_test.go index c38a9b879..43c8948f8 100644 --- a/provisioner/salt-masterless/provisioner_test.go +++ b/provisioner/salt-masterless/provisioner_test.go @@ -7,11 +7,14 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { + prefix, _ := configfile.ConfigTmpDir() + return map[string]interface{}{ - "local_state_tree": os.TempDir(), + "local_state_tree": prefix, } } @@ -83,7 +86,8 @@ func TestProvisionerPrepare_MinionConfig(t *testing.T) { t.Fatal("should have error") } - tf, err := ioutil.TempFile("", "minion") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "saltmasterless-minion") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -130,7 +134,8 @@ func TestProvisionerPrepare_GrainsFile(t *testing.T) { t.Fatal("should have error") } - tf, err := ioutil.TempFile("", "grains") + prefix, _ := configfile.ConfigTmpDir() + tf, err := ioutil.TempFile(prefix, "saltmasterless-grains") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -153,7 +158,8 @@ func TestProvisionerPrepare_LocalStateTree(t *testing.T) { t.Fatal("should have error") } - config["local_state_tree"] = os.TempDir() + prefix, _ := configfile.ConfigTmpDir() + config["local_state_tree"] = prefix err = p.Prepare(config) if err != nil { t.Fatalf("err: %s", err) @@ -170,7 +176,8 @@ func TestProvisionerPrepare_LocalPillarRoots(t *testing.T) { t.Fatal("should have error") } - config["local_pillar_roots"] = os.TempDir() + prefix, _ := configfile.ConfigTmpDir() + config["local_pillar_roots"] = prefix err = p.Prepare(config) if err != nil { t.Fatalf("err: %s", err)