cleanup temp files from command tests

Rather than try to modify all the hundreds of calls to the temp helper
functions, and cleanup the temp files at every call site, have all tests
work within a single temp directory that is removed at the end of
TestMain.
This commit is contained in:
James Bardin 2018-03-28 13:08:38 -04:00
parent 15dc75720e
commit e980156451
11 changed files with 55 additions and 161 deletions

View File

@ -252,10 +252,7 @@ func TestApply_configInvalid(t *testing.T) {
}
func TestApply_defaultState(t *testing.T) {
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
td := testTempDir(t)
statePath := filepath.Join(td, DefaultStateFilename)
// Change to the temporary directory
@ -735,10 +732,7 @@ func TestApply_planVars(t *testing.T) {
// we should be able to apply a plan file with no other file dependencies
func TestApply_planNoModuleFiles(t *testing.T) {
// temporary data directory which we can remove between commands
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatal(err)
}
td := testTempDir(t)
defer os.RemoveAll(td)
defer testChdir(t, td)()

View File

@ -27,6 +27,9 @@ import (
// This is the directory where our test fixtures are.
var fixtureDir = "./test-fixtures"
// a top level temp directory which will be cleaned after all tests
var testingDir string
func init() {
test = true
@ -37,9 +40,16 @@ func init() {
if err != nil {
panic(err)
}
testingDir, err = ioutil.TempDir(testingDir, "tf")
if err != nil {
panic(err)
}
}
func TestMain(m *testing.M) {
defer os.RemoveAll(testingDir)
flag.Parse()
if testing.Verbose() {
// if we're verbose, use the logging requested by TF_LOG
@ -55,7 +65,7 @@ func TestMain(m *testing.M) {
func tempDir(t *testing.T) string {
t.Helper()
dir, err := ioutil.TempDir("", "tf")
dir, err := ioutil.TempDir(testingDir, "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
@ -305,7 +315,7 @@ func testTempFile(t *testing.T) string {
func testTempDir(t *testing.T) string {
t.Helper()
d, err := ioutil.TempDir("", "tf")
d, err := ioutil.TempDir(testingDir, "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
@ -358,7 +368,7 @@ func testChdir(t *testing.T, new string) func() {
func testCwd(t *testing.T) (string, string) {
t.Helper()
tmp, err := ioutil.TempDir("", "tf")
tmp, err := ioutil.TempDir(testingDir, "tf")
if err != nil {
t.Fatalf("err: %v", err)
}
@ -595,7 +605,7 @@ func testRemoteState(t *testing.T, s *terraform.State, c int) (*terraform.Remote
// supplied to locate the statelocker.go source.
func testLockState(sourceDir, path string) (func(), error) {
// build and run the binary ourselves so we can quickly terminate it for cleanup
buildDir, err := ioutil.TempDir("", "locker")
buildDir, err := ioutil.TempDir(testingDir, "locker")
if err != nil {
return nil, err
}

View File

@ -12,7 +12,7 @@ import (
func TestDebugJSON2Dot(t *testing.T) {
// create the graph JSON output
logFile, err := ioutil.TempFile("", "tf")
logFile, err := ioutil.TempFile(testingDir, "tf")
if err != nil {
t.Fatal(err)
}

View File

@ -13,11 +13,7 @@ import (
)
func TestFmt_errorReporting(t *testing.T) {
tempDir, err := fmtFixtureWriteDir()
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(tempDir)
tempDir := fmtFixtureWriteDir(t)
ui := new(cli.MockUi)
c := &FmtCommand{
@ -63,11 +59,7 @@ func TestFmt_tooManyArgs(t *testing.T) {
}
func TestFmt_workingDirectory(t *testing.T) {
tempDir, err := fmtFixtureWriteDir()
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(tempDir)
tempDir := fmtFixtureWriteDir(t)
cwd, err := os.Getwd()
if err != nil {
@ -99,11 +91,7 @@ func TestFmt_workingDirectory(t *testing.T) {
}
func TestFmt_directoryArg(t *testing.T) {
tempDir, err := fmtFixtureWriteDir()
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(tempDir)
tempDir := fmtFixtureWriteDir(t)
ui := new(cli.MockUi)
c := &FmtCommand{
@ -149,11 +137,7 @@ func TestFmt_stdinArg(t *testing.T) {
}
func TestFmt_nonDefaultOptions(t *testing.T) {
tempDir, err := fmtFixtureWriteDir()
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(tempDir)
tempDir := fmtFixtureWriteDir(t)
ui := new(cli.MockUi)
c := &FmtCommand{
@ -180,11 +164,7 @@ func TestFmt_nonDefaultOptions(t *testing.T) {
}
func TestFmt_check(t *testing.T) {
tempDir, err := fmtFixtureWriteDir()
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(tempDir)
tempDir := fmtFixtureWriteDir(t)
ui := new(cli.MockUi)
c := &FmtCommand{
@ -244,17 +224,13 @@ var fmtFixture = struct {
`),
}
func fmtFixtureWriteDir() (string, error) {
dir, err := ioutil.TempDir("", "tf")
func fmtFixtureWriteDir(t *testing.T) string {
dir := testTempDir(t)
err := ioutil.WriteFile(filepath.Join(dir, fmtFixture.filename), fmtFixture.input, 0644)
if err != nil {
return "", err
t.Fatal(err)
}
err = ioutil.WriteFile(filepath.Join(dir, fmtFixture.filename), fmtFixture.input, 0644)
if err != nil {
os.RemoveAll(dir)
return "", err
}
return dir, nil
return dir
}

View File

@ -998,11 +998,7 @@ func TestInit_providerLockFile(t *testing.T) {
}
func TestInit_pluginDirReset(t *testing.T) {
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(td)
td := testTempDir(t)
defer testChdir(t, td)()
ui := new(cli.MockUi)

View File

@ -1,7 +1,6 @@
package command
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -451,10 +450,7 @@ func TestOutput_stateDefault(t *testing.T) {
// Write the state file in a temporary directory with the
// default filename.
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
td := testTempDir(t)
statePath := filepath.Join(td, DefaultStateFilename)
f, err := os.Create(statePath)

View File

@ -196,12 +196,8 @@ func TestPlan_outPath(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
tf, err := ioutil.TempFile("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
outPath := tf.Name()
os.Remove(tf.Name())
td := testTempDir(t)
outPath := filepath.Join(td, "test.plan")
p := testProvider()
ui := new(cli.MockUi)
@ -253,12 +249,8 @@ func TestPlan_outPathNoChange(t *testing.T) {
}
statePath := testStateFile(t, originalState)
tf, err := ioutil.TempFile("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
outPath := tf.Name()
os.Remove(tf.Name())
td := testTempDir(t)
outPath := filepath.Join(td, "test.plan")
p := testProvider()
ui := new(cli.MockUi)
@ -433,20 +425,8 @@ func TestPlan_refresh(t *testing.T) {
}
func TestPlan_state(t *testing.T) {
// Write out some prior state
tf, err := ioutil.TempFile("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := tf.Name()
defer os.Remove(tf.Name())
originalState := testState()
err = terraform.WriteState(originalState, tf)
tf.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := testStateFile(t, originalState)
p := testProvider()
ui := new(cli.MockUi)
@ -475,24 +455,7 @@ func TestPlan_state(t *testing.T) {
func TestPlan_stateDefault(t *testing.T) {
originalState := testState()
// Write the state file in a temporary directory with the
// default filename.
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := filepath.Join(td, DefaultStateFilename)
f, err := os.Create(statePath)
if err != nil {
t.Fatalf("err: %s", err)
}
err = terraform.WriteState(originalState, f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := testStateFile(t, originalState)
// Change to that directory
cwd, err := os.Getwd()
@ -514,6 +477,7 @@ func TestPlan_stateDefault(t *testing.T) {
}
args := []string{
"-state", statePath,
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {

View File

@ -7,7 +7,7 @@ import (
)
func TestPluginSHA256LockFile(t *testing.T) {
f, err := ioutil.TempFile("", "tf-pluginsha1lockfile-test-")
f, err := ioutil.TempFile(testingDir, "tf-pluginsha1lockfile-test-")
if err != nil {
t.Fatalf("failed to create temporary file: %s", err)
}

View File

@ -2,7 +2,6 @@ package command
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -95,11 +94,7 @@ func TestMultiVersionProviderResolver(t *testing.T) {
}
func TestPluginPath(t *testing.T) {
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(td)
td := testTempDir(t)
defer testChdir(t, td)()
pluginPath := []string{"a", "b", "c"}

View File

@ -189,17 +189,17 @@ func TestRefresh_defaultState(t *testing.T) {
// Write the state file in a temporary directory with the
// default filename.
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := filepath.Join(td, DefaultStateFilename)
statePath := testStateFile(t, originalState)
localState := &state.LocalState{Path: statePath}
if err := localState.WriteState(originalState); err != nil {
if err := localState.RefreshState(); err != nil {
t.Fatal(err)
}
serial := localState.State().Serial
s := localState.State()
if s == nil {
t.Fatal("empty test state")
}
serial := s.Serial
// Change to that directory
cwd, err := os.Getwd()
@ -224,6 +224,7 @@ func TestRefresh_defaultState(t *testing.T) {
p.RefreshReturn = newInstanceState("yes")
args := []string{
"-state", statePath,
testFixturePath("refresh"),
}
if code := c.Run(args); code != 0 {
@ -364,7 +365,7 @@ func TestRefresh_outPath(t *testing.T) {
statePath := testStateFile(t, state)
// Output path
outf, err := ioutil.TempFile("", "tf")
outf, err := ioutil.TempFile(testingDir, "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
@ -585,7 +586,7 @@ func TestRefresh_backup(t *testing.T) {
statePath := testStateFile(t, state)
// Output path
outf, err := ioutil.TempFile("", "tf")
outf, err := ioutil.TempFile(testingDir, "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
@ -594,7 +595,7 @@ func TestRefresh_backup(t *testing.T) {
os.Remove(outPath)
// Backup path
backupf, err := ioutil.TempFile("", "tf")
backupf, err := ioutil.TempFile(testingDir, "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
@ -679,7 +680,7 @@ func TestRefresh_disableBackup(t *testing.T) {
statePath := testStateFile(t, state)
// Output path
outf, err := ioutil.TempFile("", "tf")
outf, err := ioutil.TempFile(testingDir, "tf")
if err != nil {
t.Fatalf("err: %s", err)
}

View File

@ -1,8 +1,6 @@
package command
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@ -32,31 +30,8 @@ func TestShow(t *testing.T) {
func TestShow_noArgs(t *testing.T) {
// Create the default state
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := filepath.Join(td, DefaultStateFilename)
f, err := os.Create(statePath)
if err != nil {
t.Fatalf("err: %s", err)
}
err = terraform.WriteState(testState(), f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
// Change to the temporary directory
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(filepath.Dir(statePath)); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
statePath := testStateFile(t, testState())
defer testChdir(t, filepath.Dir(statePath))()
ui := new(cli.MockUi)
c := &ShowCommand{
@ -74,21 +49,8 @@ func TestShow_noArgs(t *testing.T) {
func TestShow_noArgsNoState(t *testing.T) {
// Create the default state
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := filepath.Join(td, DefaultStateFilename)
// Change to the temporary directory
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(filepath.Dir(statePath)); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
statePath := testStateFile(t, testState())
defer testChdir(t, filepath.Dir(statePath))()
ui := new(cli.MockUi)
c := &ShowCommand{