root / snf-cyclades-app / synnefo / db / pools / tests.py @ 35e2f2d4
History | View | Annotate | Download (6.5 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
|
35 |
from django.test import TestCase |
36 |
from synnefo.db.pools import (PoolManager, EmptyPool, BridgePool, |
37 |
MacPrefixPool, IPPool) |
38 |
from bitarray import bitarray |
39 |
|
40 |
|
41 |
class DummyObject(): |
42 |
def __init__(self, size): |
43 |
self.size = size
|
44 |
|
45 |
self.available_map = '' |
46 |
self.reserved_map = '' |
47 |
|
48 |
def save(self): |
49 |
pass
|
50 |
|
51 |
|
52 |
class DummyPool(PoolManager): |
53 |
def value_to_index(self, index): |
54 |
return index
|
55 |
|
56 |
def index_to_value(self, value): |
57 |
return value
|
58 |
|
59 |
|
60 |
class PoolManagerTestCase(TestCase): |
61 |
def test_created_pool(self): |
62 |
obj = DummyObject(42)
|
63 |
pool = DummyPool(obj) |
64 |
self.assertEqual(pool.to_01(), '1' * 42) |
65 |
self.assertEqual(pool.to_map(), '.' * 42) |
66 |
self.assertEqual(pool.available, bitarray('1' * 42 + '0' * 6)) |
67 |
self.assertEqual(pool.reserved, bitarray('1' * 42 + '0' * 6)) |
68 |
|
69 |
def test_save_pool(self): |
70 |
obj = DummyObject(42)
|
71 |
pool = DummyPool(obj) |
72 |
pool.save() |
73 |
self.assertNotEqual(obj.available_map, '') |
74 |
available_map = obj.available_map |
75 |
b = DummyPool(obj) |
76 |
b.save() |
77 |
self.assertEqual(obj.available_map, available_map)
|
78 |
|
79 |
def test_empty_pool(self): |
80 |
obj = DummyObject(42)
|
81 |
pool = DummyPool(obj) |
82 |
self.assertEqual(pool.empty(), False) |
83 |
for i in range(0, 42): |
84 |
self.assertEqual(pool.get(), i)
|
85 |
self.assertEqual(pool.empty(), True) |
86 |
self.assertRaises(EmptyPool, pool.get)
|
87 |
|
88 |
def test_reserved_value(self): |
89 |
obj = DummyObject(42)
|
90 |
pool = DummyPool(obj) |
91 |
available = pool.count_available() |
92 |
value = pool.get() |
93 |
self.assertEqual(pool.is_available(value), False) |
94 |
self.assertEqual(pool.count_available(), available - 1) |
95 |
pool.put(value) |
96 |
self.assertEqual(pool.is_available(value), True) |
97 |
self.assertEqual(pool.count_available(), available)
|
98 |
|
99 |
def test_external_reserved(self): |
100 |
obj = DummyObject(42)
|
101 |
pool = DummyPool(obj) |
102 |
for i in range(42, 48): |
103 |
self.assertEqual(pool.is_available(i), False) |
104 |
pool.reserve(32, external=True) |
105 |
values = [] |
106 |
while True: |
107 |
try:
|
108 |
values.append(pool.get()) |
109 |
except EmptyPool:
|
110 |
break
|
111 |
self.assertEqual(32 not in values, True) |
112 |
|
113 |
def test_external_reserved_2(self): |
114 |
obj = DummyObject(42)
|
115 |
pool = DummyPool(obj) |
116 |
self.assertEqual(pool.get(), 0) |
117 |
pool.reserve(0, external=True) |
118 |
pool.put(0)
|
119 |
self.assertEqual(pool.get(), 1) |
120 |
|
121 |
def test_extend_pool(self): |
122 |
obj = DummyObject(42)
|
123 |
pool = DummyPool(obj) |
124 |
pool.extend(7)
|
125 |
self.assertEqual(pool.to_01(), '1' * 49) |
126 |
self.assertEqual(pool.to_map(), '.' * 49) |
127 |
self.assertEqual(pool.available, bitarray('1' * 49 + '0' * 7)) |
128 |
self.assertEqual(pool.reserved, bitarray('1' * 49 + '0' * 7)) |
129 |
|
130 |
def test_shrink_pool(self): |
131 |
obj = DummyObject(42)
|
132 |
pool = DummyPool(obj) |
133 |
pool.shrink(3)
|
134 |
self.assertEqual(pool.to_01(), '1' * 39) |
135 |
self.assertEqual(pool.to_map(), '.' * 39) |
136 |
self.assertEqual(pool.available, bitarray('1' * 39 + '0' * 1)) |
137 |
self.assertEqual(pool.reserved, bitarray('1' * 39 + '0' * 1)) |
138 |
|
139 |
|
140 |
class BridgePoolTestCase(TestCase): |
141 |
def test_bridge_conversion(self): |
142 |
obj = DummyObject(13)
|
143 |
obj.base = "bridge"
|
144 |
pool = BridgePool(obj) |
145 |
for i in range(0, 13): |
146 |
self.assertEqual("bridge" + str(i + 1), pool.get()) |
147 |
pool.put("bridge2")
|
148 |
pool.put("bridge6")
|
149 |
self.assertEqual("bridge2", pool.get()) |
150 |
self.assertEqual("bridge6", pool.get()) |
151 |
self.assertEqual(pool.empty(), True) |
152 |
|
153 |
|
154 |
class MacPrefixPoolTestCase(TestCase): |
155 |
def test_invalid_mac_reservation(self): |
156 |
obj = DummyObject(65636)
|
157 |
obj.base = 'ab:ff:ff'
|
158 |
pool = MacPrefixPool(obj) |
159 |
for i in range(0, 65536): |
160 |
self.assertEqual(pool.is_available(i, index=True), False) |
161 |
|
162 |
|
163 |
class IPPoolTestCase(TestCase): |
164 |
def test_auto_reservations(self): |
165 |
obj = DummyObject(0)
|
166 |
network = DummyObject(0)
|
167 |
obj.network = network |
168 |
network.subnet = '192.168.2.0/24'
|
169 |
network.gateway = '192.168.2.1'
|
170 |
pool = IPPool(obj) |
171 |
self.assertEqual(pool.is_available('192.168.2.0'), False) |
172 |
self.assertEqual(pool.is_available('192.168.2.1'), False) |
173 |
self.assertEqual(pool.is_available('192.168.2.255'), False) |
174 |
self.assertEqual(pool.count_available(), 253) |
175 |
self.assertEqual(pool.get(), '192.168.2.2') |
176 |
|
177 |
def test_auto_reservations_2(self): |
178 |
obj = DummyObject(0)
|
179 |
network = DummyObject(0)
|
180 |
obj.network = network |
181 |
network.subnet = '192.168.2.0/31'
|
182 |
network.gateway = '192.168.2.1'
|
183 |
pool = IPPool(obj) |
184 |
self.assertEqual(pool.is_available('192.168.2.0'), False) |
185 |
self.assertEqual(pool.is_available('192.168.2.1'), False) |
186 |
self.assertEqual(pool.size(), 8) |
187 |
self.assertEqual(pool.empty(), True) |