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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/service/efs"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestDiffEFSTags(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 := diffTagsEFS(tagsFromMapEFS(tc.Old), tagsFromMapEFS(tc.New))
    53  		cm := tagsToMapEFS(c)
    54  		rm := tagsToMapEFS(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  // testAccCheckTags can be used to check the tags on a resource.
    65  func testAccCheckEFSTags(
    66  	ts *[]*efs.Tag, key string, value string) resource.TestCheckFunc {
    67  	return func(s *terraform.State) error {
    68  		m := tagsToMapEFS(*ts)
    69  		v, ok := m[key]
    70  		if value != "" && !ok {
    71  			return fmt.Errorf("Missing tag: %s", key)
    72  		} else if value == "" && ok {
    73  			return fmt.Errorf("Extra tag: %s", key)
    74  		}
    75  		if value == "" {
    76  			return nil
    77  		}
    78  
    79  		if v != value {
    80  			return fmt.Errorf("%s: bad value: %s", key, v)
    81  		}
    82  
    83  		return nil
    84  	}
    85  }