Statistics
| Branch: | Tag: | Revision:

root / db / tests.py @ dddb0035

History | View | Annotate | Download (9.3 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 FlavorCostHistoryTestCase(unittest.TestCase):
53
    def setUp(self):
54
        """Setup the test"""
55
        flavor = Flavor(pk=3, cpu=10, ram=10, disk=10)
56
        flavor.save()
57
        
58
        # Add the FlavorCostHistory
59
        fch = FlavorCostHistory(pk=1, cost_active=10, cost_inactive=5)
60
        fch.effective_from = date(day=01, month=01, year=2011)
61
        fch.flavor = flavor
62
        fch.save()
63
        
64
        fch = FlavorCostHistory(pk=2, cost_active=2, cost_inactive=1)
65
        fch.effective_from = date(day=01, month=01, year=2010)
66
        fch.flavor = flavor
67
        fch.save()
68
        
69
    def tearDown(self):
70
        """Cleaning up the data"""
71
        flavor = Flavor.objects.get(pk=3)
72
        flavor.delete()
73
        
74
    def test_flavor_cost_history(self):
75
        """Flavor Cost History unit test method"""
76
        flavor = Flavor.objects.get(pk=3)
77
        fch_list = flavor.get_price_list()
78
        
79
        self.assertEquals(len(fch_list), 2, 'Price list should have two objects! (%d!=2)' % ( len(fch_list), ))
80
        
81
        # 2010-10-10, active should be 2, inactive 1
82
        ex_date = date(year=2010, month=10, day=10)
83
        r = FlavorCostHistory.find_cost(fch_list, ex_date)
84
        
85
        self.assertEquals(r.cost_active, 2, 'Active cost for 2010-10-10 should be 2 (%d!=2)' % ( r.cost_active, ))
86
        self.assertEquals(r.cost_inactive, 1, 'Inactive cosr for 2010-10-10 should be 1 (%d!=1)' % ( r.cost_inactive, ))
87
        
88
        # 2011-11-11, active should be 10, inactive 5
89
        ex_date = date(year=2011, month=11, day=11)
90
        r = FlavorCostHistory.find_cost(fch_list, ex_date)
91
        self.assertEquals(r.cost_active, 10, 'Active cost for 2011-11-11 should be 10 (%d!=10)' % ( r.cost_active, ))
92
        self.assertEquals(r.cost_inactive, 5, 'Inactive cost for 2011-11-11 should be 5 (%d!=5)' % ( r.cost_inactive, ))
93

    
94

    
95
class FlavorTestCase(unittest.TestCase):
96
    def setUp(self):
97
        """Setup the test"""
98
        # Add the Flavor object
99
        flavor = Flavor(pk=1, cpu=10, ram=10, disk=10)
100
        flavor.save()
101
        
102
        # Add the FlavorCostHistory
103
        fch = FlavorCostHistory(pk=1, cost_active=10, cost_inactive=5)
104
        fch.effective_from = date(day=01, month=01, year=2011)
105
        fch.flavor = flavor
106
        fch.save()
107
        
108
        fch = FlavorCostHistory(pk=2, cost_active=2, cost_inactive=1)
109
        fch.effective_from = date(day=01, month=01, year=2010)
110
        fch.flavor = flavor
111
        fch.save()
112
    
113
    def tearDown(self):
114
        """Cleaning up the data"""
115
        flavor = Flavor.objects.get(pk=1)
116
        flavor.delete()
117
    
118
    def test_flavor(self):
119
        """Test a flavor object, its internal cost calculation and naming methods"""
120
        flavor = Flavor.objects.get(pk=1)
121
        
122
        self.assertEquals(flavor.cost_active, 10, 'Active cost is not calculated correctly! (%d!=10)' % ( flavor.cost_active, ) )
123
        self.assertEquals(flavor.cost_inactive, 5, 'Inactive cost is not calculated correctly! (%d!=5)' % ( flavor.cost_inactive, ) )
124
        self.assertEquals(flavor.name, u'C10R10D10', 'Invalid flavor name!')
125

    
126

    
127
class AccountingLogTestCase(unittest.TestCase):
128
    def setUp(self):
129
        """Setup the test"""
130
        userdj = User.objects.create_user('testuser','test','test2')
131
        userdj.save()
132
        
133
        # add a user
134
        user = SynnefoUser(pk=3, name='Test User', credit=100, quota=100, monthly_rate=10)
135
        user.created = datetime.datetime.now()
136
        user.user = userdj
137
        user.violations = 0
138
        user.max_violations = 5
139
        user.save()
140
        
141
        # add an Image
142
        si = Image(name='Test Name')
143
        si.updated = datetime.datetime.now()
144
        si.created = datetime.datetime.now()
145
        si.state = 'ACTIVE'
146
        si.owner = user
147
        si.description = 'testing 1.2.3'
148
        si.save()
149
        
150
        # add a Flavor
151
        flavor = Flavor(pk=11, cpu=10, ram=10, disk=10)
152
        flavor.save()
153
        
154
        # Now, add a VM
155
        vm = VirtualMachine(pk=10)
156
        vm.created = datetime.datetime.now()
157
        vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
158
        vm.hostid = 'testhostid'
159
        vm.server_label = 'agreatserver'
160
        vm.image_version = '1.0.0'
161
        vm.ipfour = '127.0.0.1'
162
        vm.ipsix = '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
163
        vm.flavor = flavor
164
        vm.sourceimage = si
165
        vm.save()
166
        
167
    def tearDown(self):
168
        """Cleaning up the data"""
169
        user = User.objects.get(username='testuser')
170
        user.delete()
171
        
172
        flavor = Flavor.objects.get(pk=11)
173
        flavor.delete()
174
        
175
    def test_accounting_log(self):
176
        """Test the Accounting Log unit method"""
177
        
178

    
179
class ChargerTestCase(unittest.TestCase):
180
    def setUp(self):
181
        """Setup the test"""
182
        userdj = User.objects.create_user('testuser','test','test2')
183
        userdj.save()
184
        
185
        # add a user
186
        user = SynnefoUser(pk=1, name='Test User', credit=100, quota=100, monthly_rate=10)
187
        user.created = datetime.datetime.now()
188
        user.user = userdj
189
        user.violations = 0
190
        user.max_violations = 5
191
        user.save()
192
        
193
        # add a Flavor
194
        flavor = Flavor(pk=1, cpu=10, ram=10, disk=10)
195
        flavor.save()
196
        
197
        # and fill the pricing list
198
        fch = FlavorCostHistory(pk=1, cost_active=10, cost_inactive=5)
199
        fch.effective_from = date(day=01, month=01, year=2010)
200
        fch.flavor = flavor
201
        fch.save()
202
        
203
        # add an Image
204
        si = Image(name='Test Name')
205
        si.updated = datetime.datetime.now()
206
        si.created = datetime.datetime.now()
207
        si.state = 'ACTIVE'
208
        si.owner = user
209
        si.description = 'testing 1.2.3'
210
        si.save()
211
        
212
        # Now, add a VM
213
        vm = VirtualMachine(pk=1)
214
        vm.created = datetime.datetime.now()
215
        vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
216
        vm.hostid = 'testhostid'
217
        vm.server_label = 'agreatserver'
218
        vm.image_version = '1.0.0'
219
        vm.ipfour = '127.0.0.1'
220
        vm.ipsix = '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
221
        vm.owner = user
222
        vm.flavor = flavor
223
        vm.sourceimage = si
224
        
225
        vm.save()
226
    
227
    def tearDown(self):
228
        """Cleaning up the data"""
229
        user = User.objects.get(username="testname")
230
        user.delete()
231
        
232
        flavor = Flavor.objects.get(pk=1)
233
    
234
    def test_charger(self):
235
        """Charger unit test method"""
236
        
237
        # charge when the vm is running
238
        charger.charge()
239
        
240
        user = SynnefoUser.objects.get(pk=1)
241
        self.assertEquals(user.credit, 90, 'Error in charging process (%d!=90, running)' % ( user.credit, ))
242
        
243
        # charge when the vm is stopped
244
        vm = VirtualMachine.objects.get(pk=1)
245
        vm.state = 'PE_VM_STOPPED'
246
        vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
247
        vm.save()
248
        
249
        charger.charge()
250
        
251
        user = SynnefoUser.objects.get(pk=1)
252
        self.assertEquals(user.credit, 85, 'Error in charging process (%d!=85, stopped)' % ( user.credit, ))
253
        
254
        # try charge until the user spends all his credits, see if the charger
255
        vm = VirtualMachine.objects.get(pk=1)
256
        vm.state = 'PE_VM_RUNNING'
257
        vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
258
        vm.save()
259
        
260
        # the user now has 85, charge until his credits drop to zero
261
        for i in range(1, 10):
262
            vm = VirtualMachine.objects.get(pk=1)
263
            vm.charged = datetime.datetime.now() - datetime.timedelta(hours=1)
264
            vm.save()
265
            charger.charge()
266
        
267
        user = SynnefoUser.objects.get(pk=1)
268
        self.assertEquals(user.credit, 0, 'Error in charging process (%d!=0, running)' % ( user.credit, ))
269
        
270