Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.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
    def test_no_holdings(self):
49
        users = ["dummy_user1", "dummy_user2"]
50
        holdings = util.get_db_holdings(users=users)
51
        self.assertEqual(holdings, {})
52

    
53
    def test_vm_holdings(self):
54
        flavor = mfactory.FlavorFactory(cpu=24, ram=8192, disk=20,
55
                                  disk_template='drbd')
56
        mfactory.VirtualMachineFactory()
57
        mfactory.VirtualMachineFactory(flavor=flavor, userid="user1")
58
        user_holdings = {"user1": {"vm": 1,
59
                                   "cpu": 24,
60
                                   "disk": 21474836480,
61
                                   "ram": 8589934592}}
62
        holdings = util.get_db_holdings(users=["user1"])
63
        self.assertEqual(holdings, user_holdings)
64
        holdings = util.get_db_holdings()
65
        self.assertEqual(holdings["user1"], user_holdings["user1"])
66

    
67
    def test_network_holdings(self):
68
        mfactory.NetworkFactory(userid="user1")
69
        mfactory.NetworkFactory(userid="user2")
70
        user_holdings = {"user2": {"network.private": 1}}
71
        holdings = util.get_db_holdings(users=["user2"])
72
        self.assertEqual(holdings, user_holdings)
73
        holdings = util.get_db_holdings()
74
        self.assertEqual(holdings["user2"], user_holdings["user2"])
75

    
76

    
77
@patch("synnefo.quotas.get_quotaholder_pending")
78
class ResolvePendingTestCase(TestCase):
79
    def setUp(self):
80
        self.p1 = mfactory.QuotaHolderSerialFactory(serial=20, pending=True)
81
        self.p1 = mfactory.QuotaHolderSerialFactory(serial=30, pending=True)
82
        self.a1 = mfactory.QuotaHolderSerialFactory(serial=15, accepted=True)
83
        self.a2 = mfactory.QuotaHolderSerialFactory(serial=25, accepted=True)
84
        self.r1 = mfactory.QuotaHolderSerialFactory(serial=18, rejected=True)
85
        self.r2 = mfactory.QuotaHolderSerialFactory(serial=23, rejected=True)
86

    
87
    def test_no_pending(self, qh):
88
        qh.return_value = []
89
        pending = quotas.resolve_pending_commissions()
90
        self.assertEqual(pending, ([], []))
91

    
92
    def test_1(self, qh):
93
        qh.return_value = [21, 25, 28]
94
        pending = quotas.resolve_pending_commissions()
95
        self.assertEqual(pending, ([25], [28, 21]))