blob: b749b94c63f793018fde060782b4928a4baf146d [file] [log] [blame]
Matteo Scandolo6739b512018-05-30 18:55:29 -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
Scott Bakere9855012019-04-01 15:01:34 -070015#from __future__ import absolute_import
16
17import imp
Matteo Scandolo6739b512018-05-30 18:55:29 -070018import unittest
Matteo Scandolo6739b512018-05-30 18:55:29 -070019import functools
Scott Bakere9855012019-04-01 15:01:34 -070020from mock import patch, Mock
Matteo Scandolo6739b512018-05-30 18:55:29 -070021import requests_mock
Matteo Scandolo6739b512018-05-30 18:55:29 -070022
Scott Bakere9855012019-04-01 15:01:34 -070023import os
24import sys
Matteo Scandolo6739b512018-05-30 18:55:29 -070025
Scott Bakere9855012019-04-01 15:01:34 -070026test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
27
Matteo Scandolo6739b512018-05-30 18:55:29 -070028
29def match_json(desired, req):
Scott Bakere9855012019-04-01 15:01:34 -070030 if desired != req.json():
Matteo Scandolo6739b512018-05-30 18:55:29 -070031 raise Exception("Got request %s, but body is not matching" % req.url)
32 return False
33 return True
34
Scott Bakere9855012019-04-01 15:01:34 -070035
Matteo Scandolo6739b512018-05-30 18:55:29 -070036class TestSyncFabricSwitch(unittest.TestCase):
37
38 def setUp(self):
39 global DeferredException
40
41 self.sys_path_save = sys.path
Matteo Scandolo6739b512018-05-30 18:55:29 -070042
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 Baker382366d2019-02-04 10:58:43 -080050 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
51 mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto")])
Matteo Scandolo6739b512018-05-30 18:55:29 -070052
Scott Baker382366d2019-02-04 10:58:43 -080053 import xossynchronizer.modelaccessor
54 import mock_modelaccessor
Scott Bakere9855012019-04-01 15:01:34 -070055 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 Baker382366d2019-02-04 10:58:43 -080057
58 from xossynchronizer.modelaccessor import model_accessor
59 self.model_accessor = model_accessor
60
61 from sync_fabric_switch import SyncFabricSwitch
Matteo Scandolo6739b512018-05-30 18:55:29 -070062
63 # import all class names to globals
64 for (k, v) in model_accessor.all_model_classes.items():
65 globals()[k] = v
66
Matteo Scandolo6739b512018-05-30 18:55:29 -070067 self.sync_step = SyncFabricSwitch
68 self.sync_step.log = Mock()
69
Matteo Scandolo6739b512018-05-30 18:55:29 -070070 # 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 Bakerafdf11d2018-08-16 15:47:55 -070083 self.fabric.provider_services = [onos_fabric_base]
Matteo Scandolo6739b512018-05-30 18:55:29 -070084
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 Bakere9855012019-04-01 15:01:34 -0700109 "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 Scandolo6739b512018-05-30 18:55:29 -0700120 }
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 Baker382366d2019-02-04 10:58:43 -0800131 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo6739b512018-05-30 18:55:29 -0700132
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 Bakere9855012019-04-01 15:01:34 -0700138 status_code=204)
Matteo Scandolo6739b512018-05-30 18:55:29 -0700139
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 Baker382366d2019-02-04 10:58:43 -0800145 self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
Matteo Scandolo6739b512018-05-30 18:55:29 -0700146
Scott Bakerafdf11d2018-08-16 15:47:55 -0700147 self.assertTrue(m.called)
Scott Baker382366d2019-02-04 10:58:43 -0800148
Scott Bakere9855012019-04-01 15:01:34 -0700149
Scott Baker382366d2019-02-04 10:58:43 -0800150if __name__ == '__main__':
Scott Bakere9855012019-04-01 15:01:34 -0700151 unittest.main()