github.com/verrazzano/verrazzano@v1.7.0/pkg/yaml/helm_test.go (about)

     1  // Copyright (c) 2021, 2022, 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  	"github.com/verrazzano/verrazzano/pkg/bom"
    11  )
    12  
    13  const oneOverrideExpect = `name: test-name
    14  `
    15  const oneListOverrideExpect = `name:
    16  - test-name
    17  `
    18  const oneListNestedExpect = `name:
    19  - name: test-name
    20  `
    21  const multipleListNestedExpect = `name:
    22  - name1: test-name
    23    name2: test-name
    24    name3: test-name
    25  `
    26  const multilineExpect = `name: |-
    27    name1
    28    name2
    29    name3
    30  `
    31  const multipleListExpect = `name:
    32  - name1: test-name
    33  - name2: test-name
    34  - name3: test-name
    35  `
    36  const escapedExpect = `name.name/name: name
    37  `
    38  
    39  const hyphenName = `name:
    40  - name-name: name
    41  `
    42  
    43  // TestExpand tests the Expand function
    44  // GIVEN a set of dot separated names
    45  // WHEN Expand is called
    46  // THEN ensure that the expanded result is correct.
    47  func TestHelmValueFileConstructor(t *testing.T) {
    48  	tests := []struct {
    49  		name        string
    50  		kvs         []bom.KeyValue
    51  		expected    string
    52  		expectError bool
    53  	}{
    54  		{
    55  			name:        "test no overrides",
    56  			kvs:         []bom.KeyValue{},
    57  			expected:    "{}\n",
    58  			expectError: false,
    59  		},
    60  		{
    61  			name: "test one override",
    62  			kvs: []bom.KeyValue{
    63  				{Key: "name", Value: "test-name"},
    64  			},
    65  			expected:    oneOverrideExpect,
    66  			expectError: false,
    67  		},
    68  		{
    69  			name: "test one list override",
    70  			kvs: []bom.KeyValue{
    71  				{Key: "name[0]", Value: "test-name"},
    72  			},
    73  			expected:    oneListOverrideExpect,
    74  			expectError: false,
    75  		},
    76  		{
    77  			name: "test nested list",
    78  			kvs: []bom.KeyValue{
    79  				{Key: "name[0].name", Value: "test-name"},
    80  			},
    81  			expected:    oneListNestedExpect,
    82  			expectError: false,
    83  		},
    84  		{
    85  			name: "test multiple list updates",
    86  			kvs: []bom.KeyValue{
    87  				{Key: "name[0].name1", Value: "test-name"},
    88  				{Key: "name[0].name2", Value: "test-name"},
    89  				{Key: "name[0].name3", Value: "test-name"},
    90  			},
    91  			expected:    multipleListNestedExpect,
    92  			expectError: false,
    93  		},
    94  		{
    95  			name: "test multiline",
    96  			kvs: []bom.KeyValue{
    97  				{Key: "name", Value: "name1\nname2\nname3"},
    98  			},
    99  			expected:    multilineExpect,
   100  			expectError: false,
   101  		},
   102  		{
   103  			name: "test array index",
   104  			kvs: []bom.KeyValue{
   105  				{Key: "name[0].name1", Value: "test-name"},
   106  				{Key: "name[1].name2", Value: "test-name"},
   107  				{Key: "name[2].name3", Value: "test-name"},
   108  			},
   109  			expected:    multipleListExpect,
   110  			expectError: false,
   111  		},
   112  		{
   113  			name: "test escaped chars",
   114  			kvs: []bom.KeyValue{
   115  				{Key: `name\.name/name`, Value: "name"},
   116  			},
   117  			expected:    escapedExpect,
   118  			expectError: false,
   119  		},
   120  		{
   121  			name: "test hyphen key",
   122  			kvs: []bom.KeyValue{
   123  				{Key: `name[0].name-name`, Value: "name"},
   124  			},
   125  			expected:    hyphenName,
   126  			expectError: false,
   127  		},
   128  	}
   129  	for _, test := range tests {
   130  		t.Run(test.name, func(t *testing.T) {
   131  			assert := assert.New(t)
   132  			s, err := HelmValueFileConstructor(test.kvs)
   133  			if test.expectError {
   134  				assert.Error(err, s, "expected error not found")
   135  			} else {
   136  				assert.NoError(err, s, "error merging profiles")
   137  			}
   138  
   139  			assert.Equal(test.expected, string(s), "Result does not match expected value")
   140  		})
   141  	}
   142  }