blob: a69c608e0ae78bd548800d8cd65faae535e44302 [file] [log] [blame]
Scott Bakerb2e8aa12018-08-15 16:04:41 -07001# 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
Scott Bakere9855012019-04-01 15:01:34 -070015#from __future__ import absolute_import
16
17import imp
Scott Bakerb2e8aa12018-08-15 16:04:41 -070018import unittest
19import json
Scott Bakere9855012019-04-01 15:01:34 -070020from mock import patch, call, Mock
21import os
22import sys
Scott Bakerb2e8aa12018-08-15 16:04:41 -070023
Scott Bakerb2e8aa12018-08-15 16:04:41 -070024
Scott Bakere9855012019-04-01 15:01:34 -070025test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
26
Scott Bakerb2e8aa12018-08-15 16:04:41 -070027
28class TestKubernetesEvent(unittest.TestCase):
29
30 def setUp(self):
31 global DeferredException
32
33 self.sys_path_save = sys.path
Scott Bakerb2e8aa12018-08-15 16:04:41 -070034
35 # Setting up the config module
36 from xosconfig import Config
37 config = os.path.join(test_path, "../test_config.yaml")
38 Config.clear()
39 Config.init(config, "synchronizer-config-schema.yaml")
40 # END Setting up the config module
41
Scott Baker382366d2019-02-04 10:58:43 -080042 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
43 mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto"),
44 ("onos-service", "onos.xproto")])
Scott Bakerb2e8aa12018-08-15 16:04:41 -070045
Scott Baker382366d2019-02-04 10:58:43 -080046 import xossynchronizer.modelaccessor
47 import mock_modelaccessor
Scott Bakere9855012019-04-01 15:01:34 -070048 imp.reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
49 imp.reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
Scott Baker382366d2019-02-04 10:58:43 -080050
51 from xossynchronizer.modelaccessor import model_accessor
52 self.model_accessor = model_accessor
53
54 from mock_modelaccessor import MockObjectList
Scott Bakerb2e8aa12018-08-15 16:04:41 -070055 from kubernetes_event import KubernetesPodDetailsEventStep
56
57 # import all class names to globals
58 for (k, v) in model_accessor.all_model_classes.items():
59 globals()[k] = v
60
61 self.event_step = KubernetesPodDetailsEventStep
62
63 self.onos = ONOSService(name="myonos",
64 id=1111,
Scott Bakere9855012019-04-01 15:01:34 -070065 rest_hostname="onos-url",
66 rest_port="8181",
67 rest_username="karaf",
68 rest_password="karaf",
Scott Bakerb2e8aa12018-08-15 16:04:41 -070069 backend_code=1,
70 backend_status="succeeded")
71
72 self.fabric_service = FabricService(name="fabric",
Scott Bakere9855012019-04-01 15:01:34 -070073 id=1112,
74 backend_code=1,
75 backend_status="succeeded",
76 provider_services=[self.onos])
Scott Bakerb2e8aa12018-08-15 16:04:41 -070077
78 self.switch = Switch(name="switch1",
79 backend_code=1,
80 backend_status="succeeded")
81
82 self.port1 = SwitchPort(name="switch1port1",
83 switch=self.switch,
84 backend_code=1,
85 backend_status="succeeded")
86
87 self.port2 = SwitchPort(name="switch1port2",
88 switch=self.switch,
89 backend_code=1,
90 backend_status="succeeded")
91
92 self.switch.ports = MockObjectList([self.port1, self.port2])
93
94 self.log = Mock()
95
96 def tearDown(self):
97 sys.path = self.sys_path_save
98
99 def test_process_event(self):
100 with patch.object(FabricService.objects, "get_items") as fabric_service_objects, \
101 patch.object(Service.objects, "get_items") as service_objects, \
102 patch.object(Switch.objects, "get_items") as switch_objects, \
103 patch.object(Switch, "save", autospec=True) as switch_save, \
104 patch.object(SwitchPort, "save", autospec=True) as switchport_save:
105 fabric_service_objects.return_value = [self.fabric_service]
106 service_objects.return_value = [self.onos, self.fabric_service]
107 switch_objects.return_value = [self.switch]
108
109 event_dict = {"status": "created",
110 "labels": {"xos_service": "myonos"}}
111 event = Mock()
112 event.value = json.dumps(event_dict)
113
Scott Baker382366d2019-02-04 10:58:43 -0800114 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
Scott Bakerb2e8aa12018-08-15 16:04:41 -0700115 step.process_event(event)
116
117 self.assertEqual(self.switch.backend_code, 0)
118 self.assertEqual(self.switch.backend_status, "resynchronize due to kubernetes event")
119
120 switch_save.assert_called_with(self.switch, update_fields=["updated", "backend_code", "backend_status"],
Scott Bakere9855012019-04-01 15:01:34 -0700121 always_update_timestamp=True)
Scott Bakerb2e8aa12018-08-15 16:04:41 -0700122
123 self.assertEqual(self.port1.backend_code, 0)
124 self.assertEqual(self.port1.backend_status, "resynchronize due to kubernetes event")
125
126 self.assertEqual(self.port2.backend_code, 0)
127 self.assertEqual(self.port2.backend_status, "resynchronize due to kubernetes event")
128
Scott Bakere9855012019-04-01 15:01:34 -0700129 switchport_save.assert_has_calls(
130 [
131 call(
132 self.port1,
133 update_fields=[
134 "updated",
135 "backend_code",
136 "backend_status"],
137 always_update_timestamp=True),
138 call(
139 self.port2,
140 update_fields=[
141 "updated",
142 "backend_code",
143 "backend_status"],
144 always_update_timestamp=True)])
Scott Bakerb2e8aa12018-08-15 16:04:41 -0700145
146 def test_process_event_unknownstatus(self):
147 with patch.object(FabricService.objects, "get_items") as fabric_service_objects, \
148 patch.object(Service.objects, "get_items") as service_objects, \
149 patch.object(Switch.objects, "get_items") as switch_objects, \
150 patch.object(Switch, "save") as switch_save, \
151 patch.object(SwitchPort, "save") as switchport_save:
152 fabric_service_objects.return_value = [self.fabric_service]
153 service_objects.return_value = [self.onos, self.fabric_service]
154 switch_objects.return_value = [self.switch]
155
156 event_dict = {"status": "something_else",
157 "labels": {"xos_service": "myonos"}}
158 event = Mock()
159 event.value = json.dumps(event_dict)
160
Scott Baker382366d2019-02-04 10:58:43 -0800161 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
Scott Bakerb2e8aa12018-08-15 16:04:41 -0700162 step.process_event(event)
163
164 self.assertEqual(self.switch.backend_code, 1)
165 self.assertEqual(self.switch.backend_status, "succeeded")
166
167 switch_save.assert_not_called()
168
169 self.assertEqual(self.port1.backend_code, 1)
170 self.assertEqual(self.port1.backend_status, "succeeded")
171
172 self.assertEqual(self.port2.backend_code, 1)
173 self.assertEqual(self.port2.backend_status, "succeeded")
174
175 switchport_save.assert_not_called()
176
177 def test_process_event_unknownservice(self):
178 with patch.object(FabricService.objects, "get_items") as fabric_service_objects, \
179 patch.object(Service.objects, "get_items") as service_objects, \
180 patch.object(Switch.objects, "get_items") as switch_objects, \
181 patch.object(Switch, "save") as switch_save, \
182 patch.object(SwitchPort, "save") as switchport_save:
183 fabric_service_objects.return_value = [self.fabric_service]
184 service_objects.return_value = [self.onos, self.fabric_service]
185 switch_objects.return_value = [self.switch]
186
187 event_dict = {"status": "created",
188 "labels": {"xos_service": "something_else"}}
189 event = Mock()
190 event.value = json.dumps(event_dict)
191
Scott Baker382366d2019-02-04 10:58:43 -0800192 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
Scott Bakerb2e8aa12018-08-15 16:04:41 -0700193 step.process_event(event)
194
195 self.assertEqual(self.switch.backend_code, 1)
196 self.assertEqual(self.switch.backend_status, "succeeded")
197
198 switch_save.assert_not_called()
199
200 self.assertEqual(self.port1.backend_code, 1)
201 self.assertEqual(self.port1.backend_status, "succeeded")
202
203 self.assertEqual(self.port2.backend_code, 1)
204 self.assertEqual(self.port2.backend_status, "succeeded")
205
206 switchport_save.assert_not_called()
207
208 def test_process_event_nolabels(self):
209 with patch.object(FabricService.objects, "get_items") as fabric_service_objects, \
210 patch.object(Service.objects, "get_items") as service_objects, \
211 patch.object(Switch.objects, "get_items") as switch_objects, \
212 patch.object(Switch, "save") as switch_save, \
213 patch.object(SwitchPort, "save") as switchport_save:
214 fabric_service_objects.return_value = [self.fabric_service]
215 service_objects.return_value = [self.onos, self.fabric_service]
216 switch_objects.return_value = [self.switch]
217
218 event_dict = {"status": "created"}
219 event = Mock()
220 event.value = json.dumps(event_dict)
221
Scott Baker382366d2019-02-04 10:58:43 -0800222 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
Scott Bakerb2e8aa12018-08-15 16:04:41 -0700223 step.process_event(event)
224
225 self.assertEqual(self.switch.backend_code, 1)
226 self.assertEqual(self.switch.backend_status, "succeeded")
227
228 switch_save.assert_not_called()
229
230 self.assertEqual(self.port1.backend_code, 1)
231 self.assertEqual(self.port1.backend_status, "succeeded")
232
233 self.assertEqual(self.port2.backend_code, 1)
234 self.assertEqual(self.port2.backend_status, "succeeded")
235
236 switchport_save.assert_not_called()
237
Scott Bakere9855012019-04-01 15:01:34 -0700238
Scott Bakerb2e8aa12018-08-15 16:04:41 -0700239if __name__ == '__main__':
240 unittest.main()