blob: 8c6e77393cddfb9977b7560dae16099e04fb307c [file] [log] [blame]
Matteo Scandolo6739b512018-05-30 18:55:29 -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
Matteo Scandolodb7adc32018-06-15 16:05:19 -070016import urllib
Matteo Scandolo6739b512018-05-30 18:55:29 -070017import functools
18from mock import patch, call, Mock, PropertyMock
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 match_json(desired, req):
48 if desired!=req.json():
49 raise Exception("Got request %s, but body is not matching" % req.url)
50 return False
51 return True
52
53class TestSyncFabricPort(unittest.TestCase):
54
55 def setUp(self):
56 global DeferredException
57
58 self.sys_path_save = sys.path
59 sys.path.append(xos_dir)
60 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
61
62 # Setting up the config module
63 from xosconfig import Config
64 config = os.path.join(test_path, "../test_config.yaml")
65 Config.clear()
66 Config.init(config, "synchronizer-config-schema.yaml")
67 # END Setting up the config module
68
69 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
70 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("fabric", "fabric.xproto")])
71 import synchronizers.new_base.modelaccessor
72
73 from sync_fabric_port import SyncFabricPort, model_accessor
74
75 # import all class names to globals
76 for (k, v) in model_accessor.all_model_classes.items():
77 globals()[k] = v
78
Matteo Scandolo6739b512018-05-30 18:55:29 -070079 self.sync_step = SyncFabricPort
80 self.sync_step.log = Mock()
81
Matteo Scandolo6739b512018-05-30 18:55:29 -070082 # mock onos-fabric
83 onos_fabric = Mock()
84 onos_fabric.name = "onos-fabric"
85 onos_fabric.rest_hostname = "onos-fabric"
86 onos_fabric.rest_port = "8181"
87 onos_fabric.rest_username = "onos"
88 onos_fabric.rest_password = "rocks"
89
90 onos_fabric_base = Mock()
91 onos_fabric_base.leaf_model = onos_fabric
92
93 self.fabric = Mock()
94 self.fabric.name = "fabric"
Scott Bakerafdf11d2018-08-16 15:47:55 -070095 self.fabric.provider_services = [onos_fabric_base]
Matteo Scandolo6739b512018-05-30 18:55:29 -070096
Matteo Scandolo6739b512018-05-30 18:55:29 -070097 def tearDown(self):
Matteo Scandolo6739b512018-05-30 18:55:29 -070098 sys.path = self.sys_path_save
99
100 @requests_mock.Mocker()
101 def test_sync_port(self, m):
Luca Prete15377872018-09-11 17:32:55 -0700102 # IPs
103 ip1 = Mock()
104 ip1.ip = "1.1.1.1/16"
105 ip1.description = "My IPv4 ip"
106 ip2 = Mock()
107 ip2.ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334/64"
108 ip2.description = "My IPv6 ip"
109 ip3 = Mock()
110 ip3.ip = "2.2.2.2/8"
111 ip3.description = "My other IPv4 ip"
112
Matteo Scandolo6739b512018-05-30 18:55:29 -0700113 intf1 = Mock()
114 intf1.name = "intf1"
Matteo Scandolo6739b512018-05-30 18:55:29 -0700115 intf1.vlanUntagged = None
Luca Prete15377872018-09-11 17:32:55 -0700116 intf1.ips.all.return_value = [ip1, ip2]
117 intf2 = Mock()
118 intf2.name = "intf2"
119 intf2.vlanUntagged = 42
120 intf2.ips.all.return_value = [ip3]
Matteo Scandolo6739b512018-05-30 18:55:29 -0700121
Luca Prete15377872018-09-11 17:32:55 -0700122 port = Mock()
123 port.id = 1
124 port.tologdict.return_value = {}
125 port.host_learning = True
126 port.interfaces.all.return_value = [intf1, intf2]
127 port.switch.ofId = "of:1234"
128 port.portId = "1"
Matteo Scandolo6739b512018-05-30 18:55:29 -0700129
130 expected_conf = {
131 "ports": {
Luca Prete15377872018-09-11 17:32:55 -0700132 "%s/%s" % (port.switch.ofId, port.portId): {
Matteo Scandolo6739b512018-05-30 18:55:29 -0700133 "interfaces": [
134 {
135 "name": intf1.name,
Luca Prete15377872018-09-11 17:32:55 -0700136 "ips": [ ip1.ip, ip2.ip ]
137 },
138 {
139 "name": intf2.name,
140 "ips": [ip3.ip],
141 "vlan-untagged": intf2.vlanUntagged
Matteo Scandolo6739b512018-05-30 18:55:29 -0700142 }
Matteo Scandolodfe75ff2018-06-15 10:52:09 -0700143 ],
144 "hostLearning": {
Luca Prete15377872018-09-11 17:32:55 -0700145 "enabled": port.host_learning
Matteo Scandolodfe75ff2018-06-15 10:52:09 -0700146 }
Matteo Scandolo6739b512018-05-30 18:55:29 -0700147 }
148 }
149 }
150
151 m.post("http://onos-fabric:8181/onos/v1/network/configuration/",
152 status_code=200,
153 additional_matcher=functools.partial(match_json, expected_conf))
154
155 with patch.object(Service.objects, "get") as onos_fabric_get:
156 onos_fabric_get.return_value = self.fabric
Luca Prete15377872018-09-11 17:32:55 -0700157 self.sync_step().sync_record(port)
Matteo Scandolo6739b512018-05-30 18:55:29 -0700158 self.assertTrue(m.called)
159
160 @requests_mock.Mocker()
161 def test_delete_port(self, m):
Luca Prete15377872018-09-11 17:32:55 -0700162 # create a mock SwitchPort instance
163 port = Mock()
164 port.id = 1
165 port.tologdict.return_value = {}
166 port.host_learning = True
167 port.switch.ofId = "of:1234"
168 port.portId = "1"
Matteo Scandolo6739b512018-05-30 18:55:29 -0700169
Matteo Scandolodb7adc32018-06-15 16:05:19 -0700170 key = urllib.quote("of:1234/1", safe='')
Matteo Scandolodb7adc32018-06-15 16:05:19 -0700171 m.delete("http://onos-fabric:8181/onos/v1/network/configuration/ports/%s" % key,
172 status_code=204)
173
Matteo Scandolo6739b512018-05-30 18:55:29 -0700174 with patch.object(Service.objects, "get") as onos_fabric_get:
175 onos_fabric_get.return_value = self.fabric
Luca Prete15377872018-09-11 17:32:55 -0700176 self.sync_step().delete_record(port)
177 self.assertTrue(m.called)
Matteo Scandolo6739b512018-05-30 18:55:29 -0700178
Luca Prete15377872018-09-11 17:32:55 -0700179 @requests_mock.Mocker()
180 def test_delete_interface(self, m):
181 ip1 = Mock()
182 ip1.ip = "1.1.1.1/16"
183 ip1.description = "My IPv4 ip"
184 ip2 = Mock()
185 ip2.ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334/64"
186 ip2.description = "My IPv6 ip"
Matteo Scandolo6739b512018-05-30 18:55:29 -0700187
Luca Prete15377872018-09-11 17:32:55 -0700188 # interfaces
189 intf1 = Mock()
190 intf1.name = "intf1"
191 intf1.vlanUntagged = None
192 intf1.ips.all.return_value = [ip1, ip2]
193
194 # bindings
195 # create a mock SwitchPort instance
196 interface_to_remove = Mock()
197 interface_to_remove.id = 1
198 interface_to_remove.tologdict.return_value = {}
199 interface_to_remove.leaf_model_name = "PortInterface"
200 interface_to_remove.port.interfaces.all.return_value = [intf1]
201 interface_to_remove.port.switch.ofId = "of:1234"
202 interface_to_remove.port.portId = "1"
203 interface_to_remove.port.host_learning = True
204
205 m.post("http://onos-fabric:8181/onos/v1/network/configuration/", status_code=200)
206
207 with patch.object(Service.objects, "get") as onos_fabric_get:
208 onos_fabric_get.return_value = self.fabric
209 self.sync_step().delete_record(interface_to_remove)
210 self.assertTrue(m.called)
211
212 @requests_mock.Mocker()
213 def test_delete_ip(self, m):
214 ip1 = Mock()
215 ip1.ip = "1.1.1.1/16"
216 ip1.description = "My IPv4 ip"
217
218 intf1 = Mock()
219 intf1.name = "intf1"
220 intf1.vlanUntagged = None
221 intf1.ips.all.return_value = [ip1]
222
223 ip_to_remove = Mock()
224 ip_to_remove.id = 1
225 ip_to_remove.leaf_model_name = "FabricIpAddress"
226 ip_to_remove.interface.port.interfaces.all.return_value = [intf1]
227 ip_to_remove.interface.port.switch.ofId = "of:1234"
228 ip_to_remove.interface.port.portId = "1"
229 ip_to_remove.interface.port.host_learning = True
230
231 m.post("http://onos-fabric:8181/onos/v1/network/configuration/", status_code=200)
232
233 with patch.object(Service.objects, "get") as onos_fabric_get:
234 onos_fabric_get.return_value = self.fabric
235 self.sync_step().delete_record(ip_to_remove)
Scott Bakerafdf11d2018-08-16 15:47:55 -0700236 self.assertTrue(m.called)