Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / quotas / tests.py @ 0c1b6127

History | View | Annotate | Download (8.7 kB)

1 d552dddd Christos Stavrakakis
#!/usr/bin/env python
2 d552dddd Christos Stavrakakis
#
3 d552dddd Christos Stavrakakis
# -*- coding: utf-8 -*-
4 d552dddd Christos Stavrakakis
#
5 d552dddd Christos Stavrakakis
# Copyright 2013 GRNET S.A. All rights reserved.
6 d552dddd Christos Stavrakakis
#
7 d552dddd Christos Stavrakakis
# Redistribution and use in source and binary forms, with or
8 d552dddd Christos Stavrakakis
# without modification, are permitted provided that the following
9 d552dddd Christos Stavrakakis
# conditions are met:
10 d552dddd Christos Stavrakakis
#
11 d552dddd Christos Stavrakakis
#   1. Redistributions of source code must retain the above
12 d552dddd Christos Stavrakakis
#      copyright notice, this list of conditions and the following
13 d552dddd Christos Stavrakakis
#      disclaimer.
14 d552dddd Christos Stavrakakis
#
15 d552dddd Christos Stavrakakis
#   2. Redistributions in binary form must reproduce the above
16 d552dddd Christos Stavrakakis
#      copyright notice, this list of conditions and the following
17 d552dddd Christos Stavrakakis
#      disclaimer in the documentation and/or other materials
18 d552dddd Christos Stavrakakis
#      provided with the distribution.
19 d552dddd Christos Stavrakakis
#
20 d552dddd Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
21 d552dddd Christos Stavrakakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 d552dddd Christos Stavrakakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 d552dddd Christos Stavrakakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
24 d552dddd Christos Stavrakakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 d552dddd Christos Stavrakakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 d552dddd Christos Stavrakakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 d552dddd Christos Stavrakakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 d552dddd Christos Stavrakakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 d552dddd Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 d552dddd Christos Stavrakakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 d552dddd Christos Stavrakakis
# POSSIBILITY OF SUCH DAMAGE.
32 d552dddd Christos Stavrakakis
#
33 d552dddd Christos Stavrakakis
# The views and conclusions contained in the software and
34 d552dddd Christos Stavrakakis
# documentation are those of the authors and should not be
35 d552dddd Christos Stavrakakis
# interpreted as representing official policies, either expressed
36 d552dddd Christos Stavrakakis
# or implied, of GRNET S.A.
37 d552dddd Christos Stavrakakis
#
38 d552dddd Christos Stavrakakis
#
39 d552dddd Christos Stavrakakis
from mock import patch
40 d552dddd Christos Stavrakakis
from django.test import TestCase
41 d552dddd Christos Stavrakakis
from synnefo.db import models_factory as mfactory
42 d552dddd Christos Stavrakakis
43 d552dddd Christos Stavrakakis
from synnefo import quotas
44 d552dddd Christos Stavrakakis
from synnefo.quotas import util
45 d552dddd Christos Stavrakakis
46 d552dddd Christos Stavrakakis
47 d552dddd Christos Stavrakakis
class GetDBHoldingsTestCase(TestCase):
48 d552dddd Christos Stavrakakis
    def test_no_holdings(self):
49 656cf771 Christos Stavrakakis
        holdings = util.get_db_holdings(user=None)
50 d552dddd Christos Stavrakakis
        self.assertEqual(holdings, {})
51 d552dddd Christos Stavrakakis
52 d552dddd Christos Stavrakakis
    def test_vm_holdings(self):
53 d552dddd Christos Stavrakakis
        flavor = mfactory.FlavorFactory(cpu=24, ram=8192, disk=20,
54 41a7fae7 Christos Stavrakakis
                                        disk_template='drbd')
55 d552dddd Christos Stavrakakis
        mfactory.VirtualMachineFactory()
56 41a7fae7 Christos Stavrakakis
        mfactory.VirtualMachineFactory(flavor=flavor, userid="user1",
57 41a7fae7 Christos Stavrakakis
                                       operstate="BUILD")
