2014-10-09 02:54:00 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-10-09 19:47:33 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-10-09 02:54:00 +02:00
|
|
|
|
2015-04-16 22:31:40 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
2014-10-09 02:54:00 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-05-12 21:58:10 +02:00
|
|
|
func TestDiffTags(t *testing.T) {
|
2014-10-09 19:47:33 +02:00
|
|
|
cases := []struct {
|
|
|
|
Old, New map[string]interface{}
|
|
|
|
Create, Remove map[string]string
|
|
|
|
}{
|
|
|
|
// Basic add/remove
|
|
|
|
{
|
|
|
|
Old: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
New: map[string]interface{}{
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
Create: map[string]string{
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
Remove: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Modify
|
|
|
|
{
|
|
|
|
Old: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
New: map[string]interface{}{
|
|
|
|
"foo": "baz",
|
|
|
|
},
|
|
|
|
Create: map[string]string{
|
|
|
|
"foo": "baz",
|
|
|
|
},
|
|
|
|
Remove: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
2015-05-12 21:58:10 +02:00
|
|
|
c, r := diffTags(tagsFromMap(tc.Old), tagsFromMap(tc.New))
|
|
|
|
cm := tagsToMap(c)
|
|
|
|
rm := tagsToMap(r)
|
2014-10-09 19:47:33 +02:00
|
|
|
if !reflect.DeepEqual(cm, tc.Create) {
|
2014-11-02 13:56:44 +01:00
|
|
|
t.Fatalf("%d: bad create: %#v", i, cm)
|
2014-10-09 19:47:33 +02:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(rm, tc.Remove) {
|
2014-11-02 13:56:44 +01:00
|
|
|
t.Fatalf("%d: bad remove: %#v", i, rm)
|
2014-10-09 19:47:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 02:54:00 +02:00
|
|
|
// testAccCheckTags can be used to check the tags on a resource.
|
2015-05-12 21:58:10 +02:00
|
|
|
func testAccCheckTags(
|
2015-04-16 22:31:40 +02:00
|
|
|
ts *[]*ec2.Tag, key string, value string) resource.TestCheckFunc {
|
2014-10-09 02:54:00 +02:00
|
|
|
return func(s *terraform.State) error {
|
2015-05-12 21:58:10 +02:00
|
|
|
m := tagsToMap(*ts)
|
2014-10-09 02:54:00 +02:00
|
|
|
v, ok := m[key]
|
2014-10-09 03:21:21 +02:00
|
|
|
if value != "" && !ok {
|
2014-10-09 02:54:00 +02:00
|
|
|
return fmt.Errorf("Missing tag: %s", key)
|
2014-10-09 03:21:21 +02:00
|
|
|
} else if value == "" && ok {
|
|
|
|
return fmt.Errorf("Extra tag: %s", key)
|
2014-10-09 02:54:00 +02:00
|
|
|
}
|
2014-10-09 03:21:21 +02:00
|
|
|
if value == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-09 02:54:00 +02:00
|
|
|
if v != value {
|
|
|
|
return fmt.Errorf("%s: bad value: %s", key, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|