blob: 2358d4adc0c9963ef9657b914898a6c36752a68a [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
79
80 self.sync_step = SyncFabricPort
81 self.sync_step.log = Mock()
82
83
84 # mock onos-fabric
85 onos_fabric = Mock()
86 onos_fabric.name = "onos-fabric"
87 onos_fabric.rest_hostname = "onos-fabric"
88 onos_fabric.rest_port = "8181"
89 onos_fabric.rest_username = "onos"
90 onos_fabric.rest_password = "rocks"
91
92 onos_fabric_base = Mock()
93 onos_fabric_base.leaf_model = onos_fabric
94
95 self.fabric = Mock()
96 self.fabric.name = "fabric"
Scott Bakerafdf11d2018-08-16 15:47:55 -070097 self.fabric.provider_services = [onos_fabric_base]
Matteo Scandolo6739b512018-05-30 18:55:29 -070098
99 # create a mock SwitchPort instance
100 self.o = Mock()
101 self.o.id = 1
102 self.o.tologdict.return_value = {}
Matteo Scandolodfe75ff2018-06-15 10:52:09 -0700103 self.o.host_learning = True
Matteo Scandolo6739b512018-05-30 18:55:29 -0700104
105
106
107
108 def tearDown(self):
109 self.o = None
110 sys.path = self.sys_path_save
111
112 @requests_mock.Mocker()
113 def test_sync_port(self, m):
114 intf1 = Mock()
115 intf1.name = "intf1"
116 intf1.ips = "1.1.1.1/16"
117 intf1.vlanUntagged = None
118
119 self.o.interfaces.all.return_value = [intf1]
120 self.o.switch.ofId = "of:1234"
121 self.o.portId = "1"
122
123 expected_conf = {
124 "ports": {
125 "%s/%s" % (self.o.switch.ofId, self.o.portId): {
126 "interfaces": [
127 {
128 "name": intf1.name,
129 "ips": [ intf1.ips ]
130 }
Matteo Scandolodfe75ff2018-06-15 10:52:09 -0700131 ],
132 "hostLearning": {
133 "enabled": self.o.host_learning
134 }
Matteo Scandolo6739b512018-05-30 18:55:29 -0700135 }
136 }
137 }
138
139 m.post("http://onos-fabric:8181/onos/v1/network/configuration/",
140 status_code=200,
141 additional_matcher=functools.partial(match_json, expected_conf))
142
143 with patch.object(Service.objects, "get") as onos_fabric_get:
144 onos_fabric_get.return_value = self.fabric
145
146 self.sync_step().sync_record(self.o)
147
148 self.assertTrue(m.called)
149
150 @requests_mock.Mocker()
151 def test_sync_port_with_vlan(self, m):
152 intf1 = Mock()
153 intf1.name = "intf1"
154 intf1.ips = "1.1.1.1/16"
155 intf1.vlanUntagged = 42
156
157 self.o.interfaces.all.return_value = [intf1]
158 self.o.switch.ofId = "of:1234"
159 self.o.portId = "1"
Matteo Scandolodfe75ff2018-06-15 10:52:09 -0700160 self.o.host_learning = False
Matteo Scandolo6739b512018-05-30 18:55:29 -0700161
162 expected_conf = {
163 "ports": {
164 "%s/%s" % (self.o.switch.ofId, self.o.portId): {
165 "interfaces": [
166 {
167 "name": intf1.name,
168 "ips": [intf1.ips],
169 "vlan-untagged": intf1.vlanUntagged
170 }
Matteo Scandolodfe75ff2018-06-15 10:52:09 -0700171 ],
172 "hostLearning": {
173 "enabled": self.o.host_learning
174 }
Matteo Scandolo6739b512018-05-30 18:55:29 -0700175 }
176 }
177 }
178
179 m.post("http://onos-fabric:8181/onos/v1/network/configuration/",
180 status_code=200,
181 additional_matcher=functools.partial(match_json, expected_conf))
182
183 with patch.object(Service.objects, "get") as onos_fabric_get:
184 onos_fabric_get.return_value = self.fabric
185
186 self.sync_step().sync_record(self.o)
187
188 self.assertTrue(m.called)
189
190 @requests_mock.Mocker()
191 def test_delete_port(self, m):
Matteo Scandolo6739b512018-05-30 18:55:29 -0700192
193 self.o.switch.ofId = "of:1234"
194 self.o.portId = "1"
195
Matteo Scandolodb7adc32018-06-15 16:05:19 -0700196 key = urllib.quote("of:1234/1", safe='')
197
198 m.delete("http://onos-fabric:8181/onos/v1/network/configuration/ports/%s" % key,
199 status_code=204)
200
Matteo Scandolo6739b512018-05-30 18:55:29 -0700201 with patch.object(Service.objects, "get") as onos_fabric_get:
202 onos_fabric_get.return_value = self.fabric
203
204 self.sync_step().delete_record(self.o)
205
Scott Bakerafdf11d2018-08-16 15:47:55 -0700206 self.assertTrue(m.called)