2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2018-03-28 16:54:08 +02:00
|
|
|
"os"
|
2017-01-19 05:47:56 +01:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2017-03-01 19:59:17 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
2018-09-28 23:04:57 +02:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
2018-10-09 00:22:59 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestLocal returns a configured Local struct with temporary paths and
|
|
|
|
// in-memory ContextOpts.
|
|
|
|
//
|
|
|
|
// No operations will be called on the returned value, so you can still set
|
|
|
|
// public fields without any locks.
|
2018-03-28 16:54:08 +02:00
|
|
|
func TestLocal(t *testing.T) (*Local, func()) {
|
2018-03-21 02:43:02 +01:00
|
|
|
t.Helper()
|
2018-07-04 12:11:35 +02:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
tempDir := testTempDir(t)
|
|
|
|
var local *Local
|
|
|
|
local = &Local{
|
|
|
|
StatePath: filepath.Join(tempDir, "state.tfstate"),
|
|
|
|
StateOutPath: filepath.Join(tempDir, "state.tfstate"),
|
|
|
|
StateBackupPath: filepath.Join(tempDir, "state.tfstate.bak"),
|
|
|
|
StateWorkspaceDir: filepath.Join(tempDir, "state.tfstate.d"),
|
|
|
|
ContextOpts: &terraform.ContextOpts{},
|
|
|
|
ShowDiagnostics: func(vals ...interface{}) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
diags = diags.Append(vals...)
|
|
|
|
for _, diag := range diags {
|
2018-10-09 21:19:24 +02:00
|
|
|
// NOTE: Since the caller here is not directly the TestLocal
|
|
|
|
// function, t.Helper doesn't apply and so the log source
|
|
|
|
// isn't correctly shown in the test log output. This seems
|
|
|
|
// unavoidable as long as this is happening so indirectly.
|
2018-03-21 02:43:02 +01:00
|
|
|
t.Log(diag.Description().Summary)
|
|
|
|
if local.CLI != nil {
|
|
|
|
local.CLI.Error(diag.Description().Summary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2018-03-28 16:54:08 +02:00
|
|
|
cleanup := func() {
|
|
|
|
if err := os.RemoveAll(tempDir); err != nil {
|
2018-09-28 23:04:57 +02:00
|
|
|
t.Fatal("error cleanup up test:", err)
|
2018-03-28 16:54:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return local, cleanup
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestLocalProvider modifies the ContextOpts of the *Local parameter to
|
|
|
|
// have a provider with the given name.
|
2018-09-28 23:04:57 +02:00
|
|
|
func TestLocalProvider(t *testing.T, b *Local, name string, schema *terraform.ProviderSchema) *terraform.MockProvider {
|
|
|
|
// Build a mock resource provider for in-memory operations
|
|
|
|
p := new(terraform.MockProvider)
|
|
|
|
p.GetSchemaReturn = schema
|
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
PlannedPrivate: req.PriorPrivate,
|
2018-08-25 01:13:50 +02:00
|
|
|
}
|
2018-09-28 23:04:57 +02:00
|
|
|
}
|
2018-10-04 22:04:10 +02:00
|
|
|
p.ReadResourceFn = func(req providers.ReadResourceRequest) providers.ReadResourceResponse {
|
|
|
|
return providers.ReadResourceResponse{NewState: req.PriorState}
|
|
|
|
}
|
2018-09-28 23:04:57 +02:00
|
|
|
|
|
|
|
// Initialize the opts
|
|
|
|
if b.ContextOpts == nil {
|
|
|
|
b.ContextOpts = &terraform.ContextOpts{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup our provider
|
|
|
|
b.ContextOpts.ProviderResolver = providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
|
|
|
name: providers.FactoryFixed(p),
|
|
|
|
},
|
|
|
|
)
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
return p
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-01 19:59:17 +01:00
|
|
|
// TestNewLocalSingle is a factory for creating a TestLocalSingleState.
|
|
|
|
// This function matches the signature required for backend/init.
|
|
|
|
func TestNewLocalSingle() backend.Backend {
|
2018-10-17 04:48:28 +02:00
|
|
|
return &TestLocalSingleState{}
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestLocalSingleState is a backend implementation that wraps Local
|
|
|
|
// and modifies it to only support single states (returns
|
|
|
|
// ErrNamedStatesNotSupported for multi-state operations).
|
|
|
|
//
|
|
|
|
// This isn't an actual use case, this is exported just to provide a
|
|
|
|
// easy way to test that behavior.
|
|
|
|
type TestLocalSingleState struct {
|
2018-10-17 04:48:28 +02:00
|
|
|
Local
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
func (b *TestLocalSingleState) State(name string) (statemgr.Full, error) {
|
2017-03-01 19:59:17 +01:00
|
|
|
if name != backend.DefaultStateName {
|
|
|
|
return nil, backend.ErrNamedStatesNotSupported
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
return b.Local.StateMgr(name)
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) States() ([]string, error) {
|
|
|
|
return nil, backend.ErrNamedStatesNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) DeleteState(string) error {
|
|
|
|
return backend.ErrNamedStatesNotSupported
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
func testTempDir(t *testing.T) string {
|
|
|
|
d, err := ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
2018-10-09 00:22:59 +02:00
|
|
|
|
|
|
|
func testStateFile(t *testing.T, path string, s *states.State) {
|
|
|
|
stateFile := statemgr.NewFilesystem(path)
|
|
|
|
stateFile.WriteState(s)
|
2018-10-17 23:05:57 +02:00
|
|
|
}
|