blob: bbbbd18d470e6cdea1b998afc8a2d8283cb73086 [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 Scandolocc94e902018-05-22 15:25:25 -070069 self.rcord_subscriber.ip_address = "1.1.1.1"
70 self.rcord_subscriber.mac_address = "00:AA:00:00:00:01"
71 self.rcord_subscriber.owner.leaf_model.access = "voltha"
72 self.rcord_subscriber.owner.provider_services = [self.volt]
73
Matteo Scandolo0813b882018-07-16 14:38:01 -040074 def tearDown(self):
75 sys.path = self.sys_path_save
Matteo Scandolocc94e902018-05-22 15:25:25 -070076
77 def test_save(self):
78 self.rcord_subscriber.save()
79 self.models_decl.RCORDSubscriber_decl.save.assert_called()
80
81 def test_validate_ip_address(self):
82 self.rcord_subscriber.ip_address = "invalid"
83 with self.assertRaises(Exception) as e:
84 self.rcord_subscriber.save()
85
86 self.assertEqual(e.exception.message, "The ip_address you specified (invalid) is not valid")
87 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
88
89 def test_validate_mac_address(self):
90 self.rcord_subscriber.mac_address = "invalid"
91 with self.assertRaises(Exception) as e:
92 self.rcord_subscriber.save()
93
94 self.assertEqual(e.exception.message, "The mac_address you specified (invalid) is not valid")
95 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
96
97 def test_valid_onu_device(self):
98 self.rcord_subscriber.save()
99 self.models_decl.RCORDSubscriber_decl.save.assert_called()
100
101 def test_invalid_onu_device(self):
102 self.volt.leaf_model.has_access_device.return_value = False
103 with self.assertRaises(Exception) as e:
104 self.rcord_subscriber.save()
105
106 self.assertEqual(e.exception.message, "The onu_device you specified (BRCM1234) does not exists")
107 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
108
109 def test_validate_c_tag(self):
110 """
111 check that other subscriber attached to the same ONU don't have the same c_tag
112 """
113
114 s = Mock()
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700115 s.c_tag = 111
Matteo Scandolocc94e902018-05-22 15:25:25 -0700116 s.onu_device = "BRCM1234"
117
118 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
119
120 with self.assertRaises(Exception) as e:
121 self.rcord_subscriber.save()
122
123 self.assertEqual(e.exception.message, "The c_tag you specified (111) has already been used on device BRCM1234")
124 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
125
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700126 def test_validate_c_tag_on_update(self):
127 s = Mock()
128 s.c_tag = 111
129 s.onu_device = "BRCM1234"
130 s.id = 1
131
132 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
133
134 self.rcord_subscriber.is_new = False
135 self.rcord_subscriber.id = 1
136 self.rcord_subscriber.save()
137
138 self.models_decl.RCORDSubscriber_decl.save.assert_called()
139
140 def test_validate_c_tag_on_update_fail(self):
141 s = Mock()
142 s.c_tag = 222
143 s.onu_device = "BRCM1234"
144 s.id = 2
145
146 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
147
148 self.rcord_subscriber.id = 1
149 self.rcord_subscriber.is_new = False
150 self.rcord_subscriber.c_tag = 222
151 with self.assertRaises(Exception) as e:
152 self.rcord_subscriber.save()
153
154 self.assertEqual(e.exception.message, "The c_tag you specified (222) has already been used on device BRCM1234")
155 self.models_decl.RCORDSubscriber_decl.save.assert_not_called()
Matteo Scandolocc94e902018-05-22 15:25:25 -0700156
157 def test_generate_c_tag(self):
158 s = Mock()
159 s.c_tag = "111"
160 s.onu_device = "BRCM1234"
161
162 self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s]
163 self.rcord_subscriber.c_tag = None
164
165 self.rcord_subscriber.save()
166
167 self.models_decl.RCORDSubscriber_decl.save.assert_called()
168 self.assertNotEquals(self.rcord_subscriber.c_tag, "111")
169 self.assertGreater(self.rcord_subscriber.c_tag, 16)
170 self.assertLess(self.rcord_subscriber.c_tag, 4097)
171
172
173if __name__ == '__main__':
174 unittest.main()