Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / quotas / tests.py @ 326c3ec8

History | View | Annotate | Download (8.7 kB)

1
#!/usr/bin/env python
2
#
3
# -*- coding: utf-8 -*-
4
#
5
# Copyright 2013 GRNET S.A. All rights reserved.
6
#
7
# Redistribution and use in source and binary forms, with or
8
# without modification, are permitted provided that the following
9
# conditions are met:
10
#
11
#   1. Redistributions of source code must retain the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer.
14
#
15
#   2. Redistributions in binary form must reproduce the above
16
#      copyright notice, this list of conditions and the following
17
#      disclaimer in the documentation and/or other materials
18
#      provided with the distribution.
19
#
20
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
21
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
24
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
# POSSIBILITY OF SUCH DAMAGE.
32
#
33
# The views and conclusions contained in the software and
34
# documentation are those of the authors and should not be
35
# interpreted as representing official policies, either expressed
36
# or implied, of GRNET S.A.
37
#
38
#
39
from mock import patch
40
from django.test import TestCase
41
from synnefo.db import models_factory as mfactory
42

    
43
from synnefo import quotas
44
from synnefo.quotas import util
45

    
46

    
47
class GetDBHoldingsTestCase(TestCase):
48
    def test_no_holdings(self):
49
        holdings = util.get_db_holdings(user=None)
50
        self.assertEqual(holdings, {})
51

    
52
    def test_vm_holdings(self):
53
        flavor = mfactory.FlavorFactory(cpu=24, ram=8192, disk=20,
54
                                        disk_template='drbd')
55
        mfactory.VirtualMachineFactory()
56
        mfactory.VirtualMachineFactory(flavor=flavor, userid="user1",
57
                                       operstate="BUILD")
58
        user_holdings = {"user1": {"cyclades.vm": 1,
59
                                   "cyclades.cpu": 24,
60
                                   "cyclades.active_cpu": 24,
61
                                   "cyclades.disk": 21474836480,
62
                                   "cyclades.ram": 8589934592,
63
                                   "cyclades.active_ram": 8589934592}}
64
        holdings = util.get_db_holdings(user="user1")
65
        self.assertEqual(holdings, user_holdings)
66
        holdings = util.get_db_holdings()
67
        self.assertEqual(holdings["user1"], user_holdings["user1"])
68
        mfactory.VirtualMachineFactory(flavor=flavor, userid="user1")
69
        ##
70
        mfactory.VirtualMachineFactory(flavor=flavor, userid="user2",
71
                                       operstate="STARTED")
72
        user_holdings = {"user2": {"cyclades.vm": 1,
73
                                   "cyclades.cpu": 24,
74
                                   "cyclades.active_cpu": 24,
75
                                   "cyclades.disk": 21474836480,
76
                                   "cyclades.ram": 8589934592,
77
                                   "cyclades.active_ram": 8589934592}}
78
        holdings = util.get_db_holdings(user="user2")
79
        self.assertEqual(holdings, user_holdings)
80
        mfactory.VirtualMachineFactory(flavor=flavor, userid="user3",
81
                                       operstate="STOPPED")
82
        user_holdings = {"user3": {"cyclades.vm": 1,
83
                                   "cyclades.cpu": 24,
84
                                   "cyclades.disk": 21474836480,
85
                                   "cyclades.ram": 8589934592}}
86
        holdings = util.get_db_holdings(user="user3")
87
        self.assertEqual(holdings, user_holdings)
88

    
89

    
90
    def test_network_holdings(self):
91
        mfactory.NetworkFactory(userid="user1")
92
        mfactory.NetworkFactory(userid="user2")
93
        user_holdings = {"user2": {"cyclades.network.private": 1}}
94
        holdings = util.get_db_holdings(user="user2")
95
        self.assertEqual(holdings, user_holdings)
96
        holdings = util.get_db_holdings()
97
        self.assertEqual(holdings["user2"], user_holdings["user2"])
98

    
99
    def test_floating_ip_holdings(self):
100
        mfactory.IPAddressFactory(userid="user1")
101
        mfactory.IPAddressFactory(userid="user1")
102
        mfactory.IPAddressFactory(userid="user2")
103
        mfactory.IPAddressFactory(userid="user3")
104
        holdings = util.get_db_holdings()
105
        self.assertEqual(holdings["user1"]["cyclades.floating_ip"], 2)
106
        self.assertEqual(holdings["user2"]["cyclades.floating_ip"], 1)
107
        self.assertEqual(holdings["user3"]["cyclades.floating_ip"], 1)
