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