blob: 1e66be2822ce4ffbc5029502565e79d33dc27d97 [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
49
50 modules = {
51 'xos.exceptions': self.xos.exceptions,
52 'models_decl': self.models_decl
53 }
54
55 self.module_patcher = patch.dict('sys.modules', modules)
56 self.module_patcher.start()
57
58 self.volt = Mock()
59
60 from models import RCORDSubscriber
61
62 self.rcord_subscriber_class = RCORDSubscriber
63
64 self.rcord_subscriber = RCORDSubscriber()
Matteo Scandoloaa37b422018-05-23 15:36:56 -070065 self.rcord_subscriber.id = None # this is a new model
66 self.rcord_subscriber.is_new = True
Matteo Scandolocc94e902018-05-22 15:25:25 -070067 self.rcord_subscriber.onu_device = "BRCM1234"
Matteo Scandoloaa37b422018-05-23 15:36:56 -070068 self.rcord_subscriber.c_tag = 111
Matteo Scandoloc348b3f2018-07-29 09:35:11 -070069 self.rcord_subscriber.s_tag = 222
Matteo Scandolocc94e902018-05-22 15:25:25 -070070 self.rcord_subscriber.ip_address = "1.1.1.1"
71 self.rcord_subscriber.mac_address = "00:AA:00:00:00:01"
72 self.rcord_subscriber.owner.leaf_model.access = "voltha"
73 self.rcord_subscriber.owner.provider_services = [self.volt]
74
Matteo Scandolo0813b882018-07-16 14:38:01 -040075 def tearDown(self):
76 sys.path = self.sys_path_save
Matteo Scandolocc94e902018-05-22 15:25:25 -070077
78 def test_save(self):
79 self.rcord_subscriber.save()
80 self.models_decl.RCORDSubscriber_decl.save.assert_called()
81
82 def test_validate_ip_address(self):
83 self.rcord_subscriber.ip_address = "invalid"
84 with self.assertRaises(Exception) as e:
85 self.rcord_subscriber.save()
86
87 self.assertEqual(e.exception.message, "The ip_address you specified (invalid) is not valid")
88 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
89
90 def test_validate_mac_address(self):
91 self.rcord_subscriber.mac_address = "invalid"
92 with self.assertRaises(Exception) as e:
93 self.rcord_subscriber.save()
94
95 self.assertEqual(e.exception.message, "The mac_address you specified (invalid) is not valid")
96 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
97
98 def test_valid_onu_device(self):
99 self.rcord_subscriber.save()
100 self.models_decl.RCORDSubscriber_decl.save.assert_called()
101
102 def test_invalid_onu_device(self):
103 self.volt.leaf_model.has_access_device.return_value = False
104 with self.assertRaises(Exception) as e:
105 self.rcord_subscriber.save()
106
107 self.assertEqual(e.exception.message, "The onu_device you specified (BRCM1234) does not exists")
108 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
109
Matteo Scandoloa1875602018-10-16 07:35:04 -0700110 def test_validate_c_tag_pass(self):
111 """
112 check that other subscriber attached to the same ONU don't have the same c_tag
113 """
114
115 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [self.rcord_subscriber]
116
117
118 self.rcord_subscriber.save()
119
120 self.models_decl.RCORDSubscriber_decl.save.assert_called()
121
122 def test_validate_c_tag_fail(self):
Matteo Scandolocc94e902018-05-22 15:25:25 -0700123 """
124 check that other subscriber attached to the same ONU don't have the same c_tag
125 """
126
127 s = Mock()
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700128 s.c_tag = 111
Matteo Scandolocc94e902018-05-22 15:25:25 -0700129 s.onu_device = "BRCM1234"
130
Matteo Scandoloa1875602018-10-16 07:35:04 -0700131 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s, self.rcord_subscriber]
Matteo Scandolocc94e902018-05-22 15:25:25 -0700132
133 with self.assertRaises(Exception) as e:
134 self.rcord_subscriber.save()
135
136 self.assertEqual(e.exception.message, "The c_tag you specified (111) has already been used on device BRCM1234")
137 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
138
Matteo Scandoloa1875602018-10-16 07:35:04 -0700139 def _test_validate_c_tag_on_same_s_tag(self):
140 """
141 check that other subscriber using the same s_tag don't have the same c_tag
142 """
143 s = Mock()
144 s.id = 123
145 s.c_tag = 111
146 s.s_tag = 222
147 s.onu_device = "BRCM1234"
148
149 with self.assertRaises(Exception) as e:
150 self.rcord_subscriber.save()
151
152 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")
153 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
154
155
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700156 def test_validate_c_tag_on_update(self):
157 s = Mock()
158 s.c_tag = 111
159 s.onu_device = "BRCM1234"
160 s.id = 1
161
162 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
163
164 self.rcord_subscriber.is_new = False
165 self.rcord_subscriber.id = 1
166 self.rcord_subscriber.save()
167
168 self.models_decl.RCORDSubscriber_decl.save.assert_called()
169
170 def test_validate_c_tag_on_update_fail(self):
171 s = Mock()
172 s.c_tag = 222
173 s.onu_device = "BRCM1234"
174 s.id = 2
175
176 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
177
178 self.rcord_subscriber.id = 1
179 self.rcord_subscriber.is_new = False
180 self.rcord_subscriber.c_tag = 222
181 with self.assertRaises(Exception) as e:
182 self.rcord_subscriber.save()
183
184 self.assertEqual(e.exception.message, "The c_tag you specified (222) has already been used on device BRCM1234")
185 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
Matteo Scandolocc94e902018-05-22 15:25:25 -0700186
187 def test_generate_c_tag(self):
188 s = Mock()
189 s.c_tag = "111"
190 s.onu_device = "BRCM1234"
191
192 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
193 self.rcord_subscriber.c_tag = None
194
195 self.rcord_subscriber.save()
196
197 self.models_decl.RCORDSubscriber_decl.save.assert_called()
198 self.assertNotEquals(self.rcord_subscriber.c_tag, "111")
199 self.assertGreater(self.rcord_subscriber.c_tag, 16)
200 self.assertLess(self.rcord_subscriber.c_tag, 4097)
201
Matteo Scandoloc348b3f2018-07-29 09:35:11 -0700202 def test_generate_s_tag(self):
203 self.rcord_subscriber.c_tag = None
204
205 self.rcord_subscriber.save()
206
207 self.models_decl.RCORDSubscriber_decl.save.assert_called()
208 self.assertNotEqual(self.rcord_subscriber.s_tag, None)
209
210 def test_provisioned_s_stag(self):
211 self.rcord_subscriber.save()
212 self.models_decl.RCORDSubscriber_decl.save.assert_called()
213 self.assertEqual(self.rcord_subscriber.s_tag, 222)
214
215
Matteo Scandolocc94e902018-05-22 15:25:25 -0700216
217if __name__ == '__main__':
218 unittest.main()