...

Source file src/sigs.k8s.io/structured-merge-diff/v4/merge/ignore_test.go

Documentation: sigs.k8s.io/structured-merge-diff/v4/merge

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package merge_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    23  	. "sigs.k8s.io/structured-merge-diff/v4/internal/fixture"
    24  )
    25  
    26  func TestIgnoredFields(t *testing.T) {
    27  	tests := map[string]TestCase{
    28  		"update_does_not_own_ignored": {
    29  			APIVersion: "v1",
    30  			Ops: []Operation{
    31  				Update{
    32  					Manager:    "default",
    33  					APIVersion: "v1",
    34  					Object: `
    35  						numeric: 1
    36  						string: "some string"
    37  					`,
    38  				},
    39  			},
    40  			Object: `
    41  				numeric: 1
    42  				string: "some string"
    43  			`,
    44  			Managed: fieldpath.ManagedFields{
    45  				"default": fieldpath.NewVersionedSet(
    46  					_NS(
    47  						_P("numeric"),
    48  					),
    49  					"v1",
    50  					false,
    51  				),
    52  			},
    53  			IgnoredFields: map[fieldpath.APIVersion]*fieldpath.Set{
    54  				"v1": _NS(
    55  					_P("string"),
    56  				),
    57  			},
    58  		},
    59  		"update_does_not_own_deep_ignored": {
    60  			APIVersion: "v1",
    61  			Ops: []Operation{
    62  				Update{
    63  					Manager:    "default",
    64  					APIVersion: "v1",
    65  					Object:     `{"numeric": 1, "obj": {"string": "foo", "numeric": 2}}`,
    66  				},
    67  			},
    68  			Object: `{"numeric": 1, "obj": {"string": "foo", "numeric": 2}}`,
    69  			Managed: fieldpath.ManagedFields{
    70  				"default": fieldpath.NewVersionedSet(
    71  					_NS(
    72  						_P("numeric"),
    73  					),
    74  					"v1",
    75  					false,
    76  				),
    77  			},
    78  			IgnoredFields: map[fieldpath.APIVersion]*fieldpath.Set{
    79  				"v1": _NS(
    80  					_P("obj"),
    81  				),
    82  			},
    83  		},
    84  		"apply_does_not_own_ignored": {
    85  			APIVersion: "v1",
    86  			Ops: []Operation{
    87  				Apply{
    88  					Manager:    "default",
    89  					APIVersion: "v1",
    90  					Object: `
    91  						numeric: 1
    92  						string: "some string"
    93  					`,
    94  				},
    95  			},
    96  			Object: `
    97  				numeric: 1
    98  				string: "some string"
    99  			`,
   100  			Managed: fieldpath.ManagedFields{
   101  				"default": fieldpath.NewVersionedSet(
   102  					_NS(
   103  						_P("numeric"),
   104  					),
   105  					"v1",
   106  					true,
   107  				),
   108  			},
   109  			IgnoredFields: map[fieldpath.APIVersion]*fieldpath.Set{
   110  				"v1": _NS(
   111  					_P("string"),
   112  				),
   113  			},
   114  		},
   115  		"apply_does_not_own_deep_ignored": {
   116  			APIVersion: "v1",
   117  			Ops: []Operation{
   118  				Apply{
   119  					Manager:    "default",
   120  					APIVersion: "v1",
   121  					Object:     `{"numeric": 1, "obj": {"string": "foo", "numeric": 2}}`,
   122  				},
   123  			},
   124  			Object: `{"numeric": 1, "obj": {"string": "foo", "numeric": 2}}`,
   125  			Managed: fieldpath.ManagedFields{
   126  				"default": fieldpath.NewVersionedSet(
   127  					_NS(
   128  						_P("numeric"),
   129  					),
   130  					"v1",
   131  					true,
   132  				),
   133  			},
   134  			IgnoredFields: map[fieldpath.APIVersion]*fieldpath.Set{
   135  				"v1": _NS(
   136  					_P("obj"),
   137  				),
   138  			},
   139  		},
   140  	}
   141  
   142  	for name, test := range tests {
   143  		t.Run(name, func(t *testing.T) {
   144  			if err := test.Test(DeducedParser); err != nil {
   145  				t.Fatal("Should fail:", err)
   146  			}
   147  		})
   148  	}
   149  }
   150  
   151  func TestIgnoredFieldsUsesVersions(t *testing.T) {
   152  	tests := map[string]TestCase{
   153  		"does_use_ignored_fields_versions": {
   154  			Ops: []Operation{
   155  				Apply{
   156  					Manager: "apply-one",
   157  					Object: `
   158  						mapOfMapsRecursive:
   159  						  a:
   160  						    b:
   161  						  c:
   162  						    d:
   163  					`,
   164  					APIVersion: "v1",
   165  				},
   166  				Apply{
   167  					Manager: "apply-two",
   168  					Object: `
   169  						mapOfMapsRecursive:
   170  						  aa:
   171  						  cc:
   172  						    dd:
   173  					`,
   174  					APIVersion: "v2",
   175  				},
   176  				Apply{
   177  					Manager: "apply-one",
   178  					Object: `
   179  						mapOfMapsRecursive:
   180  					`,
   181  					APIVersion: "v4",
   182  				},
   183  			},
   184  			// note that this still contains cccc due to ignored fields not being removed from the update result
   185  			Object: `
   186  				mapOfMapsRecursive:
   187  				  aaaa:
   188  				  cccc:
   189  				    dddd:
   190  			`,
   191  			APIVersion: "v4",
   192  			Managed: fieldpath.ManagedFields{
   193  				"apply-one": fieldpath.NewVersionedSet(
   194  					_NS(
   195  						_P("mapOfMapsRecursive"),
   196  					),
   197  					"v4",
   198  					false,
   199  				),
   200  				"apply-two": fieldpath.NewVersionedSet(
   201  					_NS(
   202  						_P("mapOfMapsRecursive", "aa"),
   203  					),
   204  					"v2",
   205  					false,
   206  				),
   207  			},
   208  			IgnoredFields: map[fieldpath.APIVersion]*fieldpath.Set{
   209  				"v1": _NS(
   210  					_P("mapOfMapsRecursive", "c"),
   211  				),
   212  				"v2": _NS(
   213  					_P("mapOfMapsRecursive", "cc"),
   214  				),
   215  				"v3": _NS(
   216  					_P("mapOfMapsRecursive", "ccc"),
   217  				),
   218  				"v4": _NS(
   219  					_P("mapOfMapsRecursive", "cccc"),
   220  				),
   221  			},
   222  		},
   223  		"update_does_not_steal_ignored": {
   224  			APIVersion: "v1",
   225  			Ops: []Operation{
   226  				Update{
   227  					Manager: "update-one",
   228  					Object: `
   229  						mapOfMapsRecursive:
   230  						  a:
   231  						    b:
   232  						  c:
   233  						    d:
   234  					`,
   235  					APIVersion: "v1",
   236  				},
   237  				Update{
   238  					Manager: "update-two",
   239  					Object: `
   240  						mapOfMapsRecursive:
   241  						  a:
   242  						    b:
   243  						  c:
   244  						    e:
   245  					`,
   246  					APIVersion: "v2",
   247  				},
   248  			},
   249  			Object: `
   250  				mapOfMapsRecursive:
   251  				  a:
   252  				    b:
   253  				  c:
   254  				    e:
   255  			`,
   256  			Managed: fieldpath.ManagedFields{
   257  				"update-one": fieldpath.NewVersionedSet(
   258  					_NS(
   259  						_P("mapOfMapsRecursive"),
   260  						_P("mapOfMapsRecursive", "a"),
   261  						_P("mapOfMapsRecursive", "a", "b"),
   262  						_P("mapOfMapsRecursive", "c"),
   263  					),
   264  					"v1",
   265  					false,
   266  				),
   267  				"update-two": fieldpath.NewVersionedSet(
   268  					_NS(
   269  						_P("mapOfMapsRecursive", "a"),
   270  						_P("mapOfMapsRecursive", "a", "b"),
   271  					),
   272  					"v2",
   273  					false,
   274  				),
   275  			},
   276  			IgnoredFields: map[fieldpath.APIVersion]*fieldpath.Set{
   277  				"v2": _NS(
   278  					_P("mapOfMapsRecursive", "c"),
   279  				),
   280  			},
   281  		},
   282  		"apply_does_not_steal_ignored": {
   283  			APIVersion: "v1",
   284  			Ops: []Operation{
   285  				Apply{
   286  					Manager: "apply-one",
   287  					Object: `
   288  						mapOfMapsRecursive:
   289  						  a:
   290  						    b:
   291  						  c:
   292  						    d:
   293  					`,
   294  					APIVersion: "v1",
   295  				},
   296  				Apply{
   297  					Manager: "apply-two",
   298  					Object: `
   299  						mapOfMapsRecursive:
   300  						  a:
   301  						    b:
   302  						  c:
   303  						    e:
   304  					`,
   305  					APIVersion: "v2",
   306  				},
   307  			},
   308  			Object: `
   309  				mapOfMapsRecursive:
   310  				  a:
   311  				    b:
   312  				  c:
   313  				    d:
   314  			`,
   315  			Managed: fieldpath.ManagedFields{
   316  				"apply-one": fieldpath.NewVersionedSet(
   317  					_NS(
   318  						_P("mapOfMapsRecursive", "a"),
   319  						_P("mapOfMapsRecursive", "a", "b"),
   320  						_P("mapOfMapsRecursive", "c"),
   321  						_P("mapOfMapsRecursive", "c", "d"),
   322  					),
   323  					"v1",
   324  					false,
   325  				),
   326  				"apply-two": fieldpath.NewVersionedSet(
   327  					_NS(
   328  						_P("mapOfMapsRecursive", "a"),
   329  						_P("mapOfMapsRecursive", "a", "b"),
   330  					),
   331  					"v2",
   332  					false,
   333  				),
   334  			},
   335  			IgnoredFields: map[fieldpath.APIVersion]*fieldpath.Set{
   336  				"v2": _NS(
   337  					_P("mapOfMapsRecursive", "c"),
   338  				),
   339  			},
   340  		},
   341  	}
   342  
   343  	for name, test := range tests {
   344  		t.Run(name, func(t *testing.T) {
   345  			if err := test.TestWithConverter(nestedTypeParser, repeatingConverter{nestedTypeParser}); err != nil {
   346  				t.Fatal(err)
   347  			}
   348  		})
   349  	}
   350  }
   351  

View as plain text