From e9b6adefeae8c65eb8aa47fef38cbf0aa424338c Mon Sep 17 00:00:00 2001 From: Matthew Patton Date: Thu, 1 Nov 2018 18:52:38 -0400 Subject: [PATCH] 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