...

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

View as plain text