Merge pull request #29647 from hashicorp/jbardin/test-temp-cleanup

temp path clean for some backend tests
This commit is contained in:
James Bardin 2021-09-24 12:27:53 -04:00 committed by GitHub
commit 8ce2254ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 66 deletions

View File

@ -27,8 +27,7 @@ import (
) )
func TestLocal_applyBasic(t *testing.T) { func TestLocal_applyBasic(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", applyFixtureSchema()) p := TestLocalProvider(t, b, "test", applyFixtureSchema())
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{ p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{
@ -73,8 +72,7 @@ test_instance.foo:
} }
func TestLocal_applyEmptyDir(t *testing.T) { func TestLocal_applyEmptyDir(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{}) p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{"id": cty.StringVal("yes")})} p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{"id": cty.StringVal("yes")})}
@ -108,8 +106,7 @@ func TestLocal_applyEmptyDir(t *testing.T) {
} }
func TestLocal_applyEmptyDirDestroy(t *testing.T) { func TestLocal_applyEmptyDirDestroy(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{}) p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{} p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{}
@ -139,8 +136,7 @@ func TestLocal_applyEmptyDirDestroy(t *testing.T) {
} }
func TestLocal_applyError(t *testing.T) { func TestLocal_applyError(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
schema := &terraform.ProviderSchema{ schema := &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{ ResourceTypes: map[string]*configschema.Block{
@ -208,8 +204,7 @@ test_instance.foo:
} }
func TestLocal_applyBackendFail(t *testing.T) { func TestLocal_applyBackendFail(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", applyFixtureSchema()) p := TestLocalProvider(t, b, "test", applyFixtureSchema())
@ -272,8 +267,7 @@ test_instance.foo: (tainted)
} }
func TestLocal_applyRefreshFalse(t *testing.T) { func TestLocal_applyRefreshFalse(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", planFixtureSchema()) p := TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState()) testStateFile(t, b.StatePath, testPlanState())

View File

@ -25,8 +25,7 @@ import (
func TestLocalRun(t *testing.T) { func TestLocalRun(t *testing.T) {
configDir := "./testdata/empty" configDir := "./testdata/empty"
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
_, configLoader, configCleanup := initwd.MustLoadConfigForTests(t, configDir) _, configLoader, configCleanup := initwd.MustLoadConfigForTests(t, configDir)
defer configCleanup() defer configCleanup()
@ -53,8 +52,7 @@ func TestLocalRun(t *testing.T) {
func TestLocalRun_error(t *testing.T) { func TestLocalRun_error(t *testing.T) {
configDir := "./testdata/invalid" configDir := "./testdata/invalid"
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
// This backend will return an error when asked to RefreshState, which // This backend will return an error when asked to RefreshState, which
// should then cause LocalRun to return with the state unlocked. // should then cause LocalRun to return with the state unlocked.
@ -85,8 +83,7 @@ func TestLocalRun_error(t *testing.T) {
func TestLocalRun_stalePlan(t *testing.T) { func TestLocalRun_stalePlan(t *testing.T) {
configDir := "./testdata/apply" configDir := "./testdata/apply"
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
_, configLoader, configCleanup := initwd.MustLoadConfigForTests(t, configDir) _, configLoader, configCleanup := initwd.MustLoadConfigForTests(t, configDir)
defer configCleanup() defer configCleanup()

View File

@ -23,8 +23,7 @@ import (
) )
func TestLocal_planBasic(t *testing.T) { func TestLocal_planBasic(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", planFixtureSchema()) p := TestLocalProvider(t, b, "test", planFixtureSchema())
op, configCleanup, done := testOperationPlan(t, "./testdata/plan") op, configCleanup, done := testOperationPlan(t, "./testdata/plan")
@ -53,8 +52,7 @@ func TestLocal_planBasic(t *testing.T) {
} }
func TestLocal_planInAutomation(t *testing.T) { func TestLocal_planInAutomation(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
TestLocalProvider(t, b, "test", planFixtureSchema()) TestLocalProvider(t, b, "test", planFixtureSchema())
const msg = `You didn't use the -out option` const msg = `You didn't use the -out option`
@ -85,8 +83,7 @@ func TestLocal_planInAutomation(t *testing.T) {
} }
func TestLocal_planNoConfig(t *testing.T) { func TestLocal_planNoConfig(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
TestLocalProvider(t, b, "test", &terraform.ProviderSchema{}) TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
op, configCleanup, done := testOperationPlan(t, "./testdata/empty") op, configCleanup, done := testOperationPlan(t, "./testdata/empty")
@ -116,8 +113,7 @@ func TestLocal_planNoConfig(t *testing.T) {
// This test validates the state lacking behavior when the inner call to // This test validates the state lacking behavior when the inner call to
// Context() fails // Context() fails
func TestLocal_plan_context_error(t *testing.T) { func TestLocal_plan_context_error(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
// This is an intentionally-invalid value to make terraform.NewContext fail // This is an intentionally-invalid value to make terraform.NewContext fail
// when b.Operation calls it. // when b.Operation calls it.
@ -157,8 +153,7 @@ func TestLocal_plan_context_error(t *testing.T) {
} }
func TestLocal_planOutputsChanged(t *testing.T) { func TestLocal_planOutputsChanged(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
testStateFile(t, b.StatePath, states.BuildState(func(ss *states.SyncState) { testStateFile(t, b.StatePath, states.BuildState(func(ss *states.SyncState) {
ss.SetOutputValue(addrs.AbsOutputValue{ ss.SetOutputValue(addrs.AbsOutputValue{
Module: addrs.RootModuleInstance, Module: addrs.RootModuleInstance,
@ -239,8 +234,7 @@ state, without changing any real infrastructure.
// Module outputs should not cause the plan to be rendered // Module outputs should not cause the plan to be rendered
func TestLocal_planModuleOutputsChanged(t *testing.T) { func TestLocal_planModuleOutputsChanged(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
testStateFile(t, b.StatePath, states.BuildState(func(ss *states.SyncState) { testStateFile(t, b.StatePath, states.BuildState(func(ss *states.SyncState) {
ss.SetOutputValue(addrs.AbsOutputValue{ ss.SetOutputValue(addrs.AbsOutputValue{
Module: addrs.RootModuleInstance.Child("mod", addrs.NoKey), Module: addrs.RootModuleInstance.Child("mod", addrs.NoKey),
@ -286,8 +280,7 @@ No changes. Your infrastructure matches the configuration.
} }
func TestLocal_planTainted(t *testing.T) { func TestLocal_planTainted(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
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 := t.TempDir() outDir := t.TempDir()
@ -343,8 +336,7 @@ Plan: 1 to add, 0 to change, 1 to destroy.`
} }
func TestLocal_planDeposedOnly(t *testing.T) { func TestLocal_planDeposedOnly(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", planFixtureSchema()) p := TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, states.BuildState(func(ss *states.SyncState) { testStateFile(t, b.StatePath, states.BuildState(func(ss *states.SyncState) {
ss.SetResourceInstanceDeposed( ss.SetResourceInstanceDeposed(
@ -457,8 +449,8 @@ Plan: 1 to add, 0 to change, 1 to destroy.`
} }
func TestLocal_planTainted_createBeforeDestroy(t *testing.T) { func TestLocal_planTainted_createBeforeDestroy(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
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 := t.TempDir() outDir := t.TempDir()
@ -514,8 +506,7 @@ Plan: 1 to add, 0 to change, 1 to destroy.`
} }
func TestLocal_planRefreshFalse(t *testing.T) { func TestLocal_planRefreshFalse(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", planFixtureSchema()) p := TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState()) testStateFile(t, b.StatePath, testPlanState())
@ -546,8 +537,7 @@ func TestLocal_planRefreshFalse(t *testing.T) {
} }
func TestLocal_planDestroy(t *testing.T) { func TestLocal_planDestroy(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
TestLocalProvider(t, b, "test", planFixtureSchema()) TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState()) testStateFile(t, b.StatePath, testPlanState())
@ -599,8 +589,7 @@ func TestLocal_planDestroy(t *testing.T) {
} }
func TestLocal_planDestroy_withDataSources(t *testing.T) { func TestLocal_planDestroy_withDataSources(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
TestLocalProvider(t, b, "test", planFixtureSchema()) TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState_withDataSource()) testStateFile(t, b.StatePath, testPlanState_withDataSource())
@ -675,8 +664,7 @@ func getAddrs(resources []*plans.ResourceInstanceChangeSrc) []string {
} }
func TestLocal_planOutPathNoChange(t *testing.T) { func TestLocal_planOutPathNoChange(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
TestLocalProvider(t, b, "test", planFixtureSchema()) TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState()) testStateFile(t, b.StatePath, testPlanState())

View File

@ -22,8 +22,7 @@ import (
) )
func TestLocal_refresh(t *testing.T) { func TestLocal_refresh(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema()) p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
testStateFile(t, b.StatePath, testRefreshState()) testStateFile(t, b.StatePath, testRefreshState())
@ -58,8 +57,7 @@ test_instance.foo:
} }
func TestLocal_refreshInput(t *testing.T) { func TestLocal_refreshInput(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
schema := &terraform.ProviderSchema{ schema := &terraform.ProviderSchema{
Provider: &configschema.Block{ Provider: &configschema.Block{
@ -121,8 +119,7 @@ test_instance.foo:
} }
func TestLocal_refreshValidate(t *testing.T) { func TestLocal_refreshValidate(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema()) p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
testStateFile(t, b.StatePath, testRefreshState()) testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil p.ReadResourceFn = nil
@ -151,8 +148,7 @@ test_instance.foo:
} }
func TestLocal_refreshValidateProviderConfigured(t *testing.T) { func TestLocal_refreshValidateProviderConfigured(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
schema := &terraform.ProviderSchema{ schema := &terraform.ProviderSchema{
Provider: &configschema.Block{ Provider: &configschema.Block{
@ -204,8 +200,7 @@ test_instance.foo:
// This test validates the state lacking behavior when the inner call to // This test validates the state lacking behavior when the inner call to
// Context() fails // Context() fails
func TestLocal_refresh_context_error(t *testing.T) { func TestLocal_refresh_context_error(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
testStateFile(t, b.StatePath, testRefreshState()) testStateFile(t, b.StatePath, testRefreshState())
op, configCleanup, done := testOperationRefresh(t, "./testdata/apply") op, configCleanup, done := testOperationRefresh(t, "./testdata/apply")
defer configCleanup() defer configCleanup()
@ -225,8 +220,7 @@ func TestLocal_refresh_context_error(t *testing.T) {
} }
func TestLocal_refreshEmptyState(t *testing.T) { func TestLocal_refreshEmptyState(t *testing.T) {
b, cleanup := TestLocal(t) b := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema()) p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
testStateFile(t, b.StatePath, states.NewState()) testStateFile(t, b.StatePath, states.NewState())

View File

@ -1,7 +1,6 @@
package local package local
import ( import (
"os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -21,9 +20,12 @@ import (
// //
// No operations will be called on the returned value, so you can still set // No operations will be called on the returned value, so you can still set
// public fields without any locks. // public fields without any locks.
func TestLocal(t *testing.T) (*Local, func()) { func TestLocal(t *testing.T) *Local {
t.Helper() t.Helper()
tempDir := t.TempDir() tempDir, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
local := New() local := New()
local.StatePath = filepath.Join(tempDir, "state.tfstate") local.StatePath = filepath.Join(tempDir, "state.tfstate")
@ -32,13 +34,7 @@ func TestLocal(t *testing.T) (*Local, func()) {
local.StateWorkspaceDir = filepath.Join(tempDir, "state.tfstate.d") local.StateWorkspaceDir = filepath.Join(tempDir, "state.tfstate.d")
local.ContextOpts = &terraform.ContextOpts{} local.ContextOpts = &terraform.ContextOpts{}
cleanup := func() { return local
if err := os.RemoveAll(tempDir); err != nil {
t.Fatal("error cleanup up test:", err)
}
}
return local, cleanup
} }
// TestLocalProvider modifies the ContextOpts of the *Local parameter to // TestLocalProvider modifies the ContextOpts of the *Local parameter to