...

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

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

     1  /*
     2  Copyright 2018 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  	"sigs.k8s.io/structured-merge-diff/v4/typed"
    25  )
    26  
    27  var setFieldsParser = func() Parser {
    28  	parser, err := typed.NewParser(`types:
    29  - name: sets
    30    map:
    31      fields:
    32      - name: list
    33        type:
    34          list:
    35            elementType:
    36              scalar: string
    37            elementRelationship: associative`)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	return SameVersionParser{T: parser.Type("sets")}
    42  }()
    43  
    44  func TestUpdateSet(t *testing.T) {
    45  	tests := map[string]TestCase{
    46  		"apply_twice": {
    47  			Ops: []Operation{
    48  				Apply{
    49  					Manager:    "default",
    50  					APIVersion: "v1",
    51  					Object: `
    52  						list:
    53  						- a
    54  						- c
    55  					`,
    56  				},
    57  				Apply{
    58  					Manager:    "default",
    59  					APIVersion: "v1",
    60  					Object: `
    61  						list:
    62  						- a
    63  						- b
    64  						- c
    65  						- d
    66  					`,
    67  				},
    68  			},
    69  			Object: `
    70  				list:
    71  				- a
    72  				- b
    73  				- c
    74  				- d
    75  			`,
    76  			APIVersion: "v1",
    77  			Managed: fieldpath.ManagedFields{
    78  				"default": fieldpath.NewVersionedSet(
    79  					_NS(
    80  						_P("list", _V("a")),
    81  						_P("list", _V("b")),
    82  						_P("list", _V("c")),
    83  						_P("list", _V("d")),
    84  					),
    85  					"v1",
    86  					false,
    87  				),
    88  			},
    89  		},
    90  		"apply_update_apply_no_overlap": {
    91  			Ops: []Operation{
    92  				Apply{
    93  					Manager:    "default",
    94  					APIVersion: "v1",
    95  					Object: `
    96  						list:
    97  						- a
    98  						- c
    99  					`,
   100  				},
   101  				Update{
   102  					Manager:    "controller",
   103  					APIVersion: "v1",
   104  					Object: `
   105  						list:
   106  						- a
   107  						- b
   108  						- c
   109  						- d
   110  					`,
   111  				},
   112  				Apply{
   113  					Manager:    "default",
   114  					APIVersion: "v1",
   115  					Object: `
   116  						list:
   117  						- a
   118  						- aprime
   119  						- c
   120  						- cprime
   121  					`,
   122  				},
   123  			},
   124  			Object: `
   125  				list:
   126  				- a
   127  				- b
   128  				- aprime
   129  				- c
   130  				- d
   131  				- cprime
   132  			`,
   133  			APIVersion: "v1",
   134  			Managed: fieldpath.ManagedFields{
   135  				"default": fieldpath.NewVersionedSet(
   136  					_NS(
   137  						_P("list", _V("a")),
   138  						_P("list", _V("aprime")),
   139  						_P("list", _V("c")),
   140  						_P("list", _V("cprime")),
   141  					),
   142  					"v1",
   143  					false,
   144  				),
   145  				"controller": fieldpath.NewVersionedSet(
   146  					_NS(
   147  						_P("list", _V("b")),
   148  						_P("list", _V("d")),
   149  					),
   150  					"v1",
   151  					false,
   152  				),
   153  			},
   154  		},
   155  		"apply_update_apply_no_overlap_and_different_version": {
   156  			Ops: []Operation{
   157  				Apply{
   158  					Manager:    "default",
   159  					APIVersion: "v1",
   160  					Object: `
   161  						list:
   162  						- a
   163  						- c
   164  					`,
   165  				},
   166  				Update{
   167  					Manager:    "controller",
   168  					APIVersion: "v2",
   169  					Object: `
   170  						list:
   171  						- a
   172  						- b
   173  						- c
   174  						- d
   175  					`,
   176  				},
   177  				Apply{
   178  					Manager:    "default",
   179  					APIVersion: "v1",
   180  					Object: `
   181  						list:
   182  						- a
   183  						- aprime
   184  						- c
   185  						- cprime
   186  					`,
   187  				},
   188  			},
   189  			Object: `
   190  				list:
   191  				- a
   192  				- b
   193  				- aprime
   194  				- c
   195  				- d
   196  				- cprime
   197  			`,
   198  			APIVersion: "v1",
   199  			Managed: fieldpath.ManagedFields{
   200  				"default": fieldpath.NewVersionedSet(
   201  					_NS(
   202  						_P("list", _V("a")),
   203  						_P("list", _V("aprime")),
   204  						_P("list", _V("c")),
   205  						_P("list", _V("cprime")),
   206  					),
   207  					"v1",
   208  					false,
   209  				),
   210  				"controller": fieldpath.NewVersionedSet(
   211  					_NS(
   212  						_P("list", _V("b")),
   213  						_P("list", _V("d")),
   214  					),
   215  					"v2",
   216  					false,
   217  				),
   218  			},
   219  		},
   220  		"apply_update_apply_with_overlap": {
   221  			Ops: []Operation{
   222  				Apply{
   223  					Manager:    "default",
   224  					APIVersion: "v1",
   225  					Object: `
   226  						list:
   227  						- a
   228  						- c
   229  					`,
   230  				},
   231  				Update{
   232  					Manager:    "controller",
   233  					APIVersion: "v1",
   234  					Object: `
   235  						list:
   236  						- a
   237  						- b
   238  						- c
   239  						- d
   240  					`,
   241  				},
   242  				Apply{
   243  					Manager:    "default",
   244  					APIVersion: "v1",
   245  					Object: `
   246  						list:
   247  						- a
   248  						- b
   249  						- c
   250  					`,
   251  				},
   252  			},
   253  			Object: `
   254  				list:
   255  				- a
   256  				- b
   257  				- c
   258  				- d
   259  			`,
   260  			APIVersion: "v1",
   261  			Managed: fieldpath.ManagedFields{
   262  				"default": fieldpath.NewVersionedSet(
   263  					_NS(
   264  						_P("list", _V("a")),
   265  						_P("list", _V("b")),
   266  						_P("list", _V("c")),
   267  					),
   268  					"v1",
   269  					false,
   270  				),
   271  				"controller": fieldpath.NewVersionedSet(
   272  					_NS(
   273  						_P("list", _V("b")),
   274  						_P("list", _V("d")),
   275  					),
   276  					"v1",
   277  					false,
   278  				),
   279  			},
   280  		},
   281  		"apply_update_apply_with_overlap_and_different_version": {
   282  			Ops: []Operation{
   283  				Apply{
   284  					Manager:    "default",
   285  					APIVersion: "v1",
   286  					Object: `
   287  						list:
   288  						- a
   289  						- c
   290  					`,
   291  				},
   292  				Update{
   293  					Manager:    "controller",
   294  					APIVersion: "v2",
   295  					Object: `
   296  						list:
   297  						- a
   298  						- b
   299  						- c
   300  						- d
   301  					`,
   302  				},
   303  				Apply{
   304  					Manager:    "default",
   305  					APIVersion: "v1",
   306  					Object: `
   307  						list:
   308  						- a
   309  						- b
   310  						- c
   311  					`,
   312  				},
   313  			},
   314  			Object: `
   315  				list:
   316  				- a
   317  				- b
   318  				- c
   319  				- d
   320  			`,
   321  			APIVersion: "v1",
   322  			Managed: fieldpath.ManagedFields{
   323  				"default": fieldpath.NewVersionedSet(
   324  					_NS(
   325  						_P("list", _V("a")),
   326  						_P("list", _V("b")),
   327  						_P("list", _V("c")),
   328  					),
   329  					"v1",
   330  					false,
   331  				),
   332  				"controller": fieldpath.NewVersionedSet(
   333  					_NS(
   334  						_P("list", _V("b")),
   335  						_P("list", _V("d")),
   336  					),
   337  					"v2",
   338  					false,
   339  				),
   340  			},
   341  		},
   342  		"apply_twice_reorder": {
   343  			Ops: []Operation{
   344  				Apply{
   345  					Manager:    "default",
   346  					APIVersion: "v1",
   347  					Object: `
   348  						list:
   349  						- a
   350  						- b
   351  						- c
   352  						- d
   353  					`,
   354  				},
   355  				Apply{
   356  					Manager:    "default",
   357  					APIVersion: "v1",
   358  					Object: `
   359  						list:
   360  						- a
   361  						- d
   362  						- c
   363  						- b
   364  					`,
   365  				},
   366  			},
   367  			Object: `
   368  				list:
   369  				- a
   370  				- d
   371  				- c
   372  				- b
   373  			`,
   374  			APIVersion: "v1",
   375  			Managed: fieldpath.ManagedFields{
   376  				"default": fieldpath.NewVersionedSet(
   377  					_NS(
   378  						_P("list", _V("a")),
   379  						_P("list", _V("b")),
   380  						_P("list", _V("c")),
   381  						_P("list", _V("d")),
   382  					),
   383  					"v1",
   384  					false,
   385  				),
   386  			},
   387  		},
   388  		"apply_update_apply_reorder": {
   389  			Ops: []Operation{
   390  				Apply{
   391  					Manager:    "default",
   392  					APIVersion: "v1",
   393  					Object: `
   394  						list:
   395  						- a
   396  						- b
   397  						- c
   398  						- d
   399  					`,
   400  				},
   401  				Update{
   402  					Manager:    "controller",
   403  					APIVersion: "v1",
   404  					Object: `
   405  						list:
   406  						- a
   407  						- d
   408  						- c
   409  						- b
   410  					`,
   411  				},
   412  				Apply{
   413  					Manager:    "default",
   414  					APIVersion: "v1",
   415  					Object: `
   416  						list:
   417  						- a
   418  						- b
   419  						- c
   420  						- d
   421  					`,
   422  				},
   423  			},
   424  			Object: `
   425  				list:
   426  				- a
   427  				- b
   428  				- c
   429  				- d
   430  			`,
   431  			APIVersion: "v1",
   432  			Managed: fieldpath.ManagedFields{
   433  				"default": fieldpath.NewVersionedSet(
   434  					_NS(
   435  						_P("list", _V("a")),
   436  						_P("list", _V("b")),
   437  						_P("list", _V("c")),
   438  						_P("list", _V("d")),
   439  					),
   440  					"v1",
   441  					false,
   442  				),
   443  			},
   444  		},
   445  		"apply_update_apply_reorder_across_versions": {
   446  			Ops: []Operation{
   447  				Apply{
   448  					Manager:    "default",
   449  					APIVersion: "v1",
   450  					Object: `
   451  						list:
   452  						- a
   453  						- b
   454  						- c
   455  						- d
   456  					`,
   457  				},
   458  				Update{
   459  					Manager:    "controller",
   460  					APIVersion: "v1",
   461  					Object: `
   462  						list:
   463  						- a
   464  						- d
   465  						- c
   466  						- b
   467  					`,
   468  				},
   469  				Apply{
   470  					Manager:    "default",
   471  					APIVersion: "v2",
   472  					Object: `
   473  						list:
   474  						- a
   475  						- b
   476  						- c
   477  						- d
   478  					`,
   479  				},
   480  			},
   481  			Object: `
   482  				list:
   483  				- a
   484  				- b
   485  				- c
   486  				- d
   487  			`,
   488  			APIVersion: "v1",
   489  			Managed: fieldpath.ManagedFields{
   490  				"default": fieldpath.NewVersionedSet(
   491  					_NS(
   492  						_P("list", _V("a")),
   493  						_P("list", _V("b")),
   494  						_P("list", _V("c")),
   495  						_P("list", _V("d")),
   496  					),
   497  					"v2",
   498  					false,
   499  				),
   500  			},
   501  		},
   502  		"apply_twice_remove": {
   503  			Ops: []Operation{
   504  				Apply{
   505  					Manager:    "default",
   506  					APIVersion: "v1",
   507  					Object: `
   508  						list:
   509  						- a
   510  						- b
   511  						- c
   512  						- d
   513  					`,
   514  				},
   515  				Apply{
   516  					Manager:    "default",
   517  					APIVersion: "v1",
   518  					Object: `
   519  						list:
   520  						- a
   521  						- c
   522  					`,
   523  				},
   524  			},
   525  			Object: `
   526  				list:
   527  				- a
   528  				- c
   529  			`,
   530  			APIVersion: "v1",
   531  			Managed: fieldpath.ManagedFields{
   532  				"default": fieldpath.NewVersionedSet(
   533  					_NS(
   534  						_P("list", _V("a")),
   535  						_P("list", _V("c")),
   536  					),
   537  					"v1",
   538  					false,
   539  				),
   540  			},
   541  		},
   542  		"apply_twice_remove_across_versions": {
   543  			Ops: []Operation{
   544  				Apply{
   545  					Manager:    "default",
   546  					APIVersion: "v1",
   547  					Object: `
   548  						list:
   549  						- a
   550  						- b
   551  						- c
   552  						- d
   553  					`,
   554  				},
   555  				Apply{
   556  					Manager:    "default",
   557  					APIVersion: "v2",
   558  					Object: `
   559  						list:
   560  						- a
   561  						- c
   562  						- e
   563  					`,
   564  				},
   565  			},
   566  			Object: `
   567  				list:
   568  				- a
   569  				- c
   570  				- e
   571  			`,
   572  			APIVersion: "v1",
   573  			Managed: fieldpath.ManagedFields{
   574  				"default": fieldpath.NewVersionedSet(
   575  					_NS(
   576  						_P("list", _V("a")),
   577  						_P("list", _V("c")),
   578  						_P("list", _V("e")),
   579  					),
   580  					"v2",
   581  					false,
   582  				),
   583  			},
   584  		},
   585  	}
   586  
   587  	for name, test := range tests {
   588  		t.Run(name, func(t *testing.T) {
   589  			if err := test.Test(setFieldsParser); err != nil {
   590  				t.Fatal(err)
   591  			}
   592  		})
   593  	}
   594  }
   595  

View as plain text