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 | |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 16 | import json |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 17 | import os, sys |
| 18 | import unittest |
| 19 | from mock import patch, PropertyMock, ANY, MagicMock |
| 20 | from unit_test_common import setup_sync_unit_test |
| 21 | |
| 22 | def fake_init_kubernetes_client(self): |
| 23 | self.v1core = MagicMock() |
| 24 | self.v1apps = MagicMock() |
| 25 | self.v1batch = MagicMock() |
| 26 | |
| 27 | class TestPullPods(unittest.TestCase): |
| 28 | |
| 29 | def setUp(self): |
| 30 | self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), |
| 31 | globals(), |
Scott Baker | a30fae7 | 2019-02-01 16:14:43 -0800 | [diff] [blame] | 32 | [("kubernetes-service", "kubernetes.xproto")] ) |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 33 | self.mockxoskafka = MagicMock() |
| 34 | |
| 35 | modules = { |
| 36 | 'xoskafka': self.mockxoskafka, |
| 37 | 'xoskafka.XOSKafkaProducer': self.mockxoskafka.XOSKafkaProducer, |
| 38 | } |
| 39 | |
| 40 | self.module_patcher = patch.dict('sys.modules', modules) |
| 41 | self.module_patcher.start() |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 42 | |
| 43 | sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../pull_steps")) |
| 44 | |
| 45 | from pull_pods import KubernetesServiceInstancePullStep |
| 46 | self.pull_step_class = KubernetesServiceInstancePullStep |
| 47 | |
| 48 | self.service = KubernetesService() |
| 49 | self.trust_domain = TrustDomain(name="test-trust", owner=self.service) |
| 50 | self.principal = Principal(name="test-principal", trust_domain = self.trust_domain) |
| 51 | self.image = Image(name="test-image", tag="1.1", kind="container") |
| 52 | |
| 53 | def tearDown(self): |
| 54 | sys.path = self.unittest_setup["sys_path_save"] |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 55 | self.module_patcher.stop() |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 56 | |
| 57 | def test_read_obj_kind(self): |
| 58 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 59 | pull_step = self.pull_step_class() |
| 60 | pull_step.v1apps.read_namespaced_replica_set.return_value = ["my_replica_set"] |
| 61 | pull_step.v1apps.read_namespaced_stateful_set.return_value = ["my_stateful_set"] |
| 62 | pull_step.v1apps.read_namespaced_daemon_set.return_value = ["my_daemon_set"] |
| 63 | pull_step.v1apps.read_namespaced_deployment.return_value = ["my_deployment"] |
| 64 | pull_step.v1batch.read_namespaced_job.return_value = ["my_job"] |
| 65 | |
| 66 | obj = pull_step.read_obj_kind("ReplicaSet", "foo", self.trust_domain) |
| 67 | self.assertEqual(obj, ["my_replica_set"]) |
| 68 | |
| 69 | obj = pull_step.read_obj_kind("StatefulSet", "foo", self.trust_domain) |
| 70 | self.assertEqual(obj, ["my_stateful_set"]) |
| 71 | |
| 72 | obj = pull_step.read_obj_kind("DaemonSet", "foo", self.trust_domain) |
| 73 | self.assertEqual(obj, ["my_daemon_set"]) |
| 74 | |
| 75 | obj = pull_step.read_obj_kind("Deployment", "foo", self.trust_domain) |
| 76 | self.assertEqual(obj, ["my_deployment"]) |
| 77 | |
| 78 | obj = pull_step.read_obj_kind("Job", "foo", self.trust_domain) |
| 79 | self.assertEqual(obj, ["my_job"]) |
| 80 | |
| 81 | def test_get_controller_from_obj(self): |
| 82 | """ Setup an owner_reference chain: leaf --> StatefulSet --> Deployment. Calling get_controller_from_obj() |
| 83 | on the leaf should return the deployment. |
| 84 | """ |
| 85 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 86 | leaf_obj = MagicMock() |
| 87 | leaf_obj.metadata.owner_references= [MagicMock(controller=True, name="my_stateful_set", kind="StatefulSet")] |
| 88 | |
| 89 | ss_obj = MagicMock() |
| 90 | ss_obj.metadata.owner_references= [MagicMock(controller=True, name="my_deployment", kind="Deployment")] |
| 91 | |
| 92 | dep_obj = MagicMock() |
| 93 | dep_obj.metadata.owner_references = [] |
| 94 | |
| 95 | pull_step = self.pull_step_class() |
| 96 | pull_step.v1apps.read_namespaced_stateful_set.return_value = ss_obj |
| 97 | pull_step.v1apps.read_namespaced_deployment.return_value = dep_obj |
| 98 | |
Scott Baker | 8b725f5 | 2019-06-11 16:14:39 -0700 | [diff] [blame] | 99 | controller = pull_step.get_controller_from_obj("mypod", leaf_obj, self.trust_domain) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 100 | self.assertEqual(controller, dep_obj) |
| 101 | |
| 102 | def test_get_slice_from_pod_exists(self): |
| 103 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client),\ |
| 104 | patch.object(self.pull_step_class, "get_controller_from_obj") as get_controller_from_obj, \ |
| 105 | patch.object(Slice.objects, "get_items") as slice_objects: |
| 106 | pull_step = self.pull_step_class() |
| 107 | |
| 108 | myslice = Slice(name="myslice") |
| 109 | |
| 110 | dep_obj = MagicMock() |
| 111 | dep_obj.metadata.name = myslice.name |
| 112 | get_controller_from_obj.return_value = dep_obj |
| 113 | |
| 114 | slice_objects.return_value = [myslice] |
| 115 | |
| 116 | pod = MagicMock() |
| 117 | |
Scott Baker | 8b725f5 | 2019-06-11 16:14:39 -0700 | [diff] [blame] | 118 | slice = pull_step.get_slice_from_pod("mypod", pod, self.trust_domain, self.principal) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 119 | self.assertEqual(slice, myslice) |
| 120 | |
| 121 | def test_get_slice_from_pod_noexist(self): |
| 122 | """ Call get_slice_from_pod() where not pre-existing slice is present. A new slice will be created, named |
| 123 | after the pod's controller. |
| 124 | """ |
| 125 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client),\ |
| 126 | patch.object(self.pull_step_class, "get_controller_from_obj") as get_controller_from_obj, \ |
| 127 | patch.object(Site.objects, "get_items") as site_objects: |
| 128 | pull_step = self.pull_step_class() |
| 129 | |
| 130 | site_objects.return_value=[Site(name="mysite")] |
| 131 | |
| 132 | dep_obj = MagicMock() |
| 133 | dep_obj.metadata.name = "my_other_slice" |
| 134 | get_controller_from_obj.return_value = dep_obj |
| 135 | |
| 136 | pod = MagicMock() |
| 137 | |
Scott Baker | 8b725f5 | 2019-06-11 16:14:39 -0700 | [diff] [blame] | 138 | slice = pull_step.get_slice_from_pod("mypod", pod, self.trust_domain, self.principal) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 139 | self.assertEqual(slice.name, "my_other_slice") |
| 140 | self.assertEqual(slice.trust_domain, self.trust_domain) |
| 141 | self.assertEqual(slice.principal, self.principal) |
| 142 | self.assertEqual(slice.xos_managed, False) |
| 143 | |
| 144 | def test_get_trustdomain_from_pod_exists(self): |
| 145 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client), \ |
| 146 | patch.object(TrustDomain.objects, "get_items") as trustdomain_objects: |
| 147 | pull_step = self.pull_step_class() |
| 148 | |
| 149 | pod = MagicMock() |
| 150 | pod.metadata.namespace = self.trust_domain.name |
| 151 | |
| 152 | trustdomain_objects.return_value = [self.trust_domain] |
| 153 | |
| 154 | trustdomain = pull_step.get_trustdomain_from_pod(pod, owner_service=self.service) |
| 155 | self.assertEqual(trustdomain, self.trust_domain) |
| 156 | |
| 157 | def test_get_trustdomain_from_pod_noexist(self): |
| 158 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 159 | pull_step = self.pull_step_class() |
| 160 | |
| 161 | pod = MagicMock() |
| 162 | pod.metadata.namespace = "new-trust" |
| 163 | |
| 164 | trustdomain = pull_step.get_trustdomain_from_pod(pod, owner_service=self.service) |
| 165 | self.assertEqual(trustdomain.name, "new-trust") |
| 166 | self.assertEqual(trustdomain.owner, self.service) |
| 167 | |
| 168 | def test_get_principal_from_pod_exists(self): |
| 169 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client), \ |
| 170 | patch.object(Principal.objects, "get_items") as principal_objects: |
| 171 | pull_step = self.pull_step_class() |
| 172 | |
| 173 | pod = MagicMock() |
| 174 | pod.spec.service_account = self.principal.name |
| 175 | |
| 176 | principal_objects.return_value = [self.principal] |
| 177 | |
| 178 | principal = pull_step.get_principal_from_pod(pod, trust_domain=self.trust_domain) |
| 179 | self.assertEqual(principal, self.principal) |
| 180 | |
| 181 | def test_get_principal_from_pod_noexist(self): |
| 182 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 183 | pull_step = self.pull_step_class() |
| 184 | |
| 185 | pod = MagicMock() |
| 186 | pod.spec.service_account = "new-principal" |
| 187 | |
| 188 | principal = pull_step.get_principal_from_pod(pod, trust_domain=self.trust_domain) |
| 189 | self.assertEqual(principal.name, "new-principal") |
| 190 | self.assertEqual(principal.trust_domain, self.trust_domain) |
| 191 | |
| 192 | def test_get_image_from_pod_exists(self): |
| 193 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client), \ |
| 194 | patch.object(Image.objects, "get_items") as image_objects: |
| 195 | pull_step = self.pull_step_class() |
| 196 | |
| 197 | container = MagicMock() |
| 198 | container.image = "%s:%s" % (self.image.name, self.image.tag) |
| 199 | |
| 200 | pod = MagicMock() |
| 201 | pod.spec.containers = [container] |
| 202 | |
| 203 | image_objects.return_value = [self.image] |
| 204 | |
| 205 | image = pull_step.get_image_from_pod(pod) |
| 206 | self.assertEqual(image, self.image) |
| 207 | |
| 208 | def test_get_image_from_pod_noexist(self): |
| 209 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 210 | pull_step = self.pull_step_class() |
| 211 | |
| 212 | container = MagicMock() |
| 213 | container.image = "new-image:2.3" \ |
| 214 | |
| 215 | pod = MagicMock() |
| 216 | pod.spec.containers = [container] |
| 217 | |
| 218 | image = pull_step.get_image_from_pod(pod) |
| 219 | self.assertEqual(image.name, "new-image") |
| 220 | self.assertEqual(image.tag, "2.3") |
| 221 | self.assertEqual(image.kind, "container") |
| 222 | |
| 223 | def make_pod(self, name, trust_domain, principal, image): |
| 224 | container = MagicMock() |
| 225 | container.image = "%s:%s" % (image.name, image.tag) |
| 226 | |
| 227 | pod = MagicMock() |
| 228 | pod.metadata.name = name |
| 229 | pod.metadata.namespace = trust_domain.name |
| 230 | pod.spec.service_account = principal.name |
| 231 | |
| 232 | return pod |
| 233 | |
| 234 | def test_pull_records_new_pod(self): |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 235 | """ A pod is found in k8s that does not exist in XOS. A new KubernetesServiceInstance should be created |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 236 | """ |
| 237 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client), \ |
| 238 | patch.object(self.pull_step_class, "get_trustdomain_from_pod") as get_trustdomain, \ |
| 239 | patch.object(self.pull_step_class, "get_principal_from_pod") as get_principal, \ |
| 240 | patch.object(self.pull_step_class, "get_slice_from_pod") as get_slice, \ |
| 241 | patch.object(self.pull_step_class, "get_image_from_pod") as get_image, \ |
| 242 | patch.object(KubernetesService.objects, "get_items") as service_objects, \ |
| 243 | patch.object(KubernetesServiceInstance.objects, "get_items") as si_objects, \ |
| 244 | patch.object(KubernetesServiceInstance, "save", autospec=True) as ksi_save: |
| 245 | |
| 246 | service_objects.return_value = [self.service] |
| 247 | |
| 248 | slice = Slice(name="myslice") |
| 249 | |
| 250 | get_trustdomain.return_value = self.trust_domain |
| 251 | get_principal.return_value = self.principal |
| 252 | get_slice.return_value = slice |
| 253 | get_image.return_value = self.image |
| 254 | |
| 255 | pod = self.make_pod("my-pod", self.trust_domain, self.principal, self.image) |
| 256 | pod.status.pod_ip = "1.2.3.4" |
| 257 | |
| 258 | pull_step = self.pull_step_class() |
| 259 | pull_step.v1core.list_pod_for_all_namespaces.return_value = MagicMock(items=[pod]) |
| 260 | |
| 261 | pull_step.pull_records() |
| 262 | |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 263 | self.assertEqual(ksi_save.call_count, 2) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 264 | saved_ksi = ksi_save.call_args[0][0] |
| 265 | |
| 266 | self.assertEqual(saved_ksi.name, "my-pod") |
| 267 | self.assertEqual(saved_ksi.pod_ip, "1.2.3.4") |
| 268 | self.assertEqual(saved_ksi.owner, self.service) |
| 269 | self.assertEqual(saved_ksi.slice, slice) |
| 270 | self.assertEqual(saved_ksi.image, self.image) |
| 271 | self.assertEqual(saved_ksi.xos_managed, False) |
| 272 | |
| 273 | def test_pull_records_missing_pod(self): |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 274 | """ A pod is found in k8s that does not exist in XOS. A new KubernetesServiceInstance should be created |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 275 | """ |
| 276 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client), \ |
| 277 | patch.object(KubernetesService.objects, "get_items") as service_objects, \ |
| 278 | patch.object(KubernetesServiceInstance.objects, "get_items") as si_objects, \ |
| 279 | patch.object(KubernetesServiceInstance, "delete", autospec=True) as ksi_delete: |
| 280 | service_objects.return_value = [self.service] |
| 281 | |
| 282 | si = KubernetesServiceInstance(name="my-pod", owner=self.service, xos_managed=False) |
| 283 | si_objects.return_value = [si] |
| 284 | |
| 285 | pull_step = self.pull_step_class() |
| 286 | pull_step.v1core.list_pod_for_all_namespaces.return_value = MagicMock(items=[]) |
| 287 | |
| 288 | pull_step.pull_records() |
| 289 | |
| 290 | self.assertEqual(ksi_delete.call_count, 1) |
| 291 | deleted_ksi = ksi_delete.call_args[0][0] |
| 292 | |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 293 | def test_pull_records_new_pod_kafka_event(self): |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 294 | """ A pod is found in k8s that does not exist in XOS. A new KubernetesServiceInstance should be created |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 295 | """ |
| 296 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client), \ |
| 297 | patch.object(self.pull_step_class, "get_trustdomain_from_pod") as get_trustdomain, \ |
| 298 | patch.object(self.pull_step_class, "get_principal_from_pod") as get_principal, \ |
| 299 | patch.object(self.pull_step_class, "get_slice_from_pod") as get_slice, \ |
| 300 | patch.object(self.pull_step_class, "get_image_from_pod") as get_image, \ |
| 301 | patch.object(self.pull_step_class, "send_notification", autospec=True) as send_notification, \ |
| 302 | patch.object(KubernetesService.objects, "get_items") as service_objects, \ |
| 303 | patch.object(KubernetesServiceInstance.objects, "get_items") as si_objects, \ |
| 304 | patch.object(KubernetesServiceInstance, "save", autospec=True) as ksi_save: |
| 305 | |
| 306 | service_objects.return_value = [self.service] |
| 307 | |
| 308 | slice = Slice(name="myslice") |
| 309 | |
| 310 | get_trustdomain.return_value = self.trust_domain |
| 311 | get_principal.return_value = self.principal |
| 312 | get_slice.return_value = slice |
| 313 | get_image.return_value = self.image |
| 314 | |
| 315 | pod = self.make_pod("my-pod", self.trust_domain, self.principal, self.image) |
| 316 | pod.status.pod_ip = "1.2.3.4" |
| 317 | |
| 318 | pull_step = self.pull_step_class() |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 319 | |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 320 | pull_step.v1core.list_pod_for_all_namespaces.return_value = MagicMock(items=[pod]) |
| 321 | |
| 322 | pull_step.pull_records() |
| 323 | |
| 324 | self.assertEqual(ksi_save.call_count, 2) |
| 325 | |
| 326 | # Inspect the last KubernetesServiceInstance that was saved. There's no way to inspect the first one saved |
| 327 | # if there are multiple calls, as the sync step will cause the object to be updated. |
| 328 | saved_ksi = ksi_save.call_args[0][0] |
| 329 | self.assertEqual(saved_ksi.name, "my-pod") |
| 330 | self.assertEqual(saved_ksi.pod_ip, "1.2.3.4") |
| 331 | self.assertEqual(saved_ksi.owner, self.service) |
| 332 | self.assertEqual(saved_ksi.slice, slice) |
| 333 | self.assertEqual(saved_ksi.image, self.image) |
| 334 | self.assertEqual(saved_ksi.xos_managed, False) |
| 335 | self.assertEqual(saved_ksi.need_event, False) |
| 336 | |
| 337 | self.assertEqual(send_notification.call_count, 1) |
| 338 | self.assertEqual(send_notification.call_args[0][1], saved_ksi) |
| 339 | self.assertEqual(send_notification.call_args[0][2], pod) |
| 340 | self.assertEqual(send_notification.call_args[0][3], "created") |
| 341 | |
| 342 | def test_pull_records_existing_pod_kafka_event(self): |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 343 | """ A pod is found in k8s that does not exist in XOS. A new KubernetesServiceInstance should be created |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 344 | """ |
| 345 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client), \ |
| 346 | patch.object(self.pull_step_class, "get_trustdomain_from_pod") as get_trustdomain, \ |
| 347 | patch.object(self.pull_step_class, "get_principal_from_pod") as get_principal, \ |
| 348 | patch.object(self.pull_step_class, "get_slice_from_pod") as get_slice, \ |
| 349 | patch.object(self.pull_step_class, "get_image_from_pod") as get_image, \ |
| 350 | patch.object(self.pull_step_class, "send_notification", autospec=True) as send_notification, \ |
| 351 | patch.object(KubernetesService.objects, "get_items") as service_objects, \ |
| 352 | patch.object(KubernetesServiceInstance.objects, "get_items") as si_objects, \ |
| 353 | patch.object(KubernetesServiceInstance, "save", autospec=True) as ksi_save: |
| 354 | |
| 355 | service_objects.return_value = [self.service] |
| 356 | |
| 357 | slice = Slice(name="myslice") |
| 358 | |
| 359 | get_trustdomain.return_value = self.trust_domain |
| 360 | get_principal.return_value = self.principal |
| 361 | get_slice.return_value = slice |
| 362 | get_image.return_value = self.image |
| 363 | |
| 364 | pod = self.make_pod("my-pod", self.trust_domain, self.principal, self.image) |
| 365 | pod.status.pod_ip = "1.2.3.4" |
| 366 | |
| 367 | xos_pod = KubernetesServiceInstance(name="my-pod", |
| 368 | pod_ip="", |
| 369 | owner=self.service, |
| 370 | slice=slice, |
| 371 | image=self.image, |
| 372 | xos_managed=False, |
| 373 | need_event=False, |
| 374 | last_event_sent="created") |
| 375 | si_objects.return_value = [xos_pod] |
| 376 | |
| 377 | pull_step = self.pull_step_class() |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 378 | |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 379 | pull_step.v1core.list_pod_for_all_namespaces.return_value = MagicMock(items=[pod]) |
| 380 | |
| 381 | pull_step.pull_records() |
| 382 | |
| 383 | self.assertEqual(ksi_save.call_count, 2) |
| 384 | |
| 385 | # Inspect the last KubernetesServiceInstance that was saved. There's no way to inspect the first one saved |
| 386 | # if there are multiple calls, as the sync step will cause the object to be updated. |
| 387 | saved_ksi = ksi_save.call_args[0][0] |
| 388 | self.assertEqual(saved_ksi.name, "my-pod") |
| 389 | self.assertEqual(saved_ksi.pod_ip, "1.2.3.4") |
| 390 | self.assertEqual(saved_ksi.owner, self.service) |
| 391 | self.assertEqual(saved_ksi.slice, slice) |
| 392 | self.assertEqual(saved_ksi.image, self.image) |
| 393 | self.assertEqual(saved_ksi.xos_managed, False) |
| 394 | self.assertEqual(saved_ksi.need_event, False) |
| 395 | |
| 396 | self.assertEqual(send_notification.call_count, 1) |
| 397 | self.assertEqual(send_notification.call_args[0][1], saved_ksi) |
| 398 | self.assertEqual(send_notification.call_args[0][2], pod) |
| 399 | self.assertEqual(send_notification.call_args[0][3], "updated") |
| 400 | |
| 401 | def test_send_notification_created(self): |
| 402 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 403 | |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 404 | pull_step = self.pull_step_class() |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 405 | |
| 406 | from xoskafka import XOSKafkaProducer |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 407 | |
| 408 | pod = self.make_pod("my-pod", self.trust_domain, self.principal, self.image) |
| 409 | pod.status.pod_ip = "1.2.3.4" |
| 410 | pod.metadata.labels = {"foo": "bar"} |
| 411 | xos_pod = KubernetesServiceInstance(name="my-pod", |
| 412 | pod_ip="", |
| 413 | owner=self.service, |
| 414 | slice=slice, |
| 415 | image=self.image, |
| 416 | xos_managed=False, |
| 417 | need_event=False, |
| 418 | last_event_sent="created") |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 419 | |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 420 | pull_step.send_notification(xos_pod, pod, "created") |
| 421 | |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 422 | self.assertEqual(XOSKafkaProducer.produce.call_count, 1) |
| 423 | topic = XOSKafkaProducer.produce.call_args[0][0] |
| 424 | key = XOSKafkaProducer.produce.call_args[0][1] |
| 425 | event = json.loads(XOSKafkaProducer.produce.call_args[0][2]) |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 426 | |
| 427 | self.assertEqual(topic, "xos.kubernetes.pod-details") |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 428 | self.assertEqual(key, "my-pod") |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 429 | |
| 430 | self.assertEqual(event["name"], "my-pod") |
| 431 | self.assertEqual(event["status"], "created") |
Scott Baker | 3c2b820 | 2018-08-15 10:51:55 -0700 | [diff] [blame] | 432 | self.assertEqual(event["producer"], "k8s-sync") |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 433 | self.assertEqual(event["labels"], {"foo": "bar"}) |
| 434 | self.assertEqual(event["netinterfaces"], [{"name": "primary", "addresses": ["1.2.3.4"]}]) |
| 435 | |
| 436 | def test_send_notification_deleted(self): |
| 437 | with patch.object(self.pull_step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 438 | pull_step = self.pull_step_class() |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 439 | |
| 440 | from xoskafka import XOSKafkaProducer |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 441 | |
| 442 | xos_pod = KubernetesServiceInstance(name="my-pod", |
| 443 | pod_ip="", |
| 444 | owner=self.service, |
| 445 | slice=slice, |
| 446 | image=self.image, |
| 447 | xos_managed=False, |
| 448 | need_event=False, |
| 449 | last_event_sent="created") |
| 450 | pull_step.send_notification(xos_pod, None, "deleted") |
| 451 | |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 452 | self.assertEqual(XOSKafkaProducer.produce.call_count, 1) |
| 453 | topic = XOSKafkaProducer.produce.call_args[0][0] |
| 454 | key = XOSKafkaProducer.produce.call_args[0][1] |
| 455 | event = json.loads(XOSKafkaProducer.produce.call_args[0][2]) |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 456 | |
| 457 | self.assertEqual(topic, "xos.kubernetes.pod-details") |
Zack Williams | 3dc9760 | 2018-09-13 22:33:26 -0700 | [diff] [blame] | 458 | self.assertEqual(key, "my-pod") |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 459 | |
| 460 | self.assertEqual(event["name"], "my-pod") |
| 461 | self.assertEqual(event["status"], "deleted") |
Scott Baker | 3c2b820 | 2018-08-15 10:51:55 -0700 | [diff] [blame] | 462 | self.assertEqual(event["producer"], "k8s-sync") |
Scott Baker | 987748d | 2018-08-09 16:14:11 -0700 | [diff] [blame] | 463 | |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 464 | if __name__ == '__main__': |
| 465 | unittest.main() |