blob: 0099881bbfeae0fec8931e463dbf16f07e229648 [file] [log] [blame]
Scott Baker82565472018-08-20 11:40:03 -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
16
17import functools
18from mock import patch, call, Mock, PropertyMock, MagicMock
19import requests_mock
20import multistructlog
21from multistructlog import create_logger
22
23import os, sys
24
25# Hack to load synchronizer framework
26test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
27xos_dir=os.path.join(test_path, "../../..")
28if not os.path.exists(os.path.join(test_path, "new_base")):
29 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
30 services_dir = os.path.join(xos_dir, "../../xos_services")
31sys.path.append(xos_dir)
32sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
33# END Hack to load synchronizer framework
34
35# generate model from xproto
36def get_models_fn(service_name, xproto_name):
37 name = os.path.join(service_name, "xos", xproto_name)
38 if os.path.exists(os.path.join(services_dir, name)):
39 return name
40 else:
41 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
42 if os.path.exists(os.path.join(services_dir, name)):
43 return name
44 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
45# END generate model from xproto
46
47def mock_get_westbound_service_instance_properties(props, prop):
48 return props[prop]
49
50def match_json(desired, req):
51 if desired!=req.json():
52 raise Exception("Got request %s, but body is not matching" % req.url)
53 return False
54 return True
55
56class TestPolicyFabricCrossconnectServiceInstance(unittest.TestCase):
57
58 def setUp(self):
59 global DeferredException
60
61 self.sys_path_save = sys.path
62 sys.path.append(xos_dir)
63 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
64
65 # Setting up the config module
66 from xosconfig import Config
67 config = os.path.join(test_path, "../test_fabric_crossconnect_config.yaml")
68 Config.clear()
69 Config.init(config, "synchronizer-config-schema.yaml")
70 # END Setting up the config module
71
72 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
73 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("fabric-crossconnect", "fabric-crossconnect.xproto")])
74 import synchronizers.new_base.modelaccessor
75
76 from mock_modelaccessor import MockObjectList
77 self.MockObjectList = MockObjectList
78
79 from model_policy_fabriccrossconnectserviceinstance import FabricCrossconnectServiceInstancePolicy, \
80 model_accessor
81
82 # import all class names to globals
83 for (k, v) in model_accessor.all_model_classes.items():
84 globals()[k] = v
85
86 self.policy_step = FabricCrossconnectServiceInstancePolicy
87 self.policy_step.log = Mock()
88
89 # mock onos-fabric
90 self.onos_fabric = Service(name = "onos-fabric",
91 rest_hostname = "onos-fabric",
92 rest_port = "8181",
93 rest_username = "onos",
94 rest_password = "rocks")
95
96 self.service = FabricCrossconnectService(name = "fcservice",
97 provider_services = [self.onos_fabric])
98
99 def mock_westbound(self, fsi, s_tag, switch_datapath_id, switch_port):
100 # Mock out a ServiceInstance so the syncstep can call get_westbound_service_instance_properties on it
101 si = ServiceInstance(id=fsi.id)
102 si.get_westbound_service_instance_properties = functools.partial(
103 mock_get_westbound_service_instance_properties,
104 {"s_tag": s_tag,
105 "switch_datapath_id": switch_datapath_id,
106 "switch_port": switch_port})
107
108 fsi.provided_links=Mock(exists=Mock(return_value=True))
109
110 return si
111
112 def test_handle_update(self):
113 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
114 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
115
116 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=None, source_port=None,
117 switch_datapath_id=None)
118
119 serviceinstance_objects.return_value = [fsi]
120
121 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
122 serviceinstance_objects.return_value = [si]
123
124 self.policy_step().handle_update(fsi)
125
126 self.assertEqual(fsi.s_tag, 111)
127 self.assertEqual(fsi.switch_datapath_id, "of:0000000000000201")
128 self.assertEqual(fsi.source_port, 3)
129
130 fcsi_save.assert_called_with(update_fields=["s_tag", "switch_datapath_id", "source_port"])
131
132 def test_handle_update_west_si_bad_s_tag(self):
133 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
134 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
135
136 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=None, source_port=None,
137 switch_datapath_id=None)
138
139 serviceinstance_objects.return_value = [fsi]
140
141 si = self.mock_westbound(fsi, s_tag=None, switch_datapath_id = "of:0000000000000201", switch_port = 3)
142 serviceinstance_objects.return_value = [si]
143
144 with self.assertRaises(Exception) as e:
145 self.policy_step().handle_update(fsi)
146
147 self.assertEqual(e.exception.message, "Westbound ServiceInstance s-tag is None on fcsi 7777")
148
149 def test_handle_update_west_si_bad_port(self):
150 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
151 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
152
153 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=None, source_port=None,
154 switch_datapath_id=None)
155
156 serviceinstance_objects.return_value = [fsi]
157
158 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = None)
159 serviceinstance_objects.return_value = [si]
160
161 with self.assertRaises(Exception) as e:
162 self.policy_step().handle_update(fsi)
163
164 self.assertEqual(e.exception.message, "Westbound ServiceInstance switch_port is None on fcsi 7777")
165
166 def test_handle_update_west_si_bad_switch_datapath_id(self):
167 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
168 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
169
170 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=None, source_port=None,
171 switch_datapath_id=None)
172
173 serviceinstance_objects.return_value = [fsi]
174
175 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = None, switch_port = 3)
176 serviceinstance_objects.return_value = [si]
177
178 with self.assertRaises(Exception) as e:
179 self.policy_step().handle_update(fsi)
180
181 self.assertEqual(e.exception.message, "Westbound ServiceInstance switch_datapath_id is unset on fcsi 7777")
182
183 def test_handle_update_no_links(self):
184 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
185 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
186
187 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=None, source_port=None,
188 switch_datapath_id=None)
189
190 fsi.provided_links = Mock(exists=Mock(return_value=False))
191
192 serviceinstance_objects.return_value = [fsi]
193
194 self.policy_step().handle_update(fsi)
195
196 fcsi_save.assert_not_called()
197
198 def test_handle_update_change_s_tag(self):
199 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
200 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
201
202 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=100, source_port=None,
203 switch_datapath_id=None)
204
205 serviceinstance_objects.return_value = [fsi]
206
207 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
208 serviceinstance_objects.return_value = [si]
209
210 with self.assertRaises(Exception) as e:
211 self.policy_step().handle_update(fsi)
212
213 self.assertEqual(e.exception.message, "Westbound ServiceInstance changing s-tag is not currently permitted")
214
215 def test_handle_update_change_switch_datapath_id(self):
216 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
217 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
218
219 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=None, source_port=None,
220 switch_datapath_id="foo")
221
222 serviceinstance_objects.return_value = [fsi]
223
224 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
225 serviceinstance_objects.return_value = [si]
226
227 with self.assertRaises(Exception) as e:
228 self.policy_step().handle_update(fsi)
229
230 self.assertEqual(e.exception.message, "Westbound ServiceInstance changing switch_datapath_id is not currently permitted")
231
232 def test_handle_update_change_source_port(self):
233 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
234 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
235
236 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=None, source_port=5,
237 switch_datapath_id=None)
238
239 serviceinstance_objects.return_value = [fsi]
240
241 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
242 serviceinstance_objects.return_value = [si]
243
244 with self.assertRaises(Exception) as e:
245 self.policy_step().handle_update(fsi)
246
247 self.assertEqual(e.exception.message, "Westbound ServiceInstance changing source_port is not currently permitted")
248
249
250 def tearDown(self):
251 self.o = None
252 sys.path = self.sys_path_save
253
254if __name__ == '__main__':
255 unittest.main()