blob: 00aded515a3583211dfacf626761fc35abe7c7c2 [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
16
17import 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"
97 self.fabric.subscriber_services = [onos_fabric_base]
98
99 # create a mock SwitchPort instance
100 self.o = Mock()
101 self.o.id = 1
102 self.o.tologdict.return_value = {}
103
104
105
106
107 def tearDown(self):
108 self.o = None
109 sys.path = self.sys_path_save
110
111 @requests_mock.Mocker()
112 def test_sync_port(self, m):
113 intf1 = Mock()
114 intf1.name = "intf1"
115 intf1.ips = "1.1.1.1/16"
116 intf1.vlanUntagged = None
117
118 self.o.interfaces.all.return_value = [intf1]
119 self.o.switch.ofId = "of:1234"
120 self.o.portId = "1"
121
122 expected_conf = {
123 "ports": {
124 "%s/%s" % (self.o.switch.ofId, self.o.portId): {
125 "interfaces": [
126 {
127 "name": intf1.name,
128 "ips": [ intf1.ips ]
129 }
130 ]
131 }
132 }
133 }
134
135 m.post("http://onos-fabric:8181/onos/v1/network/configuration/",
136 status_code=200,
137 additional_matcher=functools.partial(match_json, expected_conf))
138
139 with patch.object(Service.objects, "get") as onos_fabric_get:
140 onos_fabric_get.return_value = self.fabric
141
142 self.sync_step().sync_record(self.o)
143
144 self.assertTrue(m.called)
145
146 @requests_mock.Mocker()
147 def test_sync_port_with_vlan(self, m):
148 intf1 = Mock()
149 intf1.name = "intf1"
150 intf1.ips = "1.1.1.1/16"
151 intf1.vlanUntagged = 42
152
153 self.o.interfaces.all.return_value = [intf1]
154 self.o.switch.ofId = "of:1234"
155 self.o.portId = "1"
156
157 expected_conf = {
158 "ports": {
159 "%s/%s" % (self.o.switch.ofId, self.o.portId): {
160 "interfaces": [
161 {
162 "name": intf1.name,
163 "ips": [intf1.ips],
164 "vlan-untagged": intf1.vlanUntagged
165 }
166 ]
167 }
168 }
169 }
170
171 m.post("http://onos-fabric:8181/onos/v1/network/configuration/",
172 status_code=200,
173 additional_matcher=functools.partial(match_json, expected_conf))
174
175 with patch.object(Service.objects, "get") as onos_fabric_get:
176 onos_fabric_get.return_value = self.fabric
177
178 self.sync_step().sync_record(self.o)
179
180 self.assertTrue(m.called)
181
182 @requests_mock.Mocker()
183 def test_delete_port(self, m):
184 m.delete("http://onos-fabric:8181/onos/v1/network/configuration/ports/of:1234/1",
185 status_code=204)
186
187 self.o.switch.ofId = "of:1234"
188 self.o.portId = "1"
189
190 with patch.object(Service.objects, "get") as onos_fabric_get:
191 onos_fabric_get.return_value = self.fabric
192
193 self.sync_step().delete_record(self.o)
194
195 self.assertTrue(m.called)