|
| 1 | +/** |
| 2 | + * Copyright 2019 Google LLC |
| 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 | + |
| 17 | +import { Configs, TestRunner } from '@googlecontainertools/kpt-functions'; |
| 18 | +import { labelNamespace, LABEL_NAME, LABEL_VALUE } from './label_namespace'; |
| 19 | +import { Namespace, ConfigMap } from './gen/io.k8s.api.core.v1'; |
| 20 | + |
| 21 | +const TEST_NAMESPACE = 'testNamespace'; |
| 22 | +const TEST_LABEL_NAME = 'costCenter'; |
| 23 | +const TEST_LABEL_VALUE = 'xyz'; |
| 24 | + |
| 25 | +describe('labelNamespace', () => { |
| 26 | + let functionConfig = ConfigMap.named('foo'); |
| 27 | + functionConfig.data = {}; |
| 28 | + functionConfig.data[LABEL_NAME] = TEST_LABEL_NAME; |
| 29 | + functionConfig.data[LABEL_VALUE] = TEST_LABEL_VALUE; |
| 30 | + |
| 31 | + const RUNNER = new TestRunner(labelNamespace); |
| 32 | + |
| 33 | + it('empty input ok', RUNNER.assertCallback(new Configs(undefined, functionConfig))); |
| 34 | + |
| 35 | + it('requires functionConfig', RUNNER.assertCallback(undefined, undefined, TypeError)); |
| 36 | + |
| 37 | + it('adds label namespace when metadata.labels is undefined', async () => { |
| 38 | + const actual = new Configs(undefined, functionConfig); |
| 39 | + actual.insert(Namespace.named(TEST_NAMESPACE)); |
| 40 | + |
| 41 | + const expected = new Configs(); |
| 42 | + expected.insert( |
| 43 | + new Namespace({ |
| 44 | + metadata: { |
| 45 | + name: TEST_NAMESPACE, |
| 46 | + labels: { [TEST_LABEL_NAME]: TEST_LABEL_VALUE }, |
| 47 | + }, |
| 48 | + }), |
| 49 | + ); |
| 50 | + |
| 51 | + await RUNNER.assert(actual, expected); |
| 52 | + }); |
| 53 | + |
| 54 | + it('adds label to namespace when metadata.labels is defined', async () => { |
| 55 | + const actual = new Configs(undefined, functionConfig); |
| 56 | + actual.insert( |
| 57 | + new Namespace({ |
| 58 | + metadata: { |
| 59 | + name: TEST_NAMESPACE, |
| 60 | + labels: { a: 'b' }, |
| 61 | + }, |
| 62 | + }), |
| 63 | + ); |
| 64 | + |
| 65 | + const expected = new Configs(); |
| 66 | + expected.insert( |
| 67 | + new Namespace({ |
| 68 | + metadata: { |
| 69 | + name: TEST_NAMESPACE, |
| 70 | + labels: { |
| 71 | + a: 'b', |
| 72 | + [TEST_LABEL_NAME]: TEST_LABEL_VALUE, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }), |
| 76 | + ); |
| 77 | + |
| 78 | + await RUNNER.assert(actual, expected); |
| 79 | + }); |
| 80 | +}); |
0 commit comments