2018-09-24 18:30:39 +02:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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) {
|
|
|
|
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,
|
2018-09-25 00:00:07 +02:00
|
|
|
Type: "test_resource",
|
2018-09-24 18:30:39 +02:00
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
|
|
|
},
|
|
|
|
addrs.ProviderConfig{
|
|
|
|
Type: "test",
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2018-09-25 00:00:07 +02:00
|
|
|
State: state,
|
|
|
|
Color: disabledColorize,
|
|
|
|
Schemas: testSchemas(),
|
2018-09-24 18:30:39 +02:00
|
|
|
},
|
2018-09-26 19:11:18 +02:00
|
|
|
TestOutput,
|
2018-09-24 18:30:39 +02:00
|
|
|
},
|
2019-01-30 19:08:59 +01:00
|
|
|
{
|
|
|
|
&StateOpts{
|
|
|
|
State: nestedState(t),
|
|
|
|
Color: disabledColorize,
|
|
|
|
Schemas: testSchemas(),
|
|
|
|
},
|
|
|
|
nestedTestOutput,
|
|
|
|
},
|
2018-09-24 18:30:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
got := State(tt.State)
|
|
|
|
if got != tt.Want {
|
|
|
|
t.Errorf(
|
2018-09-26 19:11:18 +02:00
|
|
|
"wrong result\ninput: %v\ngot: \n%s\nwant: \n%s",
|
2018-09-24 18:30:39 +02:00
|
|
|
tt.State.State, got, tt.Want,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
Providers: map[string]*terraform.ProviderSchema{
|
|
|
|
"test": provider.GetSchemaReturn,
|
|
|
|
},
|
2018-09-24 18:30:39 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-26 19:11:18 +02:00
|
|
|
|
|
|
|
const TestOutput = `# test_resource.baz[0]:
|
|
|
|
resource "test_resource" "baz" {
|
|
|
|
woozles = "confuzles"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
|
|
|
|
bar = "bar value"`
|
2019-01-30 19:08:59 +01:00
|
|
|
|
|
|
|
const nestedTestOutput = `# test_resource.baz[0]:
|
|
|
|
resource "test_resource" "baz" {
|
|
|
|
woozles = "confuzles"
|
|
|
|
|
|
|
|
nested {
|
|
|
|
value = "42"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
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"}]}`),
|
|
|
|
},
|
|
|
|
addrs.ProviderConfig{
|
|
|
|
Type: "test",
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
return state
|
|
|
|
}
|