Statistics
| Branch: | Tag: | Revision:

root / db / tests.py @ c0124a81

History | View | Annotate | Download (5.6 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

    
21
class CreditAllocatorTestCase(TestCase):
22
    fixtures = [ 'db_test_data' ]
23
        
24
    def test_credit_allocator(self):
25
        """Credit Allocator unit test method"""
26
        # test the allocator
27
        credit_allocator.allocate_credit()
28
        
29
        user = SynnefoUser.objects.get(pk=30000)
30
        self.assertEquals(user.credit, 10, 'Allocation of credits failed, credit: (%d!=10)' % ( user.credit, ) )
31
        
32
        # get the quota from Limit model and check the answer
33
        limit_quota = user.credit_quota
34
        self.assertEquals(limit_quota, 100, 'User quota has not retrieved correctly (%d!=100)' % ( limit_quota, ))
35
        
36
        # test if the quota policy is endorced
37
        for i in range(1, 10):
38
            credit_allocator.allocate_credit()
39
                
40
        user = SynnefoUser.objects.get(pk=30000)
41
        self.assertEquals(user.credit, limit_quota, 'User exceeded quota! (cr:%d, qu:%d)' % ( user.credit, limit_quota ) )
42

    
43

    
44
class FlavorTestCase(TestCase):
45
    fixtures = [ 'db_test_data' ]
46
    
47
    def test_flavor(self):
48
        """Test a flavor object, its internal cost calculation and naming methods"""
49
        flavor = Flavor.objects.get(pk=30000)
50
        
51
        flavor_name = u'C%dR%dD%d' % ( flavor.cpu, flavor.ram, flavor.disk )
52
        
53
        self.assertEquals(flavor.cost_active, 10, 'Active cost is not calculated correctly! (%d!=10)' % ( flavor.cost_active, ) )
54
        self.assertEquals(flavor.cost_inactive, 5, 'Inactive cost is not calculated correctly! (%d!=5)' % ( flavor.cost_inactive, ) )
55
        self.assertEquals(flavor.name, flavor_name, 'Invalid flavor name!')
56

    
57
    def test_flavor_cost_history(self):
58
        """Flavor unit test (find_cost method)"""
59
        flavor = Flavor.objects.get(pk=30000)
60
        fch_list = flavor.get_price_list()
61

    
62
        self.assertEquals(len(fch_list), 2, 'Price list should have two objects! (%d!=2)' % ( len(fch_list), ))
63

    
64
        # 2010-10-10, active should be 2, inactive 1
65
        ex_date = date(year=2010, month=10, day=10)
66
        r = flavor.find_cost(ex_date)
67

    
68
        self.assertEquals(r.cost_active, 2, 'Active cost for 2010-10-10 should be 2 (%d!=2)' % ( r.cost_active, ))
69
        self.assertEquals(r.cost_inactive, 1, 'Inactive cosr for 2010-10-10 should be 1 (%d!=1)' % ( r.cost_inactive, ))
70

    
71
        # 2011-11-11, active should be 10, inactive 5
72
        ex_date = date(year=2011, month=11, day=11)
73
        r = flavor.find_cost(ex_date)
74
        self.assertEquals(r.cost_active, 10, 'Active cost for 2011-11-11 should be 10 (%d!=10)' % ( r.cost_active, ))
75
        self.assertEquals(r.cost_inactive, 5, 'Inactive cost for 2011-11-11 should be 5 (%d!=5)' % ( r.cost_inactive, ))
76

    
77

    
78
class DebitTestCase(TestCase):
79
    fixtures = [ 'db_test_data' ]
80
                
81
    def test_accounting_log(self):
82
        """Test the Accounting Log unit method"""
83
        vm = VirtualMachine.objects.get(pk=30000)
84
        
85
        # get all entries, should be 2
86
        entries = Debit.get_log_entries(vm, datetime.datetime(year=2009, month=01, day=01))
87
        self.assertEquals(len(entries), 2, 'Log entries should be 2 (%d!=2)' % ( len(entries), ))
88
        
89
        # get enrties only for 2011, should be 1
90
        entries = Debit.get_log_entries(vm, datetime.datetime(year=2011, month=01, day=01))
91
        self.assertEquals(len(entries), 1, 'Log entries should be 1 (%d!=1)' % ( len(entries), ))
92

    
93

    
94
class VirtualMachineTestCase(TestCase):
95
    fixtures = [ 'db_test_data' ]
96
    
97
    def test_virtual_machine(self):
98
        """Virtual Machine (model) unit test method"""
99
        vm = VirtualMachine.objects.get(pk=30002)
100
        
101
        # should be three
102
        acc_logs = vm.get_accounting_logs()
103
        
104
        self.assertEquals(len(acc_logs), 3, 'Log Entries should be 3 (%d!=3)' % ( len(acc_logs), ))
105

    
106

    
107

    
108
class ChargerTestCase(TestCase):
109
    fixtures = [ 'db_test_data' ]
110

    
111
    def test_charger(self):
112
        """Charger unit test method"""
113
        
114
        # user with pk=1 should have 100 credits
115
        user = SynnefoUser.objects.get(pk=1)
116
        user.credit = 100
117
        user.save()
118
        
119
        # charge when the vm is running
120
        charger.charge()
121
        
122
        user = SynnefoUser.objects.get(pk=1)
123
        
124
        self.assertEquals(user.credit, 90, 'Error in charging process (%d!=90, running)' % ( user.credit, ))
125
        
126
        # charge when the vm is stopped
127
        vm = VirtualMachine.objects.get(pk=1)
128
        vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
129
        vm.save()
130
        
131
        charger.charge()
132
        
133
        user = SynnefoUser.objects.get(pk=1)
134
        self.assertEquals(user.credit, 85, 'Error in charging process (%d!=85, stopped)' % ( user.credit, ))
135
        
136
        # try charge until the user spends all his credits, see if the charger
137
        vm = VirtualMachine.objects.get(pk=1)
138
        vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
139
        vm.save()
140
        
141
        # the user now has 85, charge until his credits drop to zero
142
        for i in range(1, 10):
143
            vm = VirtualMachine.objects.get(pk=1)
144
            vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
145
            vm.save()
146
            charger.charge()
147
        
148
        user = SynnefoUser.objects.get(pk=1)
149
        self.assertEquals(user.credit, 0, 'Error in charging process (%d!=0, running)' % ( user.credit, ))
150