2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2019-02-08 21:48:32 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2021-05-17 19:40:40 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/providers"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
|
|
|
"github.com/hashicorp/terraform/internal/states/statemgr"
|
2021-05-17 21:46:19 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
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.
|
2021-09-14 15:13:13 +02:00
|
|
|
func TestLocal(t *testing.T) *Local {
|
2018-03-21 02:43:02 +01:00
|
|
|
t.Helper()
|
2021-09-14 15:13:13 +02:00
|
|
|
tempDir, err := filepath.EvalSymlinks(t.TempDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-10-31 16:45:03 +01:00
|
|
|
|
|
|
|
local := New()
|
|
|
|
local.StatePath = filepath.Join(tempDir, "state.tfstate")
|
|
|
|
local.StateOutPath = filepath.Join(tempDir, "state.tfstate")
|
|
|
|
local.StateBackupPath = filepath.Join(tempDir, "state.tfstate.bak")
|
|
|
|
local.StateWorkspaceDir = filepath.Join(tempDir, "state.tfstate.d")
|
|
|
|
local.ContextOpts = &terraform.ContextOpts{}
|
|
|
|
|
2021-09-14 15:13:13 +02:00
|
|
|
return local
|
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)
|
2019-02-08 21:48:32 +01:00
|
|
|
|
|
|
|
if schema == nil {
|
|
|
|
schema = &terraform.ProviderSchema{} // default schema is empty
|
|
|
|
}
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
Provider: providers.Schema{Block: schema.Provider},
|
|
|
|
ProviderMeta: providers.Schema{Block: schema.ProviderMeta},
|
|
|
|
ResourceTypes: map[string]providers.Schema{},
|
|
|
|
DataSources: map[string]providers.Schema{},
|
|
|
|
}
|
|
|
|
for name, res := range schema.ResourceTypes {
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse.ResourceTypes[name] = providers.Schema{
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: res,
|
|
|
|
Version: int64(schema.ResourceTypeSchemaVersions[name]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for name, dat := range schema.DataSources {
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse.DataSources[name] = providers.Schema{Block: dat}
|
2021-01-12 22:13:10 +01:00
|
|
|
}
|
2019-02-08 21:48:32 +01:00
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
2019-02-08 21:48:32 +01:00
|
|
|
rSchema, _ := schema.SchemaForResourceType(addrs.ManagedResourceMode, req.TypeName)
|
|
|
|
if rSchema == nil {
|
|
|
|
rSchema = &configschema.Block{} // default schema is empty
|
|
|
|
}
|
|
|
|
plannedVals := map[string]cty.Value{}
|
|
|
|
for name, attrS := range rSchema.Attributes {
|
|
|
|
val := req.ProposedNewState.GetAttr(name)
|
|
|
|
if attrS.Computed && val.IsNull() {
|
|
|
|
val = cty.UnknownVal(attrS.Type)
|
|
|
|
}
|
|
|
|
plannedVals[name] = val
|
|
|
|
}
|
|
|
|
for name := range rSchema.BlockTypes {
|
|
|
|
// For simplicity's sake we just copy the block attributes over
|
|
|
|
// verbatim, since this package's mock providers are all relatively
|
|
|
|
// simple -- we're testing the backend, not esoteric provider features.
|
|
|
|
plannedVals[name] = req.ProposedNewState.GetAttr(name)
|
|
|
|
}
|
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
return providers.PlanResourceChangeResponse{
|
2019-02-08 21:48:32 +01:00
|
|
|
PlannedState: cty.ObjectVal(plannedVals),
|
2018-09-28 23:04:57 +02:00
|
|
|
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-12-12 17:01:18 +01:00
|
|
|
p.ReadDataSourceFn = func(req providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
|
|
|
|
return providers.ReadDataSourceResponse{State: req.Config}
|
|
|
|
}
|
2018-09-28 23:04:57 +02:00
|
|
|
|
|
|
|
// Initialize the opts
|
|
|
|
if b.ContextOpts == nil {
|
|
|
|
b.ContextOpts = &terraform.ContextOpts{}
|
|
|
|
}
|
|
|
|
|
2021-01-26 20:39:11 +01:00
|
|
|
// Set up our provider
|
2020-03-31 00:30:56 +02:00
|
|
|
b.ContextOpts.Providers = map[addrs.Provider]providers.Factory{
|
2020-04-01 21:07:05 +02:00
|
|
|
addrs.NewDefaultProvider(name): providers.FactoryFixed(p),
|
2020-03-31 00:30:56 +02:00
|
|
|
}
|
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
|
|
|
// TestLocalSingleState is a backend implementation that wraps Local
|
|
|
|
// and modifies it to only support single states (returns
|
2018-10-31 16:45:03 +01:00
|
|
|
// ErrWorkspacesNotSupported for multi-state operations).
|
2017-03-01 19:59:17 +01:00
|
|
|
//
|
|
|
|
// 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-31 16:45:03 +01:00
|
|
|
*Local
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// TestNewLocalSingle is a factory for creating a TestLocalSingleState.
|
|
|
|
// This function matches the signature required for backend/init.
|
|
|
|
func TestNewLocalSingle() backend.Backend {
|
|
|
|
return &TestLocalSingleState{Local: New()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) Workspaces() ([]string, error) {
|
|
|
|
return nil, backend.ErrWorkspacesNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) DeleteWorkspace(string) error {
|
|
|
|
return backend.ErrWorkspacesNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) StateMgr(name string) (statemgr.Full, error) {
|
2017-03-01 19:59:17 +01:00
|
|
|
if name != backend.DefaultStateName {
|
2018-10-31 16:45:03 +01:00
|
|
|
return nil, backend.ErrWorkspacesNotSupported
|
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
|
|
|
return b.Local.StateMgr(name)
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// TestLocalNoDefaultState is a backend implementation that wraps
|
|
|
|
// Local and modifies it to support named states, but not the
|
|
|
|
// default state. It returns ErrDefaultWorkspaceNotSupported when
|
|
|
|
// the DefaultStateName is used.
|
|
|
|
type TestLocalNoDefaultState struct {
|
|
|
|
*Local
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// TestNewLocalNoDefault is a factory for creating a TestLocalNoDefaultState.
|
|
|
|
// This function matches the signature required for backend/init.
|
|
|
|
func TestNewLocalNoDefault() backend.Backend {
|
|
|
|
return &TestLocalNoDefaultState{Local: New()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalNoDefaultState) Workspaces() ([]string, error) {
|
|
|
|
workspaces, err := b.Local.Workspaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filtered := workspaces[:0]
|
|
|
|
for _, name := range workspaces {
|
|
|
|
if name != backend.DefaultStateName {
|
|
|
|
filtered = append(filtered, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalNoDefaultState) DeleteWorkspace(name string) error {
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return backend.ErrDefaultWorkspaceNotSupported
|
|
|
|
}
|
|
|
|
return b.Local.DeleteWorkspace(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalNoDefaultState) StateMgr(name string) (statemgr.Full, error) {
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return nil, backend.ErrDefaultWorkspaceNotSupported
|
|
|
|
}
|
|
|
|
return b.Local.StateMgr(name)
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-04-01 21:07:05 +02:00
|
|
|
|
|
|
|
func mustProviderConfig(s string) addrs.AbsProviderConfig {
|
|
|
|
p, diags := addrs.ParseAbsProviderConfigStr(s)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Err())
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustResourceInstanceAddr(s string) addrs.AbsResourceInstance {
|
|
|
|
addr, diags := addrs.ParseAbsResourceInstanceStr(s)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Err())
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
2020-08-11 17:23:42 +02:00
|
|
|
|
|
|
|
// assertBackendStateUnlocked attempts to lock the backend state. Failure
|
|
|
|
// indicates that the state was indeed locked and therefore this function will
|
|
|
|
// return true.
|
|
|
|
func assertBackendStateUnlocked(t *testing.T, b *Local) bool {
|
|
|
|
t.Helper()
|
|
|
|
stateMgr, _ := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if _, err := stateMgr.Lock(statemgr.NewLockInfo()); err != nil {
|
|
|
|
t.Errorf("state is already locked: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertBackendStateLocked attempts to lock the backend state. Failure
|
|
|
|
// indicates that the state was already locked and therefore this function will
|
|
|
|
// return false.
|
|
|
|
func assertBackendStateLocked(t *testing.T, b *Local) bool {
|
|
|
|
t.Helper()
|
|
|
|
stateMgr, _ := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if _, err := stateMgr.Lock(statemgr.NewLockInfo()); err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
t.Error("unexpected success locking state")
|
|
|
|
return true
|
|
|
|
}
|