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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,7 +2,6 @@ package command
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -95,11 +94,7 @@ func TestMultiVersionProviderResolver(t *testing.T) {
} }
func TestPluginPath(t *testing.T) { func TestPluginPath(t *testing.T) {
td, err := ioutil.TempDir("", "tf") td := testTempDir(t)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(td)
defer testChdir(t, td)() defer testChdir(t, td)()
pluginPath := []string{"a", "b", "c"} 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 // Write the state file in a temporary directory with the
// default filename. // default filename.
td, err := ioutil.TempDir("", "tf") statePath := testStateFile(t, originalState)
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := filepath.Join(td, DefaultStateFilename)
localState := &state.LocalState{Path: statePath} localState := &state.LocalState{Path: statePath}
if err := localState.WriteState(originalState); err != nil { if err := localState.RefreshState(); err != nil {
t.Fatal(err) 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 // Change to that directory
cwd, err := os.Getwd() cwd, err := os.Getwd()
@ -224,6 +224,7 @@ func TestRefresh_defaultState(t *testing.T) {
p.RefreshReturn = newInstanceState("yes") p.RefreshReturn = newInstanceState("yes")
args := []string{ args := []string{
"-state", statePath,
testFixturePath("refresh"), testFixturePath("refresh"),
} }
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
@ -364,7 +365,7 @@ func TestRefresh_outPath(t *testing.T) {
statePath := testStateFile(t, state) statePath := testStateFile(t, state)
// Output path // Output path
outf, err := ioutil.TempFile("", "tf") outf, err := ioutil.TempFile(testingDir, "tf")
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -585,7 +586,7 @@ func TestRefresh_backup(t *testing.T) {
statePath := testStateFile(t, state) statePath := testStateFile(t, state)
// Output path // Output path
outf, err := ioutil.TempFile("", "tf") outf, err := ioutil.TempFile(testingDir, "tf")
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -594,7 +595,7 @@ func TestRefresh_backup(t *testing.T) {
os.Remove(outPath) os.Remove(outPath)
// Backup path // Backup path
backupf, err := ioutil.TempFile("", "tf") backupf, err := ioutil.TempFile(testingDir, "tf")
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -679,7 +680,7 @@ func TestRefresh_disableBackup(t *testing.T) {
statePath := testStateFile(t, state) statePath := testStateFile(t, state)
// Output path // Output path
outf, err := ioutil.TempFile("", "tf") outf, err := ioutil.TempFile(testingDir, "tf")
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }

View File

@ -1,8 +1,6 @@
package command package command
import ( import (
"io/ioutil"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@ -32,31 +30,8 @@ func TestShow(t *testing.T) {
func TestShow_noArgs(t *testing.T) { func TestShow_noArgs(t *testing.T) {
// Create the default state // Create the default state
td, err := ioutil.TempDir("", "tf") statePath := testStateFile(t, testState())
if err != nil { defer testChdir(t, filepath.Dir(statePath))()
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)
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &ShowCommand{ c := &ShowCommand{
@ -74,21 +49,8 @@ func TestShow_noArgs(t *testing.T) {
func TestShow_noArgsNoState(t *testing.T) { func TestShow_noArgsNoState(t *testing.T) {
// Create the default state // Create the default state
td, err := ioutil.TempDir("", "tf") statePath := testStateFile(t, testState())
if err != nil { defer testChdir(t, filepath.Dir(statePath))()
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)
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &ShowCommand{ c := &ShowCommand{