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