2014-07-13 04:47:31 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2019-01-12 00:13:55 +01:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-10-11 21:56:55 +02:00
|
|
|
"path/filepath"
|
2014-07-13 04:47:31 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-01-12 00:13:55 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2018-12-19 20:08:25 +01:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
|
|
"github.com/hashicorp/terraform/plans"
|
|
|
|
"github.com/hashicorp/terraform/providers"
|
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
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)
|
|
|
|
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,
|
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) {
|
2014-10-11 21:56:55 +02:00
|
|
|
// Create the default state
|
2018-03-28 19:08:38 +02:00
|
|
|
statePath := testStateFile(t, testState())
|
|
|
|
defer testChdir(t, filepath.Dir(statePath))()
|
2014-10-11 21:56:55 +02:00
|
|
|
|
2014-07-13 04:47:31 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
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,
|
2014-07-13 18:22:23 +02:00
|
|
|
},
|
2014-07-13 04:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
2014-10-11 21:56:55 +02:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShow_noArgsNoState(t *testing.T) {
|
|
|
|
// Create the default state
|
2018-03-28 19:08:38 +02:00
|
|
|
statePath := testStateFile(t, testState())
|
|
|
|
defer testChdir(t, filepath.Dir(statePath))()
|
2014-10-11 21:56:55 +02:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ShowCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2014-10-11 21:56:55 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2014-07-13 04:47:31 +02:00
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShow_plan(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
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
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,
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
func TestShow_plan_json(t *testing.T) {
|
|
|
|
planPath := showFixturePlanFile(t)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ShowCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(showFixtureProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
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,
|
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-01-12 00:13:55 +01:00
|
|
|
fixtureDir := "test-fixtures/show-json"
|
|
|
|
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) {
|
|
|
|
inputDir := filepath.Join(fixtureDir, entry.Name())
|
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(inputDir); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
p := showFixtureProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
pc := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
// showFixtureSchema returns a schema suitable for processing the configuration
|
|
|
|
// in test-fixtures/show. This schema should be assigned to a mock provider
|
|
|
|
// named "test".
|
|
|
|
func showFixtureSchema() *terraform.ProviderSchema {
|
|
|
|
return &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// showFixtureProvider returns a mock provider that is configured for basic
|
|
|
|
// operation with the configuration in test-fixtures/show. This mock has
|
|
|
|
// GetSchemaReturn, PlanResourceChangeFn, and ApplyResourceChangeFn populated,
|
|
|
|
// with the plan/apply steps just passing through the data determined by
|
|
|
|
// Terraform Core.
|
|
|
|
func showFixtureProvider() *terraform.MockProvider {
|
|
|
|
p := testProvider()
|
|
|
|
p.GetSchemaReturn = showFixtureSchema()
|
|
|
|
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)
|
|
|
|
}
|
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,
|
|
|
|
}),
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// showFixturePlanFile creates a plan file at a temporary location containing a
|
|
|
|
// single change to create the test_instance.foo that is included in the "show"
|
|
|
|
// test fixture, returning the location of that plan file.
|
|
|
|
func showFixturePlanFile(t *testing.T) string {
|
|
|
|
_, 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.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.Create,
|
|
|
|
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
|
|
|
|
// the command output.
|
|
|
|
type plan struct {
|
|
|
|
FormatVersion string `json:"format_version,omitempty"`
|
|
|
|
PlannedValues map[string]interface{} `json:"planned_values,omitempty"`
|
|
|
|
ResourceChanges []interface{} `json:"resource_changes,omitempty"`
|
|
|
|
OutputChanges map[string]interface{} `json:"output_changes,omitempty"`
|
|
|
|
PriorState string `json:"prior_state,omitempty"`
|
|
|
|
Config string `json:"configuration,omitempty"`
|
|
|
|
}
|