blob: f9faceaab6f242edfff557df41f2f248bc3a97f6 [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
Daniele Morofe693152019-09-20 16:53:40 -0700104 self.o.pipeconf = "pipeconf"
105 self.o.managementAddress = "192.168.100.123"
Matteo Scandolo6739b512018-05-30 18:55:29 -0700106
107 expected_conf = {
108 "devices": {
109 self.o.ofId: {
110 "basic": {
Scott Bakere9855012019-04-01 15:01:34 -0700111 "name": self.o.name,
Daniele Morofe693152019-09-20 16:53:40 -0700112 "driver": self.o.driver,
113 "pipeconf": self.o.pipeconf,
114 "managementAddress": self.o.managementAddress
Scott Bakere9855012019-04-01 15:01:34 -0700115 },
116 "segmentrouting": {
117 "name": self.o.name,
118 "ipv4NodeSid": self.o.ipv4NodeSid,
119 "ipv4Loopback": self.o.ipv4Loopback,
120 "routerMac": self.o.routerMac,
121 "isEdgeRouter": self.o.isEdgeRouter,
122 "adjacencySids": []
123 }
Matteo Scandolo6739b512018-05-30 18:55:29 -0700124 }
125 }
126 }
127
128 m.post("http://onos-fabric:8181/onos/v1/network/configuration/",
129 status_code=200,
130 additional_matcher=functools.partial(match_json, expected_conf))
131
132 with patch.object(Service.objects, "get") as onos_fabric_get:
133 onos_fabric_get.return_value = self.fabric
134
Scott Baker382366d2019-02-04 10:58:43 -0800135 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo6739b512018-05-30 18:55:29 -0700136
137 self.assertTrue(m.called)
138
139 @requests_mock.Mocker()
140 def test_delete_switch(self, m):
141 m.delete("http://onos-fabric:8181/onos/v1/network/configuration/devices/of:1234",
Scott Bakere9855012019-04-01 15:01:34 -0700142 status_code=204)
Matteo Scandolo6739b512018-05-30 18:55:29 -0700143
144 self.o.ofId = "of:1234"
145
146 with patch.object(Service.objects, "get") as onos_fabric_get:
147 onos_fabric_get.return_value = self.fabric
148
Scott Baker382366d2019-02-04 10:58:43 -0800149 self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
Matteo Scandolo6739b512018-05-30 18:55:29 -0700150
Scott Bakerafdf11d2018-08-16 15:47:55 -0700151 self.assertTrue(m.called)
Scott Baker382366d2019-02-04 10:58:43 -0800152
Scott Bakere9855012019-04-01 15:01:34 -0700153
Scott Baker382366d2019-02-04 10:58:43 -0800154if __name__ == '__main__':
Scott Bakere9855012019-04-01 15:01:34 -0700155 unittest.main()