Scott Baker | 1ae0dc9 | 2018-08-15 16:26:22 -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 | 1ae0dc9 | 2018-08-15 16:26:22 -0700 | [diff] [blame] | 23 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
Scott Baker | 1ae0dc9 | 2018-08-15 16:26:22 -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 | 1ae0dc9 | 2018-08-15 16:26:22 -0700 | [diff] [blame] | 31 | |
| 32 | # Setting up the config module |
| 33 | from xosconfig import Config |
| 34 | config = os.path.join(test_path, "../test_fabric_crossconnect_config.yaml") |
| 35 | Config.clear() |
| 36 | Config.init(config, "synchronizer-config-schema.yaml") |
| 37 | # END Setting up the config module |
| 38 | |
Scott Baker | 63afcc1 | 2019-02-01 15:41:46 -0800 | [diff] [blame^] | 39 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 40 | mock_modelaccessor_config(test_path, [("fabric-crossconnect", "fabric-crossconnect.xproto"), |
| 41 | ("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 xossynchronizer.modelaccessor import model_accessor |
| 49 | self.model_accessor = model_accessor |
| 50 | |
Scott Baker | 1ae0dc9 | 2018-08-15 16:26:22 -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", |
| 62 | id=1111, |
| 63 | rest_hostname = "onos-url", |
| 64 | rest_port = "8181", |
| 65 | rest_username = "karaf", |
| 66 | rest_password = "karaf", |
| 67 | backend_code=1, |
| 68 | backend_status="succeeded") |
| 69 | |
| 70 | self.fcservice = FabricCrossconnectService(name="myfcservice", |
| 71 | id=1112, |
| 72 | backend_code=1, |
| 73 | backend_status="succeeded", |
| 74 | provider_services=[self.onos]) |
| 75 | |
| 76 | self.fcsi1 = FabricCrossconnectServiceInstance(name="myfcsi1", |
| 77 | owner=self.fcservice, |
| 78 | backend_code=1, |
| 79 | backend_status="succeeded") |
| 80 | |
| 81 | self.fcsi2 = FabricCrossconnectServiceInstance(name="myfcsi2", |
| 82 | owner=self.fcservice, |
| 83 | backend_code=1, |
| 84 | backend_status="succeeded") |
| 85 | |
| 86 | self.fcservice.service_instances = MockObjectList([self.fcsi1, self.fcsi2]) |
| 87 | |
| 88 | self.log = Mock() |
| 89 | |
| 90 | def tearDown(self): |
| 91 | sys.path = self.sys_path_save |
| 92 | |
| 93 | def test_process_event(self): |
| 94 | with patch.object(FabricCrossconnectService.objects, "get_items") as fcservice_objects, \ |
| 95 | patch.object(Service.objects, "get_items") as service_objects, \ |
| 96 | patch.object(FabricCrossconnectServiceInstance, "save", autospec=True) as fcsi_save: |
| 97 | fcservice_objects.return_value = [self.fcservice] |
| 98 | service_objects.return_value = [self.onos, self.fcservice] |
| 99 | |
| 100 | event_dict = {"status": "created", |
| 101 | "labels": {"xos_service": "myonos"}} |
| 102 | event = Mock() |
| 103 | event.value = json.dumps(event_dict) |
| 104 | |
Scott Baker | 63afcc1 | 2019-02-01 15:41:46 -0800 | [diff] [blame^] | 105 | step = self.event_step(model_accessor=self.model_accessor, log=self.log) |
Scott Baker | 1ae0dc9 | 2018-08-15 16:26:22 -0700 | [diff] [blame] | 106 | step.process_event(event) |
| 107 | |
| 108 | self.assertEqual(self.fcsi1.backend_code, 0) |
| 109 | self.assertEqual(self.fcsi1.backend_status, "resynchronize due to kubernetes event") |
| 110 | |
| 111 | self.assertEqual(self.fcsi2.backend_code, 0) |
| 112 | self.assertEqual(self.fcsi2.backend_status, "resynchronize due to kubernetes event") |
| 113 | |
| 114 | fcsi_save.assert_has_calls([call(self.fcsi1, update_fields=["updated", "backend_code", "backend_status"], |
| 115 | always_update_timestamp=True), |
| 116 | call(self.fcsi2, update_fields=["updated", "backend_code", "backend_status"], |
| 117 | always_update_timestamp=True)]) |
| 118 | |
| 119 | def test_process_event_unknownstatus(self): |
| 120 | with patch.object(FabricCrossconnectService.objects, "get_items") as fcservice_objects, \ |
| 121 | patch.object(Service.objects, "get_items") as service_objects, \ |
| 122 | patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save: |
| 123 | fcservice_objects.return_value = [self.fcservice] |
| 124 | service_objects.return_value = [self.onos, self.fcservice] |
| 125 | |
| 126 | event_dict = {"status": "something_else", |
| 127 | "labels": {"xos_service": "myonos"}} |
| 128 | event = Mock() |
| 129 | event.value = json.dumps(event_dict) |
| 130 | |
Scott Baker | 63afcc1 | 2019-02-01 15:41:46 -0800 | [diff] [blame^] | 131 | step = self.event_step(model_accessor=self.model_accessor, log=self.log) |
Scott Baker | 1ae0dc9 | 2018-08-15 16:26:22 -0700 | [diff] [blame] | 132 | step.process_event(event) |
| 133 | |
| 134 | self.assertEqual(self.fcsi1.backend_code, 1) |
| 135 | self.assertEqual(self.fcsi1.backend_status, "succeeded") |
| 136 | |
| 137 | self.assertEqual(self.fcsi2.backend_code, 1) |
| 138 | self.assertEqual(self.fcsi2.backend_status, "succeeded") |
| 139 | |
| 140 | fcsi_save.assert_not_called() |
| 141 | |
| 142 | def test_process_event_unknownservice(self): |
| 143 | with patch.object(FabricCrossconnectService.objects, "get_items") as fcservice_objects, \ |
| 144 | patch.object(Service.objects, "get_items") as service_objects, \ |
| 145 | patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save: |
| 146 | fcservice_objects.return_value = [self.fcservice] |
| 147 | service_objects.return_value = [self.onos, self.fcservice] |
| 148 | |
| 149 | event_dict = {"status": "created", |
| 150 | "labels": {"xos_service": "something_else"}} |
| 151 | event = Mock() |
| 152 | event.value = json.dumps(event_dict) |
| 153 | |
Scott Baker | 63afcc1 | 2019-02-01 15:41:46 -0800 | [diff] [blame^] | 154 | step = self.event_step(model_accessor=self.model_accessor, log=self.log) |
Scott Baker | 1ae0dc9 | 2018-08-15 16:26:22 -0700 | [diff] [blame] | 155 | step.process_event(event) |
| 156 | |
| 157 | self.assertEqual(self.fcsi1.backend_code, 1) |
| 158 | self.assertEqual(self.fcsi1.backend_status, "succeeded") |
| 159 | |
| 160 | self.assertEqual(self.fcsi2.backend_code, 1) |
| 161 | self.assertEqual(self.fcsi2.backend_status, "succeeded") |
| 162 | |
| 163 | fcsi_save.assert_not_called() |
| 164 | |
| 165 | def test_process_event_nolabels(self): |
| 166 | with patch.object(FabricCrossconnectService.objects, "get_items") as fcservice_objects, \ |
| 167 | patch.object(Service.objects, "get_items") as service_objects, \ |
| 168 | patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save: |
| 169 | fcservice_objects.return_value = [self.fcservice] |
| 170 | service_objects.return_value = [self.onos, self.fcservice] |
| 171 | |
| 172 | event_dict = {"status": "created"} |
| 173 | event = Mock() |
| 174 | event.value = json.dumps(event_dict) |
| 175 | |
Scott Baker | 63afcc1 | 2019-02-01 15:41:46 -0800 | [diff] [blame^] | 176 | step = self.event_step(model_accessor=self.model_accessor, log=self.log) |
Scott Baker | 1ae0dc9 | 2018-08-15 16:26:22 -0700 | [diff] [blame] | 177 | step.process_event(event) |
| 178 | |
| 179 | self.assertEqual(self.fcsi1.backend_code, 1) |
| 180 | self.assertEqual(self.fcsi1.backend_status, "succeeded") |
| 181 | |
| 182 | self.assertEqual(self.fcsi2.backend_code, 1) |
| 183 | self.assertEqual(self.fcsi2.backend_status, "succeeded") |
| 184 | |
| 185 | fcsi_save.assert_not_called() |
| 186 | |
| 187 | if __name__ == '__main__': |
| 188 | unittest.main() |
| 189 | |
| 190 | |
| 191 | |