Statistics
| Branch: | Tag: | Revision:

root / db / tests.py @ 3846dfd0

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

    
20
class CreditAllocatorTestCase(unittest.TestCase):
21
    def setUp(self):
22
        """Setup the test"""
23
        userdj = User.objects.create(username='testuser')
24
        userdj.save()
25
        
26
        user = SynnefoUser(name='CreditAllocatorTestUser', credit=0, quota=100, monthly_rate=10)
27
        user.created = datetime.datetime.now()
28
        user.user = User.objects.get(username='testuser')
29
        user.save()
30
    
31
    def tearDown(self):
32
        """Cleaning up the data"""
33
        user = User.objects.get(username="testuser")
34
        user.delete()
35
    
36
    def test_credit_allocator(self):
37
        """Credit Allocator unit test method"""
38
        # test the allocator
39
        credit_allocator.allocate_credit()
40
        
41
        user = SynnefoUser.objects.get(name='CreditAllocatorTestUser')
42
        self.assertEquals(user.credit, 10, 'Allocation of credits failed, credit: %d (should be 10)' % ( user.credit, ) )
43
        
44
        # test if the quota policy is endorced
45
        for i in range(1, 10):
46
            credit_allocator.allocate_credit()
47
                
48
        user = SynnefoUser.objects.get(name='CreditAllocatorTestUser')
49
        self.assertEquals(user.credit, user.quota, 'User exceeded quota! (cr:%d, qu:%d)' % ( user.credit, user.quota ) )
50

    
51

    
52
class FlavorTestCase(unittest.TestCase):
53
    def setUp(self):
54
        """Setup the test"""
55
        # Add the Flavor object
56
        flavor = Flavor(pk=1, cpu=10, ram=10, disk=10)
57
        flavor.save()
58
        
59
        # Add the FlavorCostHistory
60
        fch = FlavorCostHistory(pk=1, cost_active=10, cost_inactive=5)
61
        fch.effective_from = date(day=01, month=01, year=2011)
62
        fch.flavor = flavor
63
        fch.save()
64
        
65
        fch = FlavorCostHistory(pk=2, cost_active=2, cost_inactive=1)
66
        fch.effective_from = date(day=01, month=01, year=2010)
67
        fch.flavor = flavor
68
        fch.save()
69
    
70
    def tearDown(self):
71
        """Cleaning up the data"""
72
        flavor = Flavor.objects.get(pk=1)
73
        flavor.delete()
74
        
75
        fch = FlavorCostHistory(pk=2)
76
        fch.delete()
77
    
78
    def test_flavor(self):
79
        """Test a flavor object, its internal cost calculation and naming methods"""
80
        flavor = Flavor.objects.get(pk=1)
81
        
82
        self.assertEquals(flavor.cost_active, 10, 'Active cost is not calculated correctly! (%d!=10)' % ( flavor.cost_active, ) )
83
        self.assertEquals(flavor.cost_inactive, 5, 'Inactive cost is not calculated correctly! (%d!=5)' % ( flavor.cost_inactive, ) )
84
        self.assertEquals(flavor.name, u'C10R10D10', 'Invalid flavor name!')
85

    
86

    
87
class ChargerTestCase(unittest.TestCase):
88
    def setUp(self):
89
        """Setup the test"""
90
        userdj = User.objects.create_user('testuser','test','test2')
91
        userdj.save()
92
        
93
        # add a user
94
        user = SynnefoUser(pk=1, name='Test User', credit=100, quota=100, monthly_rate=10)
95
        user.created = datetime.datetime.now()
96
        user.user = userdj
97
        user.save()
98
        
99
        # add a Flavor
100
        flavor = Flavor(pk=1, cpu=10, ram=10, disk=10)
101
        flavor.save()
102
        
103
        # and fill the pricing list
104
        fch = FlavorCostHistory(pk=1, cost_active=10, cost_inactive=5)
105
        fch.effective_from = date(day=01, month=01, year=2010)
106
        fch.flavor = flavor
107
        fch.save()
108
        
109
        # add an Image
110
        si = Image(name='Test Name')
111
        si.updated = datetime.datetime.now()
112
        si.created = datetime.datetime.now()
113
        si.state = 'ACTIVE'
114
        si.owner = user
115
        si.description = 'testing 1.2.3'
116
        si.save()
117
        
118
        # Now, add a VM
119
        vm = VirtualMachine(pk=1)
120
        vm.created = datetime.datetime.now()
121
        vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
122
        vm.hostid = 'testhostid'
123
        vm.server_label = 'agreatserver'
124
        vm.image_version = '1.0.0'
125
        vm.ipfour = '127.0.0.1'
126
        vm.ipsix = '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
127
        vm.owner = user
128
        vm.flavor = flavor
129
        vm.sourceimage = si
130
        
131
        vm.save()
132
    
133
    def tearDown(self):
134
        """Cleaning up the data"""
135
        user = User.objects.get(username="testname")
136
        user.delete()
137
        
138
        flavor = Flavor.objects.get(pk=1)
139
    
140
    def test_charger(self):
141
        """Charger unit test method"""
142
        
143
        # charge when the vm is running
144
        charger.charge()
145
        
146
        user = SynnefoUser.objects.get(pk=1)
147
        self.assertEquals(user.credit, 90, 'Error in charging process (%d!=90, running)' % ( user.credit, ))
148
        
149
        # charge when the vm is stopped
150
        vm = VirtualMachine.objects.get(pk=1)
151
        vm.state = 'PE_VM_STOPPED'
152
        vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
153
        vm.save()
154
        
155
        charger.charge()
156
        
157
        user = SynnefoUser.objects.get(pk=1)
158
        self.assertEquals(user.credit, 85, 'Error in charging process (%d!=85, stopped)' % ( user.credit, ))
159
        
160
        # try charge until the user spends all his credits, see if the charger
161
        vm = VirtualMachine.objects.get(pk=1)
162
        vm.state = 'PE_VM_RUNNING'
163
        vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
164
        vm.save()
165
        
166
        # the user now has 85, charge until his credits drop to zero
167
        for i in range(1, 10):
168
            vm = VirtualMachine.objects.get(pk=1)
169
            vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
170
            vm.save()
171
            charger.charge()
172
        
173
        user = SynnefoUser.objects.get(pk=1)
174
        self.assertEquals(user.credit, 0, 'Error in charging process (%d!=0, running)' % ( user.credit, ))
175
        
176