Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 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 | import json |
| 17 | import os |
| 18 | import sys |
| 19 | import unittest |
| 20 | from mock import patch, PropertyMock, ANY, MagicMock |
| 21 | from unit_test_common import setup_sync_unit_test |
| 22 | |
| 23 | class ApiException(Exception): |
| 24 | def __init__(self, status, *args, **kwargs): |
| 25 | super(ApiException, self).__init__(*args, **kwargs) |
| 26 | self.status = status |
| 27 | |
| 28 | def fake_init_kubernetes_client(self): |
| 29 | self.kubernetes_client = MagicMock() |
| 30 | self.v1core = MagicMock() |
| 31 | self.ApiException = ApiException |
| 32 | |
| 33 | class TestSyncConfigmap(unittest.TestCase): |
| 34 | |
| 35 | def setUp(self): |
| 36 | self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), |
| 37 | globals(), |
| 38 | [("kubernetes-service", "kubernetes.proto")] ) |
| 39 | |
| 40 | sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps")) |
| 41 | |
| 42 | from sync_configmap import SyncKubernetesConfigMap |
| 43 | self.step_class = SyncKubernetesConfigMap |
| 44 | |
| 45 | self.service = KubernetesService() |
| 46 | self.trust_domain = TrustDomain(name="test-trust", owner=self.service) |
| 47 | |
| 48 | def tearDown(self): |
| 49 | sys.path = self.unittest_setup["sys_path_save"] |
| 50 | |
| 51 | def test_get_config_map_exists(self): |
| 52 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 53 | configmap = KubernetesConfigMap(trust_domain=self.trust_domain, name="test-configmap") |
| 54 | |
| 55 | step = self.step_class() |
| 56 | map = MagicMock() |
| 57 | step.v1core.read_namespaced_config_map.return_value = map |
| 58 | |
| 59 | result = step.get_config_map(configmap) |
| 60 | |
| 61 | self.assertEqual(result, map) |
| 62 | step.v1core.read_namespaced_config_map.assert_called_with("test-configmap", "test-trust") |
| 63 | |
| 64 | def test_get_config_map_noexist(self): |
| 65 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 66 | configmap = KubernetesConfigMap(trust_domain=self.trust_domain, name="test-configmap") |
| 67 | |
| 68 | step = self.step_class() |
| 69 | map = MagicMock() |
| 70 | step.v1core.read_namespaced_config_map.side_effect = step.ApiException(status=404) |
| 71 | |
| 72 | result = step.get_config_map(configmap) |
| 73 | |
| 74 | self.assertEqual(result, None) |
| 75 | |
| 76 | def test_sync_record_create(self): |
| 77 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 78 | data = {"foo": "bar"} |
| 79 | configmap = KubernetesConfigMap(trust_domain=self.trust_domain, name="test-configmap", data=json.dumps(data)) |
| 80 | |
| 81 | step = self.step_class() |
| 82 | step.v1core.read_namespaced_config_map.side_effect = step.ApiException(status=404) |
| 83 | |
| 84 | map = MagicMock() |
| 85 | map.metadata.self_link="1234" |
| 86 | step.v1core.create_namespaced_config_map.return_value = map |
| 87 | |
| 88 | step.sync_record(configmap) |
| 89 | |
| 90 | step.v1core.create_namespaced_config_map.assert_called() |
| 91 | self.assertEqual(configmap.backend_handle, "1234") |
| 92 | |
| 93 | def test_sync_record_update(self): |
| 94 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 95 | data = {"foo": "bar"} |
| 96 | configmap = KubernetesConfigMap(trust_domain=self.trust_domain, name="test-configmap", data=json.dumps(data)) |
| 97 | |
| 98 | orig_map = MagicMock() |
| 99 | orig_map.data = {"foo": "not_bar"} |
| 100 | orig_map.metadata.self_link = "1234" |
| 101 | |
| 102 | step = self.step_class() |
| 103 | step.v1core.read_namespaced_config_map.return_value = orig_map |
| 104 | |
| 105 | new_map = MagicMock() |
| 106 | new_map.data = {"foo": "bar"} |
| 107 | new_map.metadata.self_link = "1234" |
| 108 | |
| 109 | step.v1core.patch_namespaced_config_map.return_value = new_map |
| 110 | |
| 111 | step.sync_record(configmap) |
| 112 | |
| 113 | self.assertEqual(step.v1core.patch_namespaced_config_map.call_count, 1) |
| 114 | call_args = step.v1core.patch_namespaced_config_map.call_args[0] |
| 115 | self.assertEqual(call_args[0], "test-configmap") |
| 116 | self.assertEqual(call_args[1], "test-trust") |
| 117 | self.assertEqual(call_args[2].data, {"foo": "bar"}) |
| 118 | |
| 119 | self.assertEqual(configmap.backend_handle, "1234") |
| 120 | |
Scott Baker | 393d015 | 2018-05-21 09:17:49 -0700 | [diff] [blame] | 121 | def test_delete_record(self): |
| 122 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 123 | data = {"foo": "bar"} |
| 124 | configmap = KubernetesConfigMap(trust_domain=self.trust_domain, name="test-configmap", data=json.dumps(data)) |
| 125 | |
| 126 | orig_map = MagicMock() |
| 127 | orig_map.data = {"foo": "not_bar"} |
| 128 | orig_map.metadata.self_link = "1234" |
| 129 | |
| 130 | step = self.step_class() |
| 131 | step.v1core.read_namespaced_config_map.return_value = orig_map |
| 132 | |
| 133 | step.delete_record(configmap) |
| 134 | |
| 135 | step.v1core.delete_namespaced_config_map.assert_called_with("test-configmap", self.trust_domain.name, ANY) |
| 136 | |
| 137 | |
| 138 | |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 139 | |
| 140 | if __name__ == '__main__': |
| 141 | unittest.main() |