2015-03-30 19:02:36 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2017-05-16 20:06:33 -05:00
|
|
|
"fmt"
|
2015-03-30 19:02:36 -05:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2016-05-08 04:14:13 -05:00
|
|
|
|
2018-04-06 13:07:39 -05:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-03-27 17:31:05 -05:00
|
|
|
"github.com/hashicorp/terraform/configs"
|
2015-03-30 19:02:36 -05:00
|
|
|
)
|
|
|
|
|
2016-09-13 12:44:44 -05:00
|
|
|
func TestParseResourceAddressInternal(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Input string
|
|
|
|
Expected *ResourceAddress
|
|
|
|
Output string
|
|
|
|
}{
|
|
|
|
"basic resource": {
|
|
|
|
"aws_instance.foo",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-09-13 12:44:44 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
"aws_instance.foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
"basic resource with count": {
|
|
|
|
"aws_instance.foo.1",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-09-13 12:44:44 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 1,
|
|
|
|
},
|
|
|
|
"aws_instance.foo[1]",
|
|
|
|
},
|
2016-10-16 20:27:02 -05:00
|
|
|
|
|
|
|
"data resource": {
|
|
|
|
"data.aws_ami.foo",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: DataResourceMode,
|
2016-10-16 20:27:02 -05:00
|
|
|
Type: "aws_ami",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
"data.aws_ami.foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
"data resource with count": {
|
|
|
|
"data.aws_ami.foo.1",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: DataResourceMode,
|
2016-10-16 20:27:02 -05:00
|
|
|
Type: "aws_ami",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 1,
|
|
|
|
},
|
|
|
|
"data.aws_ami.foo[1]",
|
|
|
|
},
|
|
|
|
|
|
|
|
"non-data resource with 4 elements": {
|
|
|
|
"aws_instance.foo.bar.1",
|
|
|
|
nil,
|
|
|
|
"",
|
|
|
|
},
|
2016-09-13 12:44:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for tn, tc := range cases {
|
|
|
|
t.Run(tc.Input, func(t *testing.T) {
|
|
|
|
out, err := parseResourceAddressInternal(tc.Input)
|
2016-10-16 20:27:02 -05:00
|
|
|
if (err != nil) != (tc.Expected == nil) {
|
2016-09-13 12:44:44 -05:00
|
|
|
t.Fatalf("%s: unexpected err: %#v", tn, err)
|
|
|
|
}
|
2016-10-16 20:27:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-09-13 12:44:44 -05:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(out, tc.Expected) {
|
|
|
|
t.Fatalf("bad: %q\n\nexpected:\n%#v\n\ngot:\n%#v", tn, tc.Expected, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare outputs if those exist
|
|
|
|
expected := tc.Input
|
|
|
|
if tc.Output != "" {
|
|
|
|
expected = tc.Output
|
|
|
|
}
|
|
|
|
if out.String() != expected {
|
|
|
|
t.Fatalf("bad: %q\n\nexpected: %s\n\ngot: %s", tn, expected, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare equality because the internal parse is used
|
|
|
|
// to compare equality to equal inputs.
|
|
|
|
if !out.Equals(tc.Expected) {
|
|
|
|
t.Fatalf("expected equality:\n\n%#v\n\n%#v", out, tc.Expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 19:02:36 -05:00
|
|
|
func TestParseResourceAddress(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Input string
|
|
|
|
Expected *ResourceAddress
|
2016-03-22 12:41:02 -05:00
|
|
|
Output string
|
2017-02-13 15:05:46 -06:00
|
|
|
Err bool
|
2015-03-30 19:02:36 -05:00
|
|
|
}{
|
2016-05-08 04:14:13 -05:00
|
|
|
"implicit primary managed instance, no specific index": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"aws_instance.foo",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-05-08 04:14:13 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2016-05-08 04:14:13 -05:00
|
|
|
},
|
|
|
|
"implicit primary data instance, no specific index": {
|
|
|
|
"data.aws_instance.foo",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: DataResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
2016-03-22 12:41:02 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2015-03-30 19:02:36 -05:00
|
|
|
},
|
|
|
|
"implicit primary, explicit index": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"aws_instance.foo[2]",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 2,
|
|
|
|
},
|
2016-03-22 12:41:02 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2015-03-30 19:02:36 -05:00
|
|
|
},
|
2016-01-07 14:43:43 -06:00
|
|
|
"implicit primary, explicit index over ten": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"aws_instance.foo[12]",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-01-07 14:43:43 -06:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 12,
|
|
|
|
},
|
2016-03-22 12:41:02 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2016-01-07 14:43:43 -06:00
|
|
|
},
|
2015-03-30 19:02:36 -05:00
|
|
|
"explicit primary, explicit index": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"aws_instance.foo.primary[2]",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-03-30 11:29:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
Index: 2,
|
2015-03-30 19:02:36 -05:00
|
|
|
},
|
2016-04-11 19:03:25 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2015-03-30 19:02:36 -05:00
|
|
|
},
|
|
|
|
"tainted": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"aws_instance.foo.tainted",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-03-30 11:29:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypeTainted,
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
Index: -1,
|
2015-03-30 19:02:36 -05:00
|
|
|
},
|
2016-03-22 12:41:02 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2015-03-30 19:02:36 -05:00
|
|
|
},
|
|
|
|
"deposed": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"aws_instance.foo.deposed",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-03-30 11:29:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypeDeposed,
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
Index: -1,
|
2015-03-30 19:02:36 -05:00
|
|
|
},
|
2016-03-22 12:41:02 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2015-03-30 19:02:36 -05:00
|
|
|
},
|
2015-04-30 15:49:37 -05:00
|
|
|
"with a hyphen": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"aws_instance.foo-bar",
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-04-30 15:49:37 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo-bar",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
2016-03-22 12:41:02 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2015-04-30 15:49:37 -05:00
|
|
|
},
|
2016-05-08 04:14:13 -05:00
|
|
|
"managed in a module": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"module.child.aws_instance.foo",
|
|
|
|
&ResourceAddress{
|
2015-05-01 11:01:49 -05:00
|
|
|
Path: []string{"child"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-05-08 04:14:13 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2016-05-08 04:14:13 -05:00
|
|
|
},
|
|
|
|
"data in a module": {
|
|
|
|
"module.child.data.aws_instance.foo",
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"child"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: DataResourceMode,
|
2015-05-01 11:01:49 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
2016-03-22 12:41:02 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2015-05-01 11:01:49 -05:00
|
|
|
},
|
|
|
|
"nested modules": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"module.a.module.b.module.forever.aws_instance.foo",
|
|
|
|
&ResourceAddress{
|
2015-05-01 11:01:49 -05:00
|
|
|
Path: []string{"a", "b", "forever"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-05-01 11:01:49 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
2016-03-22 12:41:02 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2015-05-01 11:01:49 -05:00
|
|
|
},
|
|
|
|
"just a module": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"module.a",
|
|
|
|
&ResourceAddress{
|
2015-05-01 11:01:49 -05:00
|
|
|
Path: []string{"a"},
|
|
|
|
Type: "",
|
|
|
|
Name: "",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
2016-03-22 12:41:02 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
2015-05-01 11:01:49 -05:00
|
|
|
},
|
|
|
|
"just a nested module": {
|
2016-03-22 12:41:02 -05:00
|
|
|
"module.a.module.b",
|
|
|
|
&ResourceAddress{
|
2015-05-01 11:01:49 -05:00
|
|
|
Path: []string{"a", "b"},
|
|
|
|
Type: "",
|
|
|
|
Name: "",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
2016-03-22 12:41:02 -05:00
|
|
|
"",
|
2017-02-13 15:05:46 -06:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
"module missing resource type": {
|
|
|
|
"module.name.foo",
|
|
|
|
nil,
|
|
|
|
"",
|
|
|
|
true,
|
2015-05-01 11:01:49 -05:00
|
|
|
},
|
2015-03-30 19:02:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for tn, tc := range cases {
|
2017-02-13 15:05:46 -06:00
|
|
|
t.Run(tn, func(t *testing.T) {
|
|
|
|
out, err := ParseResourceAddress(tc.Input)
|
|
|
|
if (err != nil) != tc.Err {
|
|
|
|
t.Fatalf("%s: unexpected err: %#v", tn, err)
|
|
|
|
}
|
|
|
|
if tc.Err {
|
|
|
|
return
|
|
|
|
}
|
2015-03-30 19:02:36 -05:00
|
|
|
|
2017-02-13 15:05:46 -06:00
|
|
|
if !reflect.DeepEqual(out, tc.Expected) {
|
|
|
|
t.Fatalf("bad: %q\n\nexpected:\n%#v\n\ngot:\n%#v", tn, tc.Expected, out)
|
|
|
|
}
|
2016-03-22 12:41:02 -05:00
|
|
|
|
2017-02-13 15:05:46 -06:00
|
|
|
expected := tc.Input
|
|
|
|
if tc.Output != "" {
|
|
|
|
expected = tc.Output
|
|
|
|
}
|
|
|
|
if out.String() != expected {
|
|
|
|
t.Fatalf("bad: %q\n\nexpected: %s\n\ngot: %s", tn, expected, out)
|
|
|
|
}
|
|
|
|
})
|
2015-03-30 19:02:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 18:59:20 -05:00
|
|
|
func TestResourceAddressContains(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Address *ResourceAddress
|
|
|
|
Other *ResourceAddress
|
|
|
|
Want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar"},
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar"},
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar", "baz"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar"},
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar", "baz"},
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar"},
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar", "baz", "foo", "pizza"},
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "bar",
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: DataResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar"},
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"baz"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar"},
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"baz", "bar"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: true,
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar", "baz"},
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"bar"},
|
|
|
|
InstanceTypeSet: false,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
Index: 1,
|
|
|
|
InstanceType: TypePrimary,
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
},
|
|
|
|
&ResourceAddress{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
Index: -1,
|
|
|
|
InstanceType: TypePrimary,
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-06-15 18:59:20 -05:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%s contains %s", test.Address, test.Other), func(t *testing.T) {
|
|
|
|
got := test.Address.Contains(test.Other)
|
|
|
|
if got != test.Want {
|
|
|
|
t.Errorf(
|
|
|
|
"wrong result\nrecv: %s\ngiven: %s\ngot: %#v\nwant: %#v",
|
|
|
|
test.Address, test.Other,
|
|
|
|
got, test.Want,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 19:02:36 -05:00
|
|
|
func TestResourceAddressEquals(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Address *ResourceAddress
|
|
|
|
Other interface{}
|
|
|
|
Expect bool
|
|
|
|
}{
|
|
|
|
"basic match": {
|
|
|
|
Address: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Expect: true,
|
|
|
|
},
|
|
|
|
"address does not set index": {
|
|
|
|
Address: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 3,
|
|
|
|
},
|
|
|
|
Expect: true,
|
|
|
|
},
|
|
|
|
"other does not set index": {
|
|
|
|
Address: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 3,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
Expect: true,
|
|
|
|
},
|
|
|
|
"neither sets index": {
|
|
|
|
Address: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
Expect: true,
|
|
|
|
},
|
2016-01-07 14:43:43 -06:00
|
|
|
"index over ten": {
|
|
|
|
Address: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-01-07 14:43:43 -06:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 1,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-01-07 14:43:43 -06:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 13,
|
|
|
|
},
|
|
|
|
Expect: false,
|
|
|
|
},
|
2015-03-30 19:02:36 -05:00
|
|
|
"different type": {
|
|
|
|
Address: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_vpc",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Expect: false,
|
|
|
|
},
|
2016-05-08 04:14:13 -05:00
|
|
|
"different mode": {
|
|
|
|
Address: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-05-08 04:14:13 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: DataResourceMode,
|
2016-05-08 04:14:13 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Expect: false,
|
|
|
|
},
|
2015-03-30 19:02:36 -05:00
|
|
|
"different name": {
|
|
|
|
Address: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "bar",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Expect: false,
|
|
|
|
},
|
|
|
|
"different instance type": {
|
|
|
|
Address: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypeTainted,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Expect: false,
|
|
|
|
},
|
|
|
|
"different index": {
|
|
|
|
Address: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-03-30 19:02:36 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 1,
|
|
|
|
},
|
|
|
|
Expect: false,
|
|
|
|
},
|
2016-05-08 04:14:13 -05:00
|
|
|
"module address matches address of managed resource inside module": {
|
2015-05-01 11:01:49 -05:00
|
|
|
Address: &ResourceAddress{
|
|
|
|
Path: []string{"a", "b"},
|
|
|
|
Type: "",
|
|
|
|
Name: "",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
|
|
|
Path: []string{"a", "b"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-05-01 11:01:49 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Expect: true,
|
|
|
|
},
|
2016-05-08 04:14:13 -05:00
|
|
|
"module address matches address of data resource inside module": {
|
|
|
|
Address: &ResourceAddress{
|
|
|
|
Path: []string{"a", "b"},
|
|
|
|
Type: "",
|
|
|
|
Name: "",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
|
|
|
Path: []string{"a", "b"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: DataResourceMode,
|
2016-05-08 04:14:13 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Expect: true,
|
|
|
|
},
|
|
|
|
"module address doesn't match managed resource outside module": {
|
|
|
|
Address: &ResourceAddress{
|
|
|
|
Path: []string{"a", "b"},
|
|
|
|
Type: "",
|
|
|
|
Name: "",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
|
|
|
Path: []string{"a"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-05-08 04:14:13 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Expect: false,
|
|
|
|
},
|
|
|
|
"module address doesn't match data resource outside module": {
|
2015-05-01 11:01:49 -05:00
|
|
|
Address: &ResourceAddress{
|
|
|
|
Path: []string{"a", "b"},
|
|
|
|
Type: "",
|
|
|
|
Name: "",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
|
|
|
Path: []string{"a"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: DataResourceMode,
|
2015-05-01 11:01:49 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Expect: false,
|
|
|
|
},
|
|
|
|
"nil path vs empty path should match": {
|
|
|
|
Address: &ResourceAddress{
|
|
|
|
Path: []string{},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-05-01 11:01:49 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
Other: &ResourceAddress{
|
|
|
|
Path: nil,
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-05-01 11:01:49 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Expect: true,
|
|
|
|
},
|
2015-03-30 19:02:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for tn, tc := range cases {
|
|
|
|
actual := tc.Address.Equals(tc.Other)
|
|
|
|
if actual != tc.Expect {
|
|
|
|
t.Fatalf("%q: expected equals: %t, got %t for:\n%#v\n%#v",
|
|
|
|
tn, tc.Expect, actual, tc.Address, tc.Other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 19:55:01 -05:00
|
|
|
|
|
|
|
func TestResourceAddressStateId(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Input *ResourceAddress
|
|
|
|
Expected string
|
|
|
|
}{
|
|
|
|
"basic resource": {
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-09-13 19:55:01 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
"aws_instance.foo",
|
|
|
|
},
|
|
|
|
|
2016-11-29 11:16:18 -06:00
|
|
|
"basic resource with index": {
|
2016-09-13 19:55:01 -05:00
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2016-09-13 19:55:01 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: 2,
|
|
|
|
},
|
2016-11-29 11:16:18 -06:00
|
|
|
"aws_instance.foo.2",
|
2016-09-13 19:55:01 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
"data resource": {
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: DataResourceMode,
|
2016-09-13 19:55:01 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
InstanceType: TypePrimary,
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
"data.aws_instance.foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for tn, tc := range cases {
|
|
|
|
t.Run(tn, func(t *testing.T) {
|
|
|
|
actual := tc.Input.stateId()
|
|
|
|
if actual != tc.Expected {
|
|
|
|
t.Fatalf("bad: %q\n\nexpected: %s\n\ngot: %s", tn, tc.Expected, actual)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-05-16 20:01:52 -05:00
|
|
|
|
|
|
|
func TestResourceAddressHasResourceSpec(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"module.foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"module.foo.module.bar",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"null_resource.baz",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"null_resource.baz[0]",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data.null_data_source.baz",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data.null_data_source.baz[0]",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"module.foo.null_resource.baz",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"module.foo.data.null_data_source.baz",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"module.foo.module.bar.null_resource.baz",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cases {
|
|
|
|
t.Run(test.Input, func(t *testing.T) {
|
|
|
|
addr, err := ParseResourceAddress(test.Input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error parsing address: %s", err)
|
|
|
|
}
|
|
|
|
got := addr.HasResourceSpec()
|
|
|
|
if got != test.Want {
|
|
|
|
t.Fatalf("%q: wrong result %#v; want %#v", test.Input, got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-05-16 20:03:54 -05:00
|
|
|
|
|
|
|
func TestResourceAddressWholeModuleAddress(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"module.foo",
|
|
|
|
"module.foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"module.foo.module.bar",
|
|
|
|
"module.foo.module.bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"null_resource.baz",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"null_resource.baz[0]",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data.null_data_source.baz",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data.null_data_source.baz[0]",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"module.foo.null_resource.baz",
|
|
|
|
"module.foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"module.foo.data.null_data_source.baz",
|
|
|
|
"module.foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"module.foo.module.bar.null_resource.baz",
|
|
|
|
"module.foo.module.bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cases {
|
|
|
|
t.Run(test.Input, func(t *testing.T) {
|
|
|
|
addr, err := ParseResourceAddress(test.Input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error parsing address: %s", err)
|
|
|
|
}
|
|
|
|
gotAddr := addr.WholeModuleAddress()
|
|
|
|
got := gotAddr.String()
|
|
|
|
if got != test.Want {
|
|
|
|
t.Fatalf("%q: wrong result %#v; want %#v", test.Input, got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-05-16 20:06:33 -05:00
|
|
|
|
2018-04-06 13:07:39 -05:00
|
|
|
func TestResourceAddressMatchesResourceConfig(t *testing.T) {
|
2018-03-27 17:31:05 -05:00
|
|
|
root := []string(nil)
|
|
|
|
child := []string{"child"}
|
|
|
|
grandchild := []string{"child", "grandchild"}
|
|
|
|
irrelevant := []string{"irrelevant"}
|
2017-05-16 20:06:33 -05:00
|
|
|
|
|
|
|
tests := []struct {
|
2018-03-27 17:31:05 -05:00
|
|
|
Addr *ResourceAddress
|
|
|
|
ModulePath []string
|
2018-04-06 13:07:39 -05:00
|
|
|
Resource *configs.Resource
|
2018-03-27 17:31:05 -05:00
|
|
|
Want bool
|
2017-05-16 20:06:33 -05:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
root,
|
2018-04-06 13:07:39 -05:00
|
|
|
&configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"child"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
child,
|
2018-04-06 13:07:39 -05:00
|
|
|
&configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"child", "grandchild"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
grandchild,
|
2018-04-06 13:07:39 -05:00
|
|
|
&configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"child"},
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
child,
|
2018-04-06 13:07:39 -05:00
|
|
|
&configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"child", "grandchild"},
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
grandchild,
|
2018-04-06 13:07:39 -05:00
|
|
|
&configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: DataResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
Index: -1,
|
|
|
|
},
|
2018-03-27 17:31:05 -05:00
|
|
|
irrelevant,
|
2018-04-06 13:07:39 -05:00
|
|
|
&configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
Index: -1,
|
|
|
|
},
|
2018-03-27 17:31:05 -05:00
|
|
|
irrelevant,
|
2018-04-06 13:07:39 -05:00
|
|
|
&configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "pizza",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
Index: -1,
|
|
|
|
},
|
2018-03-27 17:31:05 -05:00
|
|
|
irrelevant,
|
2018-04-06 13:07:39 -05:00
|
|
|
&configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"child", "grandchild"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
child,
|
2018-04-06 13:07:39 -05:00
|
|
|
&configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ResourceAddress{
|
|
|
|
Path: []string{"child"},
|
2019-08-07 16:50:59 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
Index: -1,
|
|
|
|
},
|
|
|
|
grandchild,
|
2018-04-06 13:07:39 -05:00
|
|
|
&configs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
2017-05-16 20:06:33 -05:00
|
|
|
Type: "null_resource",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%02d-%s", i, test.Addr), func(t *testing.T) {
|
2018-04-06 13:07:39 -05:00
|
|
|
got := test.Addr.MatchesResourceConfig(test.ModulePath, test.Resource)
|
2017-05-16 20:06:33 -05:00
|
|
|
if got != test.Want {
|
|
|
|
t.Errorf(
|
|
|
|
"wrong result\naddr: %s\nmod: %#v\nrsrc: %#v\ngot: %#v\nwant: %#v",
|
2018-03-27 17:31:05 -05:00
|
|
|
test.Addr, test.ModulePath, test.Resource, got, test.Want,
|
2017-05-16 20:06:33 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-06-21 19:43:56 -05:00
|
|
|
|
|
|
|
func TestResourceAddressLess(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
A string
|
|
|
|
B string
|
|
|
|
Want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"foo.bar",
|
|
|
|
"module.baz.foo.bar",
|
|
|
|
true,
|
|
|
|
},
|
2017-08-22 20:36:17 -05:00
|
|
|
{
|
|
|
|
"module.baz.foo.bar",
|
|
|
|
"zzz.bar", // would sort after "module" in lexicographical sort
|
|
|
|
false,
|
|
|
|
},
|
2017-06-21 19:43:56 -05:00
|
|
|
{
|
|
|
|
"module.baz.foo.bar",
|
|
|
|
"module.baz.foo.bar",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"module.baz.foo.bar",
|
|
|
|
"module.boz.foo.bar",
|
|
|
|
true,
|
|
|
|
},
|
2017-08-22 20:36:17 -05:00
|
|
|
{
|
|
|
|
"module.boz.foo.bar",
|
|
|
|
"module.baz.foo.bar",
|
|
|
|
false,
|
|
|
|
},
|
2017-06-21 19:43:56 -05:00
|
|
|
{
|
|
|
|
"a.b",
|
|
|
|
"b.c",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a.b",
|
|
|
|
"a.c",
|
|
|
|
true,
|
|
|
|
},
|
2017-08-22 20:36:17 -05:00
|
|
|
{
|
|
|
|
"c.b",
|
|
|
|
"b.c",
|
|
|
|
false,
|
|
|
|
},
|
2017-06-21 19:43:56 -05:00
|
|
|
{
|
|
|
|
"a.b[9]",
|
|
|
|
"a.b[10]",
|
|
|
|
true,
|
|
|
|
},
|
2017-08-22 20:36:17 -05:00
|
|
|
{
|
|
|
|
"b.b[9]",
|
|
|
|
"a.b[10]",
|
|
|
|
false,
|
|
|
|
},
|
2017-06-21 19:43:56 -05:00
|
|
|
{
|
|
|
|
"a.b",
|
|
|
|
"a.b.deposed",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a.b.tainted",
|
|
|
|
"a.b.deposed",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%s < %s", test.A, test.B), func(t *testing.T) {
|
|
|
|
addrA, err := ParseResourceAddress(test.A)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addrB, err := ParseResourceAddress(test.B)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
got := addrA.Less(addrB)
|
|
|
|
invGot := addrB.Less(addrA)
|
|
|
|
if got != test.Want {
|
|
|
|
t.Errorf(
|
|
|
|
"wrong result\ntest: %s < %s\ngot: %#v\nwant: %#v",
|
|
|
|
test.A, test.B, got, test.Want,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if test.A != test.B { // inverse test doesn't apply when equal
|
|
|
|
if invGot != !test.Want {
|
|
|
|
t.Errorf(
|
|
|
|
"wrong inverse result\ntest: %s < %s\ngot: %#v\nwant: %#v",
|
|
|
|
test.B, test.A, invGot, !test.Want,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if invGot != test.Want {
|
|
|
|
t.Errorf(
|
|
|
|
"wrong inverse result\ntest: %s < %s\ngot: %#v\nwant: %#v",
|
|
|
|
test.B, test.A, invGot, test.Want,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|