fix temp directory handling in some tests

Cleanup some more test fixtures to use t.TempDir

Use EvalSymlinks with temp dir paths to help with MacOS errors from
various terraform components.
This commit is contained in:
James Bardin 2021-09-13 13:22:36 -04:00
parent 8ed9a270e5
commit d0993b0e80
6 changed files with 18 additions and 48 deletions

View File

@ -132,7 +132,7 @@ func TestLocalRun_stalePlan(t *testing.T) {
stateFile := statefile.New(plan.PriorState, "boop", 2) stateFile := statefile.New(plan.PriorState, "boop", 2)
// Roundtrip through serialization as expected by the operation // Roundtrip through serialization as expected by the operation
outDir := testTempDir(t) outDir := t.TempDir()
defer os.RemoveAll(outDir) defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan") planPath := filepath.Join(outDir, "plan.tfplan")
if err := planfile.Create(planPath, configload.NewEmptySnapshot(), prevStateFile, stateFile, plan); err != nil { if err := planfile.Create(planPath, configload.NewEmptySnapshot(), prevStateFile, stateFile, plan); err != nil {

View File

@ -174,7 +174,7 @@ func TestLocal_planOutputsChanged(t *testing.T) {
// unknown" situation because that's already common for printing out // unknown" situation because that's already common for printing out
// resource changes and we already have many tests for that. // resource changes and we already have many tests for that.
})) }))
outDir := testTempDir(t) outDir := t.TempDir()
defer os.RemoveAll(outDir) defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan") planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan-outputs-changed") op, configCleanup, done := testOperationPlan(t, "./testdata/plan-outputs-changed")
@ -232,7 +232,7 @@ func TestLocal_planModuleOutputsChanged(t *testing.T) {
OutputValue: addrs.OutputValue{Name: "changed"}, OutputValue: addrs.OutputValue{Name: "changed"},
}, cty.StringVal("before"), false) }, cty.StringVal("before"), false)
})) }))
outDir := testTempDir(t) outDir := t.TempDir()
defer os.RemoveAll(outDir) defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan") planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan-module-outputs-changed") op, configCleanup, done := testOperationPlan(t, "./testdata/plan-module-outputs-changed")
@ -275,8 +275,7 @@ func TestLocal_planTainted(t *testing.T) {
defer cleanup() defer cleanup()
p := TestLocalProvider(t, b, "test", planFixtureSchema()) p := TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState_tainted()) testStateFile(t, b.StatePath, testPlanState_tainted())
outDir := testTempDir(t) outDir := t.TempDir()
defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan") planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan") op, configCleanup, done := testOperationPlan(t, "./testdata/plan")
defer configCleanup() defer configCleanup()
@ -356,8 +355,7 @@ func TestLocal_planDeposedOnly(t *testing.T) {
}, },
) )
})) }))
outDir := testTempDir(t) outDir := t.TempDir()
defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan") planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan") op, configCleanup, done := testOperationPlan(t, "./testdata/plan")
defer configCleanup() defer configCleanup()
@ -448,8 +446,7 @@ func TestLocal_planTainted_createBeforeDestroy(t *testing.T) {
defer cleanup() defer cleanup()
p := TestLocalProvider(t, b, "test", planFixtureSchema()) p := TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState_tainted()) testStateFile(t, b.StatePath, testPlanState_tainted())
outDir := testTempDir(t) outDir := t.TempDir()
defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan") planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan-cbd") op, configCleanup, done := testOperationPlan(t, "./testdata/plan-cbd")
defer configCleanup() defer configCleanup()
@ -540,8 +537,7 @@ func TestLocal_planDestroy(t *testing.T) {
TestLocalProvider(t, b, "test", planFixtureSchema()) TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState()) testStateFile(t, b.StatePath, testPlanState())
outDir := testTempDir(t) outDir := t.TempDir()
defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan") planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan") op, configCleanup, done := testOperationPlan(t, "./testdata/plan")
@ -594,8 +590,7 @@ func TestLocal_planDestroy_withDataSources(t *testing.T) {
TestLocalProvider(t, b, "test", planFixtureSchema()) TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState_withDataSource()) testStateFile(t, b.StatePath, testPlanState_withDataSource())
outDir := testTempDir(t) outDir := t.TempDir()
defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan") planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/destroy-with-ds") op, configCleanup, done := testOperationPlan(t, "./testdata/destroy-with-ds")
@ -670,8 +665,7 @@ func TestLocal_planOutPathNoChange(t *testing.T) {
TestLocalProvider(t, b, "test", planFixtureSchema()) TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState()) testStateFile(t, b.StatePath, testPlanState())
outDir := testTempDir(t) outDir := t.TempDir()
defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan") planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan") op, configCleanup, done := testOperationPlan(t, "./testdata/plan")

