blob: 70d20df1b3ef49d968772e4d9567948a2b67f996 [file] [log] [blame]
Scott Bakera80de332018-08-16 10:46:21 -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
15import unittest
16import json
Scott Baker7848fa52019-04-01 17:54:36 -070017from mock import patch, call, Mock
Scott Bakera80de332018-08-16 10:46:21 -070018
Scott Baker7848fa52019-04-01 17:54:36 -070019import os
20import sys
Scott Bakera80de332018-08-16 10:46:21 -070021
Scott Baker7848fa52019-04-01 17:54:36 -070022test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Scott Bakera80de332018-08-16 10:46:21 -070023
Scott Bakera80de332018-08-16 10:46:21 -070024
25class TestKubernetesEvent(unittest.TestCase):
26
27 def setUp(self):
28 global DeferredException
29
30 self.sys_path_save = sys.path
Scott Bakera80de332018-08-16 10:46:21 -070031
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 Baker806d4b72019-02-26 19:01:59 -080039 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
Scott Baker7848fa52019-04-01 17:54:36 -070040 mock_modelaccessor_config(test_path, [("onos-service", "onos.xproto"), ])
Scott Baker806d4b72019-02-26 19:01:59 -080041
42 import xossynchronizer.modelaccessor
43 import mock_modelaccessor
Scott Baker7848fa52019-04-01 17:54:36 -070044 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
Scott Baker806d4b72019-02-26 19:01:59 -080045 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
46
Scott Baker806d4b72019-02-26 19:01:59 -080047 from xossynchronizer.modelaccessor import model_accessor
48
49 self.model_accessor = model_accessor
50
Scott Bakera80de332018-08-16 10:46:21 -070051 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 Baker7848fa52019-04-01 17:54:36 -070062 rest_hostname="onos-url",
63 rest_port="8181",
64 rest_username="karaf",
65 rest_password="karaf",
Scott Bakera80de332018-08-16 10:46:21 -070066 backend_code=1,
67 backend_status="succeeded")
68
Matteo Scandolo7551b9a2018-09-10 11:30:31 -070069 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 Bakera80de332018-08-16 10:46:21 -070077 self.app1 = ONOSApp(name="myapp1",
Scott Baker7848fa52019-04-01 17:54:36 -070078 owner=self.onos,
79 backend_code=1,
80 backend_status="succeeded",
81 service_instance_attributes=self.mockAllAttr)
Scott Bakera80de332018-08-16 10:46:21 -070082
83 self.app2 = ONOSApp(name="myapp2",
Scott Baker7848fa52019-04-01 17:54:36 -070084 owner=self.onos,
85 backend_code=1,
86 backend_status="succeeded",
87 service_instance_attributes=self.mockAllAttr)
Scott Bakera80de332018-08-16 10:46:21 -070088
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 Baker7848fa52019-04-01 17:54:36 -070099 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 Bakera80de332018-08-16 10:46:21 -0700102 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 Baker806d4b72019-02-26 19:01:59 -0800109 step = self.event_step(log=self.log, model_accessor=self.model_accessor)
Scott Bakera80de332018-08-16 10:46:21 -0700110 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 Baker7848fa52019-04-01 17:54:36 -0700114 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 Bakera80de332018-08-16 10:46:21 -0700121
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 Scandolo7551b9a2018-09-10 11:30:31 -0700132 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 Bakera80de332018-08-16 10:46:21 -0700136 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 Baker806d4b72019-02-26 19:01:59 -0800147 step = self.event_step(log=self.log, model_accessor=self.model_accessor)
Scott Bakera80de332018-08-16 10:46:21 -0700148 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 Baker806d4b72019-02-26 19:01:59 -0800172 step = self.event_step(log=self.log, model_accessor=self.model_accessor)
Scott Bakera80de332018-08-16 10:46:21 -0700173 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 Baker806d4b72019-02-26 19:01:59 -0800196 step = self.event_step(log=self.log, model_accessor=self.model_accessor)
Scott Bakera80de332018-08-16 10:46:21 -0700197 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 Bakera80de332018-08-16 10:46:21 -0700211if __name__ == '__main__':
212 unittest.main()