|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +describe("EnvironmentService", function() { |
| 4 | + var EnvironmentService; |
| 5 | + |
| 6 | + beforeEach(function() { |
| 7 | + inject(function(_EnvironmentService_) { |
| 8 | + EnvironmentService = _EnvironmentService_; |
| 9 | + }); |
| 10 | + }); |
| 11 | + |
| 12 | + var objectWithContainers = function(containers) { |
| 13 | + var object = { |
| 14 | + metadata: { |
| 15 | + name: 'my-object', |
| 16 | + kind: 'DeploymentConfig' |
| 17 | + }, |
| 18 | + spec: { |
| 19 | + replicas: 2 |
| 20 | + }, |
| 21 | + status: { |
| 22 | + replicas: 2 |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + // Make a copy to check that the service isn't using object equality (object === object). |
| 27 | + containers = angular.copy(containers); |
| 28 | + _.set(object, 'spec.template.spec.containers', containers); |
| 29 | + |
| 30 | + return object; |
| 31 | + }; |
| 32 | + |
| 33 | + describe("#isEnvironmentEqual", function() { |
| 34 | + it("should not be equal if different number of containers", function() { |
| 35 | + var oneContainer = objectWithContainers([{ |
| 36 | + name: 'one', |
| 37 | + env: [] |
| 38 | + }]); |
| 39 | + var twoContainers = objectWithContainers([{ |
| 40 | + name: 'one', |
| 41 | + env: [] |
| 42 | + }, { |
| 43 | + name: 'two', |
| 44 | + env: [] |
| 45 | + }]); |
| 46 | + |
| 47 | + expect(EnvironmentService.isEnvironmentEqual(oneContainer, twoContainers)).toBe(false); |
| 48 | + expect(EnvironmentService.isEnvironmentEqual(twoContainers, oneContainer)).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should not be equal if a container has a different name", function() { |
| 52 | + var env = [{ |
| 53 | + name: 'first', |
| 54 | + value: 'value' |
| 55 | + }, { |
| 56 | + name: 'another', |
| 57 | + value: 'variable' |
| 58 | + }]; |
| 59 | + |
| 60 | + var left = objectWithContainers([{ |
| 61 | + name: 'my-container', |
| 62 | + env: env |
| 63 | + }]); |
| 64 | + |
| 65 | + var right = objectWithContainers([{ |
| 66 | + name: 'my-container-renamed', |
| 67 | + env: env |
| 68 | + }]); |
| 69 | + |
| 70 | + expect(EnvironmentService.isEnvironmentEqual(left, right)).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should not be equal if a container has a different variable value", function() { |
| 74 | + var left = objectWithContainers([{ |
| 75 | + name: 'my-container', |
| 76 | + env: [{ |
| 77 | + name: 'first', |
| 78 | + value: 'value' |
| 79 | + }, { |
| 80 | + name: 'another', |
| 81 | + value: 'variable' |
| 82 | + }] |
| 83 | + }]); |
| 84 | + |
| 85 | + var right = objectWithContainers([{ |
| 86 | + name: 'my-container', |
| 87 | + env: [{ |
| 88 | + name: 'first', |
| 89 | + value: 'value' |
| 90 | + }, { |
| 91 | + name: 'another', |
| 92 | + value: 'modified' |
| 93 | + }] |
| 94 | + }]); |
| 95 | + |
| 96 | + expect(EnvironmentService.isEnvironmentEqual(left, right)).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should not be equal if a container has a different variable name", function() { |
| 100 | + var left = objectWithContainers([{ |
| 101 | + name: 'my-container', |
| 102 | + env: [{ |
| 103 | + name: 'first', |
| 104 | + value: 'value' |
| 105 | + }, { |
| 106 | + name: 'another', |
| 107 | + value: 'variable' |
| 108 | + }] |
| 109 | + }]); |
| 110 | + |
| 111 | + var right = objectWithContainers([{ |
| 112 | + name: 'my-container', |
| 113 | + env: [{ |
| 114 | + name: 'first', |
| 115 | + value: 'value' |
| 116 | + }, { |
| 117 | + name: 'modified', |
| 118 | + value: 'variable' |
| 119 | + }] |
| 120 | + }]); |
| 121 | + |
| 122 | + expect(EnvironmentService.isEnvironmentEqual(left, right)).toBe(false); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should be equal if there are no environment variable values", function() { |
| 126 | + var containers = [{ |
| 127 | + name: 'my-container' |
| 128 | + }]; |
| 129 | + var left = objectWithContainers(containers); |
| 130 | + var right = objectWithContainers(containers); |
| 131 | + |
| 132 | + expect(EnvironmentService.isEnvironmentEqual(left, right)).toBe(true); |
| 133 | + }); |
| 134 | + |
| 135 | + it("should be equal when the environment is the same", function() { |
| 136 | + var containers = [{ |
| 137 | + name: 'my-container', |
| 138 | + env: [{ |
| 139 | + name: 'first', |
| 140 | + value: 'value' |
| 141 | + }, { |
| 142 | + name: 'another', |
| 143 | + value: 'variable' |
| 144 | + }] |
| 145 | + }]; |
| 146 | + var left = objectWithContainers(containers); |
| 147 | + var right = objectWithContainers(containers); |
| 148 | + |
| 149 | + expect(EnvironmentService.isEnvironmentEqual(left, right)).toBe(true); |
| 150 | + }); |
| 151 | + |
| 152 | + it("should be equal if there are identical valueFrom values for config maps", function() { |
| 153 | + var containers = [{ |
| 154 | + name: 'my-container', |
| 155 | + env: [{ |
| 156 | + name: 'first', |
| 157 | + value: 'value' |
| 158 | + }, { |
| 159 | + name: 'another', |
| 160 | + valueFrom: { |
| 161 | + configMapKeyRef: { |
| 162 | + name: 'my-config', |
| 163 | + key: 'my.key' |
| 164 | + } |
| 165 | + } |
| 166 | + }] |
| 167 | + }]; |
| 168 | + var left = objectWithContainers(containers); |
| 169 | + var right = objectWithContainers(containers); |
| 170 | + |
| 171 | + expect(EnvironmentService.isEnvironmentEqual(left, right)).toBe(true); |
| 172 | + }); |
| 173 | + |
| 174 | + it("should not be equal if the valueFrom refers to different object names", function() { |
| 175 | + var left = objectWithContainers([{ |
| 176 | + name: 'my-container', |
| 177 | + env: [{ |
| 178 | + name: 'first', |
| 179 | + value: 'value' |
| 180 | + }, { |
| 181 | + name: 'another', |
| 182 | + valueFrom: { |
| 183 | + configMapKeyRef: { |
| 184 | + name: 'my-config', |
| 185 | + key: 'my.key' |
| 186 | + } |
| 187 | + } |
| 188 | + }] |
| 189 | + }]); |
| 190 | + |
| 191 | + var right = objectWithContainers([{ |
| 192 | + name: 'my-container', |
| 193 | + env: [{ |
| 194 | + name: 'first', |
| 195 | + value: 'value' |
| 196 | + }, { |
| 197 | + name: 'another', |
| 198 | + valueFrom: { |
| 199 | + configMapKeyRef: { |
| 200 | + name: 'my-config-2', |
| 201 | + key: 'my.key' |
| 202 | + } |
| 203 | + } |
| 204 | + }] |
| 205 | + }]); |
| 206 | + |
| 207 | + expect(EnvironmentService.isEnvironmentEqual(left, right)).toBe(false); |
| 208 | + }); |
| 209 | + |
| 210 | + it("should not be equal if the valueFrom keys are different", function() { |
| 211 | + var left = objectWithContainers([{ |
| 212 | + name: 'my-container', |
| 213 | + env: [{ |
| 214 | + name: 'first', |
| 215 | + value: 'value' |
| 216 | + }, { |
| 217 | + name: 'another', |
| 218 | + valueFrom: { |
| 219 | + configMapKeyRef: { |
| 220 | + name: 'my-config', |
| 221 | + key: 'my.key' |
| 222 | + } |
| 223 | + } |
| 224 | + }] |
| 225 | + }]); |
| 226 | + |
| 227 | + var right = objectWithContainers([{ |
| 228 | + name: 'my-container', |
| 229 | + env: [{ |
| 230 | + name: 'first', |
| 231 | + value: 'value' |
| 232 | + }, { |
| 233 | + name: 'another', |
| 234 | + valueFrom: { |
| 235 | + configMapKeyRef: { |
| 236 | + name: 'my-config', |
| 237 | + key: 'different.key' |
| 238 | + } |
| 239 | + } |
| 240 | + }] |
| 241 | + }]); |
| 242 | + |
| 243 | + expect(EnvironmentService.isEnvironmentEqual(left, right)).toBe(false); |
| 244 | + }); |
| 245 | + |
| 246 | + it("should be equal if there are identical valueFrom values for secrets", function() { |
| 247 | + var containers = [{ |
| 248 | + name: 'my-container', |
| 249 | + env: [{ |
| 250 | + name: 'first', |
| 251 | + value: 'value' |
| 252 | + }, { |
| 253 | + name: 'another', |
| 254 | + valueFrom: { |
| 255 | + secretKeyRef: { |
| 256 | + name: 'my-secret', |
| 257 | + key: 'my.key' |
| 258 | + } |
| 259 | + } |
| 260 | + }] |
| 261 | + }]; |
| 262 | + var left = objectWithContainers(containers); |
| 263 | + var right = objectWithContainers(containers); |
| 264 | + |
| 265 | + expect(EnvironmentService.isEnvironmentEqual(left, right)).toBe(true); |
| 266 | + }); |
| 267 | + }); |
| 268 | + |
| 269 | + describe("#mergeEdits", function() { |
| 270 | + var originalContainers = [{ |
| 271 | + name: 'my-container', |
| 272 | + env: [{ |
| 273 | + name: 'FIRST', |
| 274 | + value: 'value' |
| 275 | + }, { |
| 276 | + name: 'ANOTHER', |
| 277 | + valueFrom: { |
| 278 | + secretKeyRef: { |
| 279 | + name: 'my-secret', |
| 280 | + key: 'my.key' |
| 281 | + } |
| 282 | + } |
| 283 | + }] |
| 284 | + }, { |
| 285 | + name: 'my-second-container', |
| 286 | + env: [{ |
| 287 | + name: 'CONFIG', |
| 288 | + valueFrom: { |
| 289 | + configMapKeyRef: { |
| 290 | + name: 'my-config', |
| 291 | + key: 'my.key' |
| 292 | + } |
| 293 | + } |
| 294 | + }, { |
| 295 | + name: 'FOO', |
| 296 | + value: 'bar' |
| 297 | + }] |
| 298 | + }]; |
| 299 | + |
| 300 | + // Change some values. |
| 301 | + var editedContainers = angular.copy(originalContainers); |
| 302 | + editedContainers[0].env[1] = { |
| 303 | + name: 'ANOTHER', |
| 304 | + value: 'modified' |
| 305 | + }; |
| 306 | + editedContainers[1].env[1] = { |
| 307 | + name: 'FOO', |
| 308 | + value: 'modified' |
| 309 | + }; |
| 310 | + |
| 311 | + var source = objectWithContainers(editedContainers); |
| 312 | + var target = objectWithContainers(originalContainers); |
| 313 | + _.set(target, 'metadata.annotations', { |
| 314 | + 'my-annotation': 'my-value' |
| 315 | + }); |
| 316 | + |
| 317 | + // Change a container property as well to check that we don't stomp other edits. |
| 318 | + target.spec.template.spec.containers[0].image = 'redis'; |
| 319 | + |
| 320 | + it("should correctly merge edited values", function() { |
| 321 | + var merged = EnvironmentService.mergeEdits(source, target); |
| 322 | + var expected = angular.copy(target); |
| 323 | + expected.spec.template.spec.containers[0].env = angular.copy(source.spec.template.spec.containers[0].env); |
| 324 | + expected.spec.template.spec.containers[1].env = angular.copy(source.spec.template.spec.containers[1].env); |
| 325 | + expect(merged).toEqual(expected); |
| 326 | + }); |
| 327 | + |
| 328 | + it("should create a copy of target", function() { |
| 329 | + var merged = EnvironmentService.mergeEdits(source, target); |
| 330 | + expect(merged === target).toBe(false); |
| 331 | + }); |
| 332 | + }); |
| 333 | +}); |
0 commit comments