2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-05-18 00:26:21 +02:00
|
|
|
"errors"
|
2017-02-16 01:00:59 +01:00
|
|
|
"os"
|
2017-05-18 00:26:21 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-01-19 05:47:56 +01:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
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/zclconf/go-cty/cty"
|
|
|
|
|
backend/local: Check dependency lock consistency before any operations
In historical versions of Terraform the responsibility to check this was
inside the terraform.NewContext function, along with various other
assorted concerns that made that function particularly complicated.
More recently, we reduced the responsibility of the "terraform" package
only to instantiating particular named plugins, assuming that its caller
is responsible for selecting appropriate versions of any providers that
_are_ external. However, until this commit we were just assuming that
"terraform init" had correctly selected appropriate plugins and recorded
them in the lock file, and so nothing was dealing with the problem of
ensuring that there haven't been any changes to the lock file or config
since the most recent "terraform init" which would cause us to need to
re-evaluate those decisions.
Part of the game here is to slightly extend the role of the dependency
locks object to also carry information about a subset of provider
addresses whose lock entries we're intentionally disregarding as part of
the various little edge-case features we have for overridding providers:
dev_overrides, "unmanaged providers", and the testing overrides in our
own unit tests. This is an in-memory-only annotation, never included in
the serialized plan files on disk.
I had originally intended to create a new package to encapsulate all of
this plugin-selection logic, including both the version constraint
checking here and also the handling of the provider factory functions, but
as an interim step I've just made version constraint consistency checks
the responsibility of the backend/local package, which means that we'll
always catch problems as part of preparing for local operations, while
not imposing these additional checks on commands that _don't_ run local
operations, such as "terraform apply" when in remote operations mode.
2021-09-30 02:31:43 +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:07:38 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/command/arguments"
|
|
|
|
"github.com/hashicorp/terraform/internal/command/clistate"
|
|
|
|
"github.com/hashicorp/terraform/internal/command/views"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
backend/local: Check dependency lock consistency before any operations
In historical versions of Terraform the responsibility to check this was
inside the terraform.NewContext function, along with various other
assorted concerns that made that function particularly complicated.
More recently, we reduced the responsibility of the "terraform" package
only to instantiating particular named plugins, assuming that its caller
is responsible for selecting appropriate versions of any providers that
_are_ external. However, until this commit we were just assuming that
"terraform init" had correctly selected appropriate plugins and recorded
them in the lock file, and so nothing was dealing with the problem of
ensuring that there haven't been any changes to the lock file or config
since the most recent "terraform init" which would cause us to need to
re-evaluate those decisions.
Part of the game here is to slightly extend the role of the dependency
locks object to also carry information about a subset of provider
addresses whose lock entries we're intentionally disregarding as part of
the various little edge-case features we have for overridding providers:
dev_overrides, "unmanaged providers", and the testing overrides in our
own unit tests. This is an in-memory-only annotation, never included in
the serialized plan files on disk.
I had originally intended to create a new package to encapsulate all of
this plugin-selection logic, including both the version constraint
checking here and also the handling of the provider factory functions, but
as an interim step I've just made version constraint consistency checks
the responsibility of the backend/local package, which means that we'll
always catch problems as part of preparing for local operations, while
not imposing these additional checks on commands that _don't_ run local
operations, such as "terraform apply" when in remote operations mode.
2021-09-30 02:31:43 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/depsfile"
|
2019-01-09 03:39:14 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/initwd"
|
2021-05-17 21:33:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
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"
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/terminal"
|
2021-05-17 21:46:19 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2021-05-17 19:11:06 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocal_applyBasic(t *testing.T) {
|
2021-09-14 15:13:13 +02:00
|
|
|
b := TestLocal(t)
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2018-11-15 20:26:46 +01:00
|
|
|
p := TestLocalProvider(t, b, "test", applyFixtureSchema())
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{
|
2018-09-28 23:04:57 +02:00
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
"ami": cty.StringVal("bar"),
|
|
|
|
})}
|
2017-01-19 05:47:56 +01:00
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
op, configCleanup, done := testOperationApply(t, "./testdata/apply")
|
2018-03-21 02:43:02 +01:00
|
|
|
defer configCleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result != backend.OperationSuccess {
|
|
|
|
t.Fatal("operation failed")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
if p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should not be called")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
if !p.PlanResourceChangeCalled {
|
2017-01-19 05:47:56 +01:00
|
|
|
t.Fatal("diff should be called")
|
|
|
|
}
|
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
if !p.ApplyResourceChangeCalled {
|
2017-01-19 05:47:56 +01:00
|
|
|
t.Fatal("apply should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
checkState(t, b.StateOutPath, `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = yes
|
2020-04-01 21:07:05 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2018-09-28 23:04:57 +02:00
|
|
|
ami = bar
|
|
|
|
`)
|
2020-08-11 17:23:42 +02:00
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
if errOutput := done(t).Stderr(); errOutput != "" {
|
|
|
|
t.Fatalf("unexpected error output:\n%s", errOutput)
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 01:00:59 +01:00
|
|
|
func TestLocal_applyEmptyDir(t *testing.T) {
|
2021-09-14 15:13:13 +02:00
|
|
|
b := TestLocal(t)
|
2018-03-28 16:54:08 +02:00
|
|
|
|
2018-05-23 04:57:04 +02:00
|
|
|
p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{"id": cty.StringVal("yes")})}
|
2017-02-16 01:00:59 +01:00
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
op, configCleanup, done := testOperationApply(t, "./testdata/empty")
|
2018-03-21 02:43:02 +01:00
|
|
|
defer configCleanup()
|
2017-02-16 01:00:59 +01:00
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result == backend.OperationSuccess {
|
|
|
|
t.Fatal("operation succeeded; want error")
|
2017-02-16 01:00:59 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
if p.ApplyResourceChangeCalled {
|
2017-02-16 01:00:59 +01:00
|
|
|
t.Fatal("apply should not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(b.StateOutPath); err == nil {
|
|
|
|
t.Fatal("should not exist")
|
|
|
|
}
|
2020-08-11 17:23:42 +02:00
|
|
|
|
|
|
|
// the backend should be unlocked after a run
|
|
|
|
assertBackendStateUnlocked(t, b)
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
|
2021-02-25 16:02:23 +01:00
|
|
|
if got, want := done(t).Stderr(), "Error: No configuration files"; !strings.Contains(got, want) {
|
|
|
|
t.Fatalf("unexpected error output:\n%s\nwant: %s", got, want)
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
}
|
2017-02-16 01:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocal_applyEmptyDirDestroy(t *testing.T) {
|
2021-09-14 15:13:13 +02:00
|
|
|
b := TestLocal(t)
|
2017-02-16 01:00:59 +01:00
|
|
|
|
2018-11-15 20:26:46 +01:00
|
|
|
p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
|
2021-01-12 22:13:10 +01:00
|
|
|
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{}
|
2017-02-16 01:00:59 +01:00
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
op, configCleanup, done := testOperationApply(t, "./testdata/empty")
|
2018-03-21 02:43:02 +01:00
|
|
|
defer configCleanup()
|
2021-04-06 01:28:59 +02:00
|
|
|
op.PlanMode = plans.DestroyMode
|
2017-02-16 01:00:59 +01:00
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result != backend.OperationSuccess {
|
|
|
|
t.Fatalf("apply operation failed")
|
2017-02-16 01:00:59 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
if p.ApplyResourceChangeCalled {
|
2017-02-16 01:00:59 +01:00
|
|
|
t.Fatal("apply should not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
checkState(t, b.StateOutPath, `<no state>`)
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
|
|
|
|
if errOutput := done(t).Stderr(); errOutput != "" {
|
|
|
|
t.Fatalf("unexpected error output:\n%s", errOutput)
|
|
|
|
}
|
2017-02-16 01:00:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
func TestLocal_applyError(t *testing.T) {
|
2021-09-14 15:13:13 +02:00
|
|
|
b := TestLocal(t)
|
2018-11-15 20:26:46 +01:00
|
|
|
|
2019-02-08 21:48:32 +01:00
|
|
|
schema := &terraform.ProviderSchema{
|
2018-10-02 23:58:50 +02:00
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
"id": {Type: cty.String, Computed: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-02-08 21:48:32 +01:00
|
|
|
p := TestLocalProvider(t, b, "test", schema)
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
var lock sync.Mutex
|
|
|
|
errored := false
|
2018-10-02 23:15:07 +02:00
|
|
|
p.ApplyResourceChangeFn = func(
|
|
|
|
r providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
2018-10-02 23:15:07 +02:00
|
|
|
var diags tfdiags.Diagnostics
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2018-10-02 23:15:07 +02:00
|
|
|
ami := r.Config.GetAttr("ami").AsString()
|
|
|
|
if !errored && ami == "error" {
|
2017-01-19 05:47:56 +01:00
|
|
|
errored = true
|
2021-02-25 16:02:23 +01:00
|
|
|
diags = diags.Append(errors.New("ami error"))
|
2018-10-02 23:15:07 +02:00
|
|
|
return providers.ApplyResourceChangeResponse{
|
|
|
|
Diagnostics: diags,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return providers.ApplyResourceChangeResponse{
|
|
|
|
Diagnostics: diags,
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("foo"),
|
|
|
|
"ami": cty.StringVal("bar"),
|
|
|
|
}),
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
op, configCleanup, done := testOperationApply(t, "./testdata/apply-error")
|
2018-03-21 02:43:02 +01:00
|
|
|
defer configCleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result == backend.OperationSuccess {
|
|
|
|
t.Fatal("operation succeeded; want failure")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
checkState(t, b.StateOutPath, `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = foo
|
2020-04-01 21:07:05 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2018-10-02 23:15:07 +02:00
|
|
|
ami = bar
|
2017-01-19 05:47:56 +01:00
|
|
|
`)
|
2020-08-11 17:23:42 +02:00
|
|
|
|
|
|
|
// the backend should be unlocked after a run
|
|
|
|
assertBackendStateUnlocked(t, b)
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
|
2021-02-25 16:02:23 +01:00
|
|
|
if got, want := done(t).Stderr(), "Error: ami error"; !strings.Contains(got, want) {
|
|
|
|
t.Fatalf("unexpected error output:\n%s\nwant: %s", got, want)
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2017-05-18 00:26:21 +02:00
|
|
|
func TestLocal_applyBackendFail(t *testing.T) {
|
2021-09-14 15:13:13 +02:00
|
|
|
b := TestLocal(t)
|
2018-03-28 16:54:08 +02:00
|
|
|
|
2018-10-03 01:21:33 +02:00
|
|
|
p := TestLocalProvider(t, b, "test", applyFixtureSchema())
|
2021-05-17 22:09:24 +02:00
|
|
|
|
|
|
|
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
"ami": cty.StringVal("bar"),
|
|
|
|
}),
|
|
|
|
Diagnostics: tfdiags.Diagnostics.Append(nil, errors.New("error before backend failure")),
|
|
|
|
}
|
2018-10-03 01:21:33 +02:00
|
|
|
|
2017-05-18 00:26:21 +02:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get current working directory")
|
|
|
|
}
|
|
|
|
err = os.Chdir(filepath.Dir(b.StatePath))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to set temporary working directory")
|
|
|
|
}
|
|
|
|
defer os.Chdir(wd)
|
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
op, configCleanup, done := testOperationApply(t, wd+"/testdata/apply")
|
2018-10-03 01:21:33 +02:00
|
|
|
defer configCleanup()
|
|
|
|
|
2017-05-18 00:26:21 +02:00
|
|
|
b.Backend = &backendWithFailingState{}
|
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2021-02-25 16:02:23 +01:00
|
|
|
|
|
|
|
output := done(t)
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result == backend.OperationSuccess {
|
2017-05-18 00:26:21 +02:00
|
|
|
t.Fatalf("apply succeeded; want error")
|
|
|
|
}
|
|
|
|
|
2021-02-25 16:02:23 +01:00
|
|
|
diagErr := output.Stderr()
|
2021-05-17 22:09:24 +02:00
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
if !strings.Contains(diagErr, "Error saving state: fake failure") {
|
|
|
|
t.Fatalf("missing \"fake failure\" message in diags:\n%s", diagErr)
|
2017-05-18 00:26:21 +02:00
|
|
|
}
|
|
|
|
|
2021-05-17 22:09:24 +02:00
|
|
|
if !strings.Contains(diagErr, "error before backend failure") {
|
|
|
|
t.Fatalf("missing 'error before backend failure' diagnostic from apply")
|
|
|
|
}
|
|
|
|
|
2017-05-18 00:26:21 +02:00
|
|
|
// The fallback behavior should've created a file errored.tfstate in the
|
|
|
|
// current working directory.
|
|
|
|
checkState(t, "errored.tfstate", `
|
2021-05-17 22:09:24 +02:00
|
|
|
test_instance.foo: (tainted)
|
2017-05-18 00:26:21 +02:00
|
|
|
ID = yes
|
2020-04-01 21:07:05 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2018-10-03 01:21:33 +02:00
|
|
|
ami = bar
|
2017-05-18 00:26:21 +02:00
|
|
|
`)
|
2020-08-11 17:23:42 +02:00
|
|
|
|
|
|
|
// the backend should be unlocked after a run
|
|
|
|
assertBackendStateUnlocked(t, b)
|
2017-05-18 00:26:21 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 15:44:22 +01:00
|
|
|
func TestLocal_applyRefreshFalse(t *testing.T) {
|
2021-09-14 15:13:13 +02:00
|
|
|
b := TestLocal(t)
|
2020-12-10 15:44:22 +01:00
|
|
|
|
|
|
|
p := TestLocalProvider(t, b, "test", planFixtureSchema())
|
|
|
|
testStateFile(t, b.StatePath, testPlanState())
|
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
op, configCleanup, done := testOperationApply(t, "./testdata/plan")
|
2020-12-10 15:44:22 +01:00
|
|
|
defer configCleanup()
|
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
|
|
|
if run.Result != backend.OperationSuccess {
|
|
|
|
t.Fatalf("plan operation failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should not be called")
|
|
|
|
}
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
|
|
|
|
if errOutput := done(t).Stderr(); errOutput != "" {
|
|
|
|
t.Fatalf("unexpected error output:\n%s", errOutput)
|
|
|
|
}
|
2020-12-10 15:44:22 +01:00
|
|
|
}
|
|
|
|
|
2017-05-18 00:26:21 +02:00
|
|
|
type backendWithFailingState struct {
|
|
|
|
Local
|
|
|
|
}
|
|
|
|
|
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 *backendWithFailingState) StateMgr(name string) (statemgr.Full, error) {
|
2017-05-18 00:26:21 +02:00
|
|
|
return &failingState{
|
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
|
|
|
statemgr.NewFilesystem("failing-state.tfstate"),
|
2017-05-18 00:26:21 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type failingState struct {
|
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
|
|
|
*statemgr.Filesystem
|
2017-05-18 00:26:21 +02: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 (s failingState) WriteState(state *states.State) error {
|
2017-05-18 00:26:21 +02:00
|
|
|
return errors.New("fake failure")
|
|
|
|
}
|
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
func testOperationApply(t *testing.T, configDir string) (*backend.Operation, func(), func(*testing.T) *terminal.TestOutput) {
|
2018-03-21 02:43:02 +01:00
|
|
|
t.Helper()
|
|
|
|
|
2019-01-09 03:39:14 +01:00
|
|
|
_, configLoader, configCleanup := initwd.MustLoadConfigForTests(t, configDir)
|
2018-03-21 02:43:02 +01:00
|
|
|
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
view := views.NewOperation(arguments.ViewHuman, false, views.NewView(streams))
|
|
|
|
|
backend/local: Check dependency lock consistency before any operations
In historical versions of Terraform the responsibility to check this was
inside the terraform.NewContext function, along with various other
assorted concerns that made that function particularly complicated.
More recently, we reduced the responsibility of the "terraform" package
only to instantiating particular named plugins, assuming that its caller
is responsible for selecting appropriate versions of any providers that
_are_ external. However, until this commit we were just assuming that
"terraform init" had correctly selected appropriate plugins and recorded
them in the lock file, and so nothing was dealing with the problem of
ensuring that there haven't been any changes to the lock file or config
since the most recent "terraform init" which would cause us to need to
re-evaluate those decisions.
Part of the game here is to slightly extend the role of the dependency
locks object to also carry information about a subset of provider
addresses whose lock entries we're intentionally disregarding as part of
the various little edge-case features we have for overridding providers:
dev_overrides, "unmanaged providers", and the testing overrides in our
own unit tests. This is an in-memory-only annotation, never included in
the serialized plan files on disk.
I had originally intended to create a new package to encapsulate all of
this plugin-selection logic, including both the version constraint
checking here and also the handling of the provider factory functions, but
as an interim step I've just made version constraint consistency checks
the responsibility of the backend/local package, which means that we'll
always catch problems as part of preparing for local operations, while
not imposing these additional checks on commands that _don't_ run local
operations, such as "terraform apply" when in remote operations mode.
2021-09-30 02:31:43 +02:00
|
|
|
// Many of our tests use an overridden "test" provider that's just in-memory
|
|
|
|
// inside the test process, not a separate plugin on disk.
|
|
|
|
depLocks := depsfile.NewLocks()
|
|
|
|
depLocks.SetProviderOverridden(addrs.MustParseProviderSourceString("registry.terraform.io/hashicorp/test"))
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
return &backend.Operation{
|
backend/local: Check dependency lock consistency before any operations
In historical versions of Terraform the responsibility to check this was
inside the terraform.NewContext function, along with various other
assorted concerns that made that function particularly complicated.
More recently, we reduced the responsibility of the "terraform" package
only to instantiating particular named plugins, assuming that its caller
is responsible for selecting appropriate versions of any providers that
_are_ external. However, until this commit we were just assuming that
"terraform init" had correctly selected appropriate plugins and recorded
them in the lock file, and so nothing was dealing with the problem of
ensuring that there haven't been any changes to the lock file or config
since the most recent "terraform init" which would cause us to need to
re-evaluate those decisions.
Part of the game here is to slightly extend the role of the dependency
locks object to also carry information about a subset of provider
addresses whose lock entries we're intentionally disregarding as part of
the various little edge-case features we have for overridding providers:
dev_overrides, "unmanaged providers", and the testing overrides in our
own unit tests. This is an in-memory-only annotation, never included in
the serialized plan files on disk.
I had originally intended to create a new package to encapsulate all of
this plugin-selection logic, including both the version constraint
checking here and also the handling of the provider factory functions, but
as an interim step I've just made version constraint consistency checks
the responsibility of the backend/local package, which means that we'll
always catch problems as part of preparing for local operations, while
not imposing these additional checks on commands that _don't_ run local
operations, such as "terraform apply" when in remote operations mode.
2021-09-30 02:31:43 +02:00
|
|
|
Type: backend.OperationTypeApply,
|
|
|
|
ConfigDir: configDir,
|
|
|
|
ConfigLoader: configLoader,
|
|
|
|
StateLocker: clistate.NewNoopLocker(),
|
|
|
|
View: view,
|
|
|
|
DependencyLocks: depLocks,
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
}, configCleanup, done
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 04:57:04 +02:00
|
|
|
// applyFixtureSchema returns a schema suitable for processing the
|
2019-06-30 09:38:36 +02:00
|
|
|
// configuration in testdata/apply . This schema should be
|
2018-05-23 04:57:04 +02:00
|
|
|
// assigned to a mock provider named "test".
|
|
|
|
func applyFixtureSchema() *terraform.ProviderSchema {
|
|
|
|
return &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
2018-09-28 23:04:57 +02:00
|
|
|
"id": {Type: cty.String, Computed: true},
|
2018-05-23 04:57:04 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|