github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/eval_diff_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  	"github.com/hashicorp/hcl/v2/hclsyntax"
     8  	"github.com/zclconf/go-cty/cty"
     9  )
    10  
    11  func TestProcessIgnoreChangesIndividual(t *testing.T) {
    12  	tests := map[string]struct {
    13  		Old, New cty.Value
    14  		Ignore   []string
    15  		Want     cty.Value
    16  	}{
    17  		"string": {
    18  			cty.ObjectVal(map[string]cty.Value{
    19  				"a": cty.StringVal("a value"),
    20  				"b": cty.StringVal("b value"),
    21  			}),
    22  			cty.ObjectVal(map[string]cty.Value{
    23  				"a": cty.StringVal("new a value"),
    24  				"b": cty.StringVal("new b value"),
    25  			}),
    26  			[]string{"a"},
    27  			cty.ObjectVal(map[string]cty.Value{
    28  				"a": cty.StringVal("a value"),
    29  				"b": cty.StringVal("new b value"),
    30  			}),
    31  		},
    32  		"changed type": {
    33  			cty.ObjectVal(map[string]cty.Value{
    34  				"a": cty.StringVal("a value"),
    35  				"b": cty.StringVal("b value"),
    36  			}),
    37  			cty.ObjectVal(map[string]cty.Value{
    38  				"a": cty.NumberIntVal(1),
    39  				"b": cty.StringVal("new b value"),
    40  			}),
    41  			[]string{"a"},
    42  			cty.ObjectVal(map[string]cty.Value{
    43  				"a": cty.StringVal("a value"),
    44  				"b": cty.StringVal("new b value"),
    45  			}),
    46  		},
    47  		"list": {
    48  			cty.ObjectVal(map[string]cty.Value{
    49  				"a": cty.ListVal([]cty.Value{
    50  					cty.StringVal("a0 value"),
    51  					cty.StringVal("a1 value"),
    52  				}),
    53  				"b": cty.StringVal("b value"),
    54  			}),
    55  			cty.ObjectVal(map[string]cty.Value{
    56  				"a": cty.ListVal([]cty.Value{
    57  					cty.StringVal("new a0 value"),
    58  					cty.StringVal("new a1 value"),
    59  				}),
    60  				"b": cty.StringVal("new b value"),
    61  			}),
    62  			[]string{"a"},
    63  			cty.ObjectVal(map[string]cty.Value{
    64  				"a": cty.ListVal([]cty.Value{
    65  					cty.StringVal("a0 value"),
    66  					cty.StringVal("a1 value"),
    67  				}),
    68  				"b": cty.StringVal("new b value"),
    69  			}),
    70  		},
    71  		"object attribute": {
    72  			cty.ObjectVal(map[string]cty.Value{
    73  				"a": cty.ObjectVal(map[string]cty.Value{
    74  					"foo": cty.StringVal("a.foo value"),
    75  					"bar": cty.StringVal("a.bar value"),
    76  				}),
    77  				"b": cty.StringVal("b value"),
    78  			}),
    79  			cty.ObjectVal(map[string]cty.Value{
    80  				"a": cty.ObjectVal(map[string]cty.Value{
    81  					"foo": cty.StringVal("new a.foo value"),
    82  					"bar": cty.StringVal("new a.bar value"),
    83  				}),
    84  				"b": cty.StringVal("new b value"),
    85  			}),
    86  			[]string{"a.bar"},
    87  			cty.ObjectVal(map[string]cty.Value{
    88  				"a": cty.ObjectVal(map[string]cty.Value{
    89  					"foo": cty.StringVal("new a.foo value"),
    90  					"bar": cty.StringVal("a.bar value"),
    91  				}),
    92  				"b": cty.StringVal("new b value"),
    93  			}),
    94  		},
    95  	}
    96  
    97  	for name, test := range tests {
    98  		t.Run(name, func(t *testing.T) {
    99  			ignore := make([]hcl.Traversal, len(test.Ignore))
   100  			for i, ignoreStr := range test.Ignore {
   101  				trav, diags := hclsyntax.ParseTraversalAbs([]byte(ignoreStr), "", hcl.Pos{Line: 1, Column: 1})
   102  				if diags.HasErrors() {
   103  					t.Fatalf("failed to parse %q: %s", ignoreStr, diags.Error())
   104  				}
   105  				ignore[i] = trav
   106  			}
   107  
   108  			ret, diags := processIgnoreChangesIndividual(test.Old, test.New, ignore)
   109  			if diags.HasErrors() {
   110  				t.Fatal(diags.Err())
   111  			}
   112  
   113  			if got, want := ret, test.Want; !want.RawEquals(got) {
   114  				t.Errorf("wrong result\ngot:  %#v\nwant: %#v", got, want)
   115  			}
   116  		})
   117  	}
   118  }