github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/kubernetes/patch_operations_test.go (about)

     1  package kubernetes
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestDiffStringMap(t *testing.T) {
     9  	testCases := []struct {
    10  		Path        string
    11  		Old         map[string]interface{}
    12  		New         map[string]interface{}
    13  		ExpectedOps PatchOperations
    14  	}{
    15  		{
    16  			Path: "/parent/",
    17  			Old: map[string]interface{}{
    18  				"one": "111",
    19  				"two": "222",
    20  			},
    21  			New: map[string]interface{}{
    22  				"one":   "111",
    23  				"two":   "222",
    24  				"three": "333",
    25  			},
    26  			ExpectedOps: []PatchOperation{
    27  				&AddOperation{
    28  					Path:  "/parent/three",
    29  					Value: "333",
    30  				},
    31  			},
    32  		},
    33  		{
    34  			Path: "/parent/",
    35  			Old: map[string]interface{}{
    36  				"one": "111",
    37  				"two": "222",
    38  			},
    39  			New: map[string]interface{}{
    40  				"one": "111",
    41  				"two": "abcd",
    42  			},
    43  			ExpectedOps: []PatchOperation{
    44  				&ReplaceOperation{
    45  					Path:  "/parent/two",
    46  					Value: "abcd",
    47  				},
    48  			},
    49  		},
    50  		{
    51  			Path: "/parent/",
    52  			Old: map[string]interface{}{
    53  				"one": "111",
    54  				"two": "222",
    55  			},
    56  			New: map[string]interface{}{
    57  				"two":   "abcd",
    58  				"three": "333",
    59  			},
    60  			ExpectedOps: []PatchOperation{
    61  				&RemoveOperation{Path: "/parent/one"},
    62  				&ReplaceOperation{
    63  					Path:  "/parent/two",
    64  					Value: "abcd",
    65  				},
    66  				&AddOperation{
    67  					Path:  "/parent/three",
    68  					Value: "333",
    69  				},
    70  			},
    71  		},
    72  		{
    73  			Path: "/parent/",
    74  			Old: map[string]interface{}{
    75  				"one": "111",
    76  				"two": "222",
    77  			},
    78  			New: map[string]interface{}{
    79  				"two": "222",
    80  			},
    81  			ExpectedOps: []PatchOperation{
    82  				&RemoveOperation{Path: "/parent/one"},
    83  			},
    84  		},
    85  		{
    86  			Path: "/parent/",
    87  			Old: map[string]interface{}{
    88  				"one": "111",
    89  				"two": "222",
    90  			},
    91  			New: map[string]interface{}{},
    92  			ExpectedOps: []PatchOperation{
    93  				&RemoveOperation{Path: "/parent/one"},
    94  				&RemoveOperation{Path: "/parent/two"},
    95  			},
    96  		},
    97  		{
    98  			Path: "/parent/",
    99  			Old:  map[string]interface{}{},
   100  			New: map[string]interface{}{
   101  				"one": "111",
   102  				"two": "222",
   103  			},
   104  			ExpectedOps: []PatchOperation{
   105  				&AddOperation{
   106  					Path:  "/parent/one",
   107  					Value: "111",
   108  				},
   109  				&AddOperation{
   110  					Path:  "/parent/two",
   111  					Value: "222",
   112  				},
   113  			},
   114  		},
   115  	}
   116  
   117  	for i, tc := range testCases {
   118  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
   119  			ops := diffStringMap(tc.Path, tc.Old, tc.New)
   120  			if !tc.ExpectedOps.Equal(ops) {
   121  				t.Fatalf("Operations don't match.\nExpected: %v\nGiven:    %v\n", tc.ExpectedOps, ops)
   122  			}
   123  		})
   124  	}
   125  
   126  }