root / logic / tests.py @ ace4bd5d
History | View | Annotate | Download (7.9 kB)
1 |
# vim: set fileencoding=utf-8 :
|
---|---|
2 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
3 |
#
|
4 |
# Redistribution and use in source and binary forms, with or without
|
5 |
# modification, are permitted provided that the following conditions
|
6 |
# are met:
|
7 |
#
|
8 |
# 1. Redistributions of source code must retain the above copyright
|
9 |
# notice, this list of conditions and the following disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above copyright
|
12 |
# notice, this list of conditions and the following disclaimer in the
|
13 |
# documentation and/or other materials provided with the distribution.
|
14 |
#
|
15 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
16 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
18 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
19 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
21 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
22 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
23 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
24 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
25 |
# SUCH DAMAGE.
|
26 |
#
|
27 |
# The views and conclusions contained in the software and documentation are
|
28 |
# those of the authors and should not be interpreted as representing official
|
29 |
# policies, either expressed or implied, of GRNET S.A.
|
30 |
|
31 |
# Provides automated tests for logic module
|
32 |
|
33 |
from synnefo.db.models import * |
34 |
from synnefo.logic import backend |
35 |
from synnefo.logic import credits |
36 |
from synnefo.logic import users |
37 |
from django.test import TestCase |
38 |
import time |
39 |
|
40 |
import hashlib |
41 |
|
42 |
class CostsTestCase(TestCase): |
43 |
fixtures = [ 'db_test_data' ]
|
44 |
|
45 |
def test_get_costs(self): |
46 |
"""Test the Flavor cost-related methods method"""
|
47 |
# first an easy test, a Flavor with only one FlavorCost entry
|
48 |
flavor = Flavor.objects.get(pk=30001)
|
49 |
|
50 |
start_date = datetime.datetime(year=2010, month=1, day=1) |
51 |
end_date = datetime.datetime(year=2010, month=1, day=2) |
52 |
|
53 |
# we now that the cost should be 5*24 (inactive) and 10*24 (active)
|
54 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
55 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
56 |
|
57 |
self.assertEqual(len(r_active), 1, 'get_cost_active() should have returned 1 entry (%d)' %(len(r_active),)) |
58 |
self.assertEqual(len(r_inactive), 1, 'get_cost_inactive() should have returned 1 entry (%d)'% (len(r_inactive),)) |
59 |
|
60 |
self.assertEqual(10*24, r_active[0][1], 'get_cost_active() is not working properly (%d!=%d)' % (r_active[0][1], 10*24)) |
61 |
self.assertEqual(5*24, r_inactive[0][1], 'get_cost_inactive() is not working properly (%d!=%d)' % (r_inactive[0][1], 5*24)) |
62 |
|
63 |
# The second test, will involve a more complex cost example
|
64 |
# The overall cost will be calculated by two FlavorCost entries
|
65 |
|
66 |
flavor = Flavor.objects.get(pk=30000)
|
67 |
|
68 |
start_date = datetime.datetime(year=2010, month=12, day=31) |
69 |
end_date = datetime.datetime(year=2011, month=01, day=2) |
70 |
|
71 |
# this is more complicated, active costs are 5*24 + 10*24 = 360
|
72 |
# and inactive costs are 2*24 + 5*24 = 168
|
73 |
|
74 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
75 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
76 |
|
77 |
self.assertEqual(len(r_active), 2, 'get_cost_active() should have returned 2 entries (%d)' %(len(r_active),)) |
78 |
self.assertEqual(len(r_inactive), 2, 'get_cost_inactive() should have returned 2 entries (%d)'% (len(r_inactive),)) |
79 |
|
80 |
ta_cost = sum([x[1] for x in r_active]) |
81 |
tia_cost = sum([x[1] for x in r_inactive]) |
82 |
|
83 |
self.assertEqual(360, ta_cost, 'get_cost_active() is not working properly (%d!=%d)' % (ta_cost, 360)) |
84 |
self.assertEqual(168, tia_cost, 'get_cost_inactive() is not working properly (%d!=%d)' % (tia_cost, 168)) |
85 |
|
86 |
|
87 |
class ChargeTestCase(TestCase): |
88 |
fixtures = [ 'db_test_data' ]
|
89 |
|
90 |
def test_charge_method(self): |
91 |
"""Test VirtualMachine.charge() method"""
|
92 |
|
93 |
# Since we have tested the costs, with this test
|
94 |
# we must ensure the following:
|
95 |
# 1. The vm.charged is updated
|
96 |
# 2. Users credits are decreased
|
97 |
|
98 |
vm_started = VirtualMachine.objects.get(pk=30000)
|
99 |
|
100 |
initial_date = vm_started.charged |
101 |
initial_credits = vm_started.owner.credit |
102 |
|
103 |
credits.charge(vm_started) |
104 |
|
105 |
self.assertTrue(vm_started.charged > initial_date, 'Initial charged date should not be greater') |
106 |
self.assertTrue(initial_credits > vm_started.owner.credit, 'The user should have less credits now! (%d>%d)' % (initial_credits, vm_started.owner.credit)) |
107 |
|
108 |
|
109 |
class DebitAccountTestCase(TestCase): |
110 |
fixtures = [ 'db_test_data' ]
|
111 |
|
112 |
def test_debit_account(self): |
113 |
"""Test a SynnefoUser object"""
|
114 |
s_user = SynnefoUser.objects.get(pk=30000)
|
115 |
v_machine = VirtualMachine.objects.get(pk=30000)
|
116 |
|
117 |
# charge the user
|
118 |
credits.debit_account(s_user, 10, v_machine, "This should be a structured debit message!") |
119 |
|
120 |
# should have only one debit object
|
121 |
d_list = Debit.objects.all() |
122 |
|
123 |
self.assertEqual(len(d_list), 1, 'debit_account() writes more than one or zero (%d) debit entries!' % ( len(d_list), )) |
124 |
|
125 |
# retrieve the user, now he/she should have zero credits
|
126 |
s_user = SynnefoUser.objects.get(pk=30000)
|
127 |
|
128 |
self.assertEqual(0, s_user.credit, 'SynnefoUser (pk=30000) should have zero credits (%d)' % ( s_user.credit, )) |
129 |
|
130 |
class AuthTestCase(TestCase): |
131 |
fixtures = [ 'db_test_data' ]
|
132 |
|
133 |
def _register_user(self): |
134 |
users.register_student ("Jimmy Page", "jpage", "jpage@zoso.com") |
135 |
self.user = SynnefoUser.objects.get(name = "jpage") |
136 |
|
137 |
def test_register(self): |
138 |
""" test user registration
|
139 |
"""
|
140 |
self._register_user()
|
141 |
self.assertNotEquals(self.user, None) |
142 |
|
143 |
#Check hash generation
|
144 |
md5 = hashlib.md5() |
145 |
md5.update(self.user.uniq)
|
146 |
md5.update(self.user.name)
|
147 |
md5.update(time.asctime()) |
148 |
|
149 |
self.assertEquals(self.user.auth_token, md5.hexdigest()) |
150 |
|
151 |
def test_del_user(self): |
152 |
""" test user deletion
|
153 |
"""
|
154 |
self._register_user()
|
155 |
self.assertNotEquals(self.user, None) |
156 |
|
157 |
users.delete_user(self.user)
|
158 |
|
159 |
self.assertRaises(SynnefoUser.DoesNotExist, SynnefoUser.objects.get, name = "jpage") |
160 |
|
161 |
|
162 |
class ProcessNetStatusTestCase(TestCase): |
163 |
fixtures = ['db_test_data']
|
164 |
def test_set_ipv4(self): |
165 |
"""Test reception of a net status notification"""
|
166 |
msg = {'instance': 'instance-name', |
167 |
'type': 'ganeti-net-status', |
168 |
'nics': [
|
169 |
{'ip': '192.168.33.1', 'mac': 'aa:00:00:58:1e:b9'} |
170 |
] |
171 |
} |
172 |
vm = VirtualMachine.objects.get(pk=30000)
|
173 |
backend.process_net_status(vm, msg['nics'])
|
174 |
self.assertEquals(vm.nics.all()[0].ipv4, '192.168.33.1') |
175 |
|
176 |
def test_set_empty_ipv4(self): |
177 |
"""Test reception of a net status notification with no IPv4 assigned"""
|
178 |
msg = {'instance': 'instance-name', |
179 |
'type': 'ganeti-net-status', |
180 |
'nics': [
|
181 |
{'ip': '', 'mac': 'aa:00:00:58:1e:b9'} |
182 |
] |
183 |
} |
184 |
vm = VirtualMachine.objects.get(pk=30000)
|
185 |
backend.process_net_status(vm, msg['nics'])
|
186 |
self.assertEquals(vm.nics.all()[0].ipv4, '') |
187 |
|
188 |
|
189 |
class UsersTestCase(TestCase): |
190 |
|
191 |
def test_create_uname(self): |
192 |
username = users.create_uname("Donald Knuth")
|
193 |
self.assertEquals(username, "knuthd") |
194 |
|
195 |
username = users.create_uname("Nemichandra Siddhanta Chakravati")
|
196 |
self.assertEquals(username, "chakravn") |
197 |
|
198 |
username = users.create_uname(u'Γεώργιος Παπαγεωργίου')
|
199 |
self.assertEquals(username, u'παπαγεωγ') |