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