Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -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 | |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 25 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 26 | |
| 27 | def match_json(desired, req): |
| 28 | if desired!=req.json(): |
| 29 | raise Exception("Got request %s, but body is not matching" % req.url) |
| 30 | return False |
| 31 | return True |
| 32 | |
| 33 | class TestSyncFabricSwitch(unittest.TestCase): |
| 34 | |
| 35 | def setUp(self): |
| 36 | global DeferredException |
| 37 | |
| 38 | self.sys_path_save = sys.path |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 39 | |
| 40 | # Setting up the config module |
| 41 | from xosconfig import Config |
| 42 | config = os.path.join(test_path, "../test_config.yaml") |
| 43 | Config.clear() |
| 44 | Config.init(config, "synchronizer-config-schema.yaml") |
| 45 | # END Setting up the config module |
| 46 | |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 47 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 48 | mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto")]) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 49 | |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 50 | import xossynchronizer.modelaccessor |
| 51 | import mock_modelaccessor |
| 52 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
| 53 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 54 | |
| 55 | from xossynchronizer.modelaccessor import model_accessor |
| 56 | self.model_accessor = model_accessor |
| 57 | |
| 58 | from sync_fabric_switch import SyncFabricSwitch |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 59 | |
| 60 | # import all class names to globals |
| 61 | for (k, v) in model_accessor.all_model_classes.items(): |
| 62 | globals()[k] = v |
| 63 | |
| 64 | |
| 65 | self.sync_step = SyncFabricSwitch |
| 66 | self.sync_step.log = Mock() |
| 67 | |
| 68 | |
| 69 | # mock onos-fabric |
| 70 | onos_fabric = Mock() |
| 71 | onos_fabric.name = "onos-fabric" |
| 72 | onos_fabric.rest_hostname = "onos-fabric" |
| 73 | onos_fabric.rest_port = "8181" |
| 74 | onos_fabric.rest_username = "onos" |
| 75 | onos_fabric.rest_password = "rocks" |
| 76 | |
| 77 | onos_fabric_base = Mock() |
| 78 | onos_fabric_base.leaf_model = onos_fabric |
| 79 | |
| 80 | self.fabric = Mock() |
| 81 | self.fabric.name = "fabric" |
Scott Baker | afdf11d | 2018-08-16 15:47:55 -0700 | [diff] [blame] | 82 | self.fabric.provider_services = [onos_fabric_base] |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 83 | |
| 84 | # create a mock Switch instance |
| 85 | self.o = Mock() |
| 86 | self.o.name = "MockSwitch" |
| 87 | self.o.ofId = "of:1234" |
| 88 | |
| 89 | def tearDown(self): |
| 90 | self.o = None |
| 91 | sys.path = self.sys_path_save |
| 92 | |
| 93 | @requests_mock.Mocker() |
| 94 | def test_sync_switch(self, m): |
| 95 | |
| 96 | self.o.ofId = "of:1234" |
| 97 | self.o.portId = "1" |
| 98 | self.o.driver = "ofdpa3" |
| 99 | self.o.ipv4NodeSid = "17" |
| 100 | self.o.ipv4Loopback = "192.168.0.201" |
| 101 | self.o.routerMac = "00:00:02:01:06:01" |
| 102 | self.o.isEdgeRouter = False |
| 103 | |
| 104 | expected_conf = { |
| 105 | "devices": { |
| 106 | self.o.ofId: { |
| 107 | "basic": { |
| 108 | "name": self.o.name, |
| 109 | "driver": self.o.driver |
| 110 | }, |
| 111 | "segmentrouting" : { |
| 112 | "name" : self.o.name, |
| 113 | "ipv4NodeSid" : self.o.ipv4NodeSid, |
| 114 | "ipv4Loopback" : self.o.ipv4Loopback, |
| 115 | "routerMac" : self.o.routerMac, |
| 116 | "isEdgeRouter" : self.o.isEdgeRouter, |
| 117 | "adjacencySids" : [] |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | m.post("http://onos-fabric:8181/onos/v1/network/configuration/", |
| 124 | status_code=200, |
| 125 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 126 | |
| 127 | with patch.object(Service.objects, "get") as onos_fabric_get: |
| 128 | onos_fabric_get.return_value = self.fabric |
| 129 | |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 130 | self.sync_step(model_accessor=self.model_accessor).sync_record(self.o) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 131 | |
| 132 | self.assertTrue(m.called) |
| 133 | |
| 134 | @requests_mock.Mocker() |
| 135 | def test_delete_switch(self, m): |
| 136 | m.delete("http://onos-fabric:8181/onos/v1/network/configuration/devices/of:1234", |
| 137 | status_code=204) |
| 138 | |
| 139 | self.o.ofId = "of:1234" |
| 140 | |
| 141 | with patch.object(Service.objects, "get") as onos_fabric_get: |
| 142 | onos_fabric_get.return_value = self.fabric |
| 143 | |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 144 | self.sync_step(model_accessor=self.model_accessor).delete_record(self.o) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 145 | |
Scott Baker | afdf11d | 2018-08-16 15:47:55 -0700 | [diff] [blame] | 146 | self.assertTrue(m.called) |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 147 | |
| 148 | if __name__ == '__main__': |
| 149 | unittest.main() |