...

Source file src/sigs.k8s.io/kustomize/kyaml/yaml/merge2/list_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 listTestCases = []testCase{
    11  	{description: `strategic merge patch delete 1`,
    12  		source: `
    13  apiVersion: apps/v1
    14  kind: Deployment
    15  spec:
    16    template:
    17      spec:
    18        containers:
    19        - name: foo1
    20          $patch: delete
    21        - name: foo2
    22        - name: foo3
    23  `,
    24  		dest: `
    25  apiVersion: apps/v1
    26  kind: Deployment
    27  spec:
    28    template:
    29      spec:
    30        containers:
    31        - name: foo1
    32        - name: foo2
    33        - name: foo3
    34  `,
    35  		expected: `
    36  apiVersion: apps/v1
    37  kind: Deployment
    38  spec:
    39    template:
    40      spec:
    41        containers:
    42        - name: foo2
    43        - name: foo3
    44  `,
    45  		mergeOptions: yaml.MergeOptions{
    46  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
    47  		},
    48  	},
    49  	{description: `strategic merge patch delete 2`,
    50  		source: `
    51  apiVersion: apps/v1
    52  kind: Deployment
    53  spec:
    54    template:
    55      spec:
    56        containers:
    57        - name: foo1
    58        - name: foo2
    59        - name: foo3
    60          $patch: delete
    61  `,
    62  		dest: `
    63  apiVersion: apps/v1
    64  kind: Deployment
    65  spec:
    66    template:
    67      spec:
    68        containers:
    69        - name: foo1
    70        - name: foo2
    71        - name: foo3
    72  `,
    73  		expected: `
    74  apiVersion: apps/v1
    75  kind: Deployment
    76  spec:
    77    template:
    78      spec:
    79        containers:
    80        - name: foo1
    81        - name: foo2
    82  `,
    83  		mergeOptions: yaml.MergeOptions{
    84  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
    85  		},
    86  	},
    87  	{description: `merge k8s deployment containers - prepend`,
    88  		source: `
    89  apiVersion: apps/v1
    90  kind: Deployment
    91  spec:
    92    template:
    93      spec:
    94        containers:
    95        - name: foo1
    96        - name: foo2
    97        - name: foo3
    98  `,
    99  		dest: `
   100  apiVersion: apps/v1
   101  kind: Deployment
   102  spec:
   103    template:
   104      spec:
   105        containers:
   106        - name: foo0
   107  `,
   108  		expected: `
   109  apiVersion: apps/v1
   110  kind: Deployment
   111  spec:
   112    template:
   113      spec:
   114        containers:
   115        - name: foo1
   116        - name: foo2
   117        - name: foo3
   118        - name: foo0
   119        `,
   120  		mergeOptions: yaml.MergeOptions{
   121  			ListIncreaseDirection: yaml.MergeOptionsListPrepend,
   122  		},
   123  	},
   124  	{description: `merge k8s deployment containers - append`,
   125  		source: `
   126  apiVersion: apps/v1
   127  kind: Deployment
   128  spec:
   129    template:
   130      spec:
   131        containers:
   132        - name: foo1
   133        - name: foo2
   134        - name: foo3
   135  `,
   136  		dest: `
   137  apiVersion: apps/v1
   138  kind: Deployment
   139  spec:
   140    template:
   141      spec:
   142        containers:
   143        - name: foo0
   144  `,
   145  		expected: `
   146  apiVersion: apps/v1
   147  kind: Deployment
   148  spec:
   149    template:
   150      spec:
   151        containers:
   152        - name: foo0
   153        - name: foo1
   154        - name: foo2
   155        - name: foo3
   156  `,
   157  		mergeOptions: yaml.MergeOptions{
   158  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   159  		},
   160  	},
   161  	{description: `merge k8s deployment volumes - append`,
   162  		source: `
   163  apiVersion: apps/v1
   164  kind: Deployment
   165  spec:
   166    template:
   167      spec:
   168        volumes:
   169        - name: foo1
   170        - name: foo2
   171        - name: foo3
   172  `,
   173  		dest: `
   174  apiVersion: apps/v1
   175  kind: Deployment
   176  spec:
   177    template:
   178      spec:
   179        volumes:
   180        - name: foo0
   181  `,
   182  		expected: `
   183  apiVersion: apps/v1
   184  kind: Deployment
   185  spec:
   186    template:
   187      spec:
   188        volumes:
   189        - name: foo0
   190        - name: foo1
   191        - name: foo2
   192        - name: foo3
   193  `,
   194  		mergeOptions: yaml.MergeOptions{
   195  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   196  		},
   197  	},
   198  	{description: `merge k8s deployment volumes - prepend`,
   199  		source: `
   200  apiVersion: apps/v1
   201  kind: Deployment
   202  spec:
   203    template:
   204      spec:
   205        volumes:
   206        - name: foo1
   207        - name: foo2
   208        - name: foo3
   209  `,
   210  		dest: `
   211  apiVersion: apps/v1
   212  kind: Deployment
   213  spec:
   214    template:
   215      spec:
   216        volumes:
   217        - name: foo0
   218  `,
   219  		expected: `
   220  apiVersion: apps/v1
   221  kind: Deployment
   222  spec:
   223    template:
   224      spec:
   225        volumes:
   226        - name: foo1
   227        - name: foo2
   228        - name: foo3
   229        - name: foo0
   230  `,
   231  		mergeOptions: yaml.MergeOptions{
   232  			ListIncreaseDirection: yaml.MergeOptionsListPrepend,
   233  		},
   234  	},
   235  	{description: `merge k8s deployment containers -- $patch directive`,
   236  		source: `
   237      apiVersion: apps/v1
   238      kind: Deployment
   239      spec:
   240        template:
   241          spec:
   242            containers:
   243            - name: foo1
   244            - name: foo2
   245            - name: foo3
   246            - $patch: merge
   247  `,
   248  		dest: `
   249      apiVersion: apps/v1
   250      kind: Deployment
   251      spec:
   252        template:
   253          spec:
   254            containers:
   255            - name: foo4
   256            - name: foo5
   257  `,
   258  		expected: `
   259      apiVersion: apps/v1
   260      kind: Deployment
   261      spec:
   262        template:
   263          spec:
   264            containers:
   265            - name: foo1
   266            - name: foo2
   267            - name: foo3
   268            - name: foo4
   269            - name: foo5
   270  `,
   271  	},
   272  	{description: `replace k8s deployment containers -- $patch directive`,
   273  		source: `
   274      apiVersion: apps/v1
   275      kind: Deployment
   276      spec:
   277        template:
   278          spec:
   279            containers:
   280            - name: foo1
   281            - name: foo2
   282            - name: foo3
   283            - $patch: replace
   284  `,
   285  		dest: `
   286      apiVersion: apps/v1
   287      kind: Deployment
   288      spec:
   289        template:
   290          spec:
   291            containers:
   292            - name: foo4
   293            - name: foo5
   294  `,
   295  		expected: `
   296      apiVersion: apps/v1
   297      kind: Deployment
   298      spec:
   299        template:
   300          spec:
   301            containers:
   302            - name: foo1
   303            - name: foo2
   304            - name: foo3
   305  `,
   306  	},
   307  	{description: `remove k8s deployment containers -- $patch directive`,
   308  		source: `
   309      apiVersion: apps/v1
   310      kind: Deployment
   311      spec:
   312        template:
   313          spec:
   314            containers:
   315            - name: foo1
   316            - name: foo2
   317            - name: foo3
   318            - $patch: delete
   319  `,
   320  		dest: `
   321      apiVersion: apps/v1
   322      kind: Deployment
   323      spec:
   324        template:
   325          spec:
   326            containers:
   327            - name: foo4
   328            - name: foo5
   329  `,
   330  		expected: `
   331      apiVersion: apps/v1
   332      kind: Deployment
   333      spec:
   334        template:
   335          spec: {}
   336  `,
   337  	},
   338  
   339  	{description: `replace List -- different value in dest`,
   340  		source: `
   341  kind: Deployment
   342  items:
   343  - 1
   344  - 2
   345  - 3
   346  `,
   347  		dest: `
   348  kind: Deployment
   349  items:
   350  - 0
   351  - 1
   352  `,
   353  		expected: `
   354  kind: Deployment
   355  items:
   356  - 1
   357  - 2
   358  - 3
   359  `,
   360  		mergeOptions: yaml.MergeOptions{
   361  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   362  		},
   363  	},
   364  
   365  	{description: `replace List -- missing from dest`,
   366  		source: `
   367  kind: Deployment
   368  items:
   369  - 1
   370  - 2
   371  - 3
   372  `,
   373  		dest: `
   374  kind: Deployment
   375  `,
   376  		expected: `
   377  kind: Deployment
   378  items:
   379  - 1
   380  - 2
   381  - 3
   382  `,
   383  		mergeOptions: yaml.MergeOptions{
   384  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   385  		},
   386  	},
   387  
   388  	//
   389  	// Test Case
   390  	//
   391  	{description: `keep List -- same value in src and dest`,
   392  		source: `
   393  kind: Deployment
   394  items:
   395  - 1
   396  - 2
   397  - 3
   398  `,
   399  		dest: `
   400  kind: Deployment
   401  items:
   402  - 1
   403  - 2
   404  - 3
   405  `,
   406  		expected: `
   407  kind: Deployment
   408  items:
   409  - 1
   410  - 2
   411  - 3
   412  `,
   413  		mergeOptions: yaml.MergeOptions{
   414  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   415  		},
   416  	},
   417  
   418  	//
   419  	// Test Case
   420  	//
   421  	{description: `keep List -- unspecified in src`,
   422  		source: `
   423  kind: Deployment
   424  `,
   425  		dest: `
   426  kind: Deployment
   427  items:
   428  - 1
   429  - 2
   430  - 3
   431  `,
   432  		expected: `
   433  kind: Deployment
   434  items:
   435  - 1
   436  - 2
   437  - 3
   438  `,
   439  		mergeOptions: yaml.MergeOptions{
   440  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   441  		},
   442  	},
   443  
   444  	//
   445  	// Test Case
   446  	//
   447  	{description: `remove List -- null in src`,
   448  		source: `
   449  kind: Deployment
   450  items: null
   451  `,
   452  		dest: `
   453  kind: Deployment
   454  items:
   455  - 1
   456  - 2
   457  - 3
   458  `,
   459  		expected: `
   460  kind: Deployment
   461  `,
   462  		mergeOptions: yaml.MergeOptions{
   463  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   464  		},
   465  	},
   466  
   467  	//
   468  	// Test Case
   469  	//
   470  	{description: `remove list -- empty in src`,
   471  		source: `
   472  kind: Deployment
   473  items: []
   474  `,
   475  		dest: `
   476  kind: Deployment
   477  items:
   478  - 1
   479  - 2
   480  - 3
   481  `,
   482  		expected: `
   483  kind: Deployment
   484  items: []
   485  `,
   486  		mergeOptions: yaml.MergeOptions{
   487  			ListIncreaseDirection: yaml.MergeOptionsListAppend,
   488  		},
   489  	},
   490  }
   491  

View as plain text