blob: 2496f9df4e8f9170110bce6ecc9cbc635d7387bb [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
Andy Bavier0ce0dae2019-05-10 17:46:02 -070018from mock import patch
Andy Bavier561f3e52019-03-15 10:46:05 -070019
Andy Bavier0ce0dae2019-05-10 17:46:02 -070020import os
21import sys
Andy Bavier561f3e52019-03-15 10:46:05 -070022
Andy Bavier0ce0dae2019-05-10 17:46:02 -070023test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Andy Bavier561f3e52019-03-15 10:46:05 -070024
25
26class TestModelPolicyTtWorkflowDriverServiceInstance(unittest.TestCase):
27 def setUp(self):
28
29 self.sys_path_save = sys.path
30
31 config = os.path.join(test_path, "../test_config.yaml")
32 from xosconfig import Config
33 Config.clear()
34 Config.init(config, 'synchronizer-config-schema.yaml')
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 model_policy_tt_workflow_driver_serviceinstance import TtWorkflowDriverServiceInstancePolicy, TtHelpers
48 self.TtHelpers = TtHelpers
49
Andy Bavier561f3e52019-03-15 10:46:05 -070050 # import all class names to globals
51 for (k, v) in model_accessor.all_model_classes.items():
52 globals()[k] = v
53
Andy Bavier0ce0dae2019-05-10 17:46:02 -070054 # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to
55 # creation of tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
Andy Bavier561f3e52019-03-15 10:46:05 -070056 model_accessor.reset_all_object_stores()
57
58
59 self.policy = TtWorkflowDriverServiceInstancePolicy(model_accessor=model_accessor)
60 self.si = TtWorkflowDriverServiceInstance()
61 self.si.owner = TtWorkflowDriverService()
62 self.si.serial_number = "BRCM1234"
63
64 def tearDown(self):
65 sys.path = self.sys_path_save
66
67 def test_update_onu(self):
68
69 onu = ONUDevice(
70 serial_number="BRCM1234",
71 admin_state="ENABLED"
72 )
73 with patch.object(ONUDevice.objects, "get_items") as get_onu, \
Andy Bavier0ce0dae2019-05-10 17:46:02 -070074 patch.object(onu, "save") as onu_save:
Andy Bavier561f3e52019-03-15 10:46:05 -070075 get_onu.return_value = [onu]
76
77 self.policy.update_onu("brcm1234", "ENABLED")
78 onu_save.assert_not_called()
79
80 self.policy.update_onu("brcm1234", "DISABLED")
81 self.assertEqual(onu.admin_state, "DISABLED")
Andy Bavier0ce0dae2019-05-10 17:46:02 -070082 onu_save.assert_called_with(
83 always_update_timestamp=True, update_fields=[
84 'admin_state', 'serial_number', 'updated'])
Andy Bavier561f3e52019-03-15 10:46:05 -070085
86 def test_enable_onu(self):
87 with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \
Andy Bavier0ce0dae2019-05-10 17:46:02 -070088 patch.object(self.policy, "update_onu") as update_onu:
Andy Bavier561f3e52019-03-15 10:46:05 -070089 validate_onu.return_value = [True, "valid onu"]
90
91 self.policy.process_onu_state(self.si)
92
93 update_onu.assert_called_once()
94 update_onu.assert_called_with("BRCM1234", "ENABLED")
95
96 self.assertIn("valid onu", self.si.status_message)
97
98 def test_disable_onu(self):
99 with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700100 patch.object(self.policy, "update_onu") as update_onu:
Andy Bavier561f3e52019-03-15 10:46:05 -0700101 validate_onu.return_value = [False, "invalid onu"]
102
103 self.policy.process_onu_state(self.si)
104
105 update_onu.assert_called_once()
106 update_onu.assert_called_with("BRCM1234", "DISABLED")
107
108 self.assertIn("invalid onu", self.si.status_message)
109
110 def test_handle_update_validate_onu(self):
111 """
112 Testing that handle_update calls validate_onu with the correct parameters
113 when necessary
114 """
115 with patch.object(self.policy, "process_onu_state") as process_onu_state, \
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700116 patch.object(self.policy, "update_onu") as update_onu, \
117 patch.object(self.policy, "get_subscriber") as get_subscriber:
Andy Bavier561f3e52019-03-15 10:46:05 -0700118 update_onu.return_value = None
119 get_subscriber.return_value = None
120
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700121 self.si.admin_onu_state = "AWAITING"
122 self.si.oper_onu_status = "AWAITING"
Andy Bavier561f3e52019-03-15 10:46:05 -0700123 self.policy.handle_update(self.si)
124 process_onu_state.assert_called_with(self.si)
125
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700126 self.si.admin_onu_state = "ENABLED"
127 self.si.oper_onu_status = "ENABLED"
Andy Bavier561f3e52019-03-15 10:46:05 -0700128 self.policy.handle_update(self.si)
129 process_onu_state.assert_called_with(self.si)
130
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700131 self.si.admin_onu_state = "DISABLED"
132 self.si.oper_onu_status = "DISABLED"
Andy Bavier561f3e52019-03-15 10:46:05 -0700133 self.policy.handle_update(self.si)
134 process_onu_state.assert_called_with(self.si)
135
Andy Bavier561f3e52019-03-15 10:46:05 -0700136 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:
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700163 self.si.admin_onu_state = "ENABLED"
Andy Bavier561f3e52019-03-15 10:46:05 -0700164 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
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700171 self.si.admin_onu_state = "DISABLED"
Andy Bavier561f3e52019-03-15 10:46:05 -0700172 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"
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700186 self.si.admin_onu_state = "ENABLED"
Andy Bavier561f3e52019-03-15 10:46:05 -0700187 self.policy.update_subscriber(sub, self.si)
188 sub_save.assert_not_called()
189
190 sub.status = "awaiting-auth"
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700191 self.si.admin_onu_state = "DISABLED"
Andy Bavier561f3e52019-03-15 10:46:05 -0700192 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, \
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700211 patch.object(RCORDIpAddress.objects, "get_items") as get_ips, \
212 patch.object(ip, "save_changed_fields") as ip_mock:
Andy Bavier561f3e52019-03-15 10:46:05 -0700213
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, \
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700234 patch.object(RCORDIpAddress, "save", autospec=True) as ip_mock:
Andy Bavier561f3e52019-03-15 10:46:05 -0700235
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):
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700248 self.si.admin_onu_state = "DISABLED"
Andy Bavier561f3e52019-03-15 10:46:05 -0700249
250 sub = RCORDSubscriber(
251 onu_device="BRCM1234"
252 )
253
254 with patch.object(self.policy, "get_subscriber") as get_subscriber, \
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700255 patch.object(self.policy, "update_onu") as update_onu, \
256 patch.object(self.policy, "update_subscriber") as update_subscriber:
Andy Bavier561f3e52019-03-15 10:46:05 -0700257
258 get_subscriber.return_value = None
259 self.policy.handle_update(self.si)
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700260 update_onu.assert_called_with(sub.onu_device, "DISABLED")
Andy Bavier561f3e52019-03-15 10:46:05 -0700261 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__':
Andy Bavier0ce0dae2019-05-10 17:46:02 -0700269 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
Andy Bavier561f3e52019-03-15 10:46:05 -0700270 unittest.main()