blob: 9fe1418bf40f6262c6b9e98c2b9035840be5bccf [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
Scott Baker82565472018-08-20 11:40:03 -070025test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Scott Baker82565472018-08-20 11:40:03 -070026
27def mock_get_westbound_service_instance_properties(props, prop):
28 return props[prop]
29
30def match_json(desired, req):
31 if desired!=req.json():
32 raise Exception("Got request %s, but body is not matching" % req.url)
33 return False
34 return True
35
36class TestPolicyFabricCrossconnectServiceInstance(unittest.TestCase):
37
38 def setUp(self):
39 global DeferredException
40
41 self.sys_path_save = sys.path
Scott Baker82565472018-08-20 11:40:03 -070042
43 # Setting up the config module
44 from xosconfig import Config
45 config = os.path.join(test_path, "../test_fabric_crossconnect_config.yaml")
46 Config.clear()
47 Config.init(config, "synchronizer-config-schema.yaml")
48 # END Setting up the config module
49
Scott Baker63afcc12019-02-01 15:41:46 -080050 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
51 mock_modelaccessor_config(test_path, [("fabric-crossconnect", "fabric-crossconnect.xproto"),])
Scott Baker82565472018-08-20 11:40:03 -070052
Scott Baker63afcc12019-02-01 15:41:46 -080053 import xossynchronizer.modelaccessor
54 import mock_modelaccessor
55 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
56 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
Scott Baker82565472018-08-20 11:40:03 -070057
58 from model_policy_fabriccrossconnectserviceinstance import FabricCrossconnectServiceInstancePolicy, \
59 model_accessor
60
Scott Baker63afcc12019-02-01 15:41:46 -080061 self.model_accessor = model_accessor
62
Scott Baker82565472018-08-20 11:40:03 -070063 # import all class names to globals
64 for (k, v) in model_accessor.all_model_classes.items():
65 globals()[k] = v
66
67 self.policy_step = FabricCrossconnectServiceInstancePolicy
68 self.policy_step.log = Mock()
69
70 # mock onos-fabric
71 self.onos_fabric = Service(name = "onos-fabric",
72 rest_hostname = "onos-fabric",
73 rest_port = "8181",
74 rest_username = "onos",
75 rest_password = "rocks")
76
77 self.service = FabricCrossconnectService(name = "fcservice",
78 provider_services = [self.onos_fabric])
79
80 def mock_westbound(self, fsi, s_tag, switch_datapath_id, switch_port):
81 # Mock out a ServiceInstance so the syncstep can call get_westbound_service_instance_properties on it
82 si = ServiceInstance(id=fsi.id)
83 si.get_westbound_service_instance_properties = functools.partial(
84 mock_get_westbound_service_instance_properties,
85 {"s_tag": s_tag,
86 "switch_datapath_id": switch_datapath_id,
87 "switch_port": switch_port})
88
89 fsi.provided_links=Mock(exists=Mock(return_value=True))
90
91 return si
92
93 def test_handle_update(self):
94 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
95 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
96
97 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=None, source_port=None,
98 switch_datapath_id=None)
99
100 serviceinstance_objects.return_value = [fsi]
101
102 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
103 serviceinstance_objects.return_value = [si]
104
Scott Baker63afcc12019-02-01 15:41:46 -0800105 self.policy_step(model_accessor=self.model_accessor).handle_update(fsi)
Scott Baker82565472018-08-20 11:40:03 -0700106
Scott Baker82565472018-08-20 11:40:03 -0700107 def tearDown(self):
108 self.o = None
109 sys.path = self.sys_path_save
110
111if __name__ == '__main__':
112 unittest.main()