github.com/verrazzano/verrazzano@v1.7.1/pkg/yaml/replace_merge_test.go (about)

     1  // Copyright (c) 2021, 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package yaml
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // rmBase is the base of a nested merge with a list
    13  const rmBase = `
    14  name: base
    15  host:
    16    ip: 1.2.3.4
    17    name: foo
    18  platform:
    19    vendor: company1
    20    os:
    21      name: linux
    22      patches:
    23      - version: 0.5.0
    24        date: 01/01/2020
    25  `
    26  
    27  // rmOverlay is the overlay of a nested merge with a list
    28  const rmOverlay = `
    29  host:
    30    name: bar
    31  platform:
    32    os:
    33      patches:
    34      - version: 0.6.0
    35        date: 02/02/2022
    36  `
    37  
    38  // rmMerged is the result of a nested merge where the list is replaced
    39  const rmMerged = `
    40  name: base
    41  host:
    42    ip: 1.2.3.4
    43    name: bar
    44  platform:
    45    vendor: company1
    46    os:
    47      name: linux
    48      patches:
    49      - version: 0.6.0
    50        date: 02/02/2022
    51  `
    52  
    53  // TestMergeReplace tests the ReplacementMerge function with nested YAML
    54  // GIVEN a set of nested YAML strings with embedded lists
    55  // WHEN ReplacementMerge is called
    56  // THEN ensure that the merged result is correct.
    57  func TestMergeReplace(t *testing.T) {
    58  	assert := assert.New(t)
    59  	merged, err := ReplacementMerge(rmBase, rmOverlay)
    60  	assert.NoError(err, merged, "error merging nested yaml")
    61  	assert.YAMLEq(rmMerged, merged, "nested yaml should be equal")
    62  }
    63  
    64  // istioBase is the base of an IstioOperator YAML
    65  const istioBase = `
    66  apiVersion: install.istio.io/v1alpha1
    67  kind: IstioOperator
    68  spec:
    69    components:
    70      egressGateways:
    71        - name: istio-egressgateway
    72          enabled: true
    73  
    74    # Global values passed through to helm global.yaml.
    75    # Please keep this in sync with manifests/charts/global.yaml
    76    values:
    77      global:
    78        gateways:
    79          istio-ingressgateway:
    80            serviceAnnotations:
    81              service.beta.kubernetes.io/oci-load-balancer-shape: flexible
    82  `
    83  
    84  // istiOverlay is the overlay of an IstioOperator YAML
    85  const istiOverlay = `
    86  apiVersion: install.istio.io/v1alpha1
    87  kind: IstioOperator
    88  spec:
    89    components:
    90      ingressGateways:
    91        - name: istio-ingressgateway
    92          enabled: true
    93          k8s:
    94            service:
    95              type: ClusterIP
    96              externalIPs:
    97              - 1.2.3.4
    98  `
    99  
   100  // istioMerged is the result of a merge of IstioOperator YAMLs
   101  const istioMerged = `
   102  apiVersion: install.istio.io/v1alpha1
   103  kind: IstioOperator
   104  spec:
   105    components:
   106      egressGateways:
   107        - name: istio-egressgateway
   108          enabled: true
   109      ingressGateways:
   110        - name: istio-ingressgateway
   111          enabled: true
   112          k8s:
   113            service:
   114              type: ClusterIP
   115              externalIPs:
   116              - 1.2.3.4
   117  
   118    # Global values passed through to helm global.yaml.
   119    # Please keep this in sync with manifests/charts/global.yaml
   120    values:
   121      global:
   122        gateways:
   123          istio-ingressgateway:
   124            serviceAnnotations:
   125              service.beta.kubernetes.io/oci-load-balancer-shape: flexible
   126  `
   127  
   128  const jaegerBase = `
   129  apiVersion: install.istio.io/v1alpha1
   130  kind: IstioOperator
   131  spec:
   132    values:
   133      meshConfig:
   134        defaultConfig:
   135          tracing:
   136            zipkin:
   137              address: abc.xyz
   138            tlsSettings:
   139              mode: ISTIO_MUTUAL
   140  `
   141  const jaegerOverlay = `
   142  apiVersion: install.istio.io/v1alpha1
   143  kind: IstioOperator
   144  spec:
   145    values:
   146      meshConfig:
   147        defaultConfig:
   148          tracing:
   149            sampling: 90
   150            zipkin:
   151              address: cdef.xyz
   152  `
   153  
   154  const jaegerMerged = `
   155  apiVersion: install.istio.io/v1alpha1
   156  kind: IstioOperator
   157  spec:
   158    values:
   159      meshConfig:
   160        defaultConfig:
   161          tracing:
   162            sampling: 90
   163            zipkin:
   164              address: cdef.xyz
   165            tlsSettings:
   166              mode: ISTIO_MUTUAL
   167  `
   168  
   169  // TestMergeReplaceIstio tests the ReplacementMerge function with IstiOperator YAML
   170  // GIVEN a set of nested YAML strings with embedded lists
   171  // WHEN ReplacementMerge is called
   172  // THEN ensure that the merged result is correct.
   173  func TestMergeReplaceIstio(t *testing.T) {
   174  	assert := assert.New(t)
   175  	merged, err := ReplacementMerge(istioBase, istiOverlay)
   176  	assert.NoError(err, merged, "error merging Istio YAML")
   177  	assert.YAMLEq(istioMerged, merged, "incorrect Istio merged YAML")
   178  }
   179  
   180  // TestMergeReplaceJaeger tests the ReplacementMerge function with IstiOperator YAML
   181  // GIVEN a set of nested YAML strings with embedded lists
   182  // WHEN ReplacementMerge is called
   183  // THEN ensure that the merged result is correct.
   184  func TestMergeReplaceJaeger(t *testing.T) {
   185  	assert := assert.New(t)
   186  	merged, err := ReplacementMerge(jaegerBase, jaegerOverlay)
   187  	assert.NoError(err, merged, "error merging Istio YAML")
   188  	assert.YAMLEq(jaegerMerged, merged, "incorrect Istio merged YAML")
   189  }
   190  
   191  // Complete replace YAML
   192  const rm1 = `
   193  k1: rm1-v1
   194  k2:
   195    k3: rm1-k2.3
   196    k4: rm1-k2.4
   197  `
   198  const rm2 = `
   199  k1: rm2-v1
   200  k2:
   201    k3: rm2-k2.3
   202    k4: rm2-k2.4
   203  `
   204  
   205  // rm2 merged on top of rm1
   206  const rm1_2 = `
   207  k1: rm2-v1
   208  k2:
   209    k3: rm2-k2.3
   210    k4: rm2-k2.4
   211  `
   212  
   213  // Partial replace YAML
   214  const rm3 = `
   215  k1: rm3-v1
   216  k2:
   217    k4: rm3-k2.4
   218  `
   219  const rm4 = `
   220  k1: rm4-v1
   221  k2:
   222    k3: rm4-k2.3
   223  `
   224  
   225  // rm4 merged on top of rm3
   226  const rm3_4 = `
   227  k1: rm4-v1
   228  k2:
   229    k3: rm4-k2.3
   230    k4: rm3-k2.4
   231  `
   232  
   233  // Appending keys YAML
   234  const rm5 = `
   235  k1: rm5-v1
   236  `
   237  const rm6 = `
   238  k2:
   239    k3: rm6-k2.3
   240  `
   241  const rm7 = `
   242  k2:
   243    k4: rm7-k2.4
   244  `
   245  
   246  // rm4 merged on top of rm3
   247  const rm5_6_7 = `
   248  k1: rm5-v1
   249  k2:
   250    k3: rm6-k2.3
   251    k4: rm7-k2.4
   252  `
   253  
   254  // TestReplaceMany tests the ReplacementMerge function
   255  // GIVEN a set of yamls
   256  // WHEN ReplacementMerge is called
   257  // THEN ensure that the result is correct.
   258  func TestReplaceMany(t *testing.T) {
   259  	tests := []struct {
   260  		testName string
   261  		values   []string
   262  		expected string
   263  	}{
   264  		{
   265  			testName: "1",
   266  			values:   []string{rm1, rm2},
   267  			expected: rm1_2,
   268  		},
   269  		{
   270  			testName: "2",
   271  			values:   []string{rm3, rm4},
   272  			expected: rm3_4,
   273  		},
   274  		{
   275  			testName: "3",
   276  			values:   []string{rm5, rm6, rm7},
   277  			expected: rm5_6_7,
   278  		},
   279  	}
   280  	for _, test := range tests {
   281  		t.Run(test.testName, func(t *testing.T) {
   282  			assert := assert.New(t)
   283  			s, err := ReplacementMerge(test.values...)
   284  			assert.NoError(err, s, "error merging yamls")
   285  			assert.YAMLEq(test.expected, s, "Result does not match expected value")
   286  		})
   287  	}
   288  }