Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / quotas / tests.py @ 9db290b4

History | View | Annotate | Download (9 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
    maxDiff = None
49

    
50
    def test_no_holdings(self):
51
        holdings = util.get_db_holdings(user=None)
52
        self.assertEqual(holdings, {})
53

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

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

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

    
111

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

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

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

    
136

    
137
class GetCommissionInfoTest(TestCase):
138
    maxDiff = None
139

    
140
    def test_commissions(self):
141
        flavor = mfactory.FlavorFactory(cpu=2, ram=1024, disk=20)
142
        vm = mfactory.VirtualMachineFactory(flavor=flavor)
143
        mfactory.VolumeFactory(size=20, machine=vm, deleted=False,
144
                               delete_on_termination=True)
145
        #commission = quotas.get_commission_info(vm, "BUILD")
146
        #self.assertEqual({"cyclades.vm": 1,
147
        #                  "cyclades.cpu": 2,
148
        #                  "cyclades.cpu": 2,
149
        #                  "cyclades.ram": 1048576 * 1024,
150
        #                  "cyclades.ram": 1048576 * 1024,
151
        #                  "cyclades.disk": 1073741824 * 20}, commission)
152
        vm.operstate = "STARTED"
153
        vm.save()
154
        commission = quotas.get_commission_info(vm, "STOP")
155
        self.assertEqual({"cyclades.cpu": -2,
156
                          "cyclades.ram": 1048576 * -1024}, commission)
157
        # Check None quotas if vm is already stopped
158
        vm.operstate = "STOPPED"
159
        vm.save()
160
        commission = quotas.get_commission_info(vm, "STOP")
161
        self.assertEqual(None, commission)
162
        commission = quotas.get_commission_info(vm, "START")
163
        self.assertEqual({"cyclades.cpu": 2,
164
                          "cyclades.ram": 1048576 * 1024}, commission)
165
        vm.operstate = "STARTED"
166
        vm.save()
167
        commission = quotas.get_commission_info(vm, "DESTROY")
168
        self.assertEqual({"cyclades.vm": -1,
169
                          "cyclades.total_cpu": -2,
170
                          "cyclades.cpu": -2,
171
                          "cyclades.total_ram": 1048576 * -1024,
172
                          "cyclades.ram": 1048576 * -1024,
173
                          "cyclades.disk": 1073741824 * -20}, commission)
174
        vm.operstate = "STOPPED"
175
        vm.save()
176
        commission = quotas.get_commission_info(vm, "DESTROY")
177
        self.assertEqual({"cyclades.vm": -1,
178
                          "cyclades.total_cpu": -2,
179
                          "cyclades.total_ram": -1024 << 20,
180
                          "cyclades.disk": -20 << 30}, commission)
181
        commission = quotas.get_commission_info(vm, "RESIZE")
182
        self.assertEqual(None, commission)
183
        commission = quotas.get_commission_info(vm, "RESIZE",
184
                                                {"beparams": {"vcpus": 4,
185
                                                              "maxmem": 2048}})
186
        self.assertEqual({"cyclades.total_cpu": 2,
187
                          "cyclades.total_ram": 1048576 * 1024}, commission)
188
        vm.operstate = "STOPPED"
189
        vm.save()
190
        commission = quotas.get_commission_info(vm, "REBOOT")
191
        self.assertEqual({"cyclades.cpu": 2,
192
                          "cyclades.ram": 1048576 * 1024}, commission)
193
        vm.operstate = "STARTED"
194
        vm.save()
195
        commission = quotas.get_commission_info(vm, "REBOOT")
196
        self.assertEqual(None, commission)