blob: 93379f9eacead0fdb98ba9ba12953d4fa34fca70 [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
Scott Bakere7b55e42019-04-01 17:18:03 -070018from mock import patch, Mock
Scott Baker82565472018-08-20 11:40:03 -070019
Scott Bakere7b55e42019-04-01 17:18:03 -070020import os
21import sys
Scott Baker82565472018-08-20 11:40:03 -070022
Scott Bakere7b55e42019-04-01 17:18:03 -070023test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24
Scott Baker82565472018-08-20 11:40:03 -070025
26def mock_get_westbound_service_instance_properties(props, prop):
27 return props[prop]
28
Scott Bakere7b55e42019-04-01 17:18:03 -070029
Scott Baker82565472018-08-20 11:40:03 -070030def match_json(desired, req):
Scott Bakere7b55e42019-04-01 17:18:03 -070031 if desired != req.json():
Scott Baker82565472018-08-20 11:40:03 -070032 raise Exception("Got request %s, but body is not matching" % req.url)
33 return False
34 return True
35
Scott Bakere7b55e42019-04-01 17:18:03 -070036
Scott Baker82565472018-08-20 11:40:03 -070037class TestPolicyFabricCrossconnectServiceInstance(unittest.TestCase):
38
39 def setUp(self):
40 global DeferredException
41
42 self.sys_path_save = sys.path
Scott Baker82565472018-08-20 11:40:03 -070043
44 # Setting up the config module
45 from xosconfig import Config
46 config = os.path.join(test_path, "../test_fabric_crossconnect_config.yaml")
47 Config.clear()
48 Config.init(config, "synchronizer-config-schema.yaml")
49 # END Setting up the config module
50
Scott Baker63afcc12019-02-01 15:41:46 -080051 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
Scott Bakere7b55e42019-04-01 17:18:03 -070052 mock_modelaccessor_config(test_path, [("fabric-crossconnect", "fabric-crossconnect.xproto"), ])
Scott Baker82565472018-08-20 11:40:03 -070053
Scott Baker63afcc12019-02-01 15:41:46 -080054 import xossynchronizer.modelaccessor
55 import mock_modelaccessor
Scott Bakere7b55e42019-04-01 17:18:03 -070056 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
Scott Baker63afcc12019-02-01 15:41:46 -080057 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
Scott Baker82565472018-08-20 11:40:03 -070058
59 from model_policy_fabriccrossconnectserviceinstance import FabricCrossconnectServiceInstancePolicy, \
60 model_accessor
61
Scott Baker63afcc12019-02-01 15:41:46 -080062 self.model_accessor = model_accessor
63
Scott Baker82565472018-08-20 11:40:03 -070064 # import all class names to globals
65 for (k, v) in model_accessor.all_model_classes.items():
66 globals()[k] = v
67
68 self.policy_step = FabricCrossconnectServiceInstancePolicy
69 self.policy_step.log = Mock()
70
71 # mock onos-fabric
Scott Bakere7b55e42019-04-01 17:18:03 -070072 self.onos_fabric = Service(name="onos-fabric",
73 rest_hostname="onos-fabric",
74 rest_port="8181",
75 rest_username="onos",
76 rest_password="rocks")
Scott Baker82565472018-08-20 11:40:03 -070077
Scott Bakere7b55e42019-04-01 17:18:03 -070078 self.service = FabricCrossconnectService(name="fcservice",
79 provider_services=[self.onos_fabric])
Scott Baker82565472018-08-20 11:40:03 -070080
81 def mock_westbound(self, fsi, s_tag, switch_datapath_id, switch_port):
82 # Mock out a ServiceInstance so the syncstep can call get_westbound_service_instance_properties on it
83 si = ServiceInstance(id=fsi.id)
84 si.get_westbound_service_instance_properties = functools.partial(
85 mock_get_westbound_service_instance_properties,
86 {"s_tag": s_tag,
87 "switch_datapath_id": switch_datapath_id,
88 "switch_port": switch_port})
89
Scott Bakere7b55e42019-04-01 17:18:03 -070090 fsi.provided_links = Mock(exists=Mock(return_value=True))
Scott Baker82565472018-08-20 11:40:03 -070091
92 return si
93
94 def test_handle_update(self):
Scott Bakere7b55e42019-04-01 17:18:03 -070095 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects:
Scott Baker82565472018-08-20 11:40:03 -070096 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=None, source_port=None,
97 switch_datapath_id=None)
98
99 serviceinstance_objects.return_value = [fsi]
100
Scott Bakere7b55e42019-04-01 17:18:03 -0700101 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id="of:0000000000000201", switch_port=3)
Scott Baker82565472018-08-20 11:40:03 -0700102 serviceinstance_objects.return_value = [si]
103
Scott Baker63afcc12019-02-01 15:41:46 -0800104 self.policy_step(model_accessor=self.model_accessor).handle_update(fsi)
Scott Baker82565472018-08-20 11:40:03 -0700105
Scott Baker82565472018-08-20 11:40:03 -0700106 def tearDown(self):
107 self.o = None
108 sys.path = self.sys_path_save
109
Scott Bakere7b55e42019-04-01 17:18:03 -0700110
Scott Baker82565472018-08-20 11:40:03 -0700111if __name__ == '__main__':
112 unittest.main()