Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 1 | // Copyright 2017 The kubecfg authors |
| 2 | // |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | package kubecfg |
| 17 | |
| 18 | import ( |
| 19 | "testing" |
| 20 | |
| 21 | "github.com/stretchr/testify/require" |
| 22 | ) |
| 23 | |
| 24 | func TestRemoveListFields(t *testing.T) { |
| 25 | for _, tc := range []struct { |
| 26 | config, live, expected []interface{} |
| 27 | }{ |
| 28 | { |
| 29 | config: []interface{}{"a"}, |
| 30 | live: []interface{}{"a"}, |
| 31 | expected: []interface{}{"a"}, |
| 32 | }, |
| 33 | |
| 34 | // Check that extra fields in config are not propagated. |
| 35 | { |
| 36 | config: []interface{}{"a", "b"}, |
| 37 | live: []interface{}{"a"}, |
| 38 | expected: []interface{}{"a"}, |
| 39 | }, |
| 40 | |
| 41 | // Check that extra entries in live are propagated. |
| 42 | { |
| 43 | config: []interface{}{"a"}, |
| 44 | live: []interface{}{"a", "b"}, |
| 45 | expected: []interface{}{"a", "b"}, |
| 46 | }, |
| 47 | } { |
| 48 | require.EqualValues(t, tc.expected, removeListFields(tc.config, tc.live)) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestRemoveMapFields(t *testing.T) { |
| 53 | for _, tc := range []struct { |
| 54 | config, live, expected map[string]interface{} |
| 55 | }{ |
| 56 | { |
| 57 | config: map[string]interface{}{"foo": "bar"}, |
| 58 | live: map[string]interface{}{"foo": "bar"}, |
| 59 | expected: map[string]interface{}{"foo": "bar"}, |
| 60 | }, |
| 61 | |
| 62 | { |
| 63 | config: map[string]interface{}{"foo": "bar", "bar": "baz"}, |
| 64 | live: map[string]interface{}{"foo": "bar"}, |
| 65 | expected: map[string]interface{}{"foo": "bar"}, |
| 66 | }, |
| 67 | |
| 68 | { |
| 69 | config: map[string]interface{}{"foo": "bar"}, |
| 70 | live: map[string]interface{}{"foo": "bar", "bar": "baz"}, |
| 71 | expected: map[string]interface{}{"foo": "bar"}, |
| 72 | }, |
| 73 | } { |
| 74 | require.Equal(t, tc.expected, removeMapFields(tc.config, tc.live)) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestRemoveFields(t *testing.T) { |
| 79 | emptyVal := map[string]interface{}{ |
| 80 | "args": map[string]interface{}{}, |
| 81 | "volumes": []string{}, |
| 82 | "stdin": false, |
| 83 | } |
| 84 | for _, tc := range []struct { |
| 85 | config, live, expected interface{} |
| 86 | }{ |
| 87 | // Check we can handle embedded structs. |
| 88 | { |
| 89 | config: map[string]interface{}{"foo": "bar", "bar": "baz"}, |
| 90 | live: map[string]interface{}{"foo": "bar"}, |
| 91 | expected: map[string]interface{}{"foo": "bar"}, |
| 92 | }, |
| 93 | // JSON unmarshalling can return int64 for numbers |
| 94 | // https://golang.org/pkg/encoding/json/#Number |
| 95 | { |
| 96 | config: map[string]interface{}{"foo": (int64)(10)}, |
| 97 | live: map[string]interface{}{}, |
| 98 | expected: map[string]interface{}{}, |
| 99 | }, |
| 100 | |
| 101 | // Check we can handle embedded lists. |
| 102 | { |
| 103 | config: []interface{}{"a", "b"}, |
| 104 | live: []interface{}{"a"}, |
| 105 | expected: []interface{}{"a"}, |
| 106 | }, |
| 107 | |
| 108 | // Check we can handle arbitrary types. |
| 109 | { |
| 110 | config: "a", |
| 111 | live: "b", |
| 112 | expected: "b", |
| 113 | }, |
| 114 | // Check we can handle mismatched types. |
| 115 | { |
| 116 | config: map[string]interface{}{"foo": "bar"}, |
| 117 | live: []interface{}{"foo", "bar"}, |
| 118 | expected: []interface{}{"foo", "bar"}, |
| 119 | }, |
| 120 | { |
| 121 | config: []interface{}{"foo", "bar"}, |
| 122 | live: map[string]interface{}{"foo": "bar"}, |
| 123 | expected: map[string]interface{}{"foo": "bar"}, |
| 124 | }, |
| 125 | // Check we handle empty configs by copying them as if were live |
| 126 | // (API won't return them) |
| 127 | { |
| 128 | config: emptyVal, |
| 129 | live: map[string]interface{}{}, |
| 130 | expected: emptyVal, |
| 131 | }, |
| 132 | |
| 133 | // Check we can handle combinations. |
| 134 | { |
| 135 | config: map[string]interface{}{ |
| 136 | "apiVersion": "v1", |
| 137 | "kind": "Service", |
| 138 | "metadata": map[string]interface{}{ |
| 139 | "name": "foo", |
| 140 | "namespace": "default", |
| 141 | }, |
| 142 | "spec": map[string]interface{}{ |
| 143 | "selector": map[string]interface{}{ |
| 144 | "name": "foo", |
| 145 | }, |
| 146 | "ports": []interface{}{ |
| 147 | map[string]interface{}{ |
| 148 | "name": "http", |
| 149 | "port": 80, |
| 150 | }, |
| 151 | map[string]interface{}{ |
| 152 | "name": "https", |
| 153 | "port": 443, |
| 154 | }, |
| 155 | }, |
| 156 | }, |
| 157 | }, |
| 158 | live: map[string]interface{}{ |
| 159 | "apiVersion": "v1", |
| 160 | "kind": "Service", |
| 161 | "metadata": map[string]interface{}{ |
| 162 | "name": "foo", |
| 163 | // NB Namespace missing. |
| 164 | }, |
| 165 | "spec": map[string]interface{}{ |
| 166 | "selector": map[string]interface{}{ |
| 167 | "bar": "foo", |
| 168 | }, |
| 169 | "ports": []interface{}{ |
| 170 | // NB HTTP port missing. |
| 171 | map[string]interface{}{ |
| 172 | "name": "https", |
| 173 | "port": 443, |
| 174 | }, |
| 175 | }, |
| 176 | }, |
| 177 | }, |
| 178 | expected: map[string]interface{}{ |
| 179 | "apiVersion": "v1", |
| 180 | "kind": "Service", |
| 181 | "metadata": map[string]interface{}{ |
| 182 | "name": "foo", |
| 183 | }, |
| 184 | "spec": map[string]interface{}{ |
| 185 | "selector": map[string]interface{}{}, |
| 186 | "ports": []interface{}{ |
| 187 | map[string]interface{}{ |
| 188 | "name": "https", |
| 189 | "port": 443, |
| 190 | }, |
| 191 | }, |
| 192 | }, |
| 193 | }, |
| 194 | }, |
| 195 | } { |
| 196 | require.Equal(t, tc.expected, removeFields(tc.config, tc.live)) |
| 197 | } |
| 198 | } |