root / db / tests.py @ 13b3c5ff
History | View | Annotate | Download (5.7 kB)
1 |
#
|
---|---|
2 |
# Unit Tests for db
|
3 |
#
|
4 |
# Provides automated tests for db module
|
5 |
#
|
6 |
# Copyright 2010 Greek Research and Technology Network
|
7 |
#
|
8 |
|
9 |
import unittest |
10 |
|
11 |
from datetime import datetime, date, timedelta |
12 |
|
13 |
from db.models import * |
14 |
from db import credit_allocator |
15 |
from db import charger |
16 |
|
17 |
from django.conf import settings |
18 |
from django.contrib.auth.models import User |
19 |
from django.test import TestCase |
20 |
from django.test import skip |
21 |
|
22 |
class CreditAllocatorTestCase(TestCase): |
23 |
fixtures = [ 'db_test_data' ]
|
24 |
|
25 |
def test_credit_allocator(self): |
26 |
"""Credit Allocator unit test method"""
|
27 |
# test the allocator
|
28 |
credit_allocator.allocate_credit() |
29 |
|
30 |
user = SynnefoUser.objects.get(pk=30000)
|
31 |
self.assertEquals(user.credit, 10, 'Allocation of credits failed, credit: (%d!=10)' % ( user.credit, ) ) |
32 |
|
33 |
# get the quota from Limit model and check the answer
|
34 |
limit_quota = user.credit_quota |
35 |
self.assertEquals(limit_quota, 100, 'User quota has not retrieved correctly (%d!=100)' % ( limit_quota, )) |
36 |
|
37 |
# test if the quota policy is endorced
|
38 |
for i in range(1, 10): |
39 |
credit_allocator.allocate_credit() |
40 |
|
41 |
user = SynnefoUser.objects.get(pk=30000)
|
42 |
self.assertEquals(user.credit, limit_quota, 'User exceeded quota! (cr:%d, qu:%d)' % ( user.credit, limit_quota ) ) |
43 |
|
44 |
|
45 |
class FlavorTestCase(TestCase): |
46 |
fixtures = [ 'db_test_data' ]
|
47 |
|
48 |
def test_flavor(self): |
49 |
"""Test a flavor object, its internal cost calculation and naming methods"""
|
50 |
flavor = Flavor.objects.get(pk=30000)
|
51 |
|
52 |
flavor_name = u'C%dR%dD%d' % ( flavor.cpu, flavor.ram, flavor.disk )
|
53 |
|
54 |
self.assertEquals(flavor.cost_active, 10, 'Active cost is not calculated correctly! (%d!=10)' % ( flavor.cost_active, ) ) |
55 |
self.assertEquals(flavor.cost_inactive, 5, 'Inactive cost is not calculated correctly! (%d!=5)' % ( flavor.cost_inactive, ) ) |
56 |
self.assertEquals(flavor.name, flavor_name, 'Invalid flavor name!') |
57 |
|
58 |
def test_flavor_cost_history(self): |
59 |
"""Flavor unit test (find_cost method)"""
|
60 |
flavor = Flavor.objects.get(pk=30000)
|
61 |
fch_list = flavor.get_price_list() |
62 |
|
63 |
self.assertEquals(len(fch_list), 2, 'Price list should have two objects! (%d!=2)' % ( len(fch_list), )) |
64 |
|
65 |
# 2010-10-10, active should be 2, inactive 1
|
66 |
ex_date = date(year=2010, month=10, day=10) |
67 |
r = flavor.find_cost(ex_date) |
68 |
|
69 |
self.assertEquals(r.cost_active, 2, 'Active cost for 2010-10-10 should be 2 (%d!=2)' % ( r.cost_active, )) |
70 |
self.assertEquals(r.cost_inactive, 1, 'Inactive cosr for 2010-10-10 should be 1 (%d!=1)' % ( r.cost_inactive, )) |
71 |
|
72 |
# 2011-11-11, active should be 10, inactive 5
|
73 |
ex_date = date(year=2011, month=11, day=11) |
74 |
r = flavor.find_cost(ex_date) |
75 |
self.assertEquals(r.cost_active, 10, 'Active cost for 2011-11-11 should be 10 (%d!=10)' % ( r.cost_active, )) |
76 |
self.assertEquals(r.cost_inactive, 5, 'Inactive cost for 2011-11-11 should be 5 (%d!=5)' % ( r.cost_inactive, )) |
77 |
|
78 |
|
79 |
class AccountingLogTestCase(TestCase): |
80 |
fixtures = [ 'db_test_data' ]
|
81 |
|
82 |
def test_accounting_log(self): |
83 |
"""Test the Accounting Log unit method"""
|
84 |
vm = VirtualMachine.objects.get(pk=30000)
|
85 |
|
86 |
# get all entries, should be 2
|
87 |
entries = AccountingLog.get_log_entries(vm, datetime.datetime(year=2009, month=01, day=01)) |
88 |
self.assertEquals(len(entries), 2, 'Log entries should be 2 (%d!=2)' % ( len(entries), )) |
89 |
|
90 |
# get enrties only for 2011, should be 1
|
91 |
entries = AccountingLog.get_log_entries(vm, datetime.datetime(year=2011, month=01, day=01)) |
92 |
self.assertEquals(len(entries), 1, 'Log entries should be 1 (%d!=1)' % ( len(entries), )) |
93 |
|
94 |
|
95 |
class VirtualMachineTestCase(TestCase): |
96 |
fixtures = [ 'db_test_data' ]
|
97 |
|
98 |
def test_virtual_machine(self): |
99 |
"""Virtual Machine (model) unit test method"""
|
100 |
vm = VirtualMachine.objects.get(pk=30002)
|
101 |
|
102 |
# should be three
|
103 |
acc_logs = vm.get_accounting_logs() |
104 |
|
105 |
self.assertEquals(len(acc_logs), 3, 'Log Entries should be 3 (%d!=3)' % ( len(acc_logs), )) |
106 |
|
107 |
|
108 |
|
109 |
class ChargerTestCase(TestCase): |
110 |
fixtures = [ 'db_test_data' ]
|
111 |
|
112 |
@skip
|
113 |
def test_charger(self): |
114 |
"""Charger unit test method"""
|
115 |
|
116 |
# user with pk=1 should have 100 credits
|
117 |
user = SynnefoUser.objects.get(pk=1)
|
118 |
user.credit = 100
|
119 |
user.save() |
120 |
|
121 |
# charge when the vm is running
|
122 |
charger.charge() |
123 |
|
124 |
user = SynnefoUser.objects.get(pk=1)
|
125 |
|
126 |
self.assertEquals(user.credit, 90, 'Error in charging process (%d!=90, running)' % ( user.credit, )) |
127 |
|
128 |
# charge when the vm is stopped
|
129 |
vm = VirtualMachine.objects.get(pk=1)
|
130 |
vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
|
131 |
vm.save() |
132 |
|
133 |
charger.charge() |
134 |
|
135 |
user = SynnefoUser.objects.get(pk=1)
|
136 |
self.assertEquals(user.credit, 85, 'Error in charging process (%d!=85, stopped)' % ( user.credit, )) |
137 |
|
138 |
# try charge until the user spends all his credits, see if the charger
|
139 |
vm = VirtualMachine.objects.get(pk=1)
|
140 |
vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
|
141 |
vm.save() |
142 |
|
143 |
# the user now has 85, charge until his credits drop to zero
|
144 |
for i in range(1, 10): |
145 |
vm = VirtualMachine.objects.get(pk=1)
|
146 |
vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
|
147 |
vm.save() |
148 |
charger.charge() |
149 |
|
150 |
user = SynnefoUser.objects.get(pk=1)
|
151 |
self.assertEquals(user.credit, 0, 'Error in charging process (%d!=0, running)' % ( user.credit, )) |
152 |
|