2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-01-30 04:51:54 +01:00
|
|
|
"strings"
|
2017-01-19 05:47:56 +01:00
|
|
|
"testing"
|
|
|
|
|
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: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"
|
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"
|
|
|
|
"github.com/hashicorp/terraform/internal/plans/planfile"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
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"
|
2018-05-23 04:57:04 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocal_planBasic(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2018-05-23 04:57:04 +02:00
|
|
|
p := TestLocalProvider(t, b, "test", planFixtureSchema())
|
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 := testOperationPlan(t, "./testdata/plan")
|
2018-03-21 02:43:02 +01:00
|
|
|
defer configCleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
op.PlanRefresh = true
|
|
|
|
|
|
|
|
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("plan operation failed")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
if !p.PlanResourceChangeCalled {
|
|
|
|
t.Fatal("PlanResourceChange should be called")
|
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
|
|
|
|
|
|
|
if errOutput := done(t).Stderr(); errOutput != "" {
|
|
|
|
t.Fatalf("unexpected error output:\n%s", errOutput)
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2017-09-09 02:14:37 +02:00
|
|
|
func TestLocal_planInAutomation(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2018-05-23 04:57:04 +02:00
|
|
|
TestLocalProvider(t, b, "test", planFixtureSchema())
|
2017-09-09 02:14:37 +02:00
|
|
|
|
2021-01-12 03:29:39 +01:00
|
|
|
const msg = `You didn't use the -out option`
|
2017-09-09 02:14:37 +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
|
|
|
// When we're "in automation" we omit certain text from the plan output.
|
|
|
|
// However, the responsibility for this omission is in the view, so here we
|
|
|
|
// test for its presence while the "in automation" setting is false, to
|
|
|
|
// validate that we are calling the correct view method.
|
|
|
|
//
|
|
|
|
// Ideally this test would be replaced by a call-logging mock view, but
|
|
|
|
// that's future work.
|
|
|
|
op, configCleanup, done := testOperationPlan(t, "./testdata/plan")
|
|
|
|
defer configCleanup()
|
|
|
|
op.PlanRefresh = true
|
2017-09-09 02:14:37 +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
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2017-09-09 02:14:37 +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
|
|
|
<-run.Done()
|
|
|
|
if run.Result != backend.OperationSuccess {
|
|
|
|
t.Fatalf("plan operation failed")
|
2017-09-09 02:14:37 +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 output := done(t).Stdout(); !strings.Contains(output, msg) {
|
|
|
|
t.Fatalf("missing next-steps message when not in automation\nwant: %s\noutput:\n%s", msg, output)
|
|
|
|
}
|
2017-09-09 02:14:37 +02:00
|
|
|
}
|
|
|
|
|
2017-01-30 04:51:54 +01:00
|
|
|
func TestLocal_planNoConfig(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2018-05-23 04:57:04 +02:00
|
|
|
TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
|
2017-01-30 04:51:54 +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 := testOperationPlan(t, "./testdata/empty")
|
2018-03-21 02:43:02 +01:00
|
|
|
defer configCleanup()
|
2017-01-30 04:51:54 +01:00
|
|
|
op.PlanRefresh = true
|
|
|
|
|
|
|
|
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 {
|
|
|
|
t.Fatal("plan operation succeeded; want failure")
|
2017-01-30 04:51:54 +01:00
|
|
|
}
|
2021-02-12 19:59:14 +01:00
|
|
|
|
2021-02-25 16:02:23 +01:00
|
|
|
if stderr := output.Stderr(); !strings.Contains(stderr, "No configuration files") {
|
|
|
|
t.Fatalf("bad: %s", stderr)
|
2017-01-30 04:51:54 +01:00
|
|
|
}
|
2020-08-11 17:23:42 +02:00
|
|
|
|
|
|
|
// the backend should be unlocked after a run
|
|
|
|
assertBackendStateUnlocked(t, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test validates the state lacking behavior when the inner call to
|
|
|
|
// Context() fails
|
|
|
|
func TestLocal_plan_context_error(t *testing.T) {
|
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2021-09-14 18:47:24 +02:00
|
|
|
// This is an intentionally-invalid value to make terraform.NewContext fail
|
|
|
|
// when b.Operation calls it.
|
|
|
|
// NOTE: This test was originally using a provider initialization failure
|
|
|
|
// as its forced error condition, but terraform.NewContext is no longer
|
|
|
|
// responsible for checking that. Invalid parallelism is the last situation
|
|
|
|
// where terraform.NewContext can return error diagnostics, and arguably
|
|
|
|
// we should be validating this argument at the UI layer anyway, so perhaps
|
|
|
|
// in future we'll make terraform.NewContext never return errors and then
|
|
|
|
// this test will become redundant, because its purpose is specifically
|
|
|
|
// to test that we properly unlock the state if terraform.NewContext
|
|
|
|
// returns an error.
|
|
|
|
if b.ContextOpts == nil {
|
|
|
|
b.ContextOpts = &terraform.ContextOpts{}
|
|
|
|
}
|
|
|
|
b.ContextOpts.Parallelism = -1
|
|
|
|
|
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 := testOperationPlan(t, "./testdata/plan")
|
2020-08-11 17:23:42 +02:00
|
|
|
defer configCleanup()
|
|
|
|
|
|
|
|
// we coerce a failure in Context() by omitting the provider schema
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
|
|
|
if run.Result != backend.OperationFailure {
|
|
|
|
t.Fatalf("plan operation succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-14 18:47:24 +02:00
|
|
|
if got, want := done(t).Stderr(), "Error: Invalid parallelism value"; !strings.Contains(got, want) {
|
2021-02-25 16:02:23 +01:00
|
|
|
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-30 04:51:54 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:59:06 +02:00
|
|
|
func TestLocal_planOutputsChanged(t *testing.T) {
|
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
testStateFile(t, b.StatePath, states.BuildState(func(ss *states.SyncState) {
|
|
|
|
ss.SetOutputValue(addrs.AbsOutputValue{
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
OutputValue: addrs.OutputValue{Name: "changed"},
|
|
|
|
}, cty.StringVal("before"), false)
|
|
|
|
ss.SetOutputValue(addrs.AbsOutputValue{
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
OutputValue: addrs.OutputValue{Name: "sensitive_before"},
|
|
|
|
}, cty.StringVal("before"), true)
|
|
|
|
ss.SetOutputValue(addrs.AbsOutputValue{
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
OutputValue: addrs.OutputValue{Name: "sensitive_after"},
|
|
|
|
}, cty.StringVal("before"), false)
|
|
|
|
ss.SetOutputValue(addrs.AbsOutputValue{
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
OutputValue: addrs.OutputValue{Name: "removed"}, // not present in the config fixture
|
|
|
|
}, cty.StringVal("before"), false)
|
|
|
|
ss.SetOutputValue(addrs.AbsOutputValue{
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
OutputValue: addrs.OutputValue{Name: "unchanged"},
|
|
|
|
}, cty.StringVal("before"), false)
|
|
|
|
// NOTE: This isn't currently testing the situation where the new
|
|
|
|
// value of an output is unknown, because to do that requires there to
|
|
|
|
// be at least one managed resource Create action in the plan and that
|
|
|
|
// would defeat the point of this test, which is to ensure that a
|
|
|
|
// plan containing only output changes is considered "non-empty".
|
|
|
|
// For now we're not too worried about testing the "new value is
|
|
|
|
// unknown" situation because that's already common for printing out
|
|
|
|
// resource changes and we already have many tests for that.
|
|
|
|
}))
|
2021-09-13 19:22:36 +02:00
|
|
|
outDir := t.TempDir()
|
2020-05-27 01:59:06 +02:00
|
|
|
defer os.RemoveAll(outDir)
|
|
|
|
planPath := filepath.Join(outDir, "plan.tfplan")
|
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 := testOperationPlan(t, "./testdata/plan-outputs-changed")
|
2020-05-27 01:59:06 +02:00
|
|
|
defer configCleanup()
|
|
|
|
op.PlanRefresh = true
|
|
|
|
op.PlanOutPath = planPath
|
|
|
|
cfg := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"path": cty.StringVal(b.StatePath),
|
|
|
|
})
|
|
|
|
cfgRaw, err := plans.NewDynamicValue(cfg, cfg.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
op.PlanOutBackend = &plans.Backend{
|
|
|
|
// Just a placeholder so that we can generate a valid plan file.
|
|
|
|
Type: "local",
|
|
|
|
Config: cfgRaw,
|
|
|
|
}
|
|
|
|
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 run.PlanEmpty {
|
2021-05-07 00:22:48 +02:00
|
|
|
t.Error("plan should not be empty")
|
2020-05-27 01:59:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := strings.TrimSpace(`
|
|
|
|
Changes to Outputs:
|
|
|
|
+ added = "after"
|
|
|
|
~ changed = "before" -> "after"
|
|
|
|
- removed = "before" -> null
|
|
|
|
~ sensitive_after = (sensitive value)
|
|
|
|
~ sensitive_before = (sensitive value)
|
2021-05-07 00:22:48 +02:00
|
|
|
|
|
|
|
You can apply this plan to save these new output values to the Terraform
|
|
|
|
state, without changing any real infrastructure.
|
2020-05-27 01:59:06 +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 output := done(t).Stdout(); !strings.Contains(output, expectedOutput) {
|
2021-05-07 00:22:48 +02:00
|
|
|
t.Errorf("Unexpected output:\n%s\n\nwant output containing:\n%s", output, expectedOutput)
|
2020-05-27 01:59:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 22:11:57 +01:00
|
|
|
// Module outputs should not cause the plan to be rendered
|
|
|
|
func TestLocal_planModuleOutputsChanged(t *testing.T) {
|
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
testStateFile(t, b.StatePath, states.BuildState(func(ss *states.SyncState) {
|
|
|
|
ss.SetOutputValue(addrs.AbsOutputValue{
|
|
|
|
Module: addrs.RootModuleInstance.Child("mod", addrs.NoKey),
|
|
|
|
OutputValue: addrs.OutputValue{Name: "changed"},
|
|
|
|
}, cty.StringVal("before"), false)
|
|
|
|
}))
|
2021-09-13 19:22:36 +02:00
|
|
|
outDir := t.TempDir()
|
2020-11-17 22:11:57 +01:00
|
|
|
defer os.RemoveAll(outDir)
|
|
|
|
planPath := filepath.Join(outDir, "plan.tfplan")
|
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 := testOperationPlan(t, "./testdata/plan-module-outputs-changed")
|
2020-11-17 22:11:57 +01:00
|
|
|
defer configCleanup()
|
|
|
|
op.PlanRefresh = true
|
|
|
|
op.PlanOutPath = planPath
|
|
|
|
cfg := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"path": cty.StringVal(b.StatePath),
|
|
|
|
})
|
|
|
|
cfgRaw, err := plans.NewDynamicValue(cfg, cfg.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
op.PlanOutBackend = &plans.Backend{
|
|
|
|
Type: "local",
|
|
|
|
Config: cfgRaw,
|
|
|
|
}
|
|
|
|
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 !run.PlanEmpty {
|
|
|
|
t.Fatal("plan should be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := strings.TrimSpace(`
|
2021-05-07 00:22:48 +02:00
|
|
|
No changes. Your infrastructure matches the configuration.
|
2020-11-17 22:11:57 +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
|
|
|
if output := done(t).Stdout(); !strings.Contains(output, expectedOutput) {
|
2020-11-17 22:11:57 +01:00
|
|
|
t.Fatalf("Unexpected output:\n%s\n\nwant output containing:\n%s", output, expectedOutput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:45:47 +01:00
|
|
|
func TestLocal_planTainted(t *testing.T) {
|
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
p := TestLocalProvider(t, b, "test", planFixtureSchema())
|
|
|
|
testStateFile(t, b.StatePath, testPlanState_tainted())
|
2021-09-13 19:22:36 +02:00
|
|
|
outDir := t.TempDir()
|
2018-12-14 14:45:47 +01:00
|
|
|
planPath := filepath.Join(outDir, "plan.tfplan")
|
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 := testOperationPlan(t, "./testdata/plan")
|
2018-12-14 14:45:47 +01:00
|
|
|
defer configCleanup()
|
|
|
|
op.PlanRefresh = true
|
|
|
|
op.PlanOutPath = planPath
|
|
|
|
cfg := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"path": cty.StringVal(b.StatePath),
|
|
|
|
})
|
|
|
|
cfgRaw, err := plans.NewDynamicValue(cfg, cfg.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
op.PlanOutBackend = &plans.Backend{
|
|
|
|
// Just a placeholder so that we can generate a valid plan file.
|
|
|
|
Type: "local",
|
|
|
|
Config: cfgRaw,
|
|
|
|
}
|
|
|
|
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 be called")
|
|
|
|
}
|
|
|
|
if run.PlanEmpty {
|
|
|
|
t.Fatal("plan should not be empty")
|
|
|
|
}
|
|
|
|
|
2021-01-12 03:29:39 +01:00
|
|
|
expectedOutput := `Terraform used the selected providers to generate the following execution
|
|
|
|
plan. Resource actions are indicated with the following symbols:
|
2018-12-14 14:45:47 +01:00
|
|
|
-/+ destroy and then create replacement
|
|
|
|
|
|
|
|
Terraform will perform the following actions:
|
|
|
|
|
2019-03-06 01:18:55 +01:00
|
|
|
# test_instance.foo is tainted, so must be replaced
|
2018-12-14 14:45:47 +01:00
|
|
|
-/+ resource "test_instance" "foo" {
|
command: Add experimental concise diff renderer
When rendering a diff between current state and projected state, we only
show resources and outputs which have changes. However, we show a full
structural diff for these values, which includes all attributes and
blocks for a changed resource or output. The result can be a very long
diff, which makes it difficult to verify what the changed fields are.
This commit adds an experimental concise diff renderer, which suppresses
most unchanged fields, only displaying the most relevant changes and
some identifying context. This means:
- Always show all identifying attributes, initially defined as `id`,
`name`, and `tags`, even if unchanged;
- Only show changed, added, or removed primitive values: `string`,
`number`, or `bool`;
- Only show added or removed elements in unordered collections and
structural types: `map`, `set`, and `object`;
- Show added or removed elements with any surrounding unchanged elements
for sequence types: `list` and `tuple`;
- Only show added or removed nested blocks, or blocks with changed
attributes.
If any attributes, collection elements, or blocks are hidden, a count
is kept and displayed at the end of the parent scope. This ensures that
it is clear that the diff is only displaying a subset of the resource.
The experiment is currently enabled by default, but can be disabled by
setting the TF_X_CONCISE_DIFF environment variable to 0.
2020-08-19 22:47:56 +02:00
|
|
|
# (1 unchanged attribute hidden)
|
2018-12-14 14:45:47 +01:00
|
|
|
|
command: Add experimental concise diff renderer
When rendering a diff between current state and projected state, we only
show resources and outputs which have changes. However, we show a full
structural diff for these values, which includes all attributes and
blocks for a changed resource or output. The result can be a very long
diff, which makes it difficult to verify what the changed fields are.
This commit adds an experimental concise diff renderer, which suppresses
most unchanged fields, only displaying the most relevant changes and
some identifying context. This means:
- Always show all identifying attributes, initially defined as `id`,
`name`, and `tags`, even if unchanged;
- Only show changed, added, or removed primitive values: `string`,
`number`, or `bool`;
- Only show added or removed elements in unordered collections and
structural types: `map`, `set`, and `object`;
- Show added or removed elements with any surrounding unchanged elements
for sequence types: `list` and `tuple`;
- Only show added or removed nested blocks, or blocks with changed
attributes.
If any attributes, collection elements, or blocks are hidden, a count
is kept and displayed at the end of the parent scope. This ensures that
it is clear that the diff is only displaying a subset of the resource.
The experiment is currently enabled by default, but can be disabled by
setting the TF_X_CONCISE_DIFF environment variable to 0.
2020-08-19 22:47:56 +02:00
|
|
|
# (1 unchanged block hidden)
|
2018-12-14 14:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Plan: 1 to add, 0 to change, 1 to destroy.`
|
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 output := done(t).Stdout(); !strings.Contains(output, expectedOutput) {
|
2021-05-07 00:22:48 +02:00
|
|
|
t.Fatalf("Unexpected output\ngot\n%s\n\nwant:\n%s", output, expectedOutput)
|
2018-12-14 14:45:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 23:50:32 +02:00
|
|
|
func TestLocal_planDeposedOnly(t *testing.T) {
|
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
p := TestLocalProvider(t, b, "test", planFixtureSchema())
|
|
|
|
testStateFile(t, b.StatePath, states.BuildState(func(ss *states.SyncState) {
|
|
|
|
ss.SetResourceInstanceDeposed(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
states.DeposedKey("00000000"),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{
|
|
|
|
"ami": "bar",
|
|
|
|
"network_interface": [{
|
|
|
|
"device_index": 0,
|
|
|
|
"description": "Main network interface"
|
|
|
|
}]
|
|
|
|
}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:07:05 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-06-03 23:50:32 +02:00
|
|
|
)
|
|
|
|
}))
|
2021-09-13 19:22:36 +02:00
|
|
|
outDir := t.TempDir()
|
2019-06-03 23:50:32 +02:00
|
|
|
planPath := filepath.Join(outDir, "plan.tfplan")
|
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 := testOperationPlan(t, "./testdata/plan")
|
2019-06-03 23:50:32 +02:00
|
|
|
defer configCleanup()
|
|
|
|
op.PlanRefresh = true
|
|
|
|
op.PlanOutPath = planPath
|
|
|
|
cfg := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"path": cty.StringVal(b.StatePath),
|
|
|
|
})
|
|
|
|
cfgRaw, err := plans.NewDynamicValue(cfg, cfg.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
op.PlanOutBackend = &plans.Backend{
|
|
|
|
// Just a placeholder so that we can generate a valid plan file.
|
|
|
|
Type: "local",
|
|
|
|
Config: cfgRaw,
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
2021-05-13 00:18:25 +02:00
|
|
|
if !p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should've been called to refresh the deposed object")
|
2019-06-03 23:50:32 +02:00
|
|
|
}
|
|
|
|
if run.PlanEmpty {
|
|
|
|
t.Fatal("plan should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The deposed object and the current object are distinct, so our
|
|
|
|
// plan includes separate actions for each of them. This strange situation
|
|
|
|
// is not common: it should arise only if Terraform fails during
|
|
|
|
// a create-before-destroy when the create hasn't completed yet but
|
|
|
|
// in a severe way that prevents the previous object from being restored
|
|
|
|
// as "current".
|
|
|
|
//
|
|
|
|
// However, that situation was more common in some earlier Terraform
|
|
|
|
// versions where deposed objects were not managed properly, so this
|
|
|
|
// can arise when upgrading from an older version with deposed objects
|
|
|
|
// already in the state.
|
|
|
|
//
|
|
|
|
// This is one of the few cases where we expose the idea of "deposed" in
|
|
|
|
// the UI, including the user-unfriendly "deposed key" (00000000 in this
|
|
|
|
// case) just so that users can correlate this with what they might
|
|
|
|
// see in `terraform show` and in the subsequent apply output, because
|
|
|
|
// it's also possible for there to be _multiple_ deposed objects, in the
|
|
|
|
// unlikely event that create_before_destroy _keeps_ crashing across
|
|
|
|
// subsequent runs.
|
2021-01-12 03:29:39 +01:00
|
|
|
expectedOutput := `Terraform used the selected providers to generate the following execution
|
|
|
|
plan. Resource actions are indicated with the following symbols:
|
2019-06-03 23:50:32 +02:00
|
|
|
+ create
|
|
|
|
- destroy
|
|
|
|
|
|
|
|
Terraform will perform the following actions:
|
|
|
|
|
|
|
|
# test_instance.foo will be created
|
|
|
|
+ resource "test_instance" "foo" {
|
|
|
|
+ ami = "bar"
|
|
|
|
|
|
|
|
+ network_interface {
|
|
|
|
+ description = "Main network interface"
|
|
|
|
+ device_index = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# test_instance.foo (deposed object 00000000) will be destroyed
|
2021-05-13 00:40:55 +02:00
|
|
|
# (left over from a partially-failed replacement of this instance)
|
2019-06-03 23:50:32 +02:00
|
|
|
- resource "test_instance" "foo" {
|
|
|
|
- ami = "bar" -> null
|
|
|
|
|
|
|
|
- network_interface {
|
|
|
|
- description = "Main network interface" -> null
|
|
|
|
- device_index = 0 -> null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Plan: 1 to add, 0 to change, 1 to destroy.`
|
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 output := done(t).Stdout(); !strings.Contains(output, expectedOutput) {
|
|
|
|
t.Fatalf("Unexpected output:\n%s", output)
|
2019-06-03 23:50:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:45:47 +01:00
|
|
|
func TestLocal_planTainted_createBeforeDestroy(t *testing.T) {
|
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
p := TestLocalProvider(t, b, "test", planFixtureSchema())
|
|
|
|
testStateFile(t, b.StatePath, testPlanState_tainted())
|
2021-09-13 19:22:36 +02:00
|
|
|
outDir := t.TempDir()
|
2018-12-14 14:45:47 +01:00
|
|
|
planPath := filepath.Join(outDir, "plan.tfplan")
|
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 := testOperationPlan(t, "./testdata/plan-cbd")
|
2018-12-14 14:45:47 +01:00
|
|
|
defer configCleanup()
|
|
|
|
op.PlanRefresh = true
|
|
|
|
op.PlanOutPath = planPath
|
|
|
|
cfg := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"path": cty.StringVal(b.StatePath),
|
|
|
|
})
|
|
|
|
cfgRaw, err := plans.NewDynamicValue(cfg, cfg.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
op.PlanOutBackend = &plans.Backend{
|
|
|
|
// Just a placeholder so that we can generate a valid plan file.
|
|
|
|
Type: "local",
|
|
|
|
Config: cfgRaw,
|
|
|
|
}
|
|
|
|
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 be called")
|
|
|
|
}
|
|
|
|
if run.PlanEmpty {
|
|
|
|
t.Fatal("plan should not be empty")
|
|
|
|
}
|
|
|
|
|
2021-01-12 03:29:39 +01:00
|
|
|
expectedOutput := `Terraform used the selected providers to generate the following execution
|
|
|
|
plan. Resource actions are indicated with the following symbols:
|
2018-12-14 14:45:47 +01:00
|
|
|
+/- create replacement and then destroy
|
|
|
|
|
|
|
|
Terraform will perform the following actions:
|
|
|
|
|
2019-03-06 01:18:55 +01:00
|
|
|
# test_instance.foo is tainted, so must be replaced
|
2018-12-14 14:45:47 +01:00
|
|
|
+/- resource "test_instance" "foo" {
|
command: Add experimental concise diff renderer
When rendering a diff between current state and projected state, we only
show resources and outputs which have changes. However, we show a full
structural diff for these values, which includes all attributes and
blocks for a changed resource or output. The result can be a very long
diff, which makes it difficult to verify what the changed fields are.
This commit adds an experimental concise diff renderer, which suppresses
most unchanged fields, only displaying the most relevant changes and
some identifying context. This means:
- Always show all identifying attributes, initially defined as `id`,
`name`, and `tags`, even if unchanged;
- Only show changed, added, or removed primitive values: `string`,
`number`, or `bool`;
- Only show added or removed elements in unordered collections and
structural types: `map`, `set`, and `object`;
- Show added or removed elements with any surrounding unchanged elements
for sequence types: `list` and `tuple`;
- Only show added or removed nested blocks, or blocks with changed
attributes.
If any attributes, collection elements, or blocks are hidden, a count
is kept and displayed at the end of the parent scope. This ensures that
it is clear that the diff is only displaying a subset of the resource.
The experiment is currently enabled by default, but can be disabled by
setting the TF_X_CONCISE_DIFF environment variable to 0.
2020-08-19 22:47:56 +02:00
|
|
|
# (1 unchanged attribute hidden)
|
2018-12-14 14:45:47 +01:00
|
|
|
|
command: Add experimental concise diff renderer
When rendering a diff between current state and projected state, we only
show resources and outputs which have changes. However, we show a full
structural diff for these values, which includes all attributes and
blocks for a changed resource or output. The result can be a very long
diff, which makes it difficult to verify what the changed fields are.
This commit adds an experimental concise diff renderer, which suppresses
most unchanged fields, only displaying the most relevant changes and
some identifying context. This means:
- Always show all identifying attributes, initially defined as `id`,
`name`, and `tags`, even if unchanged;
- Only show changed, added, or removed primitive values: `string`,
`number`, or `bool`;
- Only show added or removed elements in unordered collections and
structural types: `map`, `set`, and `object`;
- Show added or removed elements with any surrounding unchanged elements
for sequence types: `list` and `tuple`;
- Only show added or removed nested blocks, or blocks with changed
attributes.
If any attributes, collection elements, or blocks are hidden, a count
is kept and displayed at the end of the parent scope. This ensures that
it is clear that the diff is only displaying a subset of the resource.
The experiment is currently enabled by default, but can be disabled by
setting the TF_X_CONCISE_DIFF environment variable to 0.
2020-08-19 22:47:56 +02:00
|
|
|
# (1 unchanged block hidden)
|
2018-12-14 14:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Plan: 1 to add, 0 to change, 1 to destroy.`
|
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 output := done(t).Stdout(); !strings.Contains(output, expectedOutput) {
|
2018-12-14 14:45:47 +01:00
|
|
|
t.Fatalf("Unexpected output:\n%s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
func TestLocal_planRefreshFalse(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2018-10-04 23:37:14 +02:00
|
|
|
|
|
|
|
p := TestLocalProvider(t, b, "test", planFixtureSchema())
|
2018-10-09 00:22:59 +02:00
|
|
|
testStateFile(t, b.StatePath, testPlanState())
|
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 := testOperationPlan(t, "./testdata/plan")
|
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.Fatalf("plan 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
|
|
|
}
|
|
|
|
|
|
|
|
if !run.PlanEmpty {
|
|
|
|
t.Fatal("plan should be empty")
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocal_planDestroy(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2018-10-04 23:37:14 +02:00
|
|
|
|
2021-01-05 00:27:08 +01:00
|
|
|
TestLocalProvider(t, b, "test", planFixtureSchema())
|
2018-10-09 00:22:59 +02:00
|
|
|
testStateFile(t, b.StatePath, testPlanState())
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2021-09-13 19:22:36 +02:00
|
|
|
outDir := t.TempDir()
|
2017-01-19 05:47:56 +01:00
|
|
|
planPath := filepath.Join(outDir, "plan.tfplan")
|
|
|
|
|
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 := testOperationPlan(t, "./testdata/plan")
|
2018-03-21 02:43:02 +01:00
|
|
|
defer configCleanup()
|
2021-04-06 01:28:59 +02:00
|
|
|
op.PlanMode = plans.DestroyMode
|
2017-01-19 05:47:56 +01:00
|
|
|
op.PlanRefresh = true
|
|
|
|
op.PlanOutPath = planPath
|
2018-10-09 21:19:24 +02:00
|
|
|
cfg := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"path": cty.StringVal(b.StatePath),
|
|
|
|
})
|
|
|
|
cfgRaw, err := plans.NewDynamicValue(cfg, cfg.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
op.PlanOutBackend = &plans.Backend{
|
|
|
|
// Just a placeholder so that we can generate a valid plan file.
|
2018-10-10 01:32:09 +02:00
|
|
|
Type: "local",
|
2018-10-09 21:19:24 +02:00
|
|
|
Config: cfgRaw,
|
|
|
|
}
|
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.Fatalf("plan operation failed")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if run.PlanEmpty {
|
|
|
|
t.Fatal("plan should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
plan := testReadPlan(t, planPath)
|
2018-10-04 23:37:14 +02:00
|
|
|
for _, r := range plan.Changes.Resources {
|
|
|
|
if r.Action.String() != "Delete" {
|
|
|
|
t.Fatalf("bad: %#v", r.Action.String())
|
|
|
|
}
|
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
|
|
|
|
|
|
|
if errOutput := done(t).Stderr(); errOutput != "" {
|
|
|
|
t.Fatalf("unexpected error output:\n%s", errOutput)
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-12-12 17:01:18 +01:00
|
|
|
func TestLocal_planDestroy_withDataSources(t *testing.T) {
|
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2021-01-05 00:27:08 +01:00
|
|
|
TestLocalProvider(t, b, "test", planFixtureSchema())
|
2018-12-12 17:01:18 +01:00
|
|
|
testStateFile(t, b.StatePath, testPlanState_withDataSource())
|
|
|
|
|
2021-09-13 19:22:36 +02:00
|
|
|
outDir := t.TempDir()
|
2018-12-12 17:01:18 +01:00
|
|
|
planPath := filepath.Join(outDir, "plan.tfplan")
|
|
|
|
|
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 := testOperationPlan(t, "./testdata/destroy-with-ds")
|
2018-12-12 17:01:18 +01:00
|
|
|
defer configCleanup()
|
2021-04-06 01:28:59 +02:00
|
|
|
op.PlanMode = plans.DestroyMode
|
2018-12-12 17:01:18 +01:00
|
|
|
op.PlanRefresh = true
|
|
|
|
op.PlanOutPath = planPath
|
|
|
|
cfg := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"path": cty.StringVal(b.StatePath),
|
|
|
|
})
|
|
|
|
cfgRaw, err := plans.NewDynamicValue(cfg, cfg.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
op.PlanOutBackend = &plans.Backend{
|
|
|
|
// Just a placeholder so that we can generate a valid plan file.
|
|
|
|
Type: "local",
|
|
|
|
Config: cfgRaw,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 run.PlanEmpty {
|
|
|
|
t.Fatal("plan should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data source should still exist in the the plan file
|
|
|
|
plan := testReadPlan(t, planPath)
|
|
|
|
if len(plan.Changes.Resources) != 2 {
|
|
|
|
t.Fatalf("Expected exactly 1 resource for destruction, %d given: %q",
|
|
|
|
len(plan.Changes.Resources), getAddrs(plan.Changes.Resources))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data source should not be rendered in the output
|
|
|
|
expectedOutput := `Terraform will perform the following actions:
|
|
|
|
|
2020-09-11 23:56:12 +02:00
|
|
|
# test_instance.foo[0] will be destroyed
|
2018-12-12 17:01:18 +01:00
|
|
|
- resource "test_instance" "foo" {
|
|
|
|
- ami = "bar" -> null
|
|
|
|
|
|
|
|
- network_interface {
|
|
|
|
- description = "Main network interface" -> null
|
|
|
|
- device_index = 0 -> null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Plan: 0 to add, 0 to change, 1 to destroy.`
|
|
|
|
|
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 output := done(t).Stdout(); !strings.Contains(output, expectedOutput) {
|
2020-09-11 23:56:12 +02:00
|
|
|
t.Fatalf("Unexpected output:\n%s", output)
|
2018-12-12 17:01:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAddrs(resources []*plans.ResourceInstanceChangeSrc) []string {
|
2020-12-01 15:06:56 +01:00
|
|
|
addrs := make([]string, len(resources))
|
2018-12-12 17:01:18 +01:00
|
|
|
for i, r := range resources {
|
|
|
|
addrs[i] = r.Addr.String()
|
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
func TestLocal_planOutPathNoChange(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2018-05-23 04:57:04 +02:00
|
|
|
TestLocalProvider(t, b, "test", planFixtureSchema())
|
2018-10-09 00:22:59 +02:00
|
|
|
testStateFile(t, b.StatePath, testPlanState())
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2021-09-13 19:22:36 +02:00
|
|
|
outDir := t.TempDir()
|
2017-01-19 05:47:56 +01:00
|
|
|
planPath := filepath.Join(outDir, "plan.tfplan")
|
|
|
|
|
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 := testOperationPlan(t, "./testdata/plan")
|
2018-03-21 02:43:02 +01:00
|
|
|
defer configCleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
op.PlanOutPath = planPath
|
2018-10-09 21:19:24 +02:00
|
|
|
cfg := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"path": cty.StringVal(b.StatePath),
|
|
|
|
})
|
|
|
|
cfgRaw, err := plans.NewDynamicValue(cfg, cfg.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
op.PlanOutBackend = &plans.Backend{
|
|
|
|
// Just a placeholder so that we can generate a valid plan file.
|
2018-10-10 01:32:09 +02:00
|
|
|
Type: "local",
|
2018-10-09 21:19:24 +02:00
|
|
|
Config: cfgRaw,
|
|
|
|
}
|
2020-09-23 17:09:42 +02:00
|
|
|
op.PlanRefresh = true
|
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.Fatalf("plan operation failed")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
plan := testReadPlan(t, planPath)
|
2018-10-09 00:22:59 +02:00
|
|
|
|
|
|
|
if !plan.Changes.Empty() {
|
|
|
|
t.Fatalf("expected empty plan to be written")
|
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
|
|
|
|
|
|
|
if errOutput := done(t).Stderr(); errOutput != "" {
|
|
|
|
t.Fatalf("unexpected error output:\n%s", errOutput)
|
|
|
|
}
|
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
|
|
|
func testOperationPlan(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))
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
return &backend.Operation{
|
2021-02-25 16:02:23 +01:00
|
|
|
Type: backend.OperationTypePlan,
|
|
|
|
ConfigDir: configDir,
|
|
|
|
ConfigLoader: configLoader,
|
|
|
|
StateLocker: clistate.NewNoopLocker(),
|
|
|
|
View: view,
|
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-10-09 00:22:59 +02:00
|
|
|
// testPlanState is just a common state that we use for testing plan.
|
|
|
|
func testPlanState() *states.State {
|
|
|
|
state := states.NewState()
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
core: refactoring.ImpliedMoveStatements replaces NodeCountBoundary
Going back a long time we've had a special magic behavior which tries to
recognize a situation where a module author either added or removed the
"count" argument from a resource that already has instances, and to
silently rename the zeroth or no-key instance so that we don't plan to
destroy and recreate the associated object.
Now we have a more general idea of "move statements", and specifically
the idea of "implied" move statements which replicates the same heuristic
we used to use for this behavior, we can treat this magic renaming rule as
just another "move statement", special only in that Terraform generates it
automatically rather than it being written out explicitly in the
configuration.
In return for wiring that in, we can now remove altogether the
NodeCountBoundary graph node type and its associated graph transformer,
CountBoundaryTransformer. We handle moves as a preprocessing step before
building the plan graph, so we no longer need to include any special nodes
in the graph to deal with that situation.
The test updates here are mainly for the graph builders themselves, to
acknowledge that indeed we're no longer inserting the NodeCountBoundary
vertices. The vertices that NodeCountBoundary previously depended on now
become dependencies of the special "root" vertex, although in many cases
here we don't see that explicitly because of the transitive reduction
algorithm, which notices when there's already an equivalent indirect
dependency chain and removes the redundant edge.
We already have plenty of test coverage for these "count boundary" cases
in the context tests whose names start with TestContext2Plan_count and
TestContext2Apply_resourceCount, all of which continued to pass here
without any modification and so are not visible in the diff. The test
functions particularly relevant to this situation are:
- TestContext2Plan_countIncreaseFromNotSet
- TestContext2Plan_countDecreaseToOne
- TestContext2Plan_countOneIndex
- TestContext2Apply_countDecreaseToOneCorrupted
The last of those in particular deals with the situation where we have
both a no-key instance _and_ a zero-key instance in the prior state, which
is interesting here because to exercises an intentional interaction
between refactoring.ImpliedMoveStatements and refactoring.ApplyMoves,
where we intentionally generate an implied move statement that produces
a collision and then expect ApplyMoves to deal with it in the same way as
it would deal with all other collisions, and thus ensure we handle both
the explicit and implied collisions in the same way.
This does affect some UI-level tests, because a nice side-effect of this
new treatment of this old feature is that we can now report explicitly
in the UI that we're assigning new addresses to these objects, whereas
before we just said nothing and hoped the user would just guess what had
happened and why they therefore weren't seeing a diff.
The backend/local plan tests actually had a pre-existing bug where they
were using a state with a different instance key than the config called
for but getting away with it because we'd previously silently fix it up.
That's still fixed up, but now done with an explicit mention in the UI
and so I made the state consistent with the configuration here so that the
tests would be able to recognize _real_ differences where present, as
opposed to the errant difference caused by that inconsistency.
2021-09-18 00:32:32 +02:00
|
|
|
}.Instance(addrs.NoKey),
|
2018-10-09 00:22:59 +02:00
|
|
|
&states.ResourceInstanceObjectSrc{
|
2018-11-30 19:56:50 +01:00
|
|
|
Status: states.ObjectReady,
|
2018-10-09 00:22:59 +02:00
|
|
|
AttrsJSON: []byte(`{
|
|
|
|
"ami": "bar",
|
|
|
|
"network_interface": [{
|
|
|
|
"device_index": 0,
|
|
|
|
"description": "Main network interface"
|
|
|
|
}]
|
|
|
|
}`),
|
2017-01-19 05:47:56 +01:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:07:05 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2018-10-09 00:22:59 +02:00
|
|
|
)
|
|
|
|
return state
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-12-12 17:01:18 +01:00
|
|
|
func testPlanState_withDataSource() *states.State {
|
|
|
|
state := states.NewState()
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{
|
|
|
|
"ami": "bar",
|
|
|
|
"network_interface": [{
|
|
|
|
"device_index": 0,
|
|
|
|
"description": "Main network interface"
|
|
|
|
}]
|
|
|
|
}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:07:05 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2018-12-12 17:01:18 +01:00
|
|
|
)
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.DataResourceMode,
|
|
|
|
Type: "test_ds",
|
|
|
|
Name: "bar",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{
|
|
|
|
"filter": "foo"
|
|
|
|
}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:07:05 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2018-12-12 17:01:18 +01:00
|
|
|
)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:45:47 +01:00
|
|
|
func testPlanState_tainted() *states.State {
|
|
|
|
state := states.NewState()
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
2020-09-16 19:56:08 +02:00
|
|
|
}.Instance(addrs.NoKey),
|
2018-12-14 14:45:47 +01:00
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectTainted,
|
|
|
|
AttrsJSON: []byte(`{
|
|
|
|
"ami": "bar",
|
|
|
|
"network_interface": [{
|
|
|
|
"device_index": 0,
|
|
|
|
"description": "Main network interface"
|
|
|
|
}]
|
|
|
|
}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:07:05 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2018-12-14 14:45:47 +01:00
|
|
|
)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2018-09-29 00:57:27 +02:00
|
|
|
func testReadPlan(t *testing.T, path string) *plans.Plan {
|
2018-10-09 21:19:24 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2018-09-29 00:57:27 +02:00
|
|
|
p, err := planfile.Open(path)
|
2017-01-19 05:47:56 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-29 00:57:27 +02:00
|
|
|
defer p.Close()
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2018-09-29 00:57:27 +02:00
|
|
|
plan, err := p.ReadPlan()
|
2018-10-04 23:37:14 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2018-09-29 00:57:27 +02:00
|
|
|
return plan
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
2018-05-23 04:57:04 +02:00
|
|
|
|
|
|
|
// planFixtureSchema returns a schema suitable for processing the
|
2019-06-30 09:38:36 +02:00
|
|
|
// configuration in testdata/plan . This schema should be
|
2018-05-23 04:57:04 +02:00
|
|
|
// assigned to a mock provider named "test".
|
|
|
|
func planFixtureSchema() *terraform.ProviderSchema {
|
|
|
|
return &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
"network_interface": {
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
Block: configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"device_index": {Type: cty.Number, Optional: true},
|
|
|
|
"description": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-12-12 17:01:18 +01:00
|
|
|
DataSources: map[string]*configschema.Block{
|
|
|
|
"test_ds": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"filter": {Type: cty.String, Required: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-05-23 04:57:04 +02:00
|
|
|
}
|
|
|
|
}
|