Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2018 the original author or authors. |
| 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 | import json |
| 17 | from unittest import TestCase, main |
| 18 | |
| 19 | from bitstring import BitArray |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 20 | from common.pon_resource_manager.resource_manager import PONResourceManager |
| 21 | from mock import Mock |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class TestResourceManager(TestCase): |
| 25 | def setUp(self): |
| 26 | self._rm = PONResourceManager('xgspon', 'default', |
| 27 | '0001c889ee7189fb', 'consul', |
| 28 | 'localhost', 8500) |
| 29 | self.default_resource_range = { |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 30 | "onu_id_start": 1, |
| 31 | "onu_id_end": 127, |
| 32 | "alloc_id_start": 1024, |
| 33 | "alloc_id_end": 2816, |
| 34 | "gemport_id_start": 1024, |
| 35 | "gemport_id_end": 8960, |
| 36 | "pon_ports": 16 |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | def tearDown(self): |
| 40 | self._rm = None |
| 41 | self.default_resource_range = None |
| 42 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 43 | def test_init_pon_resource_ranges(self): |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 44 | output = json.dumps(self.default_resource_range).encode('utf-8') |
| 45 | self._rm._get_olt_vendor = Mock(return_value='default') |
| 46 | self._rm._kv_store.get_from_kv_store = Mock(return_value=output) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 47 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 48 | self._rm.init_resource_ranges_from_kv_store() |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 49 | self.assertEqual(self._rm.pon_resource_ranges, |
| 50 | self.default_resource_range) |
| 51 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 52 | self._rm.init_default_pon_resource_ranges() |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 53 | self.assertEqual(self._rm.pon_resource_ranges, |
| 54 | self.default_resource_range) |
| 55 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 56 | def test_init_resource_id_pool(self): |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 57 | self._rm._kv_store.get_from_kv_store = Mock(return_value=None) |
| 58 | self._rm._kv_store.update_to_kv_store = Mock(return_value=True) |
| 59 | status = self._rm.init_resource_id_pool(0, 'ONU_ID', 1, 127) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 60 | self.assertTrue(status) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 61 | status = self._rm.init_resource_id_pool( |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 62 | 1, 'ALLOC_ID', 1024, 16383) |
| 63 | self.assertTrue(status) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 64 | status = self._rm.init_resource_id_pool( |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 65 | 2, 'GEMPORT_ID', 1023, 65534) |
| 66 | self.assertTrue(status) |
| 67 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 68 | def test_get_resource_id(self): |
| 69 | # Get onu id test |
| 70 | onu_id_resource = self._rm._format_resource(0, 1, 127) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 71 | output = onu_id_resource.encode('utf-8') |
| 72 | self._rm._kv_store.get_from_kv_store = Mock(return_value=output) |
| 73 | self._rm._kv_store.update_to_kv_store = Mock(return_value=True) |
| 74 | result = self._rm.get_resource_id(0, 'ONU_ID') |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 75 | self.assertEqual(result, 1) |
| 76 | |
| 77 | # Get alloc id test |
| 78 | alloc_id_resource = self._rm._format_resource(1, 1024, 16383) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 79 | output = alloc_id_resource.encode('utf-8') |
| 80 | self._rm._kv_store.get_from_kv_store = Mock(return_value=output) |
| 81 | result = self._rm.get_resource_id(1, 'ALLOC_ID', 1) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 82 | self.assertEqual(result[0], 1024) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 83 | result = self._rm.get_resource_id(1, 'ALLOC_ID', 4) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 84 | self.assertEqual(result, [1024, 1025, 1026, 1027]) |
| 85 | |
| 86 | # Get gemport id test |
| 87 | gemport_id_resource = self._rm._format_resource(2, 1023, 65534) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 88 | output = gemport_id_resource.encode('utf-8') |
| 89 | self._rm._kv_store.get_from_kv_store = Mock(return_value=output) |
| 90 | result = self._rm.get_resource_id(2, 'GEMPORT_ID', 1) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 91 | self.assertEqual(result[0], 1023) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 92 | result = self._rm.get_resource_id(2, 'GEMPORT_ID', 5) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 93 | self.assertEqual(result, [1023, 1024, 1025, 1026, 1027]) |
| 94 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 95 | def test_free_resource_id(self): |
| 96 | # Free onu id test |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 97 | self._rm._kv_store.update_to_kv_store = Mock(return_value=True) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 98 | onu_id_resource = eval(self._rm._format_resource(0, 1, 127)) |
| 99 | onu_id_resource['pool'] = BitArray('0b' + onu_id_resource['pool']) |
| 100 | self._rm._generate_next_id(onu_id_resource) |
| 101 | onu_id_resource['pool'] = onu_id_resource['pool'].bin |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 102 | output = json.dumps(onu_id_resource).encode('utf-8') |
| 103 | self._rm._kv_store.get_from_kv_store = Mock(return_value=output) |
| 104 | result = self._rm.free_resource_id(0, 'ONU_ID', 1) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 105 | self.assertTrue(result) |
| 106 | |
| 107 | # Free alloc id test |
| 108 | alloc_id_resource = eval(self._rm._format_resource(1, 1024, 16383)) |
| 109 | alloc_id_resource['pool'] = BitArray('0b' + alloc_id_resource['pool']) |
| 110 | |
| 111 | for num in range(5): |
| 112 | self._rm._generate_next_id(alloc_id_resource) |
| 113 | |
| 114 | alloc_id_resource['pool'] = alloc_id_resource['pool'].bin |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 115 | output = json.dumps(alloc_id_resource).encode('utf-8') |
| 116 | self._rm._kv_store.get_from_kv_store = Mock(return_value=output) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 117 | result = self._rm.free_resource_id(1, 'ALLOC_ID', |
| 118 | [1024, 1025, 1026, 1027, 1028]) |
| 119 | self.assertTrue(result) |
| 120 | |
| 121 | # Free gemport id test |
| 122 | gemport_id_resource = eval(self._rm._format_resource(2, 1023, 65534)) |
| 123 | gemport_id_resource['pool'] = BitArray( |
| 124 | '0b' + gemport_id_resource['pool']) |
| 125 | |
| 126 | for num in range(6): |
| 127 | self._rm._generate_next_id(gemport_id_resource) |
| 128 | |
| 129 | gemport_id_resource['pool'] = gemport_id_resource['pool'].bin |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 130 | output = json.dumps(gemport_id_resource).encode('utf-8') |
| 131 | self._rm._kv_store.get_from_kv_store = Mock(return_value=output) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 132 | result = self._rm.free_resource_id(2, 'GEMPORT_ID', |
| 133 | [1023, 1024, 1025, 1026, 1027, 1028]) |
| 134 | self.assertTrue(result) |
| 135 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 136 | def test_clear_resource_id_pool(self): |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 137 | self._rm._kv_store.remove_from_kv_store = Mock(return_value=True) |
| 138 | status = self._rm.clear_resource_id_pool(0, 'ONU_ID') |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 139 | self.assertTrue(status) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 140 | self._rm._kv_store.remove_from_kv_store = Mock(return_value=False) |
| 141 | status = self._rm.clear_resource_id_pool(1, 'ALLOC_ID') |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 142 | self.assertFalse(status) |
| 143 | |
| 144 | |
| 145 | if __name__ == '__main__': |
| 146 | main() |