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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/service/autoscaling"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestDiffAutoscalingTags(t *testing.T) {
    14  	cases := []struct {
    15  		Old, New       map[string]interface{}
    16  		Create, Remove map[string]interface{}
    17  	}{
    18  		// Basic add/remove
    19  		{
    20  			Old: map[string]interface{}{
    21  				"Name": map[string]interface{}{
    22  					"value":               "bar",
    23  					"propagate_at_launch": true,
    24  				},
    25  			},
    26  			New: map[string]interface{}{
    27  				"DifferentTag": map[string]interface{}{
    28  					"value":               "baz",
    29  					"propagate_at_launch": true,
    30  				},
    31  			},
    32  			Create: map[string]interface{}{
    33  				"DifferentTag": map[string]interface{}{
    34  					"value":               "baz",
    35  					"propagate_at_launch": true,
    36  				},
    37  			},
    38  			Remove: map[string]interface{}{
    39  				"Name": map[string]interface{}{
    40  					"value":               "bar",
    41  					"propagate_at_launch": true,
    42  				},
    43  			},
    44  		},
    45  
    46  		// Modify
    47  		{
    48  			Old: map[string]interface{}{
    49  				"Name": map[string]interface{}{
    50  					"value":               "bar",
    51  					"propagate_at_launch": true,
    52  				},
    53  			},
    54  			New: map[string]interface{}{
    55  				"Name": map[string]interface{}{
    56  					"value":               "baz",
    57  					"propagate_at_launch": false,
    58  				},
    59  			},
    60  			Create: map[string]interface{}{
    61  				"Name": map[string]interface{}{
    62  					"value":               "baz",
    63  					"propagate_at_launch": false,
    64  				},
    65  			},
    66  			Remove: map[string]interface{}{
    67  				"Name": map[string]interface{}{
    68  					"value":               "bar",
    69  					"propagate_at_launch": true,
    70  				},
    71  			},
    72  		},
    73  	}
    74  
    75  	var resourceID = "sample"
    76  
    77  	for i, tc := range cases {
    78  		awsTagsOld := autoscalingTagsFromMap(tc.Old, resourceID)
    79  		awsTagsNew := autoscalingTagsFromMap(tc.New, resourceID)
    80  
    81  		c, r := diffAutoscalingTags(awsTagsOld, awsTagsNew, resourceID)
    82  
    83  		cm := autoscalingTagsToMap(c)
    84  		rm := autoscalingTagsToMap(r)
    85  		if !reflect.DeepEqual(cm, tc.Create) {
    86  			t.Fatalf("%d: bad create: \n%#v\n%#v", i, cm, tc.Create)
    87  		}
    88  		if !reflect.DeepEqual(rm, tc.Remove) {
    89  			t.Fatalf("%d: bad remove: \n%#v\n%#v", i, rm, tc.Remove)
    90  		}
    91  	}
    92  }
    93  
    94  // testAccCheckTags can be used to check the tags on a resource.
    95  func testAccCheckAutoscalingTags(
    96  	ts *[]*autoscaling.TagDescription, key string, expected map[string]interface{}) resource.TestCheckFunc {
    97  	return func(s *terraform.State) error {
    98  		m := autoscalingTagDescriptionsToMap(ts)
    99  		v, ok := m[key]
   100  		if !ok {
   101  			return fmt.Errorf("Missing tag: %s", key)
   102  		}
   103  
   104  		if v["value"] != expected["value"].(string) ||
   105  			v["propagate_at_launch"] != expected["propagate_at_launch"].(bool) {
   106  			return fmt.Errorf("%s: bad value: %s", key, v)
   107  		}
   108  
   109  		return nil
   110  	}
   111  }
   112  
   113  func testAccCheckAutoscalingTagNotExists(ts *[]*autoscaling.TagDescription, key string) resource.TestCheckFunc {
   114  	return func(s *terraform.State) error {
   115  		m := autoscalingTagDescriptionsToMap(ts)
   116  		if _, ok := m[key]; ok {
   117  			return fmt.Errorf("Tag exists when it should not: %s", key)
   118  		}
   119  
   120  		return nil
   121  	}
   122  }