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 | |
| 78 | |
| 79 | self.sync_step = SyncRoutes |
| 80 | self.sync_step.log = Mock() |
| 81 | |
| 82 | |
| 83 | # mock onos-fabric |
| 84 | onos_fabric = Mock() |
| 85 | onos_fabric.name = "onos-fabric" |
| 86 | onos_fabric.rest_hostname = "onos-fabric" |
| 87 | onos_fabric.rest_port = "8181" |
| 88 | onos_fabric.rest_username = "onos" |
| 89 | onos_fabric.rest_password = "rocks" |
| 90 | |
| 91 | onos_fabric_base = Mock() |
| 92 | onos_fabric_base.leaf_model = onos_fabric |
| 93 | |
| 94 | self.fabric = Mock() |
| 95 | self.fabric.name = "fabric" |
| 96 | self.fabric.subscriber_services = [onos_fabric_base] |
| 97 | |
| 98 | self.vrouter = Mock() |
| 99 | self.vrouter.name = "vrouter" |
| 100 | self.vrouter.subscriber_services = [self.fabric] |
| 101 | |
| 102 | # create a mock VRouterStaticRoute instance |
| 103 | self.o = Mock() |
| 104 | self.o.id = 1 |
| 105 | self.o.vrouter.owner = self.vrouter |
| 106 | self.o.tologdict.return_value = {} |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | def tearDown(self): |
| 112 | self.o = None |
| 113 | sys.path = self.sys_path_save |
| 114 | |
| 115 | @requests_mock.Mocker() |
| 116 | def test_sync_route(self, m): |
| 117 | |
| 118 | self.o.next_hop = "192.168.0.254" |
| 119 | self.o.prefix = "0.0.0.0/0" |
| 120 | |
| 121 | expected_conf = { |
| 122 | "prefix": self.o.prefix, |
| 123 | "nextHop": self.o.next_hop |
| 124 | } |
| 125 | |
| 126 | m.post("http://onos-fabric:8181/onos/routeservice/routes", |
| 127 | status_code=204, |
| 128 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 129 | |
| 130 | self.sync_step().sync_record(self.o) |
| 131 | |
| 132 | self.assertTrue(m.called) |
| 133 | |
| 134 | @requests_mock.Mocker() |
| 135 | def test_delete_route(self, m): |
| 136 | |
| 137 | self.o.next_hop = "192.168.0.254" |
| 138 | self.o.prefix = "0.0.0.0/0" |
| 139 | |
| 140 | expected_conf = { |
| 141 | "prefix": self.o.prefix, |
| 142 | "nextHop": self.o.next_hop |
| 143 | } |
| 144 | |
| 145 | m.delete("http://onos-fabric:8181/onos/routeservice/routes", |
| 146 | status_code=204, |
| 147 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 148 | |
| 149 | self.sync_step().delete_record(self.o) |
| 150 | |
| 151 | self.assertTrue(m.called) |