github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/tagsCloudtrail_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/service/cloudtrail"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestDiffCloudtrailTags(t *testing.T) {
    14  	cases := []struct {
    15  		Old, New       map[string]interface{}
    16  		Create, Remove map[string]string
    17  	}{
    18  		// Basic add/remove
    19  		{
    20  			Old: map[string]interface{}{
    21  				"foo": "bar",
    22  			},
    23  			New: map[string]interface{}{
    24  				"bar": "baz",
    25  			},
    26  			Create: map[string]string{
    27  				"bar": "baz",
    28  			},
    29  			Remove: map[string]string{
    30  				"foo": "bar",
    31  			},
    32  		},
    33  
    34  		// Modify
    35  		{
    36  			Old: map[string]interface{}{
    37  				"foo": "bar",
    38  			},
    39  			New: map[string]interface{}{
    40  				"foo": "baz",
    41  			},
    42  			Create: map[string]string{
    43  				"foo": "baz",
    44  			},
    45  			Remove: map[string]string{
    46  				"foo": "bar",
    47  			},
    48  		},
    49  	}
    50  
    51  	for i, tc := range cases {
    52  		c, r := diffTagsCloudtrail(tagsFromMapCloudtrail(tc.Old), tagsFromMapCloudtrail(tc.New))
    53  		cm := tagsToMapCloudtrail(c)
    54  		rm := tagsToMapCloudtrail(r)
    55  		if !reflect.DeepEqual(cm, tc.Create) {
    56  			t.Fatalf("%d: bad create: %#v", i, cm)
    57  		}
    58  		if !reflect.DeepEqual(rm, tc.Remove) {
    59  			t.Fatalf("%d: bad remove: %#v", i, rm)
    60  		}
    61  	}
    62  }
    63  
    64  // testAccCheckCloudTrailCheckTags can be used to check the tags on a trail
    65  func testAccCheckCloudTrailCheckTags(tags *[]*cloudtrail.Tag, expectedTags map[string]string) resource.TestCheckFunc {
    66  	return func(s *terraform.State) error {
    67  		if !reflect.DeepEqual(expectedTags, tagsToMapCloudtrail(*tags)) {
    68  			return fmt.Errorf("Tags mismatch.\nExpected: %#v\nGiven: %#v",
    69  				expectedTags, tagsToMapCloudtrail(*tags))
    70  		}
    71  		return nil
    72  	}
    73  }