2018-09-24 18:30:39 +02:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
2019-05-16 15:52:06 +02:00
|
|
|
"fmt"
|
2018-09-24 18:30:39 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-09-25 00:00:07 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
|
|
"github.com/hashicorp/terraform/providers"
|
2018-09-24 18:30:39 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2018-09-25 00:00:07 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2018-09-24 18:30:39 +02:00
|
|
|
"github.com/mitchellh/colorstring"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
var disabledColorize = &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
|
|
|
Disable: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestState(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
State *StateOpts
|
|
|
|
Want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&StateOpts{
|
2018-09-25 00:00:07 +02:00
|
|
|
State: &states.State{},
|
|
|
|
Color: disabledColorize,
|
|
|
|
Schemas: &terraform.Schemas{},
|
2018-09-24 18:30:39 +02:00
|
|
|
},
|
|
|
|
"The state file is empty. No resources are represented.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&StateOpts{
|
2019-05-16 15:52:06 +02:00
|
|
|
State: basicState(t),
|
2018-09-25 00:00:07 +02:00
|
|
|
Color: disabledColorize,
|
|
|
|
Schemas: testSchemas(),
|
2018-09-24 18:30:39 +02:00
|
|
|
},
|
2019-05-16 15:52:06 +02:00
|
|
|
basicStateOutput,
|
2018-09-24 18:30:39 +02:00
|
|
|
},
|
2019-01-30 19:08:59 +01:00
|
|
|
{
|
|
|
|
&StateOpts{
|
|
|
|
State: nestedState(t),
|
|
|
|
Color: disabledColorize,
|
|
|
|
Schemas: testSchemas(),
|
|
|
|
},
|
2019-05-16 15:52:06 +02:00
|
|
|
nestedStateOutput,
|
|
|
|
},
|
2019-07-19 22:39:16 +02:00
|
|
|
{
|
|
|
|
&StateOpts{
|
|
|
|
State: deposedState(t),
|
|
|
|
Color: disabledColorize,
|
|
|
|
Schemas: testSchemas(),
|
|
|
|
},
|
|
|
|
deposedNestedStateOutput,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&StateOpts{
|
|
|
|
State: onlyDeposedState(t),
|
|
|
|
Color: disabledColorize,
|
|
|
|
Schemas: testSchemas(),
|
|
|
|
},
|
|
|
|
onlyDeposedOutput,
|
|
|
|
},
|
2019-05-16 15:52:06 +02:00
|
|
|
{
|
|
|
|
&StateOpts{
|
|
|
|
State: stateWithMoreOutputs(t),
|
|
|
|
Color: disabledColorize,
|
|
|
|
Schemas: testSchemas(),
|
|
|
|
},
|
|
|
|
stateWithMoreOutputsOutput,
|
2019-01-30 19:08:59 +01:00
|
|
|
},
|
2018-09-24 18:30:39 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 15:52:06 +02:00
|
|
|
for i, tt := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
|
|
|
got := State(tt.State)
|
|
|
|
if got != tt.Want {
|
|
|
|
t.Errorf(
|
|
|
|
"wrong result\ninput: %v\ngot: \n%q\nwant: \n%q",
|
|
|
|
tt.State.State, got, tt.Want,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2018-09-24 18:30:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 00:00:07 +02:00
|
|
|
func testProvider() *terraform.MockProvider {
|
|
|
|
p := new(terraform.MockProvider)
|
|
|
|
p.ReadResourceFn = func(req providers.ReadResourceRequest) providers.ReadResourceResponse {
|
|
|
|
return providers.ReadResourceResponse{NewState: req.PriorState}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.GetSchemaReturn = testProviderSchema()
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func testProviderSchema() *terraform.ProviderSchema {
|
|
|
|
return &terraform.ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"region": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_resource": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Computed: true},
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
"woozles": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2019-01-30 19:08:59 +01:00
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
"nested": {
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
Block: configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"compute": {Type: cty.String, Optional: true},
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-09-25 00:00:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
DataSources: map[string]*configschema.Block{
|
|
|
|
"test_data_source": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"compute": {Type: cty.String, Optional: true},
|
|
|
|
"value": {Type: cty.String, Computed: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSchemas() *terraform.Schemas {
|
|
|
|
provider := testProvider()
|
|
|
|
return &terraform.Schemas{
|
2020-02-03 14:18:04 +01:00
|
|
|
Providers: map[addrs.Provider]*terraform.ProviderSchema{
|
2020-10-05 14:33:49 +02:00
|
|
|
addrs.NewDefaultProvider("test"): provider.GetSchemaReturn,
|
2018-09-25 00:00:07 +02:00
|
|
|
},
|
2018-09-24 18:30:39 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-26 19:11:18 +02:00
|
|
|
|
2019-05-16 15:52:06 +02:00
|
|
|
const basicStateOutput = `# data.test_data_source.data:
|
2019-02-19 23:18:47 +01:00
|
|
|
data "test_data_source" "data" {
|
|
|
|
compute = "sure"
|
|
|
|
}
|
|
|
|
|
2019-05-16 15:52:06 +02:00
|
|
|
# test_resource.baz[0]:
|
2018-09-26 19:11:18 +02:00
|
|
|
resource "test_resource" "baz" {
|
|
|
|
woozles = "confuzles"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
|
|
|
|
bar = "bar value"`
|
2019-01-30 19:08:59 +01:00
|
|
|
|
2019-05-16 15:52:06 +02:00
|
|
|
const nestedStateOutput = `# test_resource.baz[0]:
|
2019-01-30 19:08:59 +01:00
|
|
|
resource "test_resource" "baz" {
|
|
|
|
woozles = "confuzles"
|
|
|
|
|
|
|
|
nested {
|
|
|
|
value = "42"
|
|
|
|
}
|
2019-05-16 15:52:06 +02:00
|
|
|
}`
|
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
const deposedNestedStateOutput = `# test_resource.baz[0]:
|
|
|
|
resource "test_resource" "baz" {
|
|
|
|
woozles = "confuzles"
|
|
|
|
|
|
|
|
nested {
|
|
|
|
value = "42"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# test_resource.baz[0]: (deposed object 1234)
|
|
|
|
resource "test_resource" "baz" {
|
|
|
|
woozles = "confuzles"
|
|
|
|
|
|
|
|
nested {
|
|
|
|
value = "42"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
const onlyDeposedOutput = `# test_resource.baz[0]:
|
|
|
|
# test_resource.baz[0]: (deposed object 1234)
|
|
|
|
resource "test_resource" "baz" {
|
|
|
|
woozles = "confuzles"
|
|
|
|
|
|
|
|
nested {
|
|
|
|
value = "42"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# test_resource.baz[0]: (deposed object 5678)
|
|
|
|
resource "test_resource" "baz" {
|
|
|
|
woozles = "confuzles"
|
|
|
|
|
|
|
|
nested {
|
|
|
|
value = "42"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2019-05-16 15:52:06 +02:00
|
|
|
const stateWithMoreOutputsOutput = `# test_resource.baz[0]:
|
|
|
|
resource "test_resource" "baz" {
|
|
|
|
woozles = "confuzles"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
|
|
|
|
bool_var = true
|
|
|
|
int_var = 42
|
|
|
|
map_var = {
|
|
|
|
"first" = "foo"
|
|
|
|
"second" = "bar"
|
2019-01-30 19:08:59 +01:00
|
|
|
}
|
2019-05-16 15:52:06 +02:00
|
|
|
sensitive_var = "secret!!!"
|
|
|
|
string_var = "string value"`
|
|
|
|
|
|
|
|
func basicState(t *testing.T) *states.State {
|
|
|
|
state := states.NewState()
|
|
|
|
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
if rootModule == nil {
|
|
|
|
t.Errorf("root module is nil; want valid object")
|
|
|
|
}
|
2019-01-30 19:08:59 +01:00
|
|
|
|
2019-05-16 15:52:06 +02:00
|
|
|
rootModule.SetLocalValue("foo", cty.StringVal("foo value"))
|
|
|
|
rootModule.SetOutputValue("bar", cty.StringVal("bar value"), false)
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_resource",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +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-05-16 15:52:06 +02:00
|
|
|
)
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.DataResourceMode,
|
|
|
|
Type: "test_data_source",
|
|
|
|
Name: "data",
|
|
|
|
}.Instance(addrs.NoKey),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"compute":"sure"}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +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-05-16 15:52:06 +02:00
|
|
|
)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
func stateWithMoreOutputs(t *testing.T) *states.State {
|
|
|
|
state := states.NewState()
|
|
|
|
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
if rootModule == nil {
|
|
|
|
t.Errorf("root module is nil; want valid object")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootModule.SetOutputValue("string_var", cty.StringVal("string value"), false)
|
|
|
|
rootModule.SetOutputValue("int_var", cty.NumberIntVal(42), false)
|
|
|
|
rootModule.SetOutputValue("bool_var", cty.BoolVal(true), false)
|
|
|
|
rootModule.SetOutputValue("sensitive_var", cty.StringVal("secret!!!"), true)
|
|
|
|
rootModule.SetOutputValue("map_var", cty.MapVal(map[string]cty.Value{
|
|
|
|
"first": cty.StringVal("foo"),
|
|
|
|
"second": cty.StringVal("bar"),
|
|
|
|
}), false)
|
|
|
|
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_resource",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +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-05-16 15:52:06 +02:00
|
|
|
)
|
|
|
|
return state
|
|
|
|
}
|
2019-01-30 19:08:59 +01:00
|
|
|
|
|
|
|
func nestedState(t *testing.T) *states.State {
|
|
|
|
state := states.NewState()
|
|
|
|
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
if rootModule == nil {
|
|
|
|
t.Errorf("root module is nil; want valid object")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_resource",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles","nested": [{"value": "42"}]}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +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-01-30 19:08:59 +01:00
|
|
|
)
|
|
|
|
return state
|
|
|
|
}
|
2019-07-19 22:39:16 +02:00
|
|
|
|
|
|
|
func deposedState(t *testing.T) *states.State {
|
|
|
|
state := nestedState(t)
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
rootModule.SetResourceInstanceDeposed(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_resource",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
states.DeposedKey("1234"),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles","nested": [{"value": "42"}]}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +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-07-19 22:39:16 +02:00
|
|
|
)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
// replicate a corrupt resource where only a deposed exists
|
|
|
|
func onlyDeposedState(t *testing.T) *states.State {
|
|
|
|
state := states.NewState()
|
|
|
|
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
if rootModule == nil {
|
|
|
|
t.Errorf("root module is nil; want valid object")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootModule.SetResourceInstanceDeposed(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_resource",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
states.DeposedKey("1234"),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles","nested": [{"value": "42"}]}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +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-07-19 22:39:16 +02:00
|
|
|
)
|
|
|
|
rootModule.SetResourceInstanceDeposed(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_resource",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
states.DeposedKey("5678"),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles","nested": [{"value": "42"}]}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +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-07-19 22:39:16 +02:00
|
|
|
)
|
|
|
|
return state
|
|
|
|
}
|