github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/terraform/upgrade_resource_state_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/zclconf/go-cty/cty"
     8  )
     9  
    10  func TestStripRemovedStateAttributes(t *testing.T) {
    11  	cases := []struct {
    12  		name     string
    13  		state    map[string]interface{}
    14  		expect   map[string]interface{}
    15  		ty       cty.Type
    16  		modified bool
    17  	}{
    18  		{
    19  			"removed string",
    20  			map[string]interface{}{
    21  				"a": "ok",
    22  				"b": "gone",
    23  			},
    24  			map[string]interface{}{
    25  				"a": "ok",
    26  			},
    27  			cty.Object(map[string]cty.Type{
    28  				"a": cty.String,
    29  			}),
    30  			true,
    31  		},
    32  		{
    33  			"removed null",
    34  			map[string]interface{}{
    35  				"a": "ok",
    36  				"b": nil,
    37  			},
    38  			map[string]interface{}{
    39  				"a": "ok",
    40  			},
    41  			cty.Object(map[string]cty.Type{
    42  				"a": cty.String,
    43  			}),
    44  			true,
    45  		},
    46  		{
    47  			"removed nested string",
    48  			map[string]interface{}{
    49  				"a": "ok",
    50  				"b": map[string]interface{}{
    51  					"a": "ok",
    52  					"b": "removed",
    53  				},
    54  			},
    55  			map[string]interface{}{
    56  				"a": "ok",
    57  				"b": map[string]interface{}{
    58  					"a": "ok",
    59  				},
    60  			},
    61  			cty.Object(map[string]cty.Type{
    62  				"a": cty.String,
    63  				"b": cty.Object(map[string]cty.Type{
    64  					"a": cty.String,
    65  				}),
    66  			}),
    67  			true,
    68  		},
    69  		{
    70  			"removed nested list",
    71  			map[string]interface{}{
    72  				"a": "ok",
    73  				"b": map[string]interface{}{
    74  					"a": "ok",
    75  					"b": []interface{}{"removed"},
    76  				},
    77  			},
    78  			map[string]interface{}{
    79  				"a": "ok",
    80  				"b": map[string]interface{}{
    81  					"a": "ok",
    82  				},
    83  			},
    84  			cty.Object(map[string]cty.Type{
    85  				"a": cty.String,
    86  				"b": cty.Object(map[string]cty.Type{
    87  					"a": cty.String,
    88  				}),
    89  			}),
    90  			true,
    91  		},
    92  		{
    93  			"removed keys in set of objs",
    94  			map[string]interface{}{
    95  				"a": "ok",
    96  				"b": map[string]interface{}{
    97  					"a": "ok",
    98  					"set": []interface{}{
    99  						map[string]interface{}{
   100  							"x": "ok",
   101  							"y": "removed",
   102  						},
   103  						map[string]interface{}{
   104  							"x": "ok",
   105  							"y": "removed",
   106  						},
   107  					},
   108  				},
   109  			},
   110  			map[string]interface{}{
   111  				"a": "ok",
   112  				"b": map[string]interface{}{
   113  					"a": "ok",
   114  					"set": []interface{}{
   115  						map[string]interface{}{
   116  							"x": "ok",
   117  						},
   118  						map[string]interface{}{
   119  							"x": "ok",
   120  						},
   121  					},
   122  				},
   123  			},
   124  			cty.Object(map[string]cty.Type{
   125  				"a": cty.String,
   126  				"b": cty.Object(map[string]cty.Type{
   127  					"a": cty.String,
   128  					"set": cty.Set(cty.Object(map[string]cty.Type{
   129  						"x": cty.String,
   130  					})),
   131  				}),
   132  			}),
   133  			true,
   134  		},
   135  	}
   136  
   137  	for _, tc := range cases {
   138  		t.Run(tc.name, func(t *testing.T) {
   139  			modified := removeRemovedAttrs(tc.state, tc.ty)
   140  			if !reflect.DeepEqual(tc.state, tc.expect) {
   141  				t.Fatalf("expected: %#v\n      got: %#v\n", tc.expect, tc.state)
   142  			}
   143  			if modified != tc.modified {
   144  				t.Fatal("incorrect return value")
   145  			}
   146  		})
   147  	}
   148  }