root / logic / tests.py @ f533f224
History | View | Annotate | Download (6.5 kB)
1 |
# vim: set fileencoding=utf-8 :
|
---|---|
2 |
#
|
3 |
# Unit Tests for logic
|
4 |
#
|
5 |
# Provides automated tests for logic module
|
6 |
#
|
7 |
# Copyright 2010 Greek Research and Technology Network
|
8 |
#
|
9 |
|
10 |
from synnefo.db.models import * |
11 |
from synnefo.logic import backend |
12 |
from synnefo.logic import credits |
13 |
from synnefo.logic import users |
14 |
from django.test import TestCase |
15 |
import time |
16 |
|
17 |
import hashlib |
18 |
|
19 |
class CostsTestCase(TestCase): |
20 |
fixtures = [ 'db_test_data' ]
|
21 |
|
22 |
def test_get_costs(self): |
23 |
"""Test the Flavor cost-related methods method"""
|
24 |
# first an easy test, a Flavor with only one FlavorCost entry
|
25 |
flavor = Flavor.objects.get(pk=30001)
|
26 |
|
27 |
start_date = datetime.datetime(year=2010, month=1, day=1) |
28 |
end_date = datetime.datetime(year=2010, month=1, day=2) |
29 |
|
30 |
# we now that the cost should be 5*24 (inactive) and 10*24 (active)
|
31 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
32 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
33 |
|
34 |
self.assertEqual(len(r_active), 1, 'get_cost_active() should have returned 1 entry (%d)' %(len(r_active),)) |
35 |
self.assertEqual(len(r_inactive), 1, 'get_cost_inactive() should have returned 1 entry (%d)'% (len(r_inactive),)) |
36 |
|
37 |
self.assertEqual(10*24, r_active[0][1], 'get_cost_active() is not working properly (%d!=%d)' % (r_active[0][1], 10*24)) |
38 |
self.assertEqual(5*24, r_inactive[0][1], 'get_cost_inactive() is not working properly (%d!=%d)' % (r_inactive[0][1], 5*24)) |
39 |
|
40 |
# The second test, will involve a more complex cost example
|
41 |
# The overall cost will be calculated by two FlavorCost entries
|
42 |
|
43 |
flavor = Flavor.objects.get(pk=30000)
|
44 |
|
45 |
start_date = datetime.datetime(year=2010, month=12, day=31) |
46 |
end_date = datetime.datetime(year=2011, month=01, day=2) |
47 |
|
48 |
# this is more complicated, active costs are 5*24 + 10*24 = 360
|
49 |
# and inactive costs are 2*24 + 5*24 = 168
|
50 |
|
51 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
52 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
53 |
|
54 |
self.assertEqual(len(r_active), 2, 'get_cost_active() should have returned 2 entries (%d)' %(len(r_active),)) |
55 |
self.assertEqual(len(r_inactive), 2, 'get_cost_inactive() should have returned 2 entries (%d)'% (len(r_inactive),)) |
56 |
|
57 |
ta_cost = sum([x[1] for x in r_active]) |
58 |
tia_cost = sum([x[1] for x in r_inactive]) |
59 |
|
60 |
self.assertEqual(360, ta_cost, 'get_cost_active() is not working properly (%d!=%d)' % (ta_cost, 360)) |
61 |
self.assertEqual(168, tia_cost, 'get_cost_inactive() is not working properly (%d!=%d)' % (tia_cost, 168)) |
62 |
|
63 |
|
64 |
class ChargeTestCase(TestCase): |
65 |
fixtures = [ 'db_test_data' ]
|
66 |
|
67 |
def test_charge_method(self): |
68 |
"""Test VirtualMachine.charge() method"""
|
69 |
|
70 |
# Since we have tested the costs, with this test
|
71 |
# we must ensure the following:
|
72 |
# 1. The vm.charged is updated
|
73 |
# 2. Users credits are decreased
|
74 |
|
75 |
vm_started = VirtualMachine.objects.get(pk=30000)
|
76 |
|
77 |
initial_date = vm_started.charged |
78 |
initial_credits = vm_started.owner.credit |
79 |
|
80 |
credits.charge(vm_started) |
81 |
|
82 |
self.assertTrue(vm_started.charged > initial_date, 'Initial charged date should not be greater') |
83 |
self.assertTrue(initial_credits > vm_started.owner.credit, 'The user should have less credits now! (%d>%d)' % (initial_credits, vm_started.owner.credit)) |
84 |
|
85 |
|
86 |
class DebitAccountTestCase(TestCase): |
87 |
fixtures = [ 'db_test_data' ]
|
88 |
|
89 |
def test_debit_account(self): |
90 |
"""Test a SynnefoUser object"""
|
91 |
s_user = SynnefoUser.objects.get(pk=30000)
|
92 |
v_machine = VirtualMachine.objects.get(pk=30000)
|
93 |
|
94 |
# charge the user
|
95 |
credits.debit_account(s_user, 10, v_machine, "This should be a structured debit message!") |
96 |
|
97 |
# should have only one debit object
|
98 |
d_list = Debit.objects.all() |
99 |
|
100 |
self.assertEqual(len(d_list), 1, 'debit_account() writes more than one or zero (%d) debit entries!' % ( len(d_list), )) |
101 |
|
102 |
# retrieve the user, now he/she should have zero credits
|
103 |
s_user = SynnefoUser.objects.get(pk=30000)
|
104 |
|
105 |
self.assertEqual(0, s_user.credit, 'SynnefoUser (pk=30000) should have zero credits (%d)' % ( s_user.credit, )) |
106 |
|
107 |
class AuthTestCase(TestCase): |
108 |
fixtures = [ 'db_test_data' ]
|
109 |
|
110 |
def _register_user(self): |
111 |
users.register_student ("Jimmy Page", "jpage", "jpage@zoso.com") |
112 |
self.user = SynnefoUser.objects.get(name = "jpage") |
113 |
|
114 |
def test_register(self): |
115 |
""" test user registration
|
116 |
"""
|
117 |
self._register_user()
|
118 |
self.assertNotEquals(self.user, None) |
119 |
|
120 |
#Check hash generation
|
121 |
md5 = hashlib.md5() |
122 |
md5.update(self.user.uniq)
|
123 |
md5.update(self.user.name)
|
124 |
md5.update(time.asctime()) |
125 |
|
126 |
self.assertEquals(self.user.auth_token, md5.hexdigest()) |
127 |
|
128 |
def test_del_user(self): |
129 |
""" test user deletion
|
130 |
"""
|
131 |
self._register_user()
|
132 |
self.assertNotEquals(self.user, None) |
133 |
|
134 |
users.delete_user(self.user)
|
135 |
|
136 |
self.assertRaises(SynnefoUser.DoesNotExist, SynnefoUser.objects.get, name = "jpage") |
137 |
|
138 |
|
139 |
class ProcessNetStatusTestCase(TestCase): |
140 |
fixtures = ['db_test_data']
|
141 |
def test_set_ipv4(self): |
142 |
"""Test reception of a net status notification"""
|
143 |
msg = {'instance': 'instance-name', |
144 |
'type': 'ganeti-net-status', |
145 |
'nics': [
|
146 |
{'ip': '192.168.33.1', 'mac': 'aa:00:00:58:1e:b9'} |
147 |
] |
148 |
} |
149 |
vm = VirtualMachine.objects.get(pk=30000)
|
150 |
backend.process_net_status(vm, msg['nics'])
|
151 |
self.assertEquals(vm.nics.all()[0].ipv4, '192.168.33.1') |
152 |
|
153 |
def test_set_empty_ipv4(self): |
154 |
"""Test reception of a net status notification with no IPv4 assigned"""
|
155 |
msg = {'instance': 'instance-name', |
156 |
'type': 'ganeti-net-status', |
157 |
'nics': [
|
158 |
{'ip': '', 'mac': 'aa:00:00:58:1e:b9'} |
159 |
] |
160 |
} |
161 |
vm = VirtualMachine.objects.get(pk=30000)
|
162 |
backend.process_net_status(vm, msg['nics'])
|
163 |
self.assertEquals(vm.nics.all()[0].ipv4, '') |
164 |
|
165 |
|
166 |
class UsersTestCase(TestCase): |
167 |
|
168 |
def test_create_uname(self): |
169 |
username = users.create_uname("Donald Knuth")
|
170 |
self.assertEquals(username, "knuthd") |
171 |
|
172 |
username = users.create_uname("Nemichandra Siddhanta Chakravati")
|
173 |
self.assertEquals(username, "chakravn") |
174 |
|
175 |
username = users.create_uname(u'Γεώργιος Παπαγεωργίου')
|
176 |
self.assertEquals(username, u'παπαγεωγ') |