58 656cf771 Christos Stavrakakis
        user_holdings = {"user1": {"cyclades.vm": 1,
59 656cf771 Christos Stavrakakis
                                   "cyclades.cpu": 24,
60 41a7fae7 Christos Stavrakakis
                                   "cyclades.active_cpu": 24,
61 656cf771 Christos Stavrakakis
                                   "cyclades.disk": 21474836480,
62 41a7fae7 Christos Stavrakakis
                                   "cyclades.ram": 8589934592,
63 41a7fae7 Christos Stavrakakis
                                   "cyclades.active_ram": 8589934592}}
64 656cf771 Christos Stavrakakis
        holdings = util.get_db_holdings(user="user1")
65 d552dddd Christos Stavrakakis
        self.assertEqual(holdings, user_holdings)
66 d552dddd Christos Stavrakakis
        holdings = util.get_db_holdings()
67 d552dddd Christos Stavrakakis
        self.assertEqual(holdings["user1"], user_holdings["user1"])
68 41a7fae7 Christos Stavrakakis
        mfactory.VirtualMachineFactory(flavor=flavor, userid="user1")
69 41a7fae7 Christos Stavrakakis
        ##
70 41a7fae7 Christos Stavrakakis
        mfactory.VirtualMachineFactory(flavor=flavor, userid="user2",
71 41a7fae7 Christos Stavrakakis
                                       operstate="STARTED")
72 41a7fae7 Christos Stavrakakis
        user_holdings = {"user2": {"cyclades.vm": 1,
73 41a7fae7 Christos Stavrakakis
                                   "cyclades.cpu": 24,
74 41a7fae7 Christos Stavrakakis
                                   "cyclades.active_cpu": 24,
75 41a7fae7 Christos Stavrakakis
                                   "cyclades.disk": 21474836480,
76 41a7fae7 Christos Stavrakakis
                                   "cyclades.ram": 8589934592,
77 41a7fae7 Christos Stavrakakis
                                   "cyclades.active_ram": 8589934592}}
78 41a7fae7 Christos Stavrakakis
        holdings = util.get_db_holdings(user="user2")
79 41a7fae7 Christos Stavrakakis
        self.assertEqual(holdings, user_holdings)
80 41a7fae7 Christos Stavrakakis
        mfactory.VirtualMachineFactory(flavor=flavor, userid="user3",
81 41a7fae7 Christos Stavrakakis
                                       operstate="STOPPED")
82 41a7fae7 Christos Stavrakakis
        user_holdings = {"user3": {"cyclades.vm": 1,
83 41a7fae7 Christos Stavrakakis
                                   "cyclades.cpu": 24,
84 41a7fae7 Christos Stavrakakis
                                   "cyclades.disk": 21474836480,
85 41a7fae7 Christos Stavrakakis
                                   "cyclades.ram": 8589934592}}
86 41a7fae7 Christos Stavrakakis
        holdings = util.get_db_holdings(user="user3")
87 41a7fae7 Christos Stavrakakis
        self.assertEqual(holdings, user_holdings)
88 41a7fae7 Christos Stavrakakis
89 d552dddd Christos Stavrakakis
90 d552dddd Christos Stavrakakis
    def test_network_holdings(self):
91 d552dddd Christos Stavrakakis
        mfactory.NetworkFactory(userid="user1")
92 d552dddd Christos Stavrakakis
        mfactory.NetworkFactory(userid="user2")
93 656cf771 Christos Stavrakakis
        user_holdings = {"user2": {"cyclades.network.private": 1}}
94 656cf771 Christos Stavrakakis
        holdings = util.get_db_holdings(user="user2")
95 d552dddd Christos Stavrakakis
        self.assertEqual(holdings, user_holdings)
96 d552dddd Christos Stavrakakis
        holdings = util.get_db_holdings()
97 d552dddd Christos Stavrakakis
        self.assertEqual(holdings["user2"], user_holdings["user2"])
98 d552dddd Christos Stavrakakis
99 8b41683a Christos Stavrakakis
    def test_floating_ip_holdings(self):
100 8b41683a Christos Stavrakakis
        mfactory.FloatingIPFactory(userid="user1")
101 8b41683a Christos Stavrakakis
        mfactory.FloatingIPFactory(userid="user1")
102 8b41683a Christos Stavrakakis
        mfactory.FloatingIPFactory(userid="user2")
103 8b41683a Christos Stavrakakis
        mfactory.FloatingIPFactory(userid="user3")
