Matteo Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import unittest |
| 16 | |
| 17 | import functools |
| 18 | from mock import patch, call, Mock, PropertyMock |
| 19 | import requests_mock |
| 20 | import multistructlog |
| 21 | from multistructlog import create_logger |
| 22 | |
| 23 | import os, sys |
| 24 | |
| 25 | # Hack to load synchronizer framework |
| 26 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 27 | xos_dir=os.path.join(test_path, "../../..") |
| 28 | if 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") |
| 31 | sys.path.append(xos_dir) |
| 32 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 33 | # END Hack to load synchronizer framework |
| 34 | |
| 35 | # generate model from xproto |
| 36 | def 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 | |
| 47 | def 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 | |
| 53 | class 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 Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 78 | self.sync_step = SyncRoutes |
| 79 | self.sync_step.log = Mock() |
| 80 | |
Matteo Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 81 | # 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 Scandolo | 181140e | 2018-08-30 15:17:09 -0700 | [diff] [blame] | 94 | self.fabric.provider_services = [onos_fabric_base] |
Matteo Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 95 | |
| 96 | self.vrouter = Mock() |
| 97 | self.vrouter.name = "vrouter" |
Matteo Scandolo | 181140e | 2018-08-30 15:17:09 -0700 | [diff] [blame] | 98 | self.vrouter.provider_services = [self.fabric] |
Matteo Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 99 | |
| 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 Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 106 | def tearDown(self): |
| 107 | self.o = None |
| 108 | sys.path = self.sys_path_save |
| 109 | |
| 110 | @requests_mock.Mocker() |
Luca Prete | 9fae28c | 2018-11-01 10:59:52 -0700 | [diff] [blame^] | 111 | def test_sync_route_ipv4(self, m): |
Matteo Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 112 | |
Matteo Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 113 | self.o.prefix = "0.0.0.0/0" |
Luca Prete | 9fae28c | 2018-11-01 10:59:52 -0700 | [diff] [blame^] | 114 | self.o.next_hop = "192.168.0.254" |
Matteo Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 115 | |
| 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 Prete | 9fae28c | 2018-11-01 10:59:52 -0700 | [diff] [blame^] | 130 | def test_sync_route_ipv6(self, m): |
Matteo Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 131 | |
Luca Prete | 9fae28c | 2018-11-01 10:59:52 -0700 | [diff] [blame^] | 132 | 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 Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 151 | self.o.prefix = "0.0.0.0/0" |
Luca Prete | 9fae28c | 2018-11-01 10:59:52 -0700 | [diff] [blame^] | 152 | self.o.next_hop = "192.168.0.254" |
Matteo Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 153 | |
| 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 Prete | 9fae28c | 2018-11-01 10:59:52 -0700 | [diff] [blame^] | 165 | 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) |