github.com/crossplane/upjet@v1.3.0/pkg/resource/ignored_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package resource
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  
    12  	_ "embed"
    13  )
    14  
    15  func TestGetIgnoredFields(t *testing.T) {
    16  	type args struct {
    17  		initProvider map[string]any
    18  		forProvider  map[string]any
    19  	}
    20  	type want struct {
    21  		ignored []string
    22  	}
    23  	cases := map[string]struct {
    24  		reason string
    25  		args
    26  		want
    27  	}{
    28  		"InitProviderEmpty": {
    29  			reason: "Should return the empty ignored fields when initProvider is empty",
    30  			args: args{
    31  				forProvider: map[string]any{
    32  					"singleField": "bob",
    33  				},
    34  			},
    35  			want: want{
    36  				ignored: []string{},
    37  			},
    38  		},
    39  		"ForProviderEmpty": {
    40  			reason: "Should return the ignored fields when forProvider is empty",
    41  			args: args{
    42  				initProvider: map[string]any{
    43  					"singleField": "bob",
    44  				},
    45  			},
    46  			want: want{
    47  				ignored: []string{"singleField"},
    48  			},
    49  		},
    50  		"SingleField": {
    51  			reason: "Should return the single ignored field when forProvider has a different field",
    52  			args: args{
    53  				initProvider: map[string]any{
    54  					"singleField": "bob",
    55  				},
    56  				forProvider: map[string]any{
    57  					"otherField": "bob",
    58  				},
    59  			},
    60  			want: want{
    61  				ignored: []string{"singleField"},
    62  			},
    63  		},
    64  		"SingleFieldSame": {
    65  			reason: "Should not return the single ignored field when forProvider has same fields",
    66  			args: args{
    67  				initProvider: map[string]any{
    68  					"singleField": "bob",
    69  				},
    70  				forProvider: map[string]any{
    71  					"singleField": "bob",
    72  				},
    73  			},
    74  			want: want{
    75  				ignored: []string{},
    76  			},
    77  		},
    78  		"NestedArrayField": {
    79  			reason: "Should return the ignored field when init and for provider have nested arrays with different fields",
    80  			args: args{
    81  				initProvider: map[string]any{
    82  					"array": []any{
    83  						map[string]any{
    84  							"singleField": "bob",
    85  							"same":        "same",
    86  						},
    87  					},
    88  				},
    89  				forProvider: map[string]any{
    90  					"array": []any{
    91  						map[string]any{
    92  							"otherField": "bob",
    93  							"same":       "not same value",
    94  						},
    95  					},
    96  				},
    97  			},
    98  			want: want{
    99  				ignored: []string{"array[0].singleField"},
   100  			},
   101  		},
   102  		"NestedArrayFieldMultiple": {
   103  			reason: "Should return the multiple ignored fields when init and for provider have nested arrays with different fields",
   104  			args: args{
   105  				initProvider: map[string]any{
   106  					"array": []any{
   107  						map[string]any{
   108  							"singleField": "bob",
   109  							"same":        "same",
   110  						},
   111  						map[string]any{
   112  							"nextField": "bob",
   113  						},
   114  					},
   115  				},
   116  				forProvider: map[string]any{
   117  					"array": []any{
   118  						map[string]any{
   119  							"otherField": "bob",
   120  							"same":       "not same value",
   121  						},
   122  					},
   123  				},
   124  			},
   125  			want: want{
   126  				ignored: []string{"array[0].singleField", "array[1]"},
   127  			},
   128  		},
   129  		"NestedArraySameField": {
   130  			reason: "Should not the ignored field when init and for provider have nested arrays with same fields",
   131  			args: args{
   132  				initProvider: map[string]any{
   133  					"array": []any{
   134  						map[string]any{
   135  							"same": "same",
   136  						},
   137  					},
   138  				},
   139  				forProvider: map[string]any{
   140  					"array": []any{
   141  						map[string]any{
   142  							"same": "not same value",
   143  						},
   144  					},
   145  				},
   146  			},
   147  			want: want{
   148  				ignored: []string{},
   149  			},
   150  		},
   151  		"NestedMapKey": {
   152  			reason: "Should return the ignored field when init and for provider have nested maps with different keys",
   153  			args: args{
   154  				initProvider: map[string]any{
   155  					"map": map[string]any{
   156  						"key":  "bob",
   157  						"same": "same",
   158  					},
   159  				},
   160  				forProvider: map[string]any{
   161  					"map": map[string]any{
   162  						"otherKey": "bob",
   163  						"same":     "not same value",
   164  					},
   165  				},
   166  			},
   167  			want: want{
   168  				ignored: []string{"map[\"key\"]"},
   169  			},
   170  		},
   171  		"NestedMapSameKey": {
   172  			reason: "Should not return the ignored field when init and for provider have nested maps with same keys",
   173  			args: args{
   174  				initProvider: map[string]any{
   175  					"map": map[string]any{
   176  						"same": "same",
   177  					},
   178  				},
   179  				forProvider: map[string]any{
   180  					"map": map[string]any{
   181  						"same": "not same value",
   182  					},
   183  				},
   184  			},
   185  			want: want{
   186  				ignored: []string{},
   187  			},
   188  		},
   189  		"MissingMapField": {
   190  			reason: "Should return the ignored field when init and for provider have different nested maps",
   191  			args: args{
   192  				initProvider: map[string]any{
   193  					"map": map[string]any{
   194  						"key": "bob",
   195  					},
   196  				},
   197  				forProvider: map[string]any{
   198  					"othermap": map[string]any{
   199  						"key": "bob",
   200  					},
   201  				},
   202  			},
   203  			want: want{
   204  				ignored: []string{"map"},
   205  			},
   206  		},
   207  		"MissingArrayField": {
   208  			reason: "Should return the ignored field when init and for provider have different nested arrays",
   209  			args: args{
   210  				initProvider: map[string]any{
   211  					"array": []any{
   212  						"bob",
   213  					},
   214  				},
   215  				forProvider: map[string]any{
   216  					"otherArray": []any{
   217  						"bob",
   218  					},
   219  				},
   220  			},
   221  			want: want{
   222  				ignored: []string{"array"},
   223  			},
   224  		},
   225  		"MissingMultipleFields": {
   226  			reason: "Should return the ignored fields when init and for provider have different multiple fields and nested fields",
   227  			args: args{
   228  				initProvider: map[string]any{
   229  					"singleField": "bob",
   230  					"array": []any{
   231  						map[string]any{
   232  							"singleField": "bob",
   233  							"secondField": "secondBob",
   234  							"sameKey":     "same",
   235  						},
   236  					},
   237  					"map": map[string]any{
   238  						"key":      "bob",
   239  						"otherKey": "otherBob",
   240  						"sameKey":  "same",
   241  					},
   242  				},
   243  				forProvider: map[string]any{
   244  					"otherField": "bob",
   245  					"array": []any{
   246  						map[string]any{
   247  							"otherField": "bob",
   248  							"sameKey":    "not same value",
   249  						},
   250  					},
   251  					"map": map[string]any{
   252  						"sameKey": "not same value",
   253  					},
   254  				},
   255  			},
   256  			want: want{
   257  				ignored: []string{"array[0].secondField", "array[0].singleField", "map[\"key\"]", "map[\"otherKey\"]", "singleField"},
   258  			},
   259  		},
   260  		"DoubleNestedArrayField": {
   261  			reason: "Should return the ignored field when init and for provider have deep nested arrays with different fields",
   262  			args: args{
   263  				initProvider: map[string]any{
   264  					"array": []any{
   265  						map[string]any{
   266  							"deepArray": []any{
   267  								map[string]any{
   268  									"singleField": "bob",
   269  								},
   270  							},
   271  						},
   272  					},
   273  				},
   274  				forProvider: map[string]any{
   275  					"array": []any{
   276  						map[string]any{
   277  							"deepArray": []any{
   278  								map[string]any{
   279  									"otherField": "bob",
   280  								},
   281  							},
   282  						},
   283  					},
   284  				},
   285  			},
   286  			want: want{
   287  				ignored: []string{"array[0].deepArray[0].singleField"},
   288  			},
   289  		},
   290  	}
   291  	for name, tc := range cases {
   292  		t.Run(name, func(t *testing.T) {
   293  			got := GetTerraformIgnoreChanges(tc.args.forProvider, tc.args.initProvider)
   294  			if diff := cmp.Diff(tc.want.ignored, got); diff != "" {
   295  				t.Errorf("GetIgnorableFields() got = %v, want %v", got, tc.want.ignored)
   296  			}
   297  		})
   298  	}
   299  }