Statistics
| Branch: | Tag: | Revision:

root / snf-quotaholder-app / quotaholder_django / test / limits.py @ e6f3e652

History | View | Annotate | Download (5 kB)

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

    
34
from config import QHTestCase
35
from config import run_test_case
36
from config import printf
37
from config import rand_string
38
from random import randint
39

    
40
from synnefo.lib.quotaholder.api import InvalidDataError
41

    
42
class Data:
43
    def __init__(self, parent, **kwd):
44
        self.context  = kwd.get('context', parent.context)
45
        self.policy   = kwd.get('policy', parent.policy)
46
        self.capacity = kwd.get('capacity', parent.capacity)
47
        self.quantity = kwd.get('quantity', parent.quantity)
48
        self.import_limit = kwd.get('import_limit', parent.import_limit)
49
        self.export_limit = kwd.get('export_limit', parent.export_limit)
50
        # set_limits_has_rejected indicates whether the test expects a non-empty rejected list
51
        # after calling set_limits
52
        self.set_limits_has_rejected = kwd.get('set_limits_has_rejected', False)
53
        # get_limits_expected_length indicates the exact length of the limits list
54
        # returned by get_limits
55
        self.get_limits_expected_length = kwd.get('get_limits_expected_length', 1)
56

    
57
class LimitsTest(QHTestCase):
58
    context = {}
59
    policy = rand_string()
60
    capacity1 = 0
61
    capacity2 = 100
62
    capacity = capacity1
63
    quantity1 = capacity2
64
    quantity2 = capacity1
65
    quantity = quantity1
66
    import_limit_empty = 0
67
    import_limit_full_capacity = capacity
68
    import_limit_half_capacity = capacity / 2
69
    import_limit = import_limit_half_capacity
70
    export_limit_empty = 0
71
    export_limit_full_capacity = capacity
72
    export_limit_half_capacity = capacity / 2
73
    export_limit = export_limit_half_capacity
74

    
75
    def helper_set_limits(self, **kwd):
76
        """
77
        Calls set_limits and returns the rejected list (from the original API).
78
        """
79
        data = Data(self, **kwd)
80
        rejected = self.qh.set_limits(
81
            context = data.context,
82
            set_limits = [
83
                (data.policy,
84
                 data.quantity,
85
                 data.capacity,
86
                 data.import_limit,
87
                 data.export_limit)
88
            ]
89
        )
90

    
91
        if data.set_limits_has_rejected:
92
            self.assertTrue(len(rejected) > 1)
93
        else:
94
            self.assertTrue(len(rejected) == 0)
95

    
96
        return rejected
97

    
98
    def helper_get_limits(self, **kwd):
99
        """
100
        Calls get_limits and returns the limits list (from the original API)..
101
        """
102
        data = Data(self, **kwd)
103
        limits = self.qh.get_limits(
104
            context = data.context,
105
            get_limits = [data.policy]
106
        )
107

    
108
        self.assertEqual(len(limits), data.get_limits_expected_length)
109

    
110
        return limits
111

    
112
    def test_010_set_get(self):
113
        """
114
        quantity = 0, capacity = 100
115
        """
116
        self.helper_set_limits(quantity = 0, capacity = 100, should_have_rejected = False)
117
        self.helper_get_limits(get_limits_expected_length = 1)
118

    
119
    def test_011_set_get(self):
120
        """
121
        quantity = 100, capacity = 0
122
        """
123
        self.helper_set_limits(quantity = 100, capacity = 0, should_have_rejected = False)
124
        self.helper_get_limits(get_limits_expected_length = 1)
125

    
126
    def test_020_set_get_empty_policy_name(self):
127
        """
128
        Tests empty policy name
129
        """
130
        with self.assertRaises(InvalidDataError):
131
            self.helper_set_limits(policy = '', set_limits_has_rejected = False)
132
            self.helper_get_limits(policy = '', get_limits_expected_length = 1)
133

    
134

    
135
if __name__ == "__main__":
136
    import sys
137
    printf("Using {0}", sys.executable)
138
    run_test_case(LimitsTest)