blob: c11812f7782d84bd38fb8286270919ea320ec121 [file] [log] [blame]
Andy Bavier561f3e52019-03-15 10:46:05 -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
16from mock import patch, call, Mock, PropertyMock
17import json
18
19import os, sys
20
21test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
22
23class TestSyncOLTDevice(unittest.TestCase):
24
25 def setUp(self):
26
27 self.sys_path_save = sys.path
28
29 # Setting up the config module
30 from xosconfig import Config
31 config = os.path.join(test_path, "../test_config.yaml")
32 Config.clear()
33 Config.init(config, "synchronizer-config-schema.yaml")
34 # END Setting up the config module
35
36 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
37 mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"),
38 ("olt-service", "volt.xproto"),
39 ("rcord", "rcord.xproto")])
40
41 import xossynchronizer.modelaccessor
42 import mock_modelaccessor
43 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
44 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
45
46 from xossynchronizer.modelaccessor import model_accessor
47 from onu_event import ONUEventStep
48
49 # import all class names to globals
50 for (k, v) in model_accessor.all_model_classes.items():
51 globals()[k] = v
52
53 self.model_accessor = model_accessor
54 self.log = Mock()
55
56 self.event_step = ONUEventStep(model_accessor=self.model_accessor, log=self.log)
57
58 self.event = Mock()
59 self.event_dict = {
60 'status': 'activated',
Hardik Windlassac3a1652019-03-30 20:43:20 +053061 'serialNumber': 'BRCM1234',
62 'deviceId': 'of:109299321',
63 'portNumber': '16'
Andy Bavier561f3e52019-03-15 10:46:05 -070064 }
65 self.event.value = json.dumps(self.event_dict)
66
67 self.tt = TtWorkflowDriverService(name="tt-workflow-driver")
68
69 def tearDown(self):
70 sys.path = self.sys_path_save
71
72
73 def test_create_instance(self):
74
75 with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as tt_si_mock , \
76 patch.object(TtWorkflowDriverService.objects, "get_items") as service_mock, \
77 patch.object(TtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
78
79 tt_si_mock.return_value = []
80 service_mock.return_value = [self.tt]
81
82 self.event_step.process_event(self.event)
83
84 tt_si = mock_save.call_args[0][0]
85
86 self.assertEqual(mock_save.call_count, 1)
87
Hardik Windlassac3a1652019-03-30 20:43:20 +053088 self.assertEqual(tt_si.serial_number, self.event_dict['serialNumber'])
89 self.assertEqual(tt_si.of_dpid, self.event_dict['deviceId'])
90 self.assertEqual(tt_si.uni_port_id, long(self.event_dict['portNumber']))
Andy Bavier561f3e52019-03-15 10:46:05 -070091 self.assertEqual(tt_si.onu_state, "ENABLED")
92
93 def test_reuse_instance(self):
94
95 si = TtWorkflowDriverServiceInstance(
Hardik Windlassac3a1652019-03-30 20:43:20 +053096 serial_number=self.event_dict["serialNumber"],
Andy Bavier561f3e52019-03-15 10:46:05 -070097 of_dpid="foo",
98 uni_port_id="foo"
99 )
100
101 with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as tt_si_mock , \
102 patch.object(TtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
103
104 tt_si_mock.return_value = [si]
105
106 self.event_step.process_event(self.event)
107
108 tt_si = mock_save.call_args[0][0]
109
110 self.assertEqual(mock_save.call_count, 1)
111
Hardik Windlassac3a1652019-03-30 20:43:20 +0530112 self.assertEqual(tt_si.serial_number, self.event_dict['serialNumber'])
113 self.assertEqual(tt_si.of_dpid, self.event_dict['deviceId'])
114 self.assertEqual(tt_si.uni_port_id, long(self.event_dict['portNumber']))
Andy Bavier561f3e52019-03-15 10:46:05 -0700115 self.assertEqual(tt_si.onu_state, "ENABLED")
116
117 def test_disable_onu(self):
118 self.event_dict = {
119 'status': 'disabled',
Hardik Windlassac3a1652019-03-30 20:43:20 +0530120 'serialNumber': 'BRCM1234',
121 'deviceId': 'of:109299321',
122 'portNumber': '16'
Andy Bavier561f3e52019-03-15 10:46:05 -0700123 }
124 self.event.value = json.dumps(self.event_dict)
125
126 with patch.object(TtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
127
128 self.event_step.process_event(self.event)
129
130 self.assertEqual(mock_save.call_count, 0)
131
132
133if __name__ == '__main__':
Andy Bavier289d7aa2019-04-25 10:00:28 -0700134 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) # for import of helpers.py
Hardik Windlassac3a1652019-03-30 20:43:20 +0530135 unittest.main()