Revision 54ae949d snf-app/synnefo/logic/tests.py
b/snf-app/synnefo/logic/tests.py | ||
---|---|---|
39 | 39 |
|
40 | 40 |
from synnefo.db.models import * |
41 | 41 |
from synnefo.logic import backend |
42 |
from synnefo.logic import credits |
|
43 |
from synnefo.logic import users |
|
44 | 42 |
from synnefo.logic import reconciliation |
45 | 43 |
from synnefo.logic.utils import get_rsapi_state |
46 | 44 |
|
47 | 45 |
|
48 |
class CostsTestCase(TestCase): |
|
49 |
fixtures = ['db_test_data'] |
|
50 |
|
|
51 |
def test_get_costs(self): |
|
52 |
"""Test the Flavor cost-related methods method""" |
|
53 |
# first an easy test, a Flavor with only one FlavorCost entry |
|
54 |
flavor = Flavor.objects.get(pk=30001) |
|
55 |
|
|
56 |
start_date = datetime.datetime(year=2010, month=1, day=1) |
|
57 |
end_date = datetime.datetime(year=2010, month=1, day=2) |
|
58 |
|
|
59 |
# we now that the cost should be 5*24 (inactive) and 10*24 (active) |
|
60 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
|
61 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
|
62 |
|
|
63 |
self.assertEqual(len(r_active), 1, 'get_cost_active() should have returned 1 entry (%d)' %(len(r_active),)) |
|
64 |
self.assertEqual(len(r_inactive), 1, 'get_cost_inactive() should have returned 1 entry (%d)'% (len(r_inactive),)) |
|
65 |
|
|
66 |
self.assertEqual(10*24, r_active[0][1], 'get_cost_active() is not working properly (%d!=%d)' % (r_active[0][1], 10*24)) |
|
67 |
self.assertEqual(5*24, r_inactive[0][1], 'get_cost_inactive() is not working properly (%d!=%d)' % (r_inactive[0][1], 5*24)) |
|
68 |
|
|
69 |
# The second test, will involve a more complex cost example |
|
70 |
# The overall cost will be calculated by two FlavorCost entries |
|
71 |
|
|
72 |
flavor = Flavor.objects.get(pk=30000) |
|
73 |
|
|
74 |
start_date = datetime.datetime(year=2010, month=12, day=31) |
|
75 |
end_date = datetime.datetime(year=2011, month=01, day=2) |
|
76 |
|
|
77 |
# this is more complicated, active costs are 5*24 + 10*24 = 360 |
|
78 |
# and inactive costs are 2*24 + 5*24 = 168 |
|
79 |
|
|
80 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
|
81 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
|
82 |
|
|
83 |
self.assertEqual(len(r_active), 2, 'get_cost_active() should have returned 2 entries (%d)' %(len(r_active),)) |
|
84 |
self.assertEqual(len(r_inactive), 2, 'get_cost_inactive() should have returned 2 entries (%d)'% (len(r_inactive),)) |
|
85 |
|
|
86 |
ta_cost = sum([x[1] for x in r_active]) |
|
87 |
tia_cost = sum([x[1] for x in r_inactive]) |
|
88 |
|
|
89 |
self.assertEqual(360, ta_cost, 'get_cost_active() is not working properly (%d!=%d)' % (ta_cost, 360)) |
|
90 |
self.assertEqual(168, tia_cost, 'get_cost_inactive() is not working properly (%d!=%d)' % (tia_cost, 168)) |
|
91 |
|
|
92 |
|
|
93 |
class ChargeTestCase(TestCase): |
|
94 |
fixtures = ['db_test_data'] |
|
95 |
|
|
96 |
def test_charge_method(self): |
|
97 |
"""Test VirtualMachine.charge() method""" |
|
98 |
|
|
99 |
# Since we have tested the costs, with this test |
|
100 |
# we must ensure the following: |
|
101 |
# 1. The vm.charged is updated |
|
102 |
# 2. Users credits are decreased |
|
103 |
|
|
104 |
vm_started = VirtualMachine.objects.get(pk=30000) |
|
105 |
|
|
106 |
initial_date = vm_started.charged |
|
107 |
initial_credits = vm_started.owner.credit |
|
108 |
|
|
109 |
credits.charge(vm_started) |
|
110 |
|
|
111 |
self.assertTrue(vm_started.charged > initial_date, 'Initial charged date should not be greater') |
|
112 |
self.assertTrue(initial_credits > vm_started.owner.credit, 'The user should have less credits now! (%d>%d)' % (initial_credits, vm_started.owner.credit)) |
|
113 |
|
|
114 |
|
|
115 |
class DebitAccountTestCase(TestCase): |
|
116 |
fixtures = ['db_test_data'] |
|
117 |
|
|
118 |
def test_debit_account(self): |
|
119 |
"""Test a SynnefoUser object""" |
|
120 |
s_user = SynnefoUser.objects.get(pk=30000) |
|
121 |
v_machine = VirtualMachine.objects.get(pk=30000) |
|
122 |
|
|
123 |
# charge the user |
|
124 |
credits.debit_account(s_user, 10, v_machine, "This should be a structured debit message!") |
|
125 |
|
|
126 |
# should have only one debit object |
|
127 |
d_list = Debit.objects.all() |
|
128 |
|
|
129 |
self.assertEqual(len(d_list), 1, 'debit_account() writes more than one or zero (%d) debit entries!' % ( len(d_list), )) |
|
130 |
|
|
131 |
# retrieve the user, now he/she should have zero credits |
|
132 |
s_user = SynnefoUser.objects.get(pk=30000) |
|
133 |
|
|
134 |
self.assertEqual(0, s_user.credit, 'SynnefoUser (pk=30000) should have zero credits (%d)' % ( s_user.credit, )) |
|
135 |
|
|
136 |
|
|
137 |
class AuthTestCase(TestCase): |
|
138 |
fixtures = ['db_test_data'] |
|
139 |
|
|
140 |
def _register_user(self): |
|
141 |
users.register_student ("Jimmy Page", "jpage", "jpage@zoso.com") |
|
142 |
self.user = SynnefoUser.objects.get(name = "jpage") |
|
143 |
|
|
144 |
def test_register(self): |
|
145 |
""" test user registration |
|
146 |
""" |
|
147 |
self._register_user() |
|
148 |
self.assertNotEquals(self.user, None) |
|
149 |
|
|
150 |
#Check hash generation |
|
151 |
md5 = hashlib.md5() |
|
152 |
md5.update(self.user.uniq) |
|
153 |
md5.update(self.user.name) |
|
154 |
md5.update(time.asctime()) |
|
155 |
|
|
156 |
self.assertEquals(self.user.auth_token, md5.hexdigest()) |
|
157 |
|
|
158 |
def test_del_user(self): |
|
159 |
""" test user deletion |
|
160 |
""" |
|
161 |
self._register_user() |
|
162 |
self.assertNotEquals(self.user, None) |
|
163 |
|
|
164 |
users.delete_user(self.user) |
|
165 |
|
|
166 |
self.assertRaises(SynnefoUser.DoesNotExist, SynnefoUser.objects.get, |
|
167 |
name = "jpage") |
|
168 |
|
|
169 |
|
|
170 | 46 |
class ProcessOpStatusTestCase(TestCase): |
171 | 47 |
fixtures = ['db_test_data'] |
172 | 48 |
msg_op = { |
... | ... | |
297 | 173 |
self.assertEquals(vm.nics.all()[0].ipv4, '') |
298 | 174 |
|
299 | 175 |
|
300 |
class UsersTestCase(TestCase): |
|
301 |
def test_create_uname(self): |
|
302 |
username = users.create_uname("Donald Knuth") |
|
303 |
self.assertEquals(username, "knuthd") |
|
304 |
|
|
305 |
username = users.create_uname("Nemichandra Siddhanta Chakravati") |
|
306 |
self.assertEquals(username, "chakravn") |
|
307 |
|
|
308 |
username = users.create_uname(u'Γεώργιος Παπαγεωργίου') |
|
309 |
self.assertEquals(username, u'παπαγεωγ') |
|
310 |
|
|
311 |
|
|
312 | 176 |
class ProcessProgressUpdateTestCase(TestCase): |
313 | 177 |
fixtures = ['db_test_data'] |
314 | 178 |
|
Also available in: Unified diff