blob: 61b0185e711d9dbc2b12b7aec08d25875d7d94b3 [file] [log] [blame]
Scott Baker91bf91b2019-03-15 16:13:51 -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
Scott Baker91bf91b2019-03-15 16:13:51 -070018import unittest
19import json
20from mock import patch, Mock
21
22import os
23import sys
24
25test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
26
27
28class TestOnosPortEvent(unittest.TestCase):
29
30 def setUp(self):
31 global DeferredException
32
33 self.sys_path_save = sys.path
34
35 # Setting up the config module
36 from xosconfig import Config
37 config = os.path.join(test_path, "../test_config.yaml")
38 Config.clear()
39 Config.init(config, "synchronizer-config-schema.yaml")
40 # END Setting up the config module
41
42 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
43 mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto"),
44 ("onos-service", "onos.xproto")])
45
46 import xossynchronizer.modelaccessor
47 import mock_modelaccessor
Scott Bakere9855012019-04-01 15:01:34 -070048 imp.reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
49 imp.reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
Scott Baker91bf91b2019-03-15 16:13:51 -070050
51 from xossynchronizer.modelaccessor import model_accessor
52 self.model_accessor = model_accessor
53
54 from mock_modelaccessor import MockObjectList
55 from onos_event import OnosPortEventStep
56
57 # import all class names to globals
58 for (k, v) in model_accessor.all_model_classes.items():
59 globals()[k] = v
60
61 self.event_step = OnosPortEventStep
62
63 self.fabric_service = FabricService(name="fabric",
64 id=1112,
65 backend_code=1,
66 backend_status="succeeded")
67
68 self.switch = Switch(name="switch1",
69 ofId="of:0000000000000001",
70 backend_code=1,
71 backend_status="succeeded")
72
73 self.port1 = SwitchPort(name="switch1port1",
74 switch=self.switch,
75 switch_id=self.switch.id,
76 portId="1",
77 oper_status=None,
78 backend_code=1,
79 backend_status="succeeded")
80
81 self.port2 = SwitchPort(name="switch1port2",
82 switch=self.switch,
83 switch_id=self.switch.id,
84 portId="2",
85 oper_status=None,
86 backend_code=1,
87 backend_status="succeeded")
88
89 self.switch.ports = MockObjectList([self.port1, self.port2])
90
91 self.log = Mock()
92
93 def tearDown(self):
94 sys.path = self.sys_path_save
95
96 def test_process_event_enable(self):
97 with patch.object(Switch.objects, "get_items") as switch_objects, \
Scott Bakere9855012019-04-01 15:01:34 -070098 patch.object(SwitchPort.objects, "get_items") as switchport_objects:
Scott Baker91bf91b2019-03-15 16:13:51 -070099 switch_objects.return_value = [self.switch]
100 switchport_objects.return_value = [self.port1, self.port2]
101
102 event_dict = {"deviceId": self.switch.ofId,
103 "portId": self.port1.portId,
104 "enabled": True}
105 event = Mock()
106 event.value = json.dumps(event_dict)
107
108 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
109 step.process_event(event)
110
111 self.assertEqual(self.port1.oper_status, "enabled")
112
113 def test_process_event_disable(self):
114 with patch.object(Switch.objects, "get_items") as switch_objects, \
Scott Bakere9855012019-04-01 15:01:34 -0700115 patch.object(SwitchPort.objects, "get_items") as switchport_objects:
Scott Baker91bf91b2019-03-15 16:13:51 -0700116 switch_objects.return_value = [self.switch]
117 switchport_objects.return_value = [self.port1, self.port2]
118
119 event_dict = {"deviceId": self.switch.ofId,
120 "portId": self.port1.portId,
121 "enabled": False}
122 event = Mock()
123 event.value = json.dumps(event_dict)
124
125 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
126 step.process_event(event)
127
128 self.assertEqual(self.port1.oper_status, "disabled")
129
130 def test_process_event_no_switch(self):
131 with patch.object(Switch.objects, "get_items") as switch_objects, \
Scott Bakere9855012019-04-01 15:01:34 -0700132 patch.object(SwitchPort.objects, "get_items") as switchport_objects:
Scott Baker91bf91b2019-03-15 16:13:51 -0700133 switch_objects.return_value = [self.switch]
134 switchport_objects.return_value = [self.port1, self.port2]
135
136 event_dict = {"deviceId": "doesnotexist",
137 "portId": self.port1.portId,
138 "enabled": True}
139 event = Mock()
140 event.value = json.dumps(event_dict)
141
142 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
143
144 step.process_event(event)
145
146 # should not have changed
147 self.assertEqual(self.port1.oper_status, None)
148
149 def test_process_event_no_port(self):
150 with patch.object(Switch.objects, "get_items") as switch_objects, \
Scott Bakere9855012019-04-01 15:01:34 -0700151 patch.object(SwitchPort.objects, "get_items") as switchport_objects:
Scott Baker91bf91b2019-03-15 16:13:51 -0700152 switch_objects.return_value = [self.switch]
153 switchport_objects.return_value = [self.port1, self.port2]
154
155 event_dict = {"deviceId": self.switch.ofId,
156 "portId": "doesnotexist",
157 "enabled": True}
158 event = Mock()
159 event.value = json.dumps(event_dict)
160
161 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
162
163 step.process_event(event)
164
165 # should not have changed
166 self.assertEqual(self.port1.oper_status, None)
167
168
169if __name__ == '__main__':
170 unittest.main()