2014-07-13 04:47:31 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2019-01-12 00:13:55 +01:00
|
|
|
"encoding/json"
|
2020-01-13 21:10:00 +01:00
|
|
|
"fmt"
|
2019-01-12 00:13:55 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-10-11 21:56:55 +02:00
|
|
|
"path/filepath"
|
2019-07-17 03:25:30 +02:00
|
|
|
"strings"
|
2014-07-13 04:47:31 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-01-12 00:13:55 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2021-05-17 21:33:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2021-05-17 19:40:40 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/providers"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2021-12-17 23:46:42 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/states/statemgr"
|
2021-05-17 21:46:19 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2021-12-17 23:46:42 +01:00
|
|
|
"github.com/hashicorp/terraform/version"
|
2014-07-13 04:47:31 +02:00
|
|
|
"github.com/mitchellh/cli"
|
2018-12-19 20:08:25 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2014-07-13 04:47:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShow(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2014-07-13 04:47:31 +02:00
|
|
|
c := &ShowCommand{
|
2014-07-13 18:22:23 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2014-07-13 18:22:23 +02:00
|
|
|
},
|
2014-07-13 04:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"bad",
|
|
|
|
"bad",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShow_noArgs(t *testing.T) {
|
2021-05-05 20:13:20 +02:00
|
|
|
// Get a temp cwd
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
2014-10-11 21:56:55 +02:00
|
|
|
// Create the default state
|
2021-05-05 20:13:20 +02:00
|
|
|
testStateFileDefault(t, testState())
|
2014-10-11 21:56:55 +02:00
|
|
|
|
2014-07-13 04:47:31 +02:00
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2014-07-13 04:47:31 +02:00
|
|
|
c := &ShowCommand{
|
2014-07-13 18:22:23 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2014-07-13 18:22:23 +02:00
|
|
|
},
|
2014-07-13 04:47:31 +02:00
|
|
|
}
|
|
|
|
|
2021-05-05 20:13:20 +02:00
|
|
|
if code := c.Run([]string{}); code != 0 {
|
2014-10-11 21:56:55 +02:00
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
2020-01-13 21:10:00 +01:00
|
|
|
|
|
|
|
if !strings.Contains(ui.OutputWriter.String(), "# test_instance.foo:") {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/hashicorp/terraform/issues/21462
|
|
|
|
func TestShow_aliasedProvider(t *testing.T) {
|
|
|
|
// Create the default state with aliased resource
|
|
|
|
testState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
// The weird whitespace here is reflective of how this would
|
|
|
|
// get written out in a real state file, due to the indentation
|
|
|
|
// of all of the containing wrapping objects and arrays.
|
|
|
|
AttrsJSON: []byte("{\n \"id\": \"bar\"\n }"),
|
|
|
|
Status: states.ObjectReady,
|
2020-03-23 20:26:18 +01:00
|
|
|
Dependencies: []addrs.ConfigResource{},
|
2020-01-13 21:10:00 +01:00
|
|
|
},
|
2020-04-02 18:58:44 +02:00
|
|
|
addrs.RootModuleInstance.ProviderConfigAliased(addrs.NewDefaultProvider("test"), "alias"),
|
2020-01-13 21:10:00 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
statePath := testStateFile(t, testState)
|
|
|
|
stateDir := filepath.Dir(statePath)
|
|
|
|
defer os.RemoveAll(stateDir)
|
|
|
|
defer testChdir(t, stateDir)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2020-01-13 21:10:00 +01:00
|
|
|
c := &ShowCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2020-01-13 21:10:00 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// the statefile created by testStateFile is named state.tfstate
|
|
|
|
args := []string{"state.tfstate"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad exit code: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(ui.OutputWriter.String(), "# missing schema for provider \"test.alias\"") {
|
|
|
|
t.Fatalf("bad output: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
2014-10-11 21:56:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShow_noArgsNoState(t *testing.T) {
|
|
|
|
// Create the default state
|
2018-03-28 19:08:38 +02:00
|
|
|
statePath := testStateFile(t, testState())
|
2020-01-13 21:10:00 +01:00
|
|
|
stateDir := filepath.Dir(statePath)
|
|
|
|
defer os.RemoveAll(stateDir)
|
|
|
|
defer testChdir(t, stateDir)()
|
2014-10-11 21:56:55 +02:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2014-10-11 21:56:55 +02:00
|
|
|
c := &ShowCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2014-10-11 21:56:55 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:10:00 +01:00
|
|
|
// the statefile created by testStateFile is named state.tfstate
|
|
|
|
args := []string{"state.tfstate"}
|
2014-10-11 21:56:55 +02:00
|
|
|
if code := c.Run(args); code != 0 {
|
2014-07-13 04:47:31 +02:00
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
func TestShow_planNoop(t *testing.T) {
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
planPath := testPlanFileNoop(t)
|
2014-07-13 04:47:31 +02:00
|
|
|
|
2019-11-06 02:20:26 +01:00
|
|
|
ui := cli.NewMockUi()
|
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
|
|
|
view, done := testView(t)
|
2014-07-13 04:47:31 +02:00
|
|
|
c := &ShowCommand{
|
2014-07-13 18:22:23 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2014-07-13 18:22:23 +02:00
|
|
|
},
|
2014-07-13 04:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
planPath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
2019-11-06 02:20:26 +01:00
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
want := `No changes. Your infrastructure matches the configuration.`
|
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
|
|
|
got := done(t).Stdout()
|
2019-11-06 02:20:26 +01:00
|
|
|
if !strings.Contains(got, want) {
|
|
|
|
t.Errorf("missing expected output\nwant: %s\ngot:\n%s", want, got)
|
|
|
|
}
|
2014-07-13 04:47:31 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 17:53:43 +01:00
|
|
|
func TestShow_planWithChanges(t *testing.T) {
|
|
|
|
planPathWithChanges := showFixturePlanFile(t, plans.DeleteThenCreate)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
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
|
|
|
view, done := testView(t)
|
2019-12-06 17:53:43 +01:00
|
|
|
c := &ShowCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(showFixtureProvider()),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2019-12-06 17:53:43 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
planPathWithChanges,
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
want := `test_instance.foo must be replaced`
|
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
|
|
|
got := done(t).Stdout()
|
2019-12-06 17:53:43 +01:00
|
|
|
if !strings.Contains(got, want) {
|
|
|
|
t.Errorf("missing expected output\nwant: %s\ngot:\n%s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 23:46:22 +02:00
|
|
|
func TestShow_planWithForceReplaceChange(t *testing.T) {
|
|
|
|
// The main goal of this test is to see that the "replace by request"
|
|
|
|
// resource instance action reason can round-trip through a plan file and
|
|
|
|
// be reflected correctly in the "terraform show" output, the same way
|
|
|
|
// as it would appear in "terraform plan" output.
|
|
|
|
|
|
|
|
_, snap := testModuleWithSnapshot(t, "show")
|
|
|
|
plannedVal := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.UnknownVal(cty.String),
|
|
|
|
"ami": cty.StringVal("bar"),
|
|
|
|
})
|
|
|
|
priorValRaw, err := plans.NewDynamicValue(cty.NullVal(plannedVal.Type()), plannedVal.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
plannedValRaw, err := plans.NewDynamicValue(plannedVal, plannedVal.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
plan := testPlan(t)
|
|
|
|
plan.Changes.SyncWrapper().AppendResourceInstanceChange(&plans.ResourceInstanceChangeSrc{
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.CreateThenDelete,
|
|
|
|
Before: priorValRaw,
|
|
|
|
After: plannedValRaw,
|
|
|
|
},
|
|
|
|
ActionReason: plans.ResourceInstanceReplaceByRequest,
|
|
|
|
})
|
|
|
|
planFilePath := testPlanFile(
|
|
|
|
t,
|
|
|
|
snap,
|
|
|
|
states.NewState(),
|
|
|
|
plan,
|
|
|
|
)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
view, done := testView(t)
|
|
|
|
c := &ShowCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(showFixtureProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
planFilePath,
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
got := done(t).Stdout()
|
|
|
|
if want := `test_instance.foo will be replaced, as requested`; !strings.Contains(got, want) {
|
|
|
|
t.Errorf("wrong output\ngot:\n%s\n\nwant substring: %s", got, want)
|
|
|
|
}
|
|
|
|
if want := `Plan: 1 to add, 0 to change, 1 to destroy.`; !strings.Contains(got, want) {
|
|
|
|
t.Errorf("wrong output\ngot:\n%s\n\nwant substring: %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
func TestShow_plan_json(t *testing.T) {
|
2019-12-06 17:53:43 +01:00
|
|
|
planPath := showFixturePlanFile(t, plans.Create)
|
2018-12-19 20:08:25 +01:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2018-12-19 20:08:25 +01:00
|
|
|
c := &ShowCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(showFixtureProvider()),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2018-12-19 20:08:25 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-json",
|
|
|
|
planPath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 04:47:31 +02:00
|
|
|
func TestShow_state(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
originalState := testState()
|
2014-07-13 04:47:31 +02:00
|
|
|
statePath := testStateFile(t, originalState)
|
2020-01-13 21:10:00 +01:00
|
|
|
defer os.RemoveAll(filepath.Dir(statePath))
|
2014-07-13 04:47:31 +02:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2014-07-13 04:47:31 +02:00
|
|
|
c := &ShowCommand{
|
2014-07-13 18:22:23 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2014-07-13 18:22:23 +02:00
|
|
|
},
|
2014-07-13 04:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
statePath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 20:08:25 +01:00
|
|
|
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
func TestShow_json_output(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
fixtureDir := "testdata/show-json"
|
2019-01-12 00:13:55 +01:00
|
|
|
testDirs, err := ioutil.ReadDir(fixtureDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range testDirs {
|
|
|
|
if !entry.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(entry.Name(), func(t *testing.T) {
|
2019-02-19 17:12:33 +01:00
|
|
|
td := tempDir(t)
|
2019-01-12 00:13:55 +01:00
|
|
|
inputDir := filepath.Join(fixtureDir, entry.Name())
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, inputDir, td)
|
2019-02-19 17:12:33 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2019-01-12 00:13:55 +01:00
|
|
|
|
2019-07-17 03:25:30 +02:00
|
|
|
expectError := strings.Contains(entry.Name(), "error")
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
2021-06-14 15:22:22 +02:00
|
|
|
"test": {"1.2.3"},
|
2020-03-31 23:02:40 +02:00
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
|
2019-02-19 17:12:33 +01:00
|
|
|
p := showFixtureProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2019-02-19 17:12:33 +01:00
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2019-01-12 00:13:55 +01:00
|
|
|
}
|
2019-02-19 17:12:33 +01:00
|
|
|
|
|
|
|
// init
|
|
|
|
ic := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
if code := ic.Run([]string{}); code != 0 {
|
2019-07-17 03:25:30 +02:00
|
|
|
if expectError {
|
|
|
|
// this should error, but not panic.
|
|
|
|
return
|
|
|
|
}
|
2019-02-19 17:12:33 +01:00
|
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
2019-01-12 00:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pc := &PlanCommand{
|
2019-02-19 17:12:33 +01:00
|
|
|
Meta: m,
|
2019-01-12 00:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-out=terraform.plan",
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := pc.Run(args); code != 0 {
|
|
|
|
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush the plan output from the mock ui
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
sc := &ShowCommand{
|
2019-02-19 17:12:33 +01:00
|
|
|
Meta: m,
|
2019-01-12 00:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
args = []string{
|
|
|
|
"-json",
|
|
|
|
"terraform.plan",
|
|
|
|
}
|
|
|
|
defer os.Remove("terraform.plan")
|
|
|
|
|
|
|
|
if code := sc.Run(args); code != 0 {
|
|
|
|
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare ui output to wanted output
|
|
|
|
var got, want plan
|
|
|
|
|
|
|
|
gotString := ui.OutputWriter.String()
|
|
|
|
json.Unmarshal([]byte(gotString), &got)
|
|
|
|
|
|
|
|
wantFile, err := os.Open("output.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer wantFile.Close()
|
|
|
|
byteValue, err := ioutil.ReadAll(wantFile)
|
2019-03-14 22:52:07 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
json.Unmarshal([]byte(byteValue), &want)
|
|
|
|
|
|
|
|
if !cmp.Equal(got, want) {
|
|
|
|
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 22:26:47 +02:00
|
|
|
func TestShow_json_output_sensitive(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
inputDir := "testdata/show-json-sensitive"
|
|
|
|
testCopyDir(t, inputDir, td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{"test": {"1.2.3"}})
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
p := showFixtureSensitiveProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
view, _ := testView(t)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
|
|
|
ProviderSource: providerSource,
|
|
|
|
}
|
|
|
|
|
|
|
|
// init
|
|
|
|
ic := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush init output
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
|
|
|
|
pc := &PlanCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-out=terraform.plan",
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := pc.Run(args); code != 0 {
|
|
|
|
fmt.Println(ui.OutputWriter.String())
|
|
|
|
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush the plan output from the mock ui
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
sc := &ShowCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
args = []string{
|
|
|
|
"-json",
|
|
|
|
"terraform.plan",
|
|
|
|
}
|
|
|
|
defer os.Remove("terraform.plan")
|
|
|
|
|
|
|
|
if code := sc.Run(args); code != 0 {
|
|
|
|
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare ui output to wanted output
|
|
|
|
var got, want plan
|
|
|
|
|
|
|
|
gotString := ui.OutputWriter.String()
|
|
|
|
json.Unmarshal([]byte(gotString), &got)
|
|
|
|
|
|
|
|
wantFile, err := os.Open("output.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer wantFile.Close()
|
|
|
|
byteValue, err := ioutil.ReadAll(wantFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
json.Unmarshal([]byte(byteValue), &want)
|
|
|
|
|
|
|
|
if !cmp.Equal(got, want) {
|
|
|
|
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:52:07 +01:00
|
|
|
// similar test as above, without the plan
|
|
|
|
func TestShow_json_output_state(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
fixtureDir := "testdata/show-json-state"
|
2019-03-14 22:52:07 +01:00
|
|
|
testDirs, err := ioutil.ReadDir(fixtureDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range testDirs {
|
|
|
|
if !entry.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(entry.Name(), func(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
inputDir := filepath.Join(fixtureDir, entry.Name())
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, inputDir, td)
|
2019-03-14 22:52:07 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
2021-06-14 15:19:13 +02:00
|
|
|
"test": {"1.2.3"},
|
2020-03-31 23:02:40 +02:00
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
|
2019-03-14 22:52:07 +01:00
|
|
|
p := showFixtureProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2019-03-14 22:52:07 +01:00
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2019-03-14 22:52:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// init
|
|
|
|
ic := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush the plan output from the mock ui
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
sc := &ShowCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := sc.Run([]string{"-json"}); code != 0 {
|
|
|
|
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare ui output to wanted output
|
|
|
|
type state struct {
|
|
|
|
FormatVersion string `json:"format_version,omitempty"`
|
|
|
|
TerraformVersion string `json:"terraform_version"`
|
|
|
|
Values map[string]interface{} `json:"values,omitempty"`
|
2021-06-14 15:19:13 +02:00
|
|
|
SensitiveValues map[string]bool `json:"sensitive_values,omitempty"`
|
2019-03-14 22:52:07 +01:00
|
|
|
}
|
|
|
|
var got, want state
|
|
|
|
|
|
|
|
gotString := ui.OutputWriter.String()
|
|
|
|
json.Unmarshal([]byte(gotString), &got)
|
|
|
|
|
|
|
|
wantFile, err := os.Open("output.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer wantFile.Close()
|
|
|
|
byteValue, err := ioutil.ReadAll(wantFile)
|
2019-01-12 00:13:55 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
json.Unmarshal([]byte(byteValue), &want)
|
|
|
|
|
|
|
|
if !cmp.Equal(got, want) {
|
|
|
|
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 23:46:42 +01:00
|
|
|
func TestShow_planWithNonDefaultStateLineage(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("show"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// Write default state file with a testing lineage ("fake-for-testing")
|
|
|
|
testStateFileDefault(t, testState())
|
|
|
|
|
|
|
|
// Create a plan with a different lineage, which we should still be able
|
|
|
|
// to show
|
|
|
|
_, snap := testModuleWithSnapshot(t, "show")
|
|
|
|
state := testState()
|
|
|
|
plan := testPlan(t)
|
|
|
|
stateMeta := statemgr.SnapshotMeta{
|
|
|
|
Lineage: "fake-for-plan",
|
|
|
|
Serial: 1,
|
|
|
|
TerraformVersion: version.SemVer,
|
|
|
|
}
|
|
|
|
planPath := testPlanFileMatchState(t, snap, state, plan, stateMeta)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
view, done := testView(t)
|
|
|
|
c := &ShowCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
planPath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
want := `No changes. Your infrastructure matches the configuration.`
|
|
|
|
got := done(t).Stdout()
|
|
|
|
if !strings.Contains(got, want) {
|
|
|
|
t.Errorf("missing expected output\nwant: %s\ngot:\n%s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
// showFixtureSchema returns a schema suitable for processing the configuration
|
2019-06-30 09:38:36 +02:00
|
|
|
// in testdata/show. This schema should be assigned to a mock provider
|
2018-12-19 20:08:25 +01:00
|
|
|
// named "test".
|
2021-02-18 16:13:43 +01:00
|
|
|
func showFixtureSchema() *providers.GetProviderSchemaResponse {
|
|
|
|
return &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"region": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2020-07-14 21:28:31 +02:00
|
|
|
},
|
|
|
|
},
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-12-19 20:08:25 +01:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-12-19 20:08:25 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 22:26:47 +02:00
|
|
|
// showFixtureSensitiveSchema returns a schema suitable for processing the configuration
|
|
|
|
// in testdata/show. This schema should be assigned to a mock provider
|
|
|
|
// named "test". It includes a sensitive attribute.
|
|
|
|
func showFixtureSensitiveSchema() *providers.GetProviderSchemaResponse {
|
|
|
|
return &providers.GetProviderSchemaResponse{
|
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"region": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
"test_instance": {
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
"password": {Type: cty.String, Optional: true, Sensitive: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
// showFixtureProvider returns a mock provider that is configured for basic
|
2019-06-30 09:38:36 +02:00
|
|
|
// operation with the configuration in testdata/show. This mock has
|
2021-01-12 22:13:10 +01:00
|
|
|
// GetSchemaResponse, PlanResourceChangeFn, and ApplyResourceChangeFn populated,
|
2018-12-19 20:08:25 +01:00
|
|
|
// with the plan/apply steps just passing through the data determined by
|
|
|
|
// Terraform Core.
|
|
|
|
func showFixtureProvider() *terraform.MockProvider {
|
|
|
|
p := testProvider()
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = showFixtureSchema()
|
2021-06-17 20:32:47 +02:00
|
|
|
p.ReadResourceFn = func(req providers.ReadResourceRequest) providers.ReadResourceResponse {
|
|
|
|
idVal := req.PriorState.GetAttr("id")
|
|
|
|
amiVal := req.PriorState.GetAttr("ami")
|
|
|
|
if amiVal.RawEquals(cty.StringVal("refresh-me")) {
|
|
|
|
amiVal = cty.StringVal("refreshed")
|
|
|
|
}
|
|
|
|
return providers.ReadResourceResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": idVal,
|
|
|
|
"ami": amiVal,
|
|
|
|
}),
|
|
|
|
Private: req.Private,
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 20:08:25 +01:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
2019-02-08 20:58:08 +01:00
|
|
|
idVal := req.ProposedNewState.GetAttr("id")
|
|
|
|
amiVal := req.ProposedNewState.GetAttr("ami")
|
|
|
|
if idVal.IsNull() {
|
|
|
|
idVal = cty.UnknownVal(cty.String)
|
|
|
|
}
|
2021-04-28 21:02:34 +02:00
|
|
|
var reqRep []cty.Path
|
|
|
|
if amiVal.RawEquals(cty.StringVal("force-replace")) {
|
|
|
|
reqRep = append(reqRep, cty.GetAttrPath("ami"))
|
|
|
|
}
|
2018-12-19 20:08:25 +01:00
|
|
|
return providers.PlanResourceChangeResponse{
|
2019-02-08 20:58:08 +01:00
|
|
|
PlannedState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": idVal,
|
|
|
|
"ami": amiVal,
|
|
|
|
}),
|
2021-04-28 21:02:34 +02:00
|
|
|
RequiresReplace: reqRep,
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
|
2019-02-08 20:58:08 +01:00
|
|
|
idVal := req.PlannedState.GetAttr("id")
|
|
|
|
amiVal := req.PlannedState.GetAttr("ami")
|
|
|
|
if !idVal.IsKnown() {
|
|
|
|
idVal = cty.StringVal("placeholder")
|
|
|
|
}
|
2018-12-19 20:08:25 +01:00
|
|
|
return providers.ApplyResourceChangeResponse{
|
2019-02-08 20:58:08 +01:00
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": idVal,
|
|
|
|
"ami": amiVal,
|
|
|
|
}),
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-04-26 22:26:47 +02:00
|
|
|
// showFixtureSensitiveProvider returns a mock provider that is configured for basic
|
|
|
|
// operation with the configuration in testdata/show. This mock has
|
|
|
|
// GetSchemaResponse, PlanResourceChangeFn, and ApplyResourceChangeFn populated,
|
|
|
|
// with the plan/apply steps just passing through the data determined by
|
|
|
|
// Terraform Core. It also has a sensitive attribute in the provider schema.
|
|
|
|
func showFixtureSensitiveProvider() *terraform.MockProvider {
|
|
|
|
p := testProvider()
|
|
|
|
p.GetProviderSchemaResponse = showFixtureSensitiveSchema()
|
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
idVal := req.ProposedNewState.GetAttr("id")
|
|
|
|
if idVal.IsNull() {
|
|
|
|
idVal = cty.UnknownVal(cty.String)
|
|
|
|
}
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": idVal,
|
|
|
|
"ami": req.ProposedNewState.GetAttr("ami"),
|
|
|
|
"password": req.ProposedNewState.GetAttr("password"),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
|
|
|
|
idVal := req.PlannedState.GetAttr("id")
|
|
|
|
if !idVal.IsKnown() {
|
|
|
|
idVal = cty.StringVal("placeholder")
|
|
|
|
}
|
|
|
|
return providers.ApplyResourceChangeResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": idVal,
|
|
|
|
"ami": req.PlannedState.GetAttr("ami"),
|
|
|
|
"password": req.PlannedState.GetAttr("password"),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
// showFixturePlanFile creates a plan file at a temporary location containing a
|
2019-12-06 17:53:43 +01:00
|
|
|
// single change to create or update the test_instance.foo that is included in the "show"
|
2018-12-19 20:08:25 +01:00
|
|
|
// test fixture, returning the location of that plan file.
|
2019-12-06 17:53:43 +01:00
|
|
|
// `action` is the planned change you would like to elicit
|
|
|
|
func showFixturePlanFile(t *testing.T, action plans.Action) string {
|
2018-12-19 20:08:25 +01:00
|
|
|
_, snap := testModuleWithSnapshot(t, "show")
|
|
|
|
plannedVal := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.UnknownVal(cty.String),
|
|
|
|
"ami": cty.StringVal("bar"),
|
|
|
|
})
|
|
|
|
priorValRaw, err := plans.NewDynamicValue(cty.NullVal(plannedVal.Type()), plannedVal.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
plannedValRaw, err := plans.NewDynamicValue(plannedVal, plannedVal.Type())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
plan := testPlan(t)
|
|
|
|
plan.Changes.SyncWrapper().AppendResourceInstanceChange(&plans.ResourceInstanceChangeSrc{
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
2020-02-13 21:32:58 +01:00
|
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
2020-04-02 18:58:44 +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-19 20:08:25 +01:00
|
|
|
ChangeSrc: plans.ChangeSrc{
|
2019-12-06 17:53:43 +01:00
|
|
|
Action: action,
|
2018-12-19 20:08:25 +01:00
|
|
|
Before: priorValRaw,
|
|
|
|
After: plannedValRaw,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return testPlanFile(
|
|
|
|
t,
|
|
|
|
snap,
|
|
|
|
states.NewState(),
|
|
|
|
plan,
|
|
|
|
)
|
|
|
|
}
|
2019-01-12 00:13:55 +01:00
|
|
|
|
|
|
|
// this simplified plan struct allows us to preserve field order when marshaling
|
2019-02-11 22:17:03 +01:00
|
|
|
// the command output. NOTE: we are leaving "terraform_version" out of this test
|
|
|
|
// to avoid needing to constantly update the expected output; as a potential
|
|
|
|
// TODO we could write a jsonplan compare function.
|
2019-01-12 00:13:55 +01:00
|
|
|
type plan struct {
|
|
|
|
FormatVersion string `json:"format_version,omitempty"`
|
2019-02-11 22:17:03 +01:00
|
|
|
Variables map[string]interface{} `json:"variables,omitempty"`
|
2019-01-12 00:13:55 +01:00
|
|
|
PlannedValues map[string]interface{} `json:"planned_values,omitempty"`
|
2021-06-17 20:32:47 +02:00
|
|
|
ResourceDrift []interface{} `json:"resource_drift,omitempty"`
|
2019-01-12 00:13:55 +01:00
|
|
|
ResourceChanges []interface{} `json:"resource_changes,omitempty"`
|
|
|
|
OutputChanges map[string]interface{} `json:"output_changes,omitempty"`
|
2019-06-05 13:29:02 +02:00
|
|
|
PriorState priorState `json:"prior_state,omitempty"`
|
2019-02-12 21:03:07 +01:00
|
|
|
Config map[string]interface{} `json:"configuration,omitempty"`
|
2019-01-12 00:13:55 +01:00
|
|
|
}
|
2019-06-05 13:29:02 +02:00
|
|
|
|
|
|
|
type priorState struct {
|
2021-06-14 15:19:13 +02:00
|
|
|
FormatVersion string `json:"format_version,omitempty"`
|
|
|
|
Values map[string]interface{} `json:"values,omitempty"`
|
|
|
|
SensitiveValues map[string]bool `json:"sensitive_values,omitempty"`
|
2019-06-05 13:29:02 +02:00
|
|
|
}
|