View File

@ -1,7 +1,6 @@
package local package local
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -24,7 +23,7 @@ import (
// public fields without any locks. // public fields without any locks.
func TestLocal(t *testing.T) (*Local, func()) { func TestLocal(t *testing.T) (*Local, func()) {
t.Helper() t.Helper()
tempDir := testTempDir(t) tempDir := t.TempDir()
local := New() local := New()
local.StatePath = filepath.Join(tempDir, "state.tfstate") local.StatePath = filepath.Join(tempDir, "state.tfstate")
@ -189,15 +188,6 @@ func (b *TestLocalNoDefaultState) StateMgr(name string) (statemgr.Full, error) {
return b.Local.StateMgr(name) return b.Local.StateMgr(name)
} }
func testTempDir(t *testing.T) string {
d, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
return d
}
func testStateFile(t *testing.T, path string, s *states.State) { func testStateFile(t *testing.T, path string, s *states.State) {
stateFile := statemgr.NewFilesystem(path) stateFile := statemgr.NewFilesystem(path)
stateFile.WriteState(s) stateFile.WriteState(s)

View File

@ -147,16 +147,10 @@ func tempWorkingDir(t *testing.T) (*workdir.Dir, func() error) {
// the testWorkingDir commentary for an example of how to use this function // the testWorkingDir commentary for an example of how to use this function
// along with testChdir to meet the expectations of command.Meta legacy // along with testChdir to meet the expectations of command.Meta legacy
// functionality. // functionality.
func tempWorkingDirFixture(t *testing.T, fixtureName string) (*workdir.Dir, func() error) { func tempWorkingDirFixture(t *testing.T, fixtureName string) *workdir.Dir {
t.Helper() t.Helper()
dirPath, err := os.MkdirTemp("", "tf-command-test-"+fixtureName) dirPath := testTempDir(t)
if err != nil {
t.Fatal(err)
}
done := func() error {
return os.RemoveAll(dirPath)
}
t.Logf("temporary directory %s with fixture %q", dirPath, fixtureName) t.Logf("temporary directory %s with fixture %q", dirPath, fixtureName)
fixturePath := testFixturePath(fixtureName) fixturePath := testFixturePath(fixtureName)
@ -165,7 +159,7 @@ func tempWorkingDirFixture(t *testing.T, fixtureName string) (*workdir.Dir, func
// on failure, a failure to copy will prevent us from cleaning up the // on failure, a failure to copy will prevent us from cleaning up the
// temporary directory. Oh well. :( // temporary directory. Oh well. :(
return workdir.NewDir(dirPath), done return workdir.NewDir(dirPath)
} }
func testFixturePath(name string) string { func testFixturePath(name string) string {
@ -550,13 +544,8 @@ func testTempFile(t *testing.T) string {
func testTempDir(t *testing.T) string { func testTempDir(t *testing.T) string {
t.Helper() t.Helper()
d := t.TempDir()
d, err := ioutil.TempDir(testingDir, "tf") d, err := filepath.EvalSymlinks(d)
if err != nil {
t.Fatalf("err: %s", err)
}
d, err = filepath.EvalSymlinks(d)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -8,8 +8,7 @@ import (
) )
func TestGet(t *testing.T) { func TestGet(t *testing.T) {
wd, cleanup := tempWorkingDirFixture(t, "get") wd := tempWorkingDirFixture(t, "get")
defer cleanup()
defer testChdir(t, wd.RootModuleDir())() defer testChdir(t, wd.RootModuleDir())()
ui := cli.NewMockUi() ui := cli.NewMockUi()
@ -56,8 +55,7 @@ func TestGet_multipleArgs(t *testing.T) {
} }
func TestGet_update(t *testing.T) { func TestGet_update(t *testing.T) {
wd, cleanup := tempWorkingDirFixture(t, "get") wd := tempWorkingDirFixture(t, "get")
defer cleanup()
defer testChdir(t, wd.RootModuleDir())() defer testChdir(t, wd.RootModuleDir())()
ui := cli.NewMockUi() ui := cli.NewMockUi()

View File

@ -1855,8 +1855,7 @@ func TestMetaBackend_configToExtra(t *testing.T) {
// no config; return inmem backend stored in state // no config; return inmem backend stored in state
func TestBackendFromState(t *testing.T) { func TestBackendFromState(t *testing.T) {
wd, cleanup := tempWorkingDirFixture(t, "backend-from-state") wd := tempWorkingDirFixture(t, "backend-from-state")
defer cleanup()
defer testChdir(t, wd.RootModuleDir())() defer testChdir(t, wd.RootModuleDir())()
// Setup the meta // Setup the meta