blob: 3209fe3701ad41773e628e79dbd45d3cdb295dff [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
110 def test_validate_c_tag(self):
111 """
112 check that other subscriber attached to the same ONU don't have the same c_tag
113 """
114
115 s = Mock()
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700116 s.c_tag = 111
Matteo Scandolocc94e902018-05-22 15:25:25 -0700117 s.onu_device = "BRCM1234"
118
119 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
120
121 with self.assertRaises(Exception) as e:
122 self.rcord_subscriber.save()
123
124 self.assertEqual(e.exception.message, "The c_tag you specified (111) has already been used on device BRCM1234")
125 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
126
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700127 def test_validate_c_tag_on_update(self):
128 s = Mock()
129 s.c_tag = 111
130 s.onu_device = "BRCM1234"
131 s.id = 1
132
133 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
134
135 self.rcord_subscriber.is_new = False
136 self.rcord_subscriber.id = 1
137 self.rcord_subscriber.save()
138
139 self.models_decl.RCORDSubscriber_decl.save.assert_called()
140
141 def test_validate_c_tag_on_update_fail(self):
142 s = Mock()
143 s.c_tag = 222
144 s.onu_device = "BRCM1234"
145 s.id = 2
146
147 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
148
149 self.rcord_subscriber.id = 1
150 self.rcord_subscriber.is_new = False
151 self.rcord_subscriber.c_tag = 222
152 with self.assertRaises(Exception) as e:
153 self.rcord_subscriber.save()
154
155 self.assertEqual(e.exception.message, "The c_tag you specified (222) has already been used on device BRCM1234")
156 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
Matteo Scandolocc94e902018-05-22 15:25:25 -0700157
158 def test_generate_c_tag(self):
159 s = Mock()
160 s.c_tag = "111"
161 s.onu_device = "BRCM1234"
162
163 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
164 self.rcord_subscriber.c_tag = None
165
166 self.rcord_subscriber.save()
167
168 self.models_decl.RCORDSubscriber_decl.save.assert_called()
169 self.assertNotEquals(self.rcord_subscriber.c_tag, "111")
170 self.assertGreater(self.rcord_subscriber.c_tag, 16)
171 self.assertLess(self.rcord_subscriber.c_tag, 4097)
172
Matteo Scandoloc348b3f2018-07-29 09:35:11 -0700173 def test_generate_s_tag(self):
174 self.rcord_subscriber.c_tag = None
175
176 self.rcord_subscriber.save()
177
178 self.models_decl.RCORDSubscriber_decl.save.assert_called()
179 self.assertNotEqual(self.rcord_subscriber.s_tag, None)
180
181 def test_provisioned_s_stag(self):
182 self.rcord_subscriber.save()
183 self.models_decl.RCORDSubscriber_decl.save.assert_called()
184 self.assertEqual(self.rcord_subscriber.s_tag, 222)
185
186
Matteo Scandolocc94e902018-05-22 15:25:25 -0700187
188if __name__ == '__main__':
189 unittest.main()