blob: d64272f921f9cec220e0dae042cf84bc5095742a [file] [log] [blame]
Matteo Scandolo769a5a42018-05-31 16:43:39 -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 TestSyncRoutes(unittest.TestCase):
54
55 def setUp(self):
56
57 self.sys_path_save = sys.path
58 sys.path.append(xos_dir)
59 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
60
61 # Setting up the config module
62 from xosconfig import Config
63 config = os.path.join(test_path, "../test_config.yaml")
64 Config.clear()
65 Config.init(config, "synchronizer-config-schema.yaml")
66 # END Setting up the config module
67
68 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
69 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("vrouter", "vrouter.xproto")])
70 import synchronizers.new_base.modelaccessor
71
72 from sync_routes import SyncRoutes, model_accessor
73
74 # import all class names to globals
75 for (k, v) in model_accessor.all_model_classes.items():
76 globals()[k] = v
77
Matteo Scandolo769a5a42018-05-31 16:43:39 -070078 self.sync_step = SyncRoutes
79 self.sync_step.log = Mock()
80
Matteo Scandolo769a5a42018-05-31 16:43:39 -070081 # mock onos-fabric
82 onos_fabric = Mock()
83 onos_fabric.name = "onos-fabric"
84 onos_fabric.rest_hostname = "onos-fabric"
85 onos_fabric.rest_port = "8181"
86 onos_fabric.rest_username = "onos"
87 onos_fabric.rest_password = "rocks"
88
89 onos_fabric_base = Mock()
90 onos_fabric_base.leaf_model = onos_fabric
91
92 self.fabric = Mock()
93 self.fabric.name = "fabric"
Matteo Scandolo181140e2018-08-30 15:17:09 -070094 self.fabric.provider_services = [onos_fabric_base]
Matteo Scandolo769a5a42018-05-31 16:43:39 -070095
96 self.vrouter = Mock()
97 self.vrouter.name = "vrouter"
Matteo Scandolo181140e2018-08-30 15:17:09 -070098 self.vrouter.provider_services = [self.fabric]
Matteo Scandolo769a5a42018-05-31 16:43:39 -070099
100 # create a mock VRouterStaticRoute instance
101 self.o = Mock()
102 self.o.id = 1
103 self.o.vrouter.owner = self.vrouter
104 self.o.tologdict.return_value = {}
105
Matteo Scandolo769a5a42018-05-31 16:43:39 -0700106 def tearDown(self):
107 self.o = None
108 sys.path = self.sys_path_save
109
110 @requests_mock.Mocker()
Luca Prete9fae28c2018-11-01 10:59:52 -0700111 def test_sync_route_ipv4(self, m):
Matteo Scandolo769a5a42018-05-31 16:43:39 -0700112
Matteo Scandolo769a5a42018-05-31 16:43:39 -0700113 self.o.prefix = "0.0.0.0/0"
Luca Prete9fae28c2018-11-01 10:59:52 -0700114 self.o.next_hop = "192.168.0.254"
Matteo Scandolo769a5a42018-05-31 16:43:39 -0700115
116 expected_conf = {
117 "prefix": self.o.prefix,
118 "nextHop": self.o.next_hop
119 }
120
121 m.post("http://onos-fabric:8181/onos/routeservice/routes",
122 status_code=204,
123 additional_matcher=functools.partial(match_json, expected_conf))
124
125 self.sync_step().sync_record(self.o)
126
127 self.assertTrue(m.called)
128
129 @requests_mock.Mocker()
Luca Prete9fae28c2018-11-01 10:59:52 -0700130 def test_sync_route_ipv6(self, m):
Matteo Scandolo769a5a42018-05-31 16:43:39 -0700131
Luca Prete9fae28c2018-11-01 10:59:52 -0700132 self.o.prefix = "::/0"
133 self.o.next_hop = "2001:db8:abcd:0012::0/64"
134
135 expected_conf = {
136 "prefix": self.o.prefix,
137 "nextHop": self.o.next_hop
138 }
139
140 m.post("http://onos-fabric:8181/onos/routeservice/routes",
141 status_code=204,
142 additional_matcher=functools.partial(match_json, expected_conf))
143
144 self.sync_step().sync_record(self.o)
145
146 self.assertTrue(m.called)
147
148 @requests_mock.Mocker()
149 def test_delete_route_ipv4(self, m):
150
Matteo Scandolo769a5a42018-05-31 16:43:39 -0700151 self.o.prefix = "0.0.0.0/0"
Luca Prete9fae28c2018-11-01 10:59:52 -0700152 self.o.next_hop = "192.168.0.254"
Matteo Scandolo769a5a42018-05-31 16:43:39 -0700153
154 expected_conf = {
155 "prefix": self.o.prefix,
156 "nextHop": self.o.next_hop
157 }
158
159 m.delete("http://onos-fabric:8181/onos/routeservice/routes",
160 status_code=204,
161 additional_matcher=functools.partial(match_json, expected_conf))
162
163 self.sync_step().delete_record(self.o)
164
Luca Prete9fae28c2018-11-01 10:59:52 -0700165 self.assertTrue(m.called)
166
167 @requests_mock.Mocker()
168 def test_delete_route_ipv6(self, m):
169
170 self.o.prefix = "::/0"
171 self.o.next_hop = "2001:db8:abcd:0012::0/64"
172
173 expected_conf = {
174 "prefix": self.o.prefix,
175 "nextHop": self.o.next_hop
176 }
177
178 m.delete("http://onos-fabric:8181/onos/routeservice/routes",
179 status_code=204,
180 additional_matcher=functools.partial(match_json, expected_conf))
181
182 self.sync_step().delete_record(self.o)
183
184 self.assertTrue(m.called)