blob: bea8c0cfc32d56731bd38801d7ae05554d57b73e [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
21# Hack to load synchronizer framework
22test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23xos_dir=os.path.join(test_path, "../../..")
24if not os.path.exists(os.path.join(test_path, "new_base")):
25 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
26 services_dir = os.path.join(xos_dir, "../../xos_services")
27sys.path.append(xos_dir)
28sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
29# END Hack to load synchronizer framework
30
31# generate model from xproto
32def get_models_fn(service_name, xproto_name):
33 name = os.path.join(service_name, "xos", xproto_name)
34 if os.path.exists(os.path.join(services_dir, name)):
35 return name
36 else:
37 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
38 if os.path.exists(os.path.join(services_dir, name)):
39 return name
40 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
41# END generate model from xproto
42
43class TestSyncOLTDevice(unittest.TestCase):
44
45 def setUp(self):
46
47 self.sys_path_save = sys.path
48 sys.path.append(xos_dir)
49 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
50
51 # Setting up the config module
52 from xosconfig import Config
53 config = os.path.join(test_path, "../test_config.yaml")
54 Config.clear()
55 Config.init(config, "synchronizer-config-schema.yaml")
56 # END Setting up the config module
57
58 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
59 # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
60
61 # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order
62 # and apparently it is not overriding the generated model accessor
63 build_mock_modelaccessor(xos_dir, services_dir, [
64 get_models_fn("att-workflow-driver", "att-workflow-driver.xproto"),
65 get_models_fn("olt-service", "volt.xproto"),
66 get_models_fn("../profiles/rcord", "rcord.xproto")
67 ])
68 import synchronizers.new_base.modelaccessor
69 from onu_event import ONUEventStep, model_accessor
70
71 # import all class names to globals
72 for (k, v) in model_accessor.all_model_classes.items():
73 globals()[k] = v
74
75 self.log = Mock()
76
77 self.event_step = ONUEventStep(self.log)
78
79 self.event = Mock()
80 self.event_dict = {
81 'status': 'activated',
82 'serial_number': 'BRCM1234',
Matteo Scandolo53da44c2018-08-14 16:04:18 -070083 'of_dpid': 'of:109299321',
84 'uni_port_id': 16
Matteo Scandoloccef5782018-08-13 13:25:17 -070085 }
86 self.event.value = json.dumps(self.event_dict)
87
Matteo Scandolo242c2962018-08-28 16:50:47 -070088 self.att = AttWorkflowDriverService(name="att-workflow-driver")
89
Matteo Scandoloccef5782018-08-13 13:25:17 -070090 def tearDown(self):
91 sys.path = self.sys_path_save
92
93
94 def test_create_instance(self):
95
96 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock , \
Matteo Scandolo242c2962018-08-28 16:50:47 -070097 patch.object(AttWorkflowDriverService.objects, "get_items") as service_mock, \
Matteo Scandoloccef5782018-08-13 13:25:17 -070098 patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
99
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700100 att_si_mock.return_value = []
Matteo Scandolo242c2962018-08-28 16:50:47 -0700101 service_mock.return_value = [self.att]
Matteo Scandoloccef5782018-08-13 13:25:17 -0700102
103 self.event_step.process_event(self.event)
104
105 att_si = mock_save.call_args[0][0]
106
107 self.assertEqual(mock_save.call_count, 1)
108
109 self.assertEqual(att_si.serial_number, self.event_dict['serial_number'])
110 self.assertEqual(att_si.of_dpid, self.event_dict['of_dpid'])
Matteo Scandolo53da44c2018-08-14 16:04:18 -0700111 self.assertEqual(att_si.uni_port_id, self.event_dict['uni_port_id'])
Matteo Scandolo242c2962018-08-28 16:50:47 -0700112 self.assertEqual(att_si.onu_state, "ENABLED")
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700113
114 def test_reuse_instance(self):
115
116 si = AttWorkflowDriverServiceInstance(
117 serial_number=self.event_dict["serial_number"],
118 of_dpid="foo",
119 uni_port_id="foo"
120 )
121
122 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock , \
123 patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
124
125 att_si_mock.return_value = [si]
126
127 self.event_step.process_event(self.event)
128
129 att_si = mock_save.call_args[0][0]
130
131 self.assertEqual(mock_save.call_count, 1)
132
133 self.assertEqual(att_si.serial_number, self.event_dict['serial_number'])
134 self.assertEqual(att_si.of_dpid, self.event_dict['of_dpid'])
135 self.assertEqual(att_si.uni_port_id, self.event_dict['uni_port_id'])
Matteo Scandolo242c2962018-08-28 16:50:47 -0700136 self.assertEqual(att_si.onu_state, "ENABLED")
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700137
138 def test_disable_onu(self):
139 self.event_dict = {
140 'status': 'disabled',
141 'serial_number': 'BRCM1234',
142 'of_dpid': 'of:109299321',
143 'uni_port_id': 16
144 }
145 self.event.value = json.dumps(self.event_dict)
146
147 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock , \
Matteo Scandolo242c2962018-08-28 16:50:47 -0700148 patch.object(AttWorkflowDriverService.objects, "get_items") as service_mock, \
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700149 patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
150
151 att_si_mock.return_value = []
Matteo Scandolo242c2962018-08-28 16:50:47 -0700152 service_mock.return_value = [self.att]
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700153
154 self.event_step.process_event(self.event)
155
156 att_si = mock_save.call_args[0][0]
157
158 self.assertEqual(mock_save.call_count, 1)
159
160 self.assertEqual(att_si.serial_number, self.event_dict['serial_number'])
161 self.assertEqual(att_si.of_dpid, self.event_dict['of_dpid'])
162 self.assertEqual(att_si.uni_port_id, self.event_dict['uni_port_id'])
163 self.assertEqual(att_si.onu_state, "DISABLED")
164
165if __name__ == '__main__':
166 unittest.main()