Statistics
| Branch: | Tag: | Revision:

root / logic / tests.py @ 03805fc8

History | View | Annotate | Download (6 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 synnefo.db.models import *
10
from synnefo.logic import backend
11
from synnefo.logic import credits
12
from synnefo.logic import users
13
from django.test import TestCase
14
import time
15

    
16
import hashlib
17

    
18
class CostsTestCase(TestCase):
19
    fixtures = [ 'db_test_data' ]
20

    
21
    def test_get_costs(self):
22
        """Test the Flavor cost-related methods method"""
23
        # first an easy test, a Flavor with only one FlavorCost entry
24
        flavor = Flavor.objects.get(pk=30001)
25

    
26
        start_date = datetime.datetime(year=2010, month=1, day=1)
27
        end_date = datetime.datetime(year=2010, month=1, day=2)
28

    
29
        # we now that the cost should be 5*24 (inactive) and 10*24 (active)
30
        r_active = credits.get_cost_active(flavor, start_date, end_date)
31
        r_inactive = credits.get_cost_inactive(flavor, start_date, end_date)
32

    
33
        self.assertEqual(len(r_active), 1, 'get_cost_active() should have returned 1 entry (%d)' %(len(r_active),))
34
        self.assertEqual(len(r_inactive), 1, 'get_cost_inactive() should have returned 1 entry (%d)'% (len(r_inactive),))
35

    
36
        self.assertEqual(10*24, r_active[0][1], 'get_cost_active() is not working properly (%d!=%d)' % (r_active[0][1], 10*24))
37
        self.assertEqual(5*24, r_inactive[0][1], 'get_cost_inactive() is not working properly (%d!=%d)' % (r_inactive[0][1], 5*24))
38

    
39
        # The second test, will involve a more complex cost example
40
        # The overall cost will be calculated by two FlavorCost entries
41

    
42
        flavor = Flavor.objects.get(pk=30000)
43

    
44
        start_date = datetime.datetime(year=2010, month=12, day=31)
45
        end_date = datetime.datetime(year=2011, month=01, day=2)
46

    
47
        # this is more complicated, active costs are 5*24 + 10*24 = 360
48
        # and inactive costs are 2*24 + 5*24 = 168
49

    
50
        r_active = credits.get_cost_active(flavor, start_date, end_date)
51
        r_inactive = credits.get_cost_inactive(flavor, start_date, end_date)
52

    
53
        self.assertEqual(len(r_active), 2, 'get_cost_active() should have returned 2 entries (%d)' %(len(r_active),))
54
        self.assertEqual(len(r_inactive), 2, 'get_cost_inactive() should have returned 2 entries (%d)'% (len(r_inactive),))
55

    
56
        ta_cost = sum([x[1] for x in r_active])
57
        tia_cost = sum([x[1] for x in r_inactive])
58

    
59
        self.assertEqual(360, ta_cost, 'get_cost_active() is not working properly (%d!=%d)' % (ta_cost, 360))
60
        self.assertEqual(168, tia_cost, 'get_cost_inactive() is not working properly (%d!=%d)' % (tia_cost, 168))
61

    
62
        
63
class ChargeTestCase(TestCase):
64
    fixtures = [ 'db_test_data' ]
65

    
66
    def test_charge_method(self):
67
        """Test VirtualMachine.charge() method"""
68

    
69
        # Since we have tested the costs, with this test
70
        # we must ensure the following:
71
        # 1. The vm.charged is updated
72
        # 2. Users credits are decreased
73

    
74
        vm_started = VirtualMachine.objects.get(pk=30000)
75

    
76
        initial_date = vm_started.charged
77
        initial_credits = vm_started.owner.credit
78

    
79
        credits.charge(vm_started)
80

    
81
        self.assertTrue(vm_started.charged > initial_date, 'Initial charged date should not be greater')
82
        self.assertTrue(initial_credits > vm_started.owner.credit, 'The user should have less credits now! (%d>%d)' % (initial_credits, vm_started.owner.credit))
83

    
84

    
85
class DebitAccountTestCase(TestCase):
86
    fixtures = [ 'db_test_data' ]
87

    
88
    def test_debit_account(self):
89
        """Test a SynnefoUser object"""
90
        s_user = SynnefoUser.objects.get(pk=30000)
91
        v_machine = VirtualMachine.objects.get(pk=30000)
92

    
93
        # charge the user
94
        credits.debit_account(s_user, 10, v_machine, "This should be a structured debit message!")
95

    
96
        # should have only one debit object
97
        d_list = Debit.objects.all()
98

    
99
        self.assertEqual(len(d_list), 1, 'debit_account() writes more than one or zero (%d) debit entries!' % ( len(d_list), ))
100

    
101
        # retrieve the user, now he/she should have zero credits
102
        s_user = SynnefoUser.objects.get(pk=30000)
103

    
104
        self.assertEqual(0, s_user.credit, 'SynnefoUser (pk=30000) should have zero credits (%d)' % ( s_user.credit, ))
105

    
106
class AuthTestCase(TestCase):
107
    fixtures = [ 'db_test_data' ]
108

    
109
    def _register_user(self):
110
        users.register_student ("Jimmy Page", "jpage", "jpage@zoso.com")
111
        self.user = SynnefoUser.objects.get(name = "jpage")
112

    
113
    def test_register(self):
114
        """ test user registration
115
        """
116
        self._register_user()
117
        self.assertNotEquals(self.user, None)
118

    
119
        #Check hash generation
120
        md5 = hashlib.md5()
121
        md5.update(self.user.uniq)
122
        md5.update(self.user.name)
123
        md5.update(time.asctime())
124

    
125
        self.assertEquals(self.user.auth_token, md5.hexdigest())
126

    
127
    def test_del_user(self):
128
        """ test user deletion
129
        """
130
        self._register_user()
131
        self.assertNotEquals(self.user, None)
132
        
133
        users.delete_user(self.user)
134

    
135
        self.assertRaises(SynnefoUser.DoesNotExist, SynnefoUser.objects.get, name = "jpage")
136

    
137

    
138
class ProcessNetStatusTestCase(TestCase):
139
    fixtures = ['db_test_data']
140
    def test_set_ipv4(self):
141
        """Test reception of a net status notification"""
142
        msg = {'instance': 'instance-name',
143
               'type':     'ganeti-net-status',
144
               'nics': [
145
                   {'ip': '192.168.33.1', 'mac': 'aa:00:00:58:1e:b9'}
146
               ]
147
        }
148
        vm = VirtualMachine.objects.get(pk=30000)
149
        backend.process_net_status(vm, msg['nics'])
150
        self.assertEquals(vm.ipfour, '192.168.33.1')
151

    
152
    def test_set_empty_ipv4(self):
153
        """Test reception of a net status notification with no IPv4 assigned"""
154
        msg = {'instance': 'instance-name',
155
               'type':     'ganeti-net-status',
156
               'nics': [
157
                   {'ip': '', 'mac': 'aa:00:00:58:1e:b9'}
158
               ]
159
        }
160
        vm = VirtualMachine.objects.get(pk=30000)
161
        backend.process_net_status(vm, msg['nics'])
162
        self.assertEquals(vm.ipfour, '0.0.0.0')