root / snf-cyclades-app / synnefo / db / tests.py @ dccd42eb
History | View | Annotate | Download (9.3 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or without
|
4 |
# modification, are permitted provided that the following conditions
|
5 |
# are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above copyright
|
8 |
# notice, this list of conditions and the following disclaimer.
|
9 |
#
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright
|
11 |
# notice, this list of conditions and the following disclaimer in the
|
12 |
# documentation and/or other materials provided with the distribution.
|
13 |
#
|
14 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 |
# SUCH DAMAGE.
|
25 |
#
|
26 |
# The views and conclusions contained in the software and documentation are
|
27 |
# those of the authors and should not be interpreted as representing official
|
28 |
# policies, either expressed or implied, of GRNET S.A.
|
29 |
|
30 |
# Unit Tests for db
|
31 |
#
|
32 |
# Provides automated tests for db module
|
33 |
|
34 |
from django.test import TestCase |
35 |
|
36 |
# Import pool tests
|
37 |
from synnefo.db.pools.tests import * |
38 |
|
39 |
from synnefo.db.models import * |
40 |
from synnefo.db import models_factory as mfact |
41 |
from synnefo.db.pools import IPPool |
42 |
|
43 |
from django.db import IntegrityError |
44 |
from django.core.exceptions import MultipleObjectsReturned |
45 |
from mock import patch |
46 |
|
47 |
|
48 |
class FlavorTest(TestCase): |
49 |
def test_flavor_name(self): |
50 |
"""Test a flavor object name method."""
|
51 |
flavor = mfact.FlavorFactory(cpu=1, ram=1024, disk=40, |
52 |
disk_template="temp")
|
53 |
self.assertEqual(flavor.name, "C1R1024D40temp", "flavor.name is not" |
54 |
" generated correctly. Name is %s instead of C1R1024D40temp" %
|
55 |
flavor.name) |
56 |
|
57 |
|
58 |
class BackendTest(TestCase): |
59 |
def setUp(self): |
60 |
self.backend = mfact.BackendFactory()
|
61 |
|
62 |
@patch("synnefo.db.models.get_rapi_client") |
63 |
def test_get_client(self, client): |
64 |
id_ = self.backend.id
|
65 |
hash_ = self.backend.hash
|
66 |
name = self.backend.clustername
|
67 |
passwd = self.backend.password
|
68 |
user = self.backend.username
|
69 |
port = self.backend.port
|
70 |
self.backend.get_client()
|
71 |
client.assert_called_once_with(id_, hash_, name, port, user, passwd) |
72 |
|
73 |
def test_save_hash(self): |
74 |
"""Test that backend hash is generated on credential change"""
|
75 |
old_hash = self.backend.hash
|
76 |
for field in ['clustername', 'username', 'password', 'port']: |
77 |
value = 5181 if field == 'port' else 'foo' |
78 |
self.backend.__setattr__(field, value)
|
79 |
self.backend.save()
|
80 |
self.assertNotEqual(old_hash, self.backend.hash) |
81 |
old_hash = self.backend.hash
|
82 |
|
83 |
def test_unique_index(self): |
84 |
"""Test that each backend gets a unique index"""
|
85 |
backends = [self.backend]
|
86 |
for i in xrange(0, 14): |
87 |
backends.append(mfact.BackendFactory()) |
88 |
indexes = map(lambda x: x.index, backends) |
89 |
self.assertEqual(len(indexes), len(set(indexes))) |
90 |
|
91 |
def test_backend_number(self): |
92 |
"""Test that no more than 16 backends are created"""
|
93 |
for i in xrange(0, 14): |
94 |
mfact.BackendFactory() |
95 |
self.assertRaises(Exception, mfact.BackendFactory, ()) |
96 |
|
97 |
def test_delete_backend(self): |
98 |
vm = mfact.VirtualMachineFactory(backend=self.backend, deleted=True) |
99 |
bnet = mfact.BackendNetworkFactory(backend=self.backend)
|
100 |
self.backend.delete()
|
101 |
self.assertRaises(Backend.DoesNotExist, Backend.objects.get,
|
102 |
id=self.backend.id)
|
103 |
# Test that VM is not deleted
|
104 |
vm2 = VirtualMachine.objects.get(id=vm.id) |
105 |
self.assertEqual(vm2.backend, None) |
106 |
# Test tha backend networks are deleted, but not the network
|
107 |
self.assertRaises(BackendNetwork.DoesNotExist,
|
108 |
BackendNetwork.objects.get, id=bnet.id) |
109 |
Network.objects.get(id=bnet.network.id) |
110 |
|
111 |
def test_delete_active_backend(self): |
112 |
"""Test that a backend with non-deleted VMS is not deleted"""
|
113 |
mfact.VirtualMachineFactory(backend=self.backend)
|
114 |
self.assertRaises(IntegrityError, self.backend.delete, ()) |
115 |
|
116 |
def test_password_encryption(self): |
117 |
password_hash = self.backend.password
|
118 |
self.backend.password = '123' |
119 |
self.assertNotEqual(self.backend.password_hash, '123') |
120 |
self.assertNotEqual(self.backend.password_hash, password_hash) |
121 |
self.assertEqual(self.backend.password, '123') |
122 |
|
123 |
|
124 |
class VirtualMachineTest(TestCase): |
125 |
def setUp(self): |
126 |
self.vm = mfact.VirtualMachineFactory()
|
127 |
|
128 |
@patch("synnefo.db.models.get_rapi_client") |
129 |
def test_get_client(self, client): |
130 |
backend = self.vm.backend
|
131 |
id_ = backend.id |
132 |
hash_ = backend.hash |
133 |
name = backend.clustername |
134 |
passwd = backend.password |
135 |
user = backend.username |
136 |
port = backend.port |
137 |
self.vm.get_client()
|
138 |
client.assert_called_once_with(id_, hash_, name, port, user, passwd) |
139 |
|
140 |
def test_create(self): |
141 |
vm = mfact.VirtualMachineFactory() |
142 |
self.assertEqual(vm.action, None) |
143 |
self.assertEqual(vm.backendjobid, None) |
144 |
self.assertEqual(vm.backendjobstatus, None) |
145 |
self.assertEqual(vm.backendopcode, None) |
146 |
self.assertEqual(vm.backendlogmsg, None) |
147 |
self.assertEqual(vm.operstate, 'BUILD') |
148 |
|
149 |
|
150 |
class NetworkTest(TestCase): |
151 |
def setUp(self): |
152 |
self.net = mfact.NetworkFactory()
|
153 |
|
154 |
def test_tags(self): |
155 |
net1 = mfact.NetworkFactory(flavor='IP_LESS_ROUTED')
|
156 |
self.assertEqual(net1.backend_tag, ['ip-less-routed']) |
157 |
net1 = mfact.NetworkFactory(flavor='CUSTOM')
|
158 |
self.assertEqual(net1.backend_tag, [])
|
159 |
|
160 |
def test_create_backend_network(self): |
161 |
len_backends = len(Backend.objects.all())
|
162 |
back = mfact.BackendFactory() |
163 |
self.net.create_backend_network(backend=back)
|
164 |
BackendNetwork.objects.get(network=self.net, backend=back)
|
165 |
back1 = mfact.BackendFactory() |
166 |
back2 = mfact.BackendFactory() |
167 |
self.net.create_backend_network()
|
168 |
BackendNetwork.objects.get(network=self.net, backend=back1)
|
169 |
BackendNetwork.objects.get(network=self.net, backend=back2)
|
170 |
self.assertEqual(len(BackendNetwork.objects.filter(network=self.net)), |
171 |
len_backends + 3)
|
172 |
|
173 |
def test_pool(self): |
174 |
pool = self.net.get_pool()
|
175 |
pool.network = self.net
|
176 |
self.assertTrue(isinstance(pool, IPPool)) |
177 |
|
178 |
def test_reserve_ip(self): |
179 |
net1 = mfact.NetworkFactory(subnet='192.168.2.0/24')
|
180 |
net1.reserve_address('192.168.2.12')
|
181 |
pool = net1.get_pool() |
182 |
self.assertFalse(pool.is_available('192.168.2.12')) |
183 |
net1.release_address('192.168.2.12')
|
184 |
pool = net1.get_pool() |
185 |
self.assertTrue(pool.is_available('192.168.2.12')) |
186 |
|
187 |
|
188 |
class BackendNetworkTest(TestCase): |
189 |
def test_mac_prefix(self): |
190 |
network = mfact.NetworkFactory(mac_prefix='aa:bb:c')
|
191 |
backend = mfact.BackendFactory() |
192 |
bnet = mfact.BackendNetworkFactory(network=network, backend=backend) |
193 |
self.assertTrue(backend.index < 10) |
194 |
self.assertEqual(bnet.mac_prefix, 'aa:bb:c%s' % backend.index) |
195 |
|
196 |
def test_invalid_mac(self): |
197 |
network = mfact.NetworkFactory(mac_prefix='zz:bb:c')
|
198 |
backend = mfact.BackendFactory() |
199 |
self.assertRaises(utils.InvalidMacAddress,
|
200 |
mfact.BackendNetworkFactory, network=network, backend=backend) |
201 |
|
202 |
|
203 |
class BridgePoolTest(TestCase): |
204 |
def test_no_pool(self): |
205 |
self.assertRaises(BridgePoolTable.DoesNotExist,
|
206 |
BridgePoolTable.get_pool) |
207 |
|
208 |
def test_two_pools(self): |
209 |
mfact.BridgePoolTableFactory() |
210 |
mfact.BridgePoolTableFactory() |
211 |
self.assertRaises(MultipleObjectsReturned, BridgePoolTable.get_pool)
|
212 |
|
213 |
|
214 |
class AESTest(TestCase): |
215 |
def test_encrypt_decrtypt(self): |
216 |
from synnefo.db import aes_encrypt as aes |
217 |
old = 'bar'
|
218 |
new = aes.decrypt_db_charfield(aes.encrypt_db_charfield(old)) |
219 |
self.assertEqual(old, new)
|
220 |
|
221 |
def test_password_change(self): |
222 |
from synnefo.db import aes_encrypt as aes |
223 |
old_pass = aes.SECRET_ENCRYPTION_KEY |
224 |
old = 'bar'
|
225 |
encrypted = aes.encrypt_db_charfield(old) |
226 |
aes.SECRET_ENCRYPTION_KEY = 'foo2'
|
227 |
self.assertRaises(aes.CorruptedPassword, aes.decrypt_db_charfield,
|
228 |
encrypted) |
229 |
aes.SECRET_ENCRYPTION_KEY = old_pass |
230 |
new = aes.decrypt_db_charfield(encrypted) |
231 |
self.assertEqual(old, new)
|
232 |
|
233 |
def test_big_secret(self): |
234 |
from synnefo.db import aes_encrypt as aes |
235 |
old = aes.SECRET_ENCRYPTION_KEY |
236 |
aes.SECRET_ENCRYPTION_KEY = \ |
237 |
'91490231234814234812348913289481294812398421893489'
|
238 |
self.assertRaises(ValueError, aes.encrypt_db_charfield, 'la') |
239 |
aes.SECRET_ENCRYPTION_KEY = old |