108

    
109

    
110
@patch("synnefo.quotas.get_quotaholder_pending")
111
class ResolvePendingTestCase(TestCase):
112
    def setUp(self):
113
        self.p1 = mfactory.QuotaHolderSerialFactory(serial=20, pending=True)
114
        self.p1 = mfactory.QuotaHolderSerialFactory(serial=30, pending=True)
115
        self.a1 = mfactory.QuotaHolderSerialFactory(serial=15, pending=False,
116
                                                    accept=True)
117
        self.a2 = mfactory.QuotaHolderSerialFactory(serial=25, pending=False,
118
                                                    accept=True)
119
        self.r1 = mfactory.QuotaHolderSerialFactory(serial=18, pending=False,
120
                                                    accept=False)
121
        self.r2 = mfactory.QuotaHolderSerialFactory(serial=23, pending=False,
122
                                                    accept=False)
123

    
124
    def test_no_pending(self, qh):
125
        qh.return_value = []
126
        pending = quotas.resolve_pending_commissions()
127
        self.assertEqual(pending, ([], []))
128

    
129
    def test_1(self, qh):
130
        qh.return_value = [21, 25, 28]
131
        pending = quotas.resolve_pending_commissions()
132
        self.assertEqual(pending, ([25], [28, 21]))
133

    
134

    
135
class GetCommissionInfoTest(TestCase):
136
    def test_commissions(self):
137
        flavor = mfactory.FlavorFactory(cpu=2, ram=1024, disk=20)
138
        vm = mfactory.VirtualMachineFactory(flavor=flavor)
139
        #commission = quotas.get_commission_info(vm, "BUILD")
140
        #self.assertEqual({"cyclades.vm": 1,
141
        #                  "cyclades.cpu": 2,
142
        #                  "cyclades.active_cpu": 2,
143
        #                  "cyclades.ram": 1048576 * 1024,
144
        #                  "cyclades.active_ram": 1048576 * 1024,
145
        #                  "cyclades.disk": 1073741824 * 20}, commission)
146
        vm.operstate = "STARTED"
147
        vm.save()
148
        commission = quotas.get_commission_info(vm, "STOP")
149
        self.assertEqual({"cyclades.active_cpu": -2,
150
                          "cyclades.active_ram": 1048576 * -1024}, commission)
151
        # Check None quotas if vm is already stopped
152
        vm.operstate = "STOPPED"
153
        vm.save()
154
        commission = quotas.get_commission_info(vm, "STOP")
155
        self.assertEqual(None, commission)
156
        commission = quotas.get_commission_info(vm, "START")
157
        self.assertEqual({"cyclades.active_cpu": 2,
158
                          "cyclades.active_ram": 1048576 * 1024}, commission)
159
        vm.operstate = "STARTED"
160
        vm.save()
161
        commission = quotas.get_commission_info(vm, "DESTROY")
162
        self.assertEqual({"cyclades.vm": -1,
163
                          "cyclades.cpu": -2,
164
                          "cyclades.active_cpu": -2,
165
                          "cyclades.ram": 1048576 * -1024,
166
                          "cyclades.active_ram": 1048576 * -1024,
167
                          "cyclades.disk": 1073741824 * -20}, commission)
168
        vm.operstate = "STOPPED"
169
        vm.save()
170
        commission = quotas.get_commission_info(vm, "DESTROY")
171
        self.assertEqual({"cyclades.vm": -1,
172
                          "cyclades.cpu": -2,
173
                          "cyclades.ram": 1048576 * -1024,
174
                          "cyclades.disk": 1073741824 * -20}, commission)
175
        commission = quotas.get_commission_info(vm, "RESIZE")
176
        self.assertEqual(None, commission)
177
        commission = quotas.get_commission_info(vm, "RESIZE",
178
                                                {"beparams": {"vcpus": 4,
179
                                                              "maxmem":2048}})
180
        self.assertEqual({"cyclades.cpu": 2,
181
                          "cyclades.ram": 1048576 * 1024}, commission)
182
        vm.operstate = "STOPPED"
183
        vm.save()
184
        commission = quotas.get_commission_info(vm, "REBOOT")
185
        self.assertEqual({"cyclades.active_cpu": 2,
186
                          "cyclades.active_ram": 1048576 * 1024}, commission)
187
        vm.operstate = "STARTED"
188
        vm.save()
189
        commission = quotas.get_commission_info(vm, "REBOOT")
190
        self.assertEqual(None, commission)