104 8b41683a Christos Stavrakakis
        holdings = util.get_db_holdings()
105 8b41683a Christos Stavrakakis
        self.assertEqual(holdings["user1"]["cyclades.floating_ip"], 2)
106 8b41683a Christos Stavrakakis
        self.assertEqual(holdings["user2"]["cyclades.floating_ip"], 1)
107 8b41683a Christos Stavrakakis
        self.assertEqual(holdings["user3"]["cyclades.floating_ip"], 1)
108 8b41683a Christos Stavrakakis
109 d552dddd Christos Stavrakakis
110 d552dddd Christos Stavrakakis
@patch("synnefo.quotas.get_quotaholder_pending")
111 d552dddd Christos Stavrakakis
class ResolvePendingTestCase(TestCase):
112 d552dddd Christos Stavrakakis
    def setUp(self):
113 d552dddd Christos Stavrakakis
        self.p1 = mfactory.QuotaHolderSerialFactory(serial=20, pending=True)
114 d552dddd Christos Stavrakakis
        self.p1 = mfactory.QuotaHolderSerialFactory(serial=30, pending=True)
115 184a2a8c Christos Stavrakakis
        self.a1 = mfactory.QuotaHolderSerialFactory(serial=15, pending=False,
116 184a2a8c Christos Stavrakakis
                                                    accept=True)
117 184a2a8c Christos Stavrakakis
        self.a2 = mfactory.QuotaHolderSerialFactory(serial=25, pending=False,
118 184a2a8c Christos Stavrakakis
                                                    accept=True)
119 184a2a8c Christos Stavrakakis
        self.r1 = mfactory.QuotaHolderSerialFactory(serial=18, pending=False,
120 184a2a8c Christos Stavrakakis
                                                    accept=False)
121 184a2a8c Christos Stavrakakis
        self.r2 = mfactory.QuotaHolderSerialFactory(serial=23, pending=False,
122 184a2a8c Christos Stavrakakis
                                                    accept=False)
123 d552dddd Christos Stavrakakis
124 d552dddd Christos Stavrakakis
    def test_no_pending(self, qh):
125 d552dddd Christos Stavrakakis
        qh.return_value = []
126 d552dddd Christos Stavrakakis
        pending = quotas.resolve_pending_commissions()
127 d552dddd Christos Stavrakakis
        self.assertEqual(pending, ([], []))
128 d552dddd Christos Stavrakakis
129 d552dddd Christos Stavrakakis
    def test_1(self, qh):
130 d552dddd Christos Stavrakakis
        qh.return_value = [21, 25, 28]
131 d552dddd Christos Stavrakakis
        pending = quotas.resolve_pending_commissions()
132 d552dddd Christos Stavrakakis
        self.assertEqual(pending, ([25], [28, 21]))
133 41a7fae7 Christos Stavrakakis
134 41a7fae7 Christos Stavrakakis
135 41a7fae7 Christos Stavrakakis
class GetCommissionInfoTest(TestCase):
136 41a7fae7 Christos Stavrakakis
    def test_commissions(self):
137 41a7fae7 Christos Stavrakakis
        flavor = mfactory.FlavorFactory(cpu=2, ram=1024, disk=20)
138 41a7fae7 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory(flavor=flavor)
139 41a7fae7 Christos Stavrakakis
        #commission = quotas.get_commission_info(vm, "BUILD")
140 41a7fae7 Christos Stavrakakis
        #self.assertEqual({"cyclades.vm": 1,
141 41a7fae7 Christos Stavrakakis
        #                  "cyclades.cpu": 2,
142 41a7fae7 Christos Stavrakakis
        #                  "cyclades.active_cpu": 2,
143 41a7fae7 Christos Stavrakakis
        #                  "cyclades.ram": 1048576 * 1024,
144 41a7fae7 Christos Stavrakakis
        #                  "cyclades.active_ram": 1048576 * 1024,
145 41a7fae7 Christos Stavrakakis
        #                  "cyclades.disk": 1073741824 * 20}, commission)
146 41a7fae7 Christos Stavrakakis
        vm.operstate = "STARTED"
147 41a7fae7 Christos Stavrakakis
        vm.save()
148 41a7fae7 Christos Stavrakakis
        commission = quotas.get_commission_info(vm, "STOP")
