blob: 4e939b8bcd7f32b6f09d59f475f003632dd2e9ec [file] [log] [blame]
Matteo Scandoloccef5782018-08-13 13:25:17 -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
Matteo Scandoloccef5782018-08-13 13:25:17 -070021test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Matteo Scandoloccef5782018-08-13 13:25:17 -070022
23class TestSyncOLTDevice(unittest.TestCase):
24
25 def setUp(self):
26
27 self.sys_path_save = sys.path
Matteo Scandoloccef5782018-08-13 13:25:17 -070028
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
Scott Baker71d20472019-02-01 12:05:35 -080036 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
37 mock_modelaccessor_config(test_path, [("att-workflow-driver", "att-workflow-driver.xproto"),
38 ("olt-service", "volt.xproto"),
Matteo Scandoload6c8942019-02-14 13:02:08 -080039 ("rcord", "rcord.xproto")])
Matteo Scandoloccef5782018-08-13 13:25:17 -070040
Scott Baker71d20472019-02-01 12:05:35 -080041 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
Matteo Scandoloccef5782018-08-13 13:25:17 -070048
49 # import all class names to globals
50 for (k, v) in model_accessor.all_model_classes.items():
51 globals()[k] = v
52
Scott Baker71d20472019-02-01 12:05:35 -080053 self.model_accessor = model_accessor
Matteo Scandoloccef5782018-08-13 13:25:17 -070054 self.log = Mock()
55
Scott Baker71d20472019-02-01 12:05:35 -080056 self.event_step = ONUEventStep(model_accessor=self.model_accessor, log=self.log)
Matteo Scandoloccef5782018-08-13 13:25:17 -070057
58 self.event = Mock()
59 self.event_dict = {
60 'status': 'activated',
61 'serial_number': 'BRCM1234',
Matteo Scandolo53da44c2018-08-14 16:04:18 -070062 'of_dpid': 'of:109299321',
63 'uni_port_id': 16
Matteo Scandoloccef5782018-08-13 13:25:17 -070064 }
65 self.event.value = json.dumps(self.event_dict)
66
Matteo Scandolo242c2962018-08-28 16:50:47 -070067 self.att = AttWorkflowDriverService(name="att-workflow-driver")
68
Matteo Scandoloccef5782018-08-13 13:25:17 -070069 def tearDown(self):
70 sys.path = self.sys_path_save
71
72
73 def test_create_instance(self):
74
75 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock , \
Matteo Scandolo242c2962018-08-28 16:50:47 -070076 patch.object(AttWorkflowDriverService.objects, "get_items") as service_mock, \
Matteo Scandoloccef5782018-08-13 13:25:17 -070077 patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
78
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070079 att_si_mock.return_value = []
Matteo Scandolo242c2962018-08-28 16:50:47 -070080 service_mock.return_value = [self.att]
Matteo Scandoloccef5782018-08-13 13:25:17 -070081
82 self.event_step.process_event(self.event)
83
84 att_si = mock_save.call_args[0][0]
85
86 self.assertEqual(mock_save.call_count, 1)
87
88 self.assertEqual(att_si.serial_number, self.event_dict['serial_number'])
89 self.assertEqual(att_si.of_dpid, self.event_dict['of_dpid'])
Matteo Scandolo53da44c2018-08-14 16:04:18 -070090 self.assertEqual(att_si.uni_port_id, self.event_dict['uni_port_id'])
Matteo Scandolo242c2962018-08-28 16:50:47 -070091 self.assertEqual(att_si.onu_state, "ENABLED")
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070092
93 def test_reuse_instance(self):
94
95 si = AttWorkflowDriverServiceInstance(
96 serial_number=self.event_dict["serial_number"],
97 of_dpid="foo",
98 uni_port_id="foo"
99 )
100
101 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock , \
102 patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
103
104 att_si_mock.return_value = [si]
105
106 self.event_step.process_event(self.event)
107
108 att_si = mock_save.call_args[0][0]
109
110 self.assertEqual(mock_save.call_count, 1)
111
112 self.assertEqual(att_si.serial_number, self.event_dict['serial_number'])
113 self.assertEqual(att_si.of_dpid, self.event_dict['of_dpid'])
114 self.assertEqual(att_si.uni_port_id, self.event_dict['uni_port_id'])
Matteo Scandolo242c2962018-08-28 16:50:47 -0700115 self.assertEqual(att_si.onu_state, "ENABLED")
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700116
117 def test_disable_onu(self):
118 self.event_dict = {
119 'status': 'disabled',
120 'serial_number': 'BRCM1234',
121 'of_dpid': 'of:109299321',
122 'uni_port_id': 16
123 }
124 self.event.value = json.dumps(self.event_dict)
125
Matteo Scandolo7e22ca12019-03-01 11:57:03 -0800126 with patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700127
128 self.event_step.process_event(self.event)
129
Matteo Scandolo7e22ca12019-03-01 11:57:03 -0800130 self.assertEqual(mock_save.call_count, 0)
131
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700132
133if __name__ == '__main__':
Scott Baker71d20472019-02-01 12:05:35 -0800134 sys.path.append("..") # for import of helpers.py
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700135 unittest.main()