blob: 3d3c3f29236d2c120ff73271c633e28999bb6ea0 [file] [log] [blame]
Matteo Scandolocc94e902018-05-22 15:25:25 -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
Matteo Scandolo0813b882018-07-16 14:38:01 -040016import os, sys
Matteo Scandolocc94e902018-05-22 15:25:25 -070017from mock import patch, Mock, MagicMock
18
Matteo Scandolo0813b882018-07-16 14:38:01 -040019test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
20service_dir=os.path.join(test_path, "../../../..")
21xos_dir=os.path.join(test_path, "../../..")
22if not os.path.exists(os.path.join(test_path, "new_base")):
23 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
24 services_dir=os.path.join(xos_dir, "../../xos_services")
25
Matteo Scandolocc94e902018-05-22 15:25:25 -070026# mocking XOS exception, as they're based in Django
27class Exceptions:
28 XOSValidationError = Exception
29 XOSProgrammingError = Exception
30 XOSPermissionDenied = Exception
31
32class XOS:
33 exceptions = Exceptions
34
35class TestRCORDModels(unittest.TestCase):
36 def setUp(self):
Matteo Scandolo0813b882018-07-16 14:38:01 -040037
38 self.sys_path_save = sys.path
39 sys.path.append(xos_dir)
40
Matteo Scandolocc94e902018-05-22 15:25:25 -070041 self.xos = XOS
42
43 self.models_decl = Mock()
44 self.models_decl.RCORDSubscriber_decl = MagicMock
45 self.models_decl.RCORDSubscriber_decl.save = Mock()
46 self.models_decl.RCORDSubscriber_decl.objects = Mock()
47 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = []
48
Luca Preted6700ba2018-09-12 16:40:49 -070049 self.models_decl.RCORDIpAddress_decl = MagicMock
50 self.models_decl.RCORDIpAddress_decl.save = Mock()
51 self.models_decl.RCORDIpAddress_decl.objects = Mock()
52 self.models_decl.RCORDIpAddress_decl.objects.filter.return_value = []
53
Matteo Scandolocc94e902018-05-22 15:25:25 -070054
55 modules = {
56 'xos.exceptions': self.xos.exceptions,
57 'models_decl': self.models_decl
58 }
59
60 self.module_patcher = patch.dict('sys.modules', modules)
61 self.module_patcher.start()
62
63 self.volt = Mock()
64
Luca Preted6700ba2018-09-12 16:40:49 -070065 from models import RCORDSubscriber, RCORDIpAddress
Matteo Scandolocc94e902018-05-22 15:25:25 -070066
67 self.rcord_subscriber_class = RCORDSubscriber
68
69 self.rcord_subscriber = RCORDSubscriber()
Matteo Scandoloaa37b422018-05-23 15:36:56 -070070 self.rcord_subscriber.id = None # this is a new model
71 self.rcord_subscriber.is_new = True
Matteo Scandolocc94e902018-05-22 15:25:25 -070072 self.rcord_subscriber.onu_device = "BRCM1234"
Matteo Scandoloaa37b422018-05-23 15:36:56 -070073 self.rcord_subscriber.c_tag = 111
Matteo Scandoloc348b3f2018-07-29 09:35:11 -070074 self.rcord_subscriber.s_tag = 222
Luca Preted6700ba2018-09-12 16:40:49 -070075 self.rcord_subscriber.ips = Mock()
76 self.rcord_subscriber.ips.all.return_value = []
Matteo Scandolocc94e902018-05-22 15:25:25 -070077 self.rcord_subscriber.mac_address = "00:AA:00:00:00:01"
78 self.rcord_subscriber.owner.leaf_model.access = "voltha"
79 self.rcord_subscriber.owner.provider_services = [self.volt]
80
Luca Preted6700ba2018-09-12 16:40:49 -070081 self.rcord_ip = RCORDIpAddress()
82 self.rcord_ip.subscriber = 1;
83
Matteo Scandolo0813b882018-07-16 14:38:01 -040084 def tearDown(self):
85 sys.path = self.sys_path_save
Matteo Scandolocc94e902018-05-22 15:25:25 -070086
87 def test_save(self):
88 self.rcord_subscriber.save()
89 self.models_decl.RCORDSubscriber_decl.save.assert_called()
90
Luca Preted6700ba2018-09-12 16:40:49 -070091 def _test_validate_ipv4_address(self):
92 self.rcord_ip.ip = "192.168.0."
Matteo Scandolocc94e902018-05-22 15:25:25 -070093 with self.assertRaises(Exception) as e:
Luca Preted6700ba2018-09-12 16:40:49 -070094 self.rcord_ip.save()
Matteo Scandolocc94e902018-05-22 15:25:25 -070095
Luca Preted6700ba2018-09-12 16:40:49 -070096 self.assertEqual(e.exception.message, "The IP specified is not valid: 192.168.0.")
97 self.models_decl.RCORDIpAddress.save.assert_not_called()
98
99 def test_validate_ipv6_address(self):
100 self.rcord_ip.ip = "2001:0db8:85a3:0000:0000:8a2e:03"
101 with self.assertRaises(Exception) as e:
102 self.rcord_ip.save()
103
104 self.assertEqual(e.exception.message, "The IP specified is not valid: 2001:0db8:85a3:0000:0000:8a2e:03")
105 self.models_decl.RCORDIpAddress.save.assert_not_called()
Matteo Scandolocc94e902018-05-22 15:25:25 -0700106
107 def test_validate_mac_address(self):
108 self.rcord_subscriber.mac_address = "invalid"
109 with self.assertRaises(Exception) as e:
110 self.rcord_subscriber.save()
111
Luca Preted6700ba2018-09-12 16:40:49 -0700112 self.assertEqual(e.exception.message, "The MAC address specified is not valid: invalid")
Matteo Scandolocc94e902018-05-22 15:25:25 -0700113 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
114
115 def test_valid_onu_device(self):
116 self.rcord_subscriber.save()
117 self.models_decl.RCORDSubscriber_decl.save.assert_called()
118
119 def test_invalid_onu_device(self):
120 self.volt.leaf_model.has_access_device.return_value = False
121 with self.assertRaises(Exception) as e:
122 self.rcord_subscriber.save()
123
124 self.assertEqual(e.exception.message, "The onu_device you specified (BRCM1234) does not exists")
125 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
126
Matteo Scandoloa1875602018-10-16 07:35:04 -0700127 def test_validate_c_tag_pass(self):
128 """
129 check that other subscriber attached to the same ONU don't have the same c_tag
130 """
131
132 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [self.rcord_subscriber]
133
134
135 self.rcord_subscriber.save()
136
137 self.models_decl.RCORDSubscriber_decl.save.assert_called()
138
139 def test_validate_c_tag_fail(self):
Matteo Scandolocc94e902018-05-22 15:25:25 -0700140 """
141 check that other subscriber attached to the same ONU don't have the same c_tag
142 """
143
144 s = Mock()
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700145 s.c_tag = 111
Matteo Scandolocc94e902018-05-22 15:25:25 -0700146 s.onu_device = "BRCM1234"
147
Matteo Scandoloa1875602018-10-16 07:35:04 -0700148 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s, self.rcord_subscriber]
Matteo Scandolocc94e902018-05-22 15:25:25 -0700149
150 with self.assertRaises(Exception) as e:
151 self.rcord_subscriber.save()
152
153 self.assertEqual(e.exception.message, "The c_tag you specified (111) has already been used on device BRCM1234")
154 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
155
Matteo Scandoloa1875602018-10-16 07:35:04 -0700156 def _test_validate_c_tag_on_same_s_tag(self):
157 """
158 check that other subscriber using the same s_tag don't have the same c_tag
159 """
160 s = Mock()
161 s.id = 123
162 s.c_tag = 111
163 s.s_tag = 222
164 s.onu_device = "BRCM1234"
165
166 with self.assertRaises(Exception) as e:
167 self.rcord_subscriber.save()
168
169 self.assertEqual(e.exception.message, "The c_tag you specified (111) has already been used by Subscriber with id 123 and the same s_tag: 222")
170 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
171
172
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700173 def test_validate_c_tag_on_update(self):
174 s = Mock()
175 s.c_tag = 111
176 s.onu_device = "BRCM1234"
177 s.id = 1
178
179 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
180
181 self.rcord_subscriber.is_new = False
182 self.rcord_subscriber.id = 1
183 self.rcord_subscriber.save()
184
185 self.models_decl.RCORDSubscriber_decl.save.assert_called()
186
187 def test_validate_c_tag_on_update_fail(self):
188 s = Mock()
189 s.c_tag = 222
190 s.onu_device = "BRCM1234"
191 s.id = 2
192
193 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
194
195 self.rcord_subscriber.id = 1
196 self.rcord_subscriber.is_new = False
197 self.rcord_subscriber.c_tag = 222
198 with self.assertRaises(Exception) as e:
199 self.rcord_subscriber.save()
200
201 self.assertEqual(e.exception.message, "The c_tag you specified (222) has already been used on device BRCM1234")
202 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
Matteo Scandolocc94e902018-05-22 15:25:25 -0700203
204 def test_generate_c_tag(self):
205 s = Mock()
206 s.c_tag = "111"
207 s.onu_device = "BRCM1234"
208
209 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
210 self.rcord_subscriber.c_tag = None
211
212 self.rcord_subscriber.save()
213
214 self.models_decl.RCORDSubscriber_decl.save.assert_called()
215 self.assertNotEquals(self.rcord_subscriber.c_tag, "111")
216 self.assertGreater(self.rcord_subscriber.c_tag, 16)
217 self.assertLess(self.rcord_subscriber.c_tag, 4097)
218
Matteo Scandoloc348b3f2018-07-29 09:35:11 -0700219 def test_generate_s_tag(self):
220 self.rcord_subscriber.c_tag = None
221
222 self.rcord_subscriber.save()
223
224 self.models_decl.RCORDSubscriber_decl.save.assert_called()
225 self.assertNotEqual(self.rcord_subscriber.s_tag, None)
226
227 def test_provisioned_s_stag(self):
228 self.rcord_subscriber.save()
229 self.models_decl.RCORDSubscriber_decl.save.assert_called()
230 self.assertEqual(self.rcord_subscriber.s_tag, 222)
231
232
Matteo Scandolocc94e902018-05-22 15:25:25 -0700233
234if __name__ == '__main__':
235 unittest.main()