...

Source file src/sigs.k8s.io/kustomize/kyaml/yaml/merge2/element_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/yaml/merge2

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package merge2_test
     5  
     6  import (
     7  	"sigs.k8s.io/kustomize/kyaml/yaml"
     8  )
     9  
    10  var elementTestCases = []testCase{
    11  	{description: `merge Element -- keep field in dest`,
    12  		source: `
    13  apiVersion: apps/v1
    14  kind: Deployment
    15  spec:
    16    template:
    17      spec:
    18        containers:
    19        - name: foo
    20          image: foo:v1
    21  `,
    22  		dest: `
    23  apiVersion: apps/v1
    24  kind: Deployment
    25  spec:
    26    template:
    27      spec:
    28        containers:
    29        - name: foo
    30          image: foo:v0
    31          command: ['run.sh']
    32  `,
    33  		expected: `
    34  apiVersion: apps/v1
    35  kind: Deployment
    36  spec:
    37    template:
    38      spec:
    39        containers:
    40        - name: foo
    41          image: foo:v1
    42          command: ['run.sh']
    43  `,
    44  		mergeOptions: yaml.MergeOptions{
    45  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
    46  		},
    47  	},
    48  
    49  	{description: `merge Element -- add field to dest`,
    50  		source: `
    51  apiVersion: apps/v1
    52  kind: Deployment
    53  spec:
    54    template:
    55      spec:
    56        containers:
    57        - name: foo
    58          image: foo:v1
    59          command: ['run.sh']
    60  `,
    61  		dest: `
    62  apiVersion: apps/v1
    63  kind: Deployment
    64  spec:
    65    template:
    66      spec:
    67        containers:
    68        - name: foo
    69          image: foo:v0
    70  `,
    71  		expected: `
    72  apiVersion: apps/v1
    73  kind: Deployment
    74  spec:
    75    template:
    76      spec:
    77        containers:
    78        - name: foo
    79          image: foo:v1
    80          command: ['run.sh']
    81  `,
    82  		mergeOptions: yaml.MergeOptions{
    83  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
    84  		},
    85  	},
    86  
    87  	{description: `merge Element -- add list, empty in dest`,
    88  		source: `
    89  apiVersion: apps/v1
    90  kind: Deployment
    91  spec:
    92    template:
    93      spec:
    94        containers:
    95        - name: foo
    96          image: foo:v1
    97          command: ['run.sh']
    98  `,
    99  		dest: `
   100  apiVersion: apps/v1
   101  kind: Deployment
   102  spec:
   103    template:
   104      spec:
   105        containers: []
   106  `,
   107  		expected: `
   108  apiVersion: apps/v1
   109  kind: Deployment
   110  spec:
   111    template:
   112      spec:
   113        containers:
   114        - name: foo
   115          image: foo:v1
   116          command: ['run.sh']
   117  `,
   118  		mergeOptions: yaml.MergeOptions{
   119  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   120  		},
   121  	},
   122  
   123  	{description: `merge Element -- add list, missing from dest`,
   124  		source: `
   125  apiVersion: apps/v1
   126  kind: Deployment
   127  spec:
   128    template:
   129      spec:
   130        containers:
   131        - name: foo
   132          image: foo:v1
   133          command: ['run.sh']
   134  `,
   135  		dest: `
   136  apiVersion: apps/v1
   137  kind: Deployment
   138  `,
   139  		expected: `
   140  apiVersion: apps/v1
   141  kind: Deployment
   142  spec:
   143    template:
   144      spec:
   145        containers:
   146        - name: foo
   147          image: foo:v1
   148          command: ['run.sh']
   149  `,
   150  		mergeOptions: yaml.MergeOptions{
   151  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   152  		},
   153  	},
   154  
   155  	{description: `merge Element -- add Element first`,
   156  		source: `
   157  apiVersion: apps/v1
   158  kind: Deployment
   159  spec:
   160    template:
   161      spec:
   162        containers:
   163        - name: bar
   164          image: bar:v1
   165          command: ['run2.sh']
   166        - name: foo
   167          image: foo:v1
   168  `,
   169  		dest: `
   170  apiVersion: apps/v1
   171  kind: Deployment
   172  spec:
   173    template:
   174      spec:
   175        containers:
   176        - name: foo
   177          image: foo:v0
   178  `,
   179  		expected: `
   180  apiVersion: apps/v1
   181  kind: Deployment
   182  spec:
   183    template:
   184      spec:
   185        containers:
   186        - name: foo
   187          image: foo:v1
   188        - name: bar
   189          image: bar:v1
   190          command: ['run2.sh']
   191  `,
   192  		mergeOptions: yaml.MergeOptions{
   193  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   194  		},
   195  	},
   196  
   197  	{description: `merge Element -- add Element second`,
   198  		source: `
   199  apiVersion: apps/v1
   200  kind: Deployment
   201  spec:
   202    template:
   203      spec:
   204        containers:
   205        - name: foo
   206          image: foo:v1
   207        - name: bar
   208          image: bar:v1
   209          command: ['run2.sh']
   210  `,
   211  		dest: `
   212  apiVersion: apps/v1
   213  kind: Deployment
   214  spec:
   215    template:
   216      spec:
   217        containers:
   218        - name: foo
   219          image: foo:v0
   220  `,
   221  		expected: `
   222  apiVersion: apps/v1
   223  kind: Deployment
   224  spec:
   225    template:
   226      spec:
   227        containers:
   228        - name: foo
   229          image: foo:v1
   230        - name: bar
   231          image: bar:v1
   232          command: ['run2.sh']
   233  `,
   234  		mergeOptions: yaml.MergeOptions{
   235  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   236  		},
   237  	},
   238  
   239  	{description: `merge Element -- add Element third`,
   240  		source: `
   241  apiVersion: apps/v1
   242  kind: Deployment
   243  spec:
   244    template:
   245      spec:
   246        containers:
   247        - name: bar
   248          image: bar:v1
   249          command: ['run2.sh']
   250        - name: foo
   251          image: foo:v1
   252  `,
   253  		dest: `
   254  apiVersion: apps/v1
   255  kind: Deployment
   256  spec:
   257    template:
   258      spec:
   259        containers:
   260        - name: foo
   261          image: foo:v0
   262  `,
   263  		expected: `
   264  apiVersion: apps/v1
   265  kind: Deployment
   266  spec:
   267    template:
   268      spec:
   269        containers:
   270        - name: bar
   271          image: bar:v1
   272          command: ['run2.sh']
   273        - name: foo
   274          image: foo:v1
   275  `,
   276  		mergeOptions: yaml.MergeOptions{
   277  			ListIncreaseDirection: yaml.MergeOptionsListPrepend,
   278  		},
   279  	},
   280  
   281  	{description: `merge Element -- add Element fourth`,
   282  		source: `
   283  apiVersion: apps/v1
   284  kind: Deployment
   285  spec:
   286    template:
   287      spec:
   288        containers:
   289        - name: foo
   290          image: foo:v1
   291        - name: bar
   292          image: bar:v1
   293          command: ['run2.sh']
   294  `,
   295  		dest: `
   296  apiVersion: apps/v1
   297  kind: Deployment
   298  spec:
   299    template:
   300      spec:
   301        containers:
   302        - name: foo
   303          image: foo:v0
   304  `,
   305  		expected: `
   306  apiVersion: apps/v1
   307  kind: Deployment
   308  spec:
   309    template:
   310      spec:
   311        containers:
   312        - name: foo
   313          image: foo:v1
   314        - name: bar
   315          image: bar:v1
   316          command: ['run2.sh']
   317  `,
   318  		mergeOptions: yaml.MergeOptions{
   319  			ListIncreaseDirection: yaml.MergeOptionsListPrepend,
   320  		},
   321  	},
   322  
   323  	//
   324  	// Test Case
   325  	//
   326  	{description: `keep list -- list missing from src`,
   327  		source: `
   328  apiVersion: apps/v1
   329  kind: Deployment
   330  `,
   331  		dest: `
   332  apiVersion: apps/v1
   333  kind: Deployment
   334  spec:
   335    template:
   336      spec:
   337        containers:
   338        - name: foo
   339          image: foo:v1
   340        - name: bar
   341          image: bar:v1
   342          command: ['run2.sh']
   343  `,
   344  		expected: `
   345  apiVersion: apps/v1
   346  kind: Deployment
   347  spec:
   348    template:
   349      spec:
   350        containers:
   351        - name: foo
   352          image: foo:v1
   353        - name: bar
   354          image: bar:v1
   355          command: ['run2.sh']
   356  `,
   357  		mergeOptions: yaml.MergeOptions{
   358  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   359  		},
   360  	},
   361  
   362  	//
   363  	// Test Case
   364  	//
   365  	{description: `keep Element -- element missing in src`,
   366  		source: `
   367  apiVersion: apps/v1
   368  kind: Deployment
   369  spec:
   370    template:
   371      spec:
   372        containers:
   373        - name: foo
   374          image: foo:v1
   375  `,
   376  		dest: `
   377  apiVersion: apps/v1
   378  kind: Deployment
   379  spec:
   380    template:
   381      spec:
   382        containers:
   383        - name: foo
   384          image: foo:v0
   385        - name: bar
   386          image: bar:v1
   387          command: ['run2.sh']
   388  `,
   389  		expected: `
   390  apiVersion: apps/v1
   391  kind: Deployment
   392  spec:
   393    template:
   394      spec:
   395        containers:
   396        - name: foo
   397          image: foo:v1
   398        - name: bar
   399          image: bar:v1
   400          command: ['run2.sh']
   401  `,
   402  		mergeOptions: yaml.MergeOptions{
   403  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   404  		},
   405  	},
   406  
   407  	//
   408  	// Test Case
   409  	//
   410  	{description: `keep element -- empty list in src`,
   411  		source: `
   412  apiVersion: apps/v1
   413  kind: Deployment
   414  spec:
   415    template:
   416      spec:
   417        containers: []
   418  `,
   419  		dest: `
   420  apiVersion: apps/v1
   421  kind: Deployment
   422  spec:
   423    template:
   424      spec:
   425        containers:
   426        - name: foo
   427          image: foo:v1
   428        - name: bar
   429          image: bar:v1
   430          command: ['run2.sh']
   431  `,
   432  		expected: `
   433  apiVersion: apps/v1
   434  kind: Deployment
   435  spec:
   436    template:
   437      spec:
   438        containers:
   439        - name: foo
   440          image: foo:v1
   441        - name: bar
   442          image: bar:v1
   443          command: ['run2.sh']
   444  `,
   445  		mergeOptions: yaml.MergeOptions{
   446  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   447  		},
   448  	},
   449  
   450  	//
   451  	// Test Case
   452  	//
   453  	{description: `remove Element -- null in src`,
   454  		source: `
   455  apiVersion: apps/v1
   456  kind: Deployment
   457  spec:
   458    template:
   459      spec:
   460        containers: null
   461  `,
   462  		dest: `
   463  apiVersion: apps/v1
   464  kind: Deployment
   465  spec:
   466    template:
   467      spec:
   468        containers:
   469        - name: foo
   470          image: foo:v1
   471        - name: bar
   472          image: bar:v1
   473          command: ['run2.sh']
   474  `,
   475  		expected: `
   476  apiVersion: apps/v1
   477  kind: Deployment
   478  spec:
   479    template:
   480      spec: {}
   481  `,
   482  		mergeOptions: yaml.MergeOptions{
   483  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   484  		},
   485  	},
   486  
   487  	//
   488  	// Test Case
   489  	//
   490  	{description: `infer merge keys merge'`,
   491  		source: `
   492  apiVersion: custom
   493  kind: Deployment
   494  containers:
   495  - name: foo
   496    command: ['run2.sh']
   497  `,
   498  		dest: `
   499  apiVersion: custom
   500  kind: Deployment
   501  containers:
   502  - name: foo
   503    image: foo:bar
   504  `,
   505  		expected: `
   506  apiVersion: custom
   507  kind: Deployment
   508  containers:
   509  - name: foo
   510    image: foo:bar
   511    command: ['run2.sh']
   512  `,
   513  		infer: true,
   514  		mergeOptions: yaml.MergeOptions{
   515  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   516  		},
   517  	},
   518  
   519  	//
   520  	// Test Case
   521  	//
   522  	{description: `no infer merge keys merge using schema`,
   523  		source: `
   524  apiVersion: apps/v1
   525  kind: Deployment
   526  spec:
   527    template:
   528      spec:
   529        containers:
   530        - name: foo
   531          command: ['run2.sh']
   532  `,
   533  		dest: `
   534  apiVersion: apps/v1
   535  kind: Deployment
   536  spec:
   537    template:
   538      spec:
   539        containers:
   540        - name: foo
   541          image: foo:bar
   542  `,
   543  		expected: `
   544  apiVersion: apps/v1
   545  kind: Deployment
   546  spec:
   547    template:
   548      spec:
   549        containers:
   550        - name: foo
   551          image: foo:bar
   552          command: ['run2.sh']
   553  `,
   554  		infer: false,
   555  		mergeOptions: yaml.MergeOptions{
   556  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   557  		},
   558  	},
   559  
   560  	//
   561  	// Test Case
   562  	//
   563  	{description: `no infer merge keys merge using explicit schema as line comment'`,
   564  		source: `
   565  apiVersion: custom
   566  kind: Deployment
   567  containers:
   568  - name: foo
   569    command: ['run2.sh']
   570  `,
   571  		dest: `
   572  apiVersion: custom
   573  kind: Deployment
   574  containers: # {"items":{"$ref": "#/definitions/io.k8s.api.core.v1.Container"},"type":"array","x-kubernetes-patch-merge-key":"name","x-kubernetes-patch-strategy": "merge"}
   575  - name: foo # hell ow
   576    image: foo:bar
   577  `,
   578  		expected: `
   579  apiVersion: custom
   580  kind: Deployment
   581  containers: # {"items":{"$ref": "#/definitions/io.k8s.api.core.v1.Container"},"type":"array","x-kubernetes-patch-merge-key":"name","x-kubernetes-patch-strategy": "merge"}
   582  - name: foo
   583    image: foo:bar
   584    command: ['run2.sh']
   585  `,
   586  		infer: false,
   587  		mergeOptions: yaml.MergeOptions{
   588  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   589  		},
   590  	},
   591  
   592  	{description: `merge_primitive_finalizers`,
   593  		source: `
   594  apiVersion: apps/v1
   595  kind: Deployment
   596  metadata:
   597    finalizers:
   598    - a
   599    - b 
   600  `,
   601  		dest: `
   602  apiVersion: apps/v1
   603  kind: Deployment
   604  metadata:
   605    finalizers:
   606    - b
   607    - c
   608  `,
   609  		expected: `
   610  apiVersion: apps/v1
   611  kind: Deployment
   612  metadata:
   613    finalizers:
   614    - b
   615    - c
   616    - a
   617  `,
   618  		mergeOptions: yaml.MergeOptions{
   619  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   620  		},
   621  	},
   622  
   623  	{description: `merge_primitive_items`,
   624  		source: `
   625  apiVersion: apps/v1
   626  kind: Deployment
   627  items: # {"type":"array", "x-kubernetes-patch-strategy": "merge"}
   628  - a
   629  - b
   630  `,
   631  		dest: `
   632  apiVersion: apps/v1
   633  kind: Deployment
   634  items:
   635  - b
   636  - c
   637  `,
   638  		expected: `
   639  apiVersion: apps/v1
   640  kind: Deployment
   641  items:
   642  - b
   643  - c
   644  - a
   645  `,
   646  		mergeOptions: yaml.MergeOptions{
   647  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   648  		},
   649  	},
   650  }
   651  

View as plain text