blob: a067779997a97b5792b408259b50dc7f7cef11cd [file] [log] [blame]
Andy Bavier561f3e52019-03-15 10:46:05 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17import unittest
18from mock import patch, call, Mock, PropertyMock
19
20import os, sys
21
22test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23
24
25class TestModelPolicyTtWorkflowDriverServiceInstance(unittest.TestCase):
26 def setUp(self):
27
28 self.sys_path_save = sys.path
29
30 config = os.path.join(test_path, "../test_config.yaml")
31 from xosconfig import Config
32 Config.clear()
33 Config.init(config, 'synchronizer-config-schema.yaml')
34
35 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
36 mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"),
37 ("olt-service", "volt.xproto"),
38 ("rcord", "rcord.xproto")])
39
40 import xossynchronizer.modelaccessor
41 import mock_modelaccessor
42 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
43 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
44
45 from xossynchronizer.modelaccessor import model_accessor
46 from model_policy_tt_workflow_driver_serviceinstance import TtWorkflowDriverServiceInstancePolicy, TtHelpers
47 self.TtHelpers = TtHelpers
48
49 from mock_modelaccessor import MockObjectList
50
51 # import all class names to globals
52 for (k, v) in model_accessor.all_model_classes.items():
53 globals()[k] = v
54
55 # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
56 # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
57 model_accessor.reset_all_object_stores()
58
59
60 self.policy = TtWorkflowDriverServiceInstancePolicy(model_accessor=model_accessor)
61 self.si = TtWorkflowDriverServiceInstance()
62 self.si.owner = TtWorkflowDriverService()
63 self.si.serial_number = "BRCM1234"
64
65 def tearDown(self):
66 sys.path = self.sys_path_save
67
68 def test_update_onu(self):
69
70 onu = ONUDevice(
71 serial_number="BRCM1234",
72 admin_state="ENABLED"
73 )
74 with patch.object(ONUDevice.objects, "get_items") as get_onu, \
75 patch.object(onu, "save") as onu_save:
76 get_onu.return_value = [onu]
77
78 self.policy.update_onu("brcm1234", "ENABLED")
79 onu_save.assert_not_called()
80
81 self.policy.update_onu("brcm1234", "DISABLED")
82 self.assertEqual(onu.admin_state, "DISABLED")
83 onu_save.assert_called_with(always_update_timestamp=True, update_fields=['admin_state', 'serial_number', 'updated'])
84
85
86 def test_enable_onu(self):
87 with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \
88 patch.object(self.policy, "update_onu") as update_onu, \
89 patch.object(self.si, "save") as save_si:
90 validate_onu.return_value = [True, "valid onu"]
91
92 self.policy.process_onu_state(self.si)
93
94 update_onu.assert_called_once()
95 update_onu.assert_called_with("BRCM1234", "ENABLED")
96
97 self.assertIn("valid onu", self.si.status_message)
98
99 def test_disable_onu(self):
100 with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \
101 patch.object(self.policy, "update_onu") as update_onu, \
102 patch.object(self.si, "save") as save_si:
103 validate_onu.return_value = [False, "invalid onu"]
104
105 self.policy.process_onu_state(self.si)
106
107 update_onu.assert_called_once()
108 update_onu.assert_called_with("BRCM1234", "DISABLED")
109
110 self.assertIn("invalid onu", self.si.status_message)
111
112 def test_handle_update_validate_onu(self):
113 """
114 Testing that handle_update calls validate_onu with the correct parameters
115 when necessary
116 """
117 with patch.object(self.policy, "process_onu_state") as process_onu_state, \
118 patch.object(self.policy, "update_onu") as update_onu, \
119 patch.object(self.policy, "get_subscriber") as get_subscriber:
120 update_onu.return_value = None
121 get_subscriber.return_value = None
122
123 self.si.onu_state = "AWAITING"
124 self.policy.handle_update(self.si)
125 process_onu_state.assert_called_with(self.si)
126
127 self.si.onu_state = "ENABLED"
128 self.policy.handle_update(self.si)
129 process_onu_state.assert_called_with(self.si)
130
131 self.si.onu_state = "DISABLED"
132 self.policy.handle_update(self.si)
133 process_onu_state.assert_called_with(self.si)
134
135
136 def test_get_subscriber(self):
137
138 sub = RCORDSubscriber(
139 onu_device="BRCM1234"
140 )
141
142 with patch.object(RCORDSubscriber.objects, "get_items") as get_subscribers:
143 get_subscribers.return_value = [sub]
144
145 res = self.policy.get_subscriber("BRCM1234")
146 self.assertEqual(res, sub)
147
148 res = self.policy.get_subscriber("brcm1234")
149 self.assertEqual(res, sub)
150
151 res = self.policy.get_subscriber("foo")
152 self.assertEqual(res, None)
153
154 def test_update_subscriber(self):
155
156 sub = RCORDSubscriber(
157 onu_device="BRCM1234"
158 )
159
160 self.si.status_message = "some content"
161
162 with patch.object(sub, "save") as sub_save:
163 self.si.onu_state = "ENABLED"
164 sub.status = "awaiting-auth"
165 self.policy.update_subscriber(sub, self.si)
166 self.assertEqual(sub.status, "enabled")
167 sub_save.assert_called()
168 sub_save.reset_mock()
169 sub.status = None
170
171 self.si.onu_state = "DISABLED"
172 sub.status = "enabled"
173 self.policy.update_subscriber(sub, self.si)
174 self.assertEqual(sub.status, "awaiting-auth")
175 sub_save.assert_called()
176 sub_save.reset_mock()
177 sub.status = None
178
179 def test_update_subscriber_not(self):
180 sub = RCORDSubscriber(
181 onu_device="BRCM1234"
182 )
183
184 with patch.object(sub, "save") as sub_save:
185 sub.status = "enabled"
186 self.si.onu_state = "ENABLED"
187 self.policy.update_subscriber(sub, self.si)
188 sub_save.assert_not_called()
189
190 sub.status = "awaiting-auth"
191 self.si.onu_state = "DISABLED"
192 self.policy.update_subscriber(sub, self.si)
193 sub_save.assert_not_called()
194
195 def test_update_subscriber_dhcp_with_exiting_ip(self):
196 sub = RCORDSubscriber(
197 id=10,
198 onu_device="BRCM1234"
199 )
200
201 ip = RCORDIpAddress(
202 subscriber_id=sub.id,
203 ip='10.11.2.23'
204 )
205
206 self.si.dhcp_state = "DHCPACK"
207 self.si.ip_address = "10.11.2.23"
208 self.si.mac_address = "4321"
209
210 with patch.object(sub, "save") as sub_save, \
211 patch.object(RCORDIpAddress.objects, "get_items") as get_ips, \
212 patch.object(ip, "save_changed_fields") as ip_mock:
213
214 get_ips.return_value = [ip]
215 ip_mock.return_value = []
216
217 self.policy.update_subscriber(sub, self.si)
218 sub_save.assert_called()
219 self.assertEqual(sub.mac_address, self.si.mac_address)
220
221 ip_mock.assert_called_with()
222
223 def test_update_subscriber_dhcp_with_new_ip(self):
224 sub = RCORDSubscriber(
225 id=10,
226 onu_device="BRCM1234"
227 )
228
229 self.si.dhcp_state = "DHCPACK"
230 self.si.ip_address = "10.11.2.23"
231 self.si.mac_address = "4321"
232
233 with patch.object(sub, "save") as sub_save, \
234 patch.object(RCORDIpAddress, "save", autospec=True) as ip_mock:
235
236 ip_mock.return_value = []
237
238 self.policy.update_subscriber(sub, self.si)
239 sub_save.assert_called()
240 self.assertEqual(sub.mac_address, self.si.mac_address)
241
242 saved_ip = ip_mock.call_args[0][0]
243 self.assertEqual(saved_ip.ip, self.si.ip_address)
244 self.assertEqual(saved_ip.subscriber_id, sub.id)
245 self.assertEqual(saved_ip.description, "DHCP Assigned IP Address")
246
247 def test_handle_update_subscriber(self):
248 self.si.onu_state = "DISABLED"
249
250 sub = RCORDSubscriber(
251 onu_device="BRCM1234"
252 )
253
254 with patch.object(self.policy, "get_subscriber") as get_subscriber, \
255 patch.object(self.policy, "update_onu") as update_onu, \
256 patch.object(self.policy, "update_subscriber") as update_subscriber:
257
258 get_subscriber.return_value = None
259 self.policy.handle_update(self.si)
260 update_onu.assert_called_with(sub.onu_device, "DISABLED");
261 self.assertEqual(update_subscriber.call_count, 0)
262
263 get_subscriber.return_value = sub
264 self.policy.handle_update(self.si)
265 update_subscriber.assert_called_with(sub, self.si)
266
267
268if __name__ == '__main__':
269 unittest.main()
270