blob: 2184a6d13d4398ab76a8715bdb7b0f60eb73663e [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
15import unittest
16
17import functools
18from mock import patch, call, Mock, PropertyMock
19import requests_mock
20import multistructlog
21from multistructlog import create_logger
22
23import os, sys
24
Matteo Scandolo6739b512018-05-30 18:55:29 -070025test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Matteo Scandolo6739b512018-05-30 18:55:29 -070026
27def 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
33class TestSyncFabricSwitch(unittest.TestCase):
34
35 def setUp(self):
36 global DeferredException
37
38 self.sys_path_save = sys.path
Matteo Scandolo6739b512018-05-30 18:55:29 -070039
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 Baker382366d2019-02-04 10:58:43 -080047 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
48 mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto")])
Matteo Scandolo6739b512018-05-30 18:55:29 -070049
Scott Baker382366d2019-02-04 10:58:43 -080050 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 Scandolo6739b512018-05-30 18:55:29 -070059
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 Bakerafdf11d2018-08-16 15:47:55 -070082 self.fabric.provider_services = [onos_fabric_base]
Matteo Scandolo6739b512018-05-30 18:55:29 -070083
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 Baker382366d2019-02-04 10:58:43 -0800130 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo6739b512018-05-30 18:55:29 -0700131
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 Baker382366d2019-02-04 10:58:43 -0800144 self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
Matteo Scandolo6739b512018-05-30 18:55:29 -0700145
Scott Bakerafdf11d2018-08-16 15:47:55 -0700146 self.assertTrue(m.called)
Scott Baker382366d2019-02-04 10:58:43 -0800147
148if __name__ == '__main__':
149 unittest.main()