|
| 1 | +package deployment |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestProcessConfigMapData_NumbersAsInts(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + data map[string]string |
| 14 | + prefix string |
| 15 | + expected map[string]interface{} |
| 16 | + expectedType any |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "string value remains string", |
| 20 | + data: map[string]string{ |
| 21 | + "foo": "bar", |
| 22 | + }, |
| 23 | + prefix: "", |
| 24 | + expected: map[string]interface{}{ |
| 25 | + "foo": "bar", |
| 26 | + }, |
| 27 | + expectedType: "", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "integer number decoded correctly", |
| 31 | + data: map[string]string{ |
| 32 | + "intVal": "123", |
| 33 | + }, |
| 34 | + prefix: "", |
| 35 | + expected: map[string]interface{}{ |
| 36 | + "intVal": json.Number("123"), |
| 37 | + }, |
| 38 | + expectedType: json.Number("1"), |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "float number decoded correctly", |
| 42 | + data: map[string]string{ |
| 43 | + "floatVal": "123.45", |
| 44 | + }, |
| 45 | + prefix: "", |
| 46 | + expected: map[string]interface{}{ |
| 47 | + "floatVal": json.Number("123.45"), |
| 48 | + }, |
| 49 | + expectedType: json.Number("1"), |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "with prefix", |
| 53 | + data: map[string]string{ |
| 54 | + "somekey": "42", |
| 55 | + }, |
| 56 | + prefix: "myprefix-", |
| 57 | + expected: map[string]interface{}{ |
| 58 | + "myprefix-somekey": json.Number("42"), |
| 59 | + }, |
| 60 | + expectedType: json.Number("1"), |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + for _, tt := range tests { |
| 65 | + t.Run(tt.name, func(t *testing.T) { |
| 66 | + result := make(map[string]any) |
| 67 | + err := processConfigMapData(tt.data, tt.prefix, result) |
| 68 | + assert.NoError(t, err) |
| 69 | + |
| 70 | + for k, v := range tt.expected { |
| 71 | + actual, ok := result[k] |
| 72 | + assert.True(t, ok, "key %s should exist", k) |
| 73 | + |
| 74 | + assert.IsType(t, tt.expectedType, actual) |
| 75 | + |
| 76 | + expectedVal, ok1 := v.(json.Number) |
| 77 | + actualVal, ok2 := actual.(json.Number) |
| 78 | + |
| 79 | + if ok1 && ok2 { |
| 80 | + assert.Equal(t, expectedVal, actualVal) |
| 81 | + } else { |
| 82 | + assert.Equal(t, v, actual) |
| 83 | + } |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestProcessSecretData_NumbersAsInts(t *testing.T) { |
| 90 | + tests := []struct { |
| 91 | + name string |
| 92 | + data map[string][]byte |
| 93 | + prefix string |
| 94 | + expected map[string]interface{} |
| 95 | + expectedType any |
| 96 | + }{ |
| 97 | + { |
| 98 | + name: "string value remains string", |
| 99 | + data: map[string][]byte{ |
| 100 | + "foo": []byte("bar"), |
| 101 | + }, |
| 102 | + prefix: "", |
| 103 | + expected: map[string]interface{}{ |
| 104 | + "foo": "bar", |
| 105 | + }, |
| 106 | + expectedType: "", |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "integer number decoded correctly", |
| 110 | + data: map[string][]byte{ |
| 111 | + "intVal": []byte("123"), |
| 112 | + }, |
| 113 | + prefix: "", |
| 114 | + expected: map[string]interface{}{ |
| 115 | + "intVal": json.Number("123"), |
| 116 | + }, |
| 117 | + expectedType: json.Number("1"), |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "float number decoded correctly", |
| 121 | + data: map[string][]byte{ |
| 122 | + "floatVal": []byte("123.45"), |
| 123 | + }, |
| 124 | + prefix: "", |
| 125 | + expected: map[string]interface{}{ |
| 126 | + "floatVal": json.Number("123.45"), |
| 127 | + }, |
| 128 | + expectedType: json.Number("1"), |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "with prefix", |
| 132 | + data: map[string][]byte{ |
| 133 | + "somekey": []byte("42"), |
| 134 | + }, |
| 135 | + prefix: "myprefix-", |
| 136 | + expected: map[string]interface{}{ |
| 137 | + "myprefix-somekey": json.Number("42"), |
| 138 | + }, |
| 139 | + expectedType: json.Number("1"), |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + for _, tt := range tests { |
| 144 | + t.Run(tt.name, func(t *testing.T) { |
| 145 | + result := make(map[string]any) |
| 146 | + err := processSecretData(tt.data, tt.prefix, result) |
| 147 | + assert.NoError(t, err) |
| 148 | + |
| 149 | + for k, v := range tt.expected { |
| 150 | + actual, ok := result[k] |
| 151 | + assert.True(t, ok, "key %s should exist", k) |
| 152 | + |
| 153 | + assert.IsType(t, tt.expectedType, actual) |
| 154 | + |
| 155 | + expectedVal, ok1 := v.(json.Number) |
| 156 | + actualVal, ok2 := actual.(json.Number) |
| 157 | + |
| 158 | + if ok1 && ok2 { |
| 159 | + assert.Equal(t, expectedVal, actualVal) |
| 160 | + } else { |
| 161 | + assert.Equal(t, v, actual) |
| 162 | + } |
| 163 | + } |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
0 commit comments