149 41a7fae7 Christos Stavrakakis
        self.assertEqual({"cyclades.active_cpu": -2,
150 41a7fae7 Christos Stavrakakis
                          "cyclades.active_ram": 1048576 * -1024}, commission)
151 41a7fae7 Christos Stavrakakis
        # Check None quotas if vm is already stopped
152 41a7fae7 Christos Stavrakakis
        vm.operstate = "STOPPED"
153 41a7fae7 Christos Stavrakakis
        vm.save()
154 41a7fae7 Christos Stavrakakis
        commission = quotas.get_commission_info(vm, "STOP")
155 41a7fae7 Christos Stavrakakis
        self.assertEqual(None, commission)
156 41a7fae7 Christos Stavrakakis
        commission = quotas.get_commission_info(vm, "START")
157 41a7fae7 Christos Stavrakakis
        self.assertEqual({"cyclades.active_cpu": 2,
158 41a7fae7 Christos Stavrakakis
                          "cyclades.active_ram": 1048576 * 1024}, commission)
159 41a7fae7 Christos Stavrakakis
        vm.operstate = "STARTED"
160 41a7fae7 Christos Stavrakakis
        vm.save()
161 41a7fae7 Christos Stavrakakis
        commission = quotas.get_commission_info(vm, "DESTROY")
162 41a7fae7 Christos Stavrakakis
        self.assertEqual({"cyclades.vm": -1,
163 41a7fae7 Christos Stavrakakis
                          "cyclades.cpu": -2,
164 41a7fae7 Christos Stavrakakis
                          "cyclades.active_cpu": -2,
165 41a7fae7 Christos Stavrakakis
                          "cyclades.ram": 1048576 * -1024,
166 41a7fae7 Christos Stavrakakis
                          "cyclades.active_ram": 1048576 * -1024,
167 41a7fae7 Christos Stavrakakis
                          "cyclades.disk": 1073741824 * -20}, commission)
168 41a7fae7 Christos Stavrakakis
        vm.operstate = "STOPPED"
169 41a7fae7 Christos Stavrakakis
        vm.save()
170 41a7fae7 Christos Stavrakakis
        commission = quotas.get_commission_info(vm, "DESTROY")
171 41a7fae7 Christos Stavrakakis
        self.assertEqual({"cyclades.vm": -1,
172 41a7fae7 Christos Stavrakakis
                          "cyclades.cpu": -2,
173 41a7fae7 Christos Stavrakakis
                          "cyclades.ram": 1048576 * -1024,
174 41a7fae7 Christos Stavrakakis
                          "cyclades.disk": 1073741824 * -20}, commission)
175 41a7fae7 Christos Stavrakakis
        commission = quotas.get_commission_info(vm, "RESIZE")
176 41a7fae7 Christos Stavrakakis
        self.assertEqual(None, commission)
177 41a7fae7 Christos Stavrakakis
        commission = quotas.get_commission_info(vm, "RESIZE",
178 41a7fae7 Christos Stavrakakis
                                                {"beparams": {"vcpus": 4,
179 41a7fae7 Christos Stavrakakis
                                                              "maxmem":2048}})
180 41a7fae7 Christos Stavrakakis
        self.assertEqual({"cyclades.cpu": 2,
181 41a7fae7 Christos Stavrakakis
                          "cyclades.ram": 1048576 * 1024}, commission)
182 41a7fae7 Christos Stavrakakis
        vm.operstate = "STOPPED"
183 41a7fae7 Christos Stavrakakis
        vm.save()
184 41a7fae7 Christos Stavrakakis
        commission = quotas.get_commission_info(vm, "REBOOT")
185 41a7fae7 Christos Stavrakakis
        self.assertEqual({"cyclades.active_cpu": 2,
186 41a7fae7 Christos Stavrakakis
                          "cyclades.active_ram": 1048576 * 1024}, commission)
187 41a7fae7 Christos Stavrakakis
        vm.operstate = "STARTED"
188 41a7fae7 Christos Stavrakakis
        vm.save()
189 41a7fae7 Christos Stavrakakis
        commission = quotas.get_commission_info(vm, "REBOOT")
190 41a7fae7 Christos Stavrakakis
        self.assertEqual(None, commission)