Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import unittest |
| 16 | import json |
| 17 | import functools |
| 18 | from mock import patch, call, Mock, PropertyMock |
| 19 | import requests_mock |
| 20 | |
| 21 | import os, sys |
| 22 | |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 23 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 24 | |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 25 | |
| 26 | class TestKubernetesEvent(unittest.TestCase): |
| 27 | |
| 28 | def setUp(self): |
| 29 | global DeferredException |
| 30 | |
| 31 | self.sys_path_save = sys.path |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 32 | |
| 33 | # Setting up the config module |
| 34 | from xosconfig import Config |
| 35 | config = os.path.join(test_path, "../test_config.yaml") |
| 36 | Config.clear() |
| 37 | Config.init(config, "synchronizer-config-schema.yaml") |
| 38 | # END Setting up the config module |
| 39 | |
Scott Baker | 806d4b7 | 2019-02-26 19:01:59 -0800 | [diff] [blame] | 40 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 41 | mock_modelaccessor_config(test_path, [("onos-service", "onos.xproto"),]) |
| 42 | |
| 43 | import xossynchronizer.modelaccessor |
| 44 | import mock_modelaccessor |
| 45 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
| 46 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 47 | |
| 48 | from kubernetes_event import KubernetesPodDetailsEventStep |
| 49 | |
| 50 | from xossynchronizer.modelaccessor import model_accessor |
| 51 | |
| 52 | self.model_accessor = model_accessor |
| 53 | |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 54 | from mock_modelaccessor import MockObjectList |
| 55 | |
| 56 | from kubernetes_event import KubernetesPodDetailsEventStep |
| 57 | |
| 58 | # import all class names to globals |
| 59 | for (k, v) in model_accessor.all_model_classes.items(): |
| 60 | globals()[k] = v |
| 61 | |
| 62 | self.event_step = KubernetesPodDetailsEventStep |
| 63 | |
| 64 | self.onos = ONOSService(name="myonos", |
| 65 | rest_hostname = "onos-url", |
| 66 | rest_port = "8181", |
| 67 | rest_username = "karaf", |
| 68 | rest_password = "karaf", |
| 69 | backend_code=1, |
| 70 | backend_status="succeeded") |
| 71 | |
Matteo Scandolo | 7551b9a | 2018-09-10 11:30:31 -0700 | [diff] [blame] | 72 | self.attr = ServiceInstanceAttribute( |
| 73 | name="foo", |
| 74 | value="bar" |
| 75 | ) |
| 76 | |
| 77 | self.mockAllAttr = Mock() |
| 78 | self.mockAllAttr.all.return_value = [self.attr] |
| 79 | |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 80 | self.app1 = ONOSApp(name="myapp1", |
| 81 | owner=self.onos, |
| 82 | backend_code=1, |
Matteo Scandolo | 7551b9a | 2018-09-10 11:30:31 -0700 | [diff] [blame] | 83 | backend_status="succeeded", |
| 84 | service_instance_attributes=self.mockAllAttr) |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 85 | |
| 86 | self.app2 = ONOSApp(name="myapp2", |
| 87 | owner=self.onos, |
| 88 | backend_code=1, |
Matteo Scandolo | 7551b9a | 2018-09-10 11:30:31 -0700 | [diff] [blame] | 89 | backend_status="succeeded", |
| 90 | service_instance_attributes=self.mockAllAttr) |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 91 | |
| 92 | self.onos.service_instances = MockObjectList([self.app1, self.app2]) |
| 93 | |
| 94 | self.log = Mock() |
| 95 | |
| 96 | def tearDown(self): |
| 97 | self.onos = None |
| 98 | sys.path = self.sys_path_save |
| 99 | |
| 100 | def test_process_event(self): |
| 101 | with patch.object(ONOSService.objects, "get_items") as service_objects, \ |
| 102 | patch.object(ONOSService, "save", autospec=True) as service_save, \ |
Matteo Scandolo | 7551b9a | 2018-09-10 11:30:31 -0700 | [diff] [blame] | 103 | patch.object(ONOSApp, "save", autospec=True) as app_save, \ |
| 104 | patch.object(ServiceInstanceAttribute, "save", autospec=True) as attr_save: |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 105 | service_objects.return_value = [self.onos] |
| 106 | |
| 107 | event_dict = {"status": "created", |
| 108 | "labels": {"xos_service": "myonos"}} |
| 109 | event = Mock() |
| 110 | event.value = json.dumps(event_dict) |
| 111 | |
Scott Baker | 806d4b7 | 2019-02-26 19:01:59 -0800 | [diff] [blame] | 112 | step = self.event_step(log=self.log, model_accessor=self.model_accessor) |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 113 | step.process_event(event) |
| 114 | |
| 115 | self.assertEqual(self.onos.backend_code, 0) |
| 116 | self.assertEqual(self.onos.backend_status, "resynchronize due to kubernetes event") |
| 117 | service_save.assert_called_with(self=self.onos, update_fields=["updated", "backend_code", "backend_status"], |
| 118 | always_update_timestamp=True) |
| 119 | |
| 120 | self.assertEqual(self.app1.backend_code, 0) |
| 121 | self.assertEqual(self.app1.backend_status, "resynchronize due to kubernetes event") |
| 122 | |
| 123 | self.assertEqual(self.app2.backend_code, 0) |
| 124 | self.assertEqual(self.app2.backend_status, "resynchronize due to kubernetes event") |
| 125 | app_save.assert_has_calls([call(self.app1, update_fields=["updated", "backend_code", "backend_status"], |
| 126 | always_update_timestamp=True), |
| 127 | call(self.app2, update_fields=["updated", "backend_code", "backend_status"], |
| 128 | always_update_timestamp=True)]) |
| 129 | |
Matteo Scandolo | 7551b9a | 2018-09-10 11:30:31 -0700 | [diff] [blame] | 130 | self.assertEqual(self.attr.backend_code, 0) |
| 131 | self.assertEqual(self.attr.backend_status, "resynchronize due to kubernetes event") |
| 132 | self.assertEqual(attr_save.call_count, 2) |
| 133 | |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 134 | def test_process_event_unknownstatus(self): |
| 135 | with patch.object(ONOSService.objects, "get_items") as service_objects, \ |
| 136 | patch.object(ONOSService, "save") as service_save, \ |
| 137 | patch.object(ONOSApp, "save") as app_save: |
| 138 | service_objects.return_value = [self.onos] |
| 139 | |
| 140 | event_dict = {"status": "something_else", |
| 141 | "labels": {"xos_service": "myonos"}} |
| 142 | event = Mock() |
| 143 | event.value = json.dumps(event_dict) |
| 144 | |
Scott Baker | 806d4b7 | 2019-02-26 19:01:59 -0800 | [diff] [blame] | 145 | step = self.event_step(log=self.log, model_accessor=self.model_accessor) |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 146 | step.process_event(event) |
| 147 | |
| 148 | self.assertEqual(self.onos.backend_code, 1) |
| 149 | self.assertEqual(self.onos.backend_status, "succeeded") |
| 150 | service_save.assert_not_called() |
| 151 | |
| 152 | self.assertEqual(self.app1.backend_code, 1) |
| 153 | self.assertEqual(self.app1.backend_status, "succeeded") |
| 154 | app_save.assert_not_called() |
| 155 | |
| 156 | self.assertEqual(self.app2.backend_code, 1) |
| 157 | self.assertEqual(self.app2.backend_status, "succeeded") |
| 158 | |
| 159 | def test_process_event_unknownservice(self): |
| 160 | with patch.object(ONOSService.objects, "get_items") as service_objects, \ |
| 161 | patch.object(ONOSService, "save") as service_save, \ |
| 162 | patch.object(ONOSApp, "save") as app_save: |
| 163 | service_objects.return_value = [self.onos] |
| 164 | |
| 165 | event_dict = {"status": "created", |
| 166 | "labels": {"xos_service": "some_other_service"}} |
| 167 | event = Mock() |
| 168 | event.value = json.dumps(event_dict) |
| 169 | |
Scott Baker | 806d4b7 | 2019-02-26 19:01:59 -0800 | [diff] [blame] | 170 | step = self.event_step(log=self.log, model_accessor=self.model_accessor) |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 171 | step.process_event(event) |
| 172 | |
| 173 | self.assertEqual(self.onos.backend_code, 1) |
| 174 | self.assertEqual(self.onos.backend_status, "succeeded") |
| 175 | service_save.assert_not_called() |
| 176 | |
| 177 | self.assertEqual(self.app1.backend_code, 1) |
| 178 | self.assertEqual(self.app1.backend_status, "succeeded") |
| 179 | app_save.assert_not_called() |
| 180 | |
| 181 | self.assertEqual(self.app2.backend_code, 1) |
| 182 | self.assertEqual(self.app2.backend_status, "succeeded") |
| 183 | |
| 184 | def test_process_event_nolabels(self): |
| 185 | with patch.object(ONOSService.objects, "get_items") as service_objects, \ |
| 186 | patch.object(ONOSService, "save") as service_save, \ |
| 187 | patch.object(ONOSApp, "save") as app_save: |
| 188 | service_objects.return_value = [self.onos] |
| 189 | |
| 190 | event_dict = {"status": "created"} |
| 191 | event = Mock() |
| 192 | event.value = json.dumps(event_dict) |
| 193 | |
Scott Baker | 806d4b7 | 2019-02-26 19:01:59 -0800 | [diff] [blame] | 194 | step = self.event_step(log=self.log, model_accessor=self.model_accessor) |
Scott Baker | a80de33 | 2018-08-16 10:46:21 -0700 | [diff] [blame] | 195 | step.process_event(event) |
| 196 | |
| 197 | self.assertEqual(self.onos.backend_code, 1) |
| 198 | self.assertEqual(self.onos.backend_status, "succeeded") |
| 199 | service_save.assert_not_called() |
| 200 | |
| 201 | self.assertEqual(self.app1.backend_code, 1) |
| 202 | self.assertEqual(self.app1.backend_status, "succeeded") |
| 203 | app_save.assert_not_called() |
| 204 | |
| 205 | self.assertEqual(self.app2.backend_code, 1) |
| 206 | self.assertEqual(self.app2.backend_status, "succeeded") |
| 207 | |
| 208 | |
| 209 | |
| 210 | if __name__ == '__main__': |
| 211 | unittest.main() |
| 212 | |
| 213 | |
| 214 | |