root / logic / tests.py @ 17fc7729
History | View | Annotate | Download (5.4 kB)
1 |
#
|
---|---|
2 |
# Unit Tests for logic
|
3 |
#
|
4 |
# Provides automated tests for logic module
|
5 |
#
|
6 |
# Copyright 2010 Greek Research and Technology Network
|
7 |
#
|
8 |
|
9 |
from db.models import * |
10 |
|
11 |
from logic import credits |
12 |
|
13 |
from django.test import TestCase |
14 |
|
15 |
class CostsTestCase(TestCase): |
16 |
fixtures = [ 'db_test_data' ]
|
17 |
|
18 |
def test_get_costs(self): |
19 |
"""Test the Flavor cost-related methods method"""
|
20 |
# first an easy test, a Flavor with only one FlavorCost entry
|
21 |
flavor = Flavor.objects.get(pk=30001)
|
22 |
|
23 |
start_date = datetime.datetime(year=2010, month=1, day=1) |
24 |
end_date = datetime.datetime(year=2010, month=1, day=2) |
25 |
|
26 |
# we now that the cost should be 5*24 (inactive) and 10*24 (active)
|
27 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
28 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
29 |
|
30 |
self.assertEqual(len(r_active), 1, 'get_cost_active() should have returned 1 entry (%d)' %(len(r_active),)) |
31 |
self.assertEqual(len(r_inactive), 1, 'get_cost_inactive() should have returned 1 entry (%d)'% (len(r_inactive),)) |
32 |
|
33 |
self.assertEqual(10*24, r_active[0][1], 'get_cost_active() is not working properly (%d!=%d)' % (r_active[0][1], 10*24)) |
34 |
self.assertEqual(5*24, r_inactive[0][1], 'get_cost_inactive() is not working properly (%d!=%d)' % (r_inactive[0][1], 5*24)) |
35 |
|
36 |
# The second test, will involve a more complex cost example
|
37 |
# The overall cost will be calculated by two FlavorCost entries
|
38 |
|
39 |
flavor = Flavor.objects.get(pk=30000)
|
40 |
|
41 |
start_date = datetime.datetime(year=2010, month=12, day=31) |
42 |
end_date = datetime.datetime(year=2011, month=01, day=2) |
43 |
|
44 |
# this is more complicated, active costs are 5*24 + 10*24 = 360
|
45 |
# and inactive costs are 2*24 + 5*24 = 168
|
46 |
|
47 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
48 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
49 |
|
50 |
self.assertEqual(len(r_active), 2, 'get_cost_active() should have returned 2 entries (%d)' %(len(r_active),)) |
51 |
self.assertEqual(len(r_inactive), 2, 'get_cost_inactive() should have returned 2 entries (%d)'% (len(r_inactive),)) |
52 |
|
53 |
ta_cost = sum([x[1] for x in r_active]) |
54 |
tia_cost = sum([x[1] for x in r_inactive]) |
55 |
|
56 |
self.assertEqual(360, ta_cost, 'get_cost_active() is not working properly (%d!=%d)' % (ta_cost, 360)) |
57 |
self.assertEqual(168, tia_cost, 'get_cost_inactive() is not working properly (%d!=%d)' % (tia_cost, 168)) |
58 |
|
59 |
|
60 |
class ChargeTestCase(TestCase): |
61 |
fixtures = [ 'db_test_data' ]
|
62 |
|
63 |
def test_charge_method(self): |
64 |
"""Test VirtualMachine.charge() method"""
|
65 |
|
66 |
# Since we have tested the costs, with this test
|
67 |
# we must ensure the following:
|
68 |
# 1. The vm.charged is updated
|
69 |
# 2. Users credits are decreased
|
70 |
|
71 |
vm_started = VirtualMachine.objects.get(pk=30000)
|
72 |
|
73 |
initial_date = vm_started.charged |
74 |
initial_credits = vm_started.owner.credit |
75 |
|
76 |
credits.charge(vm_started) |
77 |
|
78 |
self.assertTrue(vm_started.charged > initial_date, 'Initial charged date should not be greater') |
79 |
self.assertTrue(initial_credits > vm_started.owner.credit, 'The user should have less credits now! (%d>%d)' % (initial_credits, vm_started.owner.credit)) |
80 |
|
81 |
|
82 |
class DebitAccountTestCase(TestCase): |
83 |
fixtures = [ 'db_test_data' ]
|
84 |
|
85 |
def test_debit_account(self): |
86 |
"""Test a SynnefoUser object"""
|
87 |
s_user = SynnefoUser.objects.get(pk=30000)
|
88 |
v_machine = VirtualMachine.objects.get(pk=30000)
|
89 |
|
90 |
# charge the user
|
91 |
credits.debit_account(s_user, 10, v_machine, "This should be a structured debit message!") |
92 |
|
93 |
# should have only one debit object
|
94 |
d_list = Debit.objects.all() |
95 |
|
96 |
self.assertEqual(len(d_list), 1, 'debit_account() writes more than one or zero (%d) debit entries!' % ( len(d_list), )) |
97 |
|
98 |
# retrieve the user, now he/she should have zero credits
|
99 |
s_user = SynnefoUser.objects.get(pk=30000)
|
100 |
|
101 |
self.assertEqual(0, s_user.credit, 'SynnefoUser (pk=30000) should have zero credits (%d)' % ( s_user.credit, )) |
102 |
|
103 |
class AuthTestCase(TestCase): |
104 |
fixtures = ['api_test_data', 'auth_test_data'] |
105 |
apibase = '/api/v1.0'
|
106 |
|
107 |
def setUp(self): |
108 |
self.client = Client()
|
109 |
|
110 |
def test_auth_headers(self): |
111 |
""" test whether the authentication mechanism sets the correct headers
|
112 |
"""
|
113 |
#Check with non-existing user
|
114 |
response = self.client.get( self.apibase + '/servers', {}, |
115 |
**{'X-Auth-User':'notme', |
116 |
'X-Auth-Key':'0xdeadbabe'}) |
117 |
self.assertEquals(response.status_code, 401) |
118 |
|
119 |
#Check with existing user
|
120 |
response = self.client.get( self.apibase + '/', {}, |
121 |
**{'X-Auth-User':'testuser', |
122 |
'X-Auth-Key':'testuserpasswd'}) |
123 |
self.assertEquals(response.status_code, 204) |
124 |
self.assertNotEqual(response['X-Auth-Token'], None) |
125 |
self.assertEquals(response['X-Server-Management-Url'], '') |
126 |
self.assertEquals(response['X-Storage-Url'], '') |
127 |
self.assertEquals(response['X-CDN-Management-Url'], '') |
128 |
|
129 |
#Check access now that we do have an auth token
|
130 |
token = response['X-Auth-Token']
|
131 |
response = self.client.get (self.apibase + '/servers/detail', {}, |
132 |
**{'X-Auth-Token': token})
|
133 |
self.assertEquals(response.status_code, 200) |