sync straggler to refactor. implement tests at packer/configfile instead of buried in docker.

pull/6942/head^2
Matthew Patton 7 years ago
parent 852113ed07
commit e9b6adefea

@ -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))
}
}

@ -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))
}
}

@ -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

Loading…
Cancel
Save