...

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

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

     1  /*
     2  Copyright 2019 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 extractParser = 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      - name: atomicList
    39        type:
    40          list:
    41            elementType:
    42              scalar: string
    43            elementRelationship: atomic
    44      - name: map
    45        type:
    46          map:
    47            elementType:
    48              scalar: string
    49            elementRelationship: separable
    50      - name: atomicMap
    51        type:
    52          map:
    53            elementType:
    54              scalar: string
    55            elementRelationship: atomic`)
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  	return SameVersionParser{T: parser.Type("sets")}
    60  }()
    61  
    62  func TestExtractApply(t *testing.T) {
    63  	tests := map[string]TestCase{
    64  		"apply_one_extract_apply_one_own_both": {
    65  			Ops: []Operation{
    66  				Apply{
    67  					Manager: "default",
    68  					Object: `
    69  						list:
    70  						- a
    71  					`,
    72  					APIVersion: "v1",
    73  				},
    74  				ExtractApply{
    75  					Manager: "default",
    76  					Object: `
    77  							list:
    78  							- b
    79  						`,
    80  					APIVersion: "v1",
    81  				},
    82  			},
    83  			Object: `
    84  				list:
    85  				- a
    86  				- b
    87  			`,
    88  			APIVersion: "v1",
    89  			Managed: fieldpath.ManagedFields{
    90  				"default": fieldpath.NewVersionedSet(
    91  					_NS(
    92  						_P("list", _V("a")),
    93  						_P("list", _V("b")),
    94  					),
    95  					"v1",
    96  					false,
    97  				),
    98  			},
    99  		},
   100  		"extract_apply_from_beginning": {
   101  			Ops: []Operation{
   102  				ExtractApply{
   103  					Manager: "default",
   104  					Object: `
   105  						list:
   106  						- a
   107  					`,
   108  					APIVersion: "v1",
   109  				},
   110  				ExtractApply{
   111  					Manager: "default",
   112  					Object: `
   113  							list:
   114  							- b
   115  						`,
   116  					APIVersion: "v1",
   117  				},
   118  			},
   119  			Object: `
   120  				list:
   121  				- a
   122  				- b
   123  			`,
   124  			APIVersion: "v1",
   125  			Managed: fieldpath.ManagedFields{
   126  				"default": fieldpath.NewVersionedSet(
   127  					_NS(
   128  						_P("list", _V("a")),
   129  						_P("list", _V("b")),
   130  					),
   131  					"v1",
   132  					false,
   133  				),
   134  			},
   135  		},
   136  		"apply_after_extract_remove_fields": {
   137  			Ops: []Operation{
   138  				ExtractApply{
   139  					Manager: "default",
   140  					Object: `
   141  						list:
   142  						- a
   143  					`,
   144  					APIVersion: "v1",
   145  				},
   146  				Apply{
   147  					Manager: "default",
   148  					Object: `
   149  						list:
   150  						- b
   151  					`,
   152  					APIVersion: "v1",
   153  				},
   154  			},
   155  			Object: `
   156  				list:
   157  				- b
   158  			`,
   159  			APIVersion: "v1",
   160  			Managed: fieldpath.ManagedFields{
   161  				"default": fieldpath.NewVersionedSet(
   162  					_NS(
   163  						_P("list", _V("b")),
   164  					),
   165  					"v1",
   166  					false,
   167  				),
   168  			},
   169  		},
   170  		"apply_one_controller_remove_extract_apply_one": {
   171  			Ops: []Operation{
   172  				Apply{
   173  					Manager: "default",
   174  					Object: `
   175  						list:
   176  						- a
   177  					`,
   178  					APIVersion: "v1",
   179  				},
   180  				Update{
   181  					Manager: "controller",
   182  					Object: `
   183  						list:
   184  						 - b
   185  					`,
   186  					APIVersion: "v1",
   187  				},
   188  				ExtractApply{
   189  					Manager: "default",
   190  					Object: `
   191  						list:
   192  						- c
   193  					`,
   194  					APIVersion: "v1",
   195  				},
   196  			},
   197  			Object: `
   198  				list:
   199  				- b
   200  				- c
   201  			`,
   202  			APIVersion: "v1",
   203  			Managed: fieldpath.ManagedFields{
   204  				"default": fieldpath.NewVersionedSet(
   205  					_NS(
   206  						_P("list", _V("c")),
   207  					),
   208  					"v1",
   209  					false,
   210  				),
   211  				"controller": fieldpath.NewVersionedSet(
   212  					_NS(
   213  						_P("list", _V("b")),
   214  					),
   215  					"v1",
   216  					false,
   217  				),
   218  			},
   219  		},
   220  		"extract_apply_retain_ownership_after_controller_update": {
   221  			Ops: []Operation{
   222  				Apply{
   223  					Manager: "default",
   224  					Object: `
   225  						list:
   226  						- a
   227  					`,
   228  					APIVersion: "v1",
   229  				},
   230  				Update{
   231  					Manager: "controller",
   232  					Object: `
   233  						list:
   234  						 - a
   235  						 - b
   236  					`,
   237  					APIVersion: "v1",
   238  				},
   239  				ExtractApply{
   240  					Manager: "default",
   241  					Object: `
   242  						list:
   243  						- c
   244  					`,
   245  					APIVersion: "v1",
   246  				},
   247  			},
   248  			Object: `
   249  				list:
   250  				- a
   251  				- b
   252  				- c
   253  			`,
   254  			APIVersion: "v1",
   255  			Managed: fieldpath.ManagedFields{
   256  				"default": fieldpath.NewVersionedSet(
   257  					_NS(
   258  						_P("list", _V("a")),
   259  						_P("list", _V("c")),
   260  					),
   261  					"v1",
   262  					false,
   263  				),
   264  				"controller": fieldpath.NewVersionedSet(
   265  					_NS(
   266  						_P("list", _V("b")),
   267  					),
   268  					"v1",
   269  					false,
   270  				),
   271  			},
   272  		},
   273  		"extract_apply_share_ownership_after_another_apply": {
   274  			Ops: []Operation{
   275  				Apply{
   276  					Manager: "apply-one",
   277  					Object: `
   278  						list:
   279  						- a
   280  					`,
   281  					APIVersion: "v1",
   282  				},
   283  				Apply{
   284  					Manager: "apply-two",
   285  					Object: `
   286  						list:
   287  						 - a
   288  						 - b
   289  					`,
   290  					APIVersion: "v1",
   291  				},
   292  				ExtractApply{
   293  					Manager: "apply-one",
   294  					Object: `
   295  						list:
   296  						- c
   297  					`,
   298  					APIVersion: "v1",
   299  				},
   300  			},
   301  			Object: `
   302  				list:
   303  				- a
   304  				- b
   305  				- c
   306  			`,
   307  			APIVersion: "v1",
   308  			Managed: fieldpath.ManagedFields{
   309  				"apply-one": fieldpath.NewVersionedSet(
   310  					_NS(
   311  						_P("list", _V("a")),
   312  						_P("list", _V("c")),
   313  					),
   314  					"v1",
   315  					false,
   316  				),
   317  				"apply-two": fieldpath.NewVersionedSet(
   318  					_NS(
   319  						_P("list", _V("a")),
   320  						_P("list", _V("b")),
   321  					),
   322  					"v1",
   323  					false,
   324  				),
   325  			},
   326  		},
   327  		"apply_two_cant_delete_object_also_owned_by_extract_apply": {
   328  			Ops: []Operation{
   329  				Apply{
   330  					Manager: "apply-one",
   331  					Object: `
   332  						list:
   333  						- a
   334  					`,
   335  					APIVersion: "v1",
   336  				},
   337  				Apply{
   338  					Manager: "apply-two",
   339  					Object: `
   340  						list:
   341  						 - a
   342  						 - b
   343  					`,
   344  					APIVersion: "v1",
   345  				},
   346  				ExtractApply{
   347  					Manager: "apply-one",
   348  					Object: `
   349  						list:
   350  						- c
   351  					`,
   352  					APIVersion: "v1",
   353  				},
   354  				Apply{
   355  					Manager: "apply-two",
   356  					Object: `
   357  						list:
   358  						 - b
   359  					`,
   360  					APIVersion: "v1",
   361  				},
   362  			},
   363  			Object: `
   364  				list:
   365  				- a
   366  				- b
   367  				- c
   368  			`,
   369  			APIVersion: "v1",
   370  			Managed: fieldpath.ManagedFields{
   371  				"apply-one": fieldpath.NewVersionedSet(
   372  					_NS(
   373  						_P("list", _V("a")),
   374  						_P("list", _V("c")),
   375  					),
   376  					"v1",
   377  					false,
   378  				),
   379  				"apply-two": fieldpath.NewVersionedSet(
   380  					_NS(
   381  						_P("list", _V("b")),
   382  					),
   383  					"v1",
   384  					false,
   385  				),
   386  			},
   387  		},
   388  		"extract_apply_empty_structure_list": {
   389  			Ops: []Operation{
   390  				ExtractApply{
   391  					Manager: "apply-one",
   392  					Object: `
   393  						list:
   394  					`,
   395  					APIVersion: "v1",
   396  				},
   397  				Apply{
   398  					Manager: "apply-two",
   399  					Object: `
   400  						list:
   401  						 - a
   402  						 - b
   403  					`,
   404  					APIVersion: "v1",
   405  				},
   406  			},
   407  			Object: `
   408  				list:
   409  				- a
   410  				- b
   411  			`,
   412  			APIVersion: "v1",
   413  			Managed: fieldpath.ManagedFields{
   414  				"apply-one": fieldpath.NewVersionedSet(
   415  					_NS(
   416  						_P("list"),
   417  					),
   418  					"v1",
   419  					false,
   420  				),
   421  				"apply-two": fieldpath.NewVersionedSet(
   422  					_NS(
   423  						_P("list", _V("a")),
   424  						_P("list", _V("b")),
   425  					),
   426  					"v1",
   427  					false,
   428  				),
   429  			},
   430  		},
   431  		"extract_apply_empty_structure_remove_list": {
   432  			Ops: []Operation{
   433  				ExtractApply{
   434  					Manager: "apply-one",
   435  					Object: `
   436  						list:
   437  					`,
   438  					APIVersion: "v1",
   439  				},
   440  				Apply{
   441  					Manager: "apply-two",
   442  					Object: `
   443  						list:
   444  						 - a
   445  						 - b
   446  					`,
   447  					APIVersion: "v1",
   448  				},
   449  				Apply{
   450  					Manager: "apply-two",
   451  					Object: `
   452  						list:
   453  						 - b
   454  					`,
   455  					APIVersion: "v1",
   456  				},
   457  			},
   458  			Object: `
   459  				list:
   460  				- b
   461  			`,
   462  			APIVersion: "v1",
   463  			Managed: fieldpath.ManagedFields{
   464  				"apply-one": fieldpath.NewVersionedSet(
   465  					_NS(
   466  						_P("list"),
   467  					),
   468  					"v1",
   469  					false,
   470  				),
   471  				"apply-two": fieldpath.NewVersionedSet(
   472  					_NS(
   473  						_P("list", _V("b")),
   474  					),
   475  					"v1",
   476  					false,
   477  				),
   478  			},
   479  		},
   480  		"extract_apply_empty_structure_add_later_list": {
   481  			Ops: []Operation{
   482  				ExtractApply{
   483  					Manager: "apply-one",
   484  					Object: `
   485  						list:
   486  					`,
   487  					APIVersion: "v1",
   488  				},
   489  				Apply{
   490  					Manager: "apply-two",
   491  					Object: `
   492  						list:
   493  						 - a
   494  						 - b
   495  					`,
   496  					APIVersion: "v1",
   497  				},
   498  				ExtractApply{
   499  					Manager: "apply-one",
   500  					Object: `
   501  						list:
   502  						- c
   503  					`,
   504  					APIVersion: "v1",
   505  				},
   506  				Apply{
   507  					Manager: "apply-two",
   508  					Object: `
   509  						list:
   510  						 - b
   511  					`,
   512  					APIVersion: "v1",
   513  				},
   514  			},
   515  			Object: `
   516  				list:
   517  				- b
   518  				- c
   519  			`,
   520  			APIVersion: "v1",
   521  			Managed: fieldpath.ManagedFields{
   522  				"apply-one": fieldpath.NewVersionedSet(
   523  					_NS(
   524  						_P("list", _V("c")),
   525  					),
   526  					"v1",
   527  					false,
   528  				),
   529  				"apply-two": fieldpath.NewVersionedSet(
   530  					_NS(
   531  						_P("list", _V("b")),
   532  					),
   533  					"v1",
   534  					false,
   535  				),
   536  			},
   537  		},
   538  		"extract_apply_empty_structure_map": {
   539  			Ops: []Operation{
   540  				ExtractApply{
   541  					Manager: "apply-one",
   542  					Object: `
   543  						map:
   544  					`,
   545  					APIVersion: "v1",
   546  				},
   547  				Apply{
   548  					Manager: "apply-two",
   549  					Object: `
   550  						map:
   551  						 a: c
   552  						 b: d
   553  					`,
   554  					APIVersion: "v1",
   555  				},
   556  			},
   557  			Object: `
   558  				map:
   559  				  a: c
   560  				  b: d
   561  			`,
   562  			APIVersion: "v1",
   563  			Managed: fieldpath.ManagedFields{
   564  				"apply-one": fieldpath.NewVersionedSet(
   565  					_NS(
   566  						_P("map"),
   567  					),
   568  					"v1",
   569  					false,
   570  				),
   571  				"apply-two": fieldpath.NewVersionedSet(
   572  					_NS(
   573  						_P("map", "a"),
   574  						_P("map", "b"),
   575  					),
   576  					"v1",
   577  					false,
   578  				),
   579  			},
   580  		},
   581  		"extract_apply_empty_structure_remove_map": {
   582  			Ops: []Operation{
   583  				ExtractApply{
   584  					Manager: "apply-one",
   585  					Object: `
   586  						map:
   587  					`,
   588  					APIVersion: "v1",
   589  				},
   590  				Apply{
   591  					Manager: "apply-two",
   592  					Object: `
   593  						map:
   594  						 a: c
   595  						 b: d
   596  					`,
   597  					APIVersion: "v1",
   598  				},
   599  				Apply{
   600  					Manager: "apply-two",
   601  					Object: `
   602  						map:
   603  						 b: d
   604  					`,
   605  					APIVersion: "v1",
   606  				},
   607  			},
   608  			Object: `
   609  				map:
   610  				  b: d
   611  			`,
   612  			APIVersion: "v1",
   613  			Managed: fieldpath.ManagedFields{
   614  				"apply-one": fieldpath.NewVersionedSet(
   615  					_NS(
   616  						_P("map"),
   617  					),
   618  					"v1",
   619  					false,
   620  				),
   621  				"apply-two": fieldpath.NewVersionedSet(
   622  					_NS(
   623  						_P("map", "b"),
   624  					),
   625  					"v1",
   626  					false,
   627  				),
   628  			},
   629  		},
   630  		"extract_apply_empty_structure_add_later_map": {
   631  			Ops: []Operation{
   632  				ExtractApply{
   633  					Manager: "apply-one",
   634  					Object: `
   635  						map:
   636  					`,
   637  					APIVersion: "v1",
   638  				},
   639  				Apply{
   640  					Manager: "apply-two",
   641  					Object: `
   642  						map:
   643  						 a: c
   644  						 b: d
   645  					`,
   646  					APIVersion: "v1",
   647  				},
   648  				ExtractApply{
   649  					Manager: "apply-one",
   650  					Object: `
   651  						map:
   652  						  e: f
   653  					`,
   654  					APIVersion: "v1",
   655  				},
   656  				Apply{
   657  					Manager: "apply-two",
   658  					Object: `
   659  						map:
   660  						 b: d
   661  					`,
   662  					APIVersion: "v1",
   663  				},
   664  			},
   665  			Object: `
   666  				map:
   667  				  b: d
   668  				  e: f
   669  			`,
   670  			APIVersion: "v1",
   671  			Managed: fieldpath.ManagedFields{
   672  				"apply-one": fieldpath.NewVersionedSet(
   673  					_NS(
   674  						_P("map", "e"),
   675  					),
   676  					"v1",
   677  					false,
   678  				),
   679  				"apply-two": fieldpath.NewVersionedSet(
   680  					_NS(
   681  						_P("map", "b"),
   682  					),
   683  					"v1",
   684  					false,
   685  				),
   686  			},
   687  		},
   688  		"extract_apply_atomic_list": {
   689  			Ops: []Operation{
   690  				ExtractApply{
   691  					Manager: "apply-one",
   692  					Object: `
   693  						atomicList:
   694  						- a
   695  						- b
   696  						- c
   697  					`,
   698  					APIVersion: "v1",
   699  				},
   700  			},
   701  			Object: `
   702  				atomicList:
   703  				- a
   704  				- b
   705  				- c
   706  			`,
   707  			APIVersion: "v1",
   708  			Managed: fieldpath.ManagedFields{
   709  				"apply-one": fieldpath.NewVersionedSet(
   710  					_NS(
   711  						_P("atomicList"),
   712  					),
   713  					"v1",
   714  					false,
   715  				),
   716  			},
   717  		},
   718  		"extract_apply_atomic_map": {
   719  			Ops: []Operation{
   720  				ExtractApply{
   721  					Manager: "apply-one",
   722  					Object: `
   723  						atomicMap:
   724  						 a: c
   725  						 b: d
   726  					`,
   727  					APIVersion: "v1",
   728  				},
   729  			},
   730  			Object: `
   731  				atomicMap:
   732  				 a: c
   733  				 b: d
   734  			`,
   735  			APIVersion: "v1",
   736  			Managed: fieldpath.ManagedFields{
   737  				"apply-one": fieldpath.NewVersionedSet(
   738  					_NS(
   739  						_P("atomicMap"),
   740  					),
   741  					"v1",
   742  					false,
   743  				),
   744  			},
   745  		},
   746  	}
   747  
   748  	for name, test := range tests {
   749  		t.Run(name, func(t *testing.T) {
   750  			if err := test.Test(extractParser); err != nil {
   751  				t.Fatal(err)
   752  			}
   753  		})
   754  	}
   755  }
   756  

View as plain text