blob: bccdfdc2845c7a6ad9033ba3d6f50a96a348185c [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
Scott Baker82565472018-08-20 11:40:03 -0700126 def tearDown(self):
127 self.o = None
128 sys.path = self.sys_path_save
129
130if __name__ == '__main__':
131 unittest.main()