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