|
|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package format
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
|
@ -18,46 +19,6 @@ var disabledColorize = &colorstring.Colorize{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestState(t *testing.T) {
|
|
|
|
|
state := states.NewState()
|
|
|
|
|
|
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
|
if rootModule == nil {
|
|
|
|
|
t.Errorf("root module is nil; want valid object")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"}`),
|
|
|
|
|
},
|
|
|
|
|
addrs.ProviderConfig{
|
|
|
|
|
Type: "test",
|
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
|
)
|
|
|
|
|
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"}`),
|
|
|
|
|
},
|
|
|
|
|
addrs.ProviderConfig{
|
|
|
|
|
Type: "test",
|
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
State *StateOpts
|
|
|
|
|
Want string
|
|
|
|
|
@ -72,11 +33,11 @@ func TestState(t *testing.T) {
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
&StateOpts{
|
|
|
|
|
State: state,
|
|
|
|
|
State: basicState(t),
|
|
|
|
|
Color: disabledColorize,
|
|
|
|
|
Schemas: testSchemas(),
|
|
|
|
|
},
|
|
|
|
|
TestOutput,
|
|
|
|
|
basicStateOutput,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
&StateOpts{
|
|
|
|
|
@ -84,18 +45,28 @@ func TestState(t *testing.T) {
|
|
|
|
|
Color: disabledColorize,
|
|
|
|
|
Schemas: testSchemas(),
|
|
|
|
|
},
|
|
|
|
|
nestedTestOutput,
|
|
|
|
|
nestedStateOutput,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
&StateOpts{
|
|
|
|
|
State: stateWithMoreOutputs(t),
|
|
|
|
|
Color: disabledColorize,
|
|
|
|
|
Schemas: testSchemas(),
|
|
|
|
|
},
|
|
|
|
|
stateWithMoreOutputsOutput,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
got := State(tt.State)
|
|
|
|
|
if got != tt.Want {
|
|
|
|
|
t.Errorf(
|
|
|
|
|
"wrong result\ninput: %v\ngot: \n%s\nwant: \n%s",
|
|
|
|
|
tt.State.State, got, tt.Want,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -157,12 +128,12 @@ func testSchemas() *terraform.Schemas {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const TestOutput = `# data.test_data_source.data:
|
|
|
|
|
const basicStateOutput = `# data.test_data_source.data:
|
|
|
|
|
data "test_data_source" "data" {
|
|
|
|
|
compute = "sure"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# test_resource.baz[0]:
|
|
|
|
|
# test_resource.baz[0]:
|
|
|
|
|
resource "test_resource" "baz" {
|
|
|
|
|
woozles = "confuzles"
|
|
|
|
|
}
|
|
|
|
|
@ -172,16 +143,109 @@ Outputs:
|
|
|
|
|
|
|
|
|
|
bar = "bar value"`
|
|
|
|
|
|
|
|
|
|
const nestedTestOutput = `# test_resource.baz[0]:
|
|
|
|
|
const nestedStateOutput = `# test_resource.baz[0]:
|
|
|
|
|
resource "test_resource" "baz" {
|
|
|
|
|
woozles = "confuzles"
|
|
|
|
|
|
|
|
|
|
nested {
|
|
|
|
|
value = "42"
|
|
|
|
|
}
|
|
|
|
|
}`
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
`
|
|
|
|
|
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"}`),
|
|
|
|
|
},
|
|
|
|
|
addrs.ProviderConfig{
|
|
|
|
|
Type: "test",
|
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
|
)
|
|
|
|
|
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"}`),
|
|
|
|
|
},
|
|
|
|
|
addrs.ProviderConfig{
|
|
|
|
|
Type: "test",
|
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
|
)
|
|
|
|
|
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"}`),
|
|
|
|
|
},
|
|
|
|
|
addrs.ProviderConfig{
|
|
|
|
|
Type: "test",
|
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
|
)
|
|
|
|
|
return state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nestedState(t *testing.T) *states.State {
|
|
|
|
|
state := states.NewState()
|
|
|
|
|
|