terraform: test for ResourceAddress.stateId()

This commit is contained in:
Mitchell Hashimoto 2016-09-13 17:55:01 -07:00
parent 5828a0a9ac
commit dcc3eb3011
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 49 additions and 1 deletions

View File

@ -88,7 +88,6 @@ func (r *ResourceAddress) String() string {
// stateId returns the ID that this resource should be entered with // stateId returns the ID that this resource should be entered with
// in the state. This is also used for diffs. In the future, we'd like to // in the state. This is also used for diffs. In the future, we'd like to
// move away from this string field so I don't export this. // move away from this string field so I don't export this.
// TODO: test
func (r *ResourceAddress) stateId() string { func (r *ResourceAddress) stateId() string {
result := fmt.Sprintf("%s.%s", r.Type, r.Name) result := fmt.Sprintf("%s.%s", r.Type, r.Name)
switch r.Mode { switch r.Mode {

View File

@ -521,3 +521,52 @@ func TestResourceAddressEquals(t *testing.T) {
} }
} }
} }
func TestResourceAddressStateId(t *testing.T) {
cases := map[string]struct {
Input *ResourceAddress
Expected string
}{
"basic resource": {
&ResourceAddress{
Mode: config.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
InstanceType: TypePrimary,
Index: -1,
},
"aws_instance.foo",
},
"basic resource ignores count": {
&ResourceAddress{
Mode: config.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
InstanceType: TypePrimary,
Index: 2,
},
"aws_instance.foo",
},
"data resource": {
&ResourceAddress{
Mode: config.DataResourceMode,
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)
}
})
}
}