Statistics
| Branch: | Tag: | Revision:

root / snf-quotaholder-app / quotaholder_django / test / simpletests.py @ 187bd5d9

History | View | Annotate | Download (8.3 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 rand_string
37
from config import printf
38

    
39
from synnefo.lib.commissioning import CallError
40
from synnefo.lib.quotaholder.api import InvalidDataError, NoEntityError
41
from synnefo.lib.quotaholder.api.quotaholder import (
42
    Name, Key, Quantity, Capacity, ImportLimit, ExportLimit, Resource, Flags)
43

    
44
DEFAULT_IMPORTED = 0
45
DEFAULT_EXPORTED = 0
46
DEFAULT_RETURNED = 0
47
DEFAULT_RELEASED = 0
48
DEFAULT_HOLDING = (0, 0, 0, 0)
49

    
50
class QHAPITest(QHTestCase):
51

    
52
    @classmethod
53
    def setUpClass(self):
54
        QHTestCase.setUpClass()
55
        e = self.rand_entity()
56
        k = Key.random()
57
        r = self.qh.create_entity(create_entity=[(e, 'system', k, '')])
58
        self.e_name = e
59
        self.e_key = k
60
        self.client = self.rand_entity()
61

    
62
    @classmethod
63
    def rand_name(self, exclude=[]):
64
        for i in range(1,100):
65
            r = Name().random()
66
            if r not in exclude:
67
                exclude.append(r)
68
                return r
69
        else:
70
            m = 'Could not make a unique random name'
71
            raise Exception(m)
72

    
73
    used_entities = ['system']
74

    
75
    @classmethod
76
    def rand_entity(self):
77
        return self.rand_name(self.used_entities)
78

    
79
    used_policies = []
80

    
81
    @classmethod
82
    def rand_policy(self):
83
        return self.rand_name(self.used_policies)
84

    
85
    used_resources = []
86

    
87
    @classmethod
88
    def rand_resource(self):
89
        return self.rand_name(self.used_resources)
90

    
91
    def rand_limits(self):
92
        q = Capacity.random() # Nonnegative
93
        c = Capacity.random()
94
        il = ImportLimit.random()
95
        el = ExportLimit.random()
96
        return q, c, il, el
97

    
98
    def rand_policy_limits(self):
99
        p = self.rand_policy()
100
        limits = self.rand_limits()
101
        return p, limits
102

    
103
    def rand_flags(self):
104
        return Flags.random()
105

    
106
    def new_entity(self, parent='system', parent_key=''):
107
        e = self.rand_entity()
108
        k = Key.random()
109
        r = self.qh.create_entity(create_entity=[(e, parent, k, parent_key)])
110
        self.assertEqual(r, [])
111
        return e, k
112

    
113
    def new_policy(self):
114
        p, limits = self.rand_policy_limits()
115
        r = self.qh.set_limits(set_limits=[(p,) + limits])
116
        self.assertEqual(r, [])
117
        return p, limits
118

    
119
    def test_001_list_entities(self):
120
        r = self.qh.list_entities(entity='system', key='')
121
        self.assertEqual(r, ['system', self.e_name])
122

    
123
        with self.assertRaises(NoEntityError):
124
            self.qh.list_entities(entity='doesnotexist', key='')
125

    
126
        with self.assertRaises(InvalidDataError):
127
            self.qh.list_entities(entity='system; SELECT ALL', key='')
128

    
129
    def test_002_create_entity(self):
130
        e = self.rand_entity()
131
        k = Key.random()
132
        r = self.qh.create_entity(
133
            create_entity=[(self.e_name, 'system', self.e_key, ''),
134
                           (e, self.e_name, k, self.e_key),
135
                           (e, self.e_name, k, self.e_key)])
136
        self.assertEqual(r, [0,2])
137

    
138
    def test_003_release_entity(self):
139
        e, k = self.new_entity()
140
        r = self.qh.release_entity(release_entity=[(e, k)])
141
        self.assertEqual(r, [])
142

    
143
    def test_004_set_entity_key(self):
144
        e, k = self.new_entity()
145
        k1 = Key.random()
146
        k2 = Key.random()
147
        r = self.qh.set_entity_key(set_entity_key=[(e, k1, k2)])
148
        self.assertEqual(r, [e])
149
        r = self.qh.set_entity_key(set_entity_key=[(e, k, k2)])
150
        self.assertEqual(r, [])
151
        r = self.qh.release_entity(release_entity=[(e, k)])
152
        self.assertEqual(r, [e])
153

    
154
    def test_005_get_entity(self):
155
        e = self.rand_entity()
156
        k = Key.random()
157
        r = self.qh.get_entity(get_entity=[(self.e_name, self.e_key), (e, k)])
158
        self.assertEqual(r, [(self.e_name, 'system')])
159

    
160
    def test_006_get_set_limits(self):
161

    
162
        p1, limits1 = self.rand_policy_limits()
163
        limits2 = self.rand_limits()
164
        r = self.qh.set_limits(set_limits=[(p1,) + limits1,
165
                                           (p1,) + limits2])
166
        self.assertEqual(r, [])
167

    
168
        p2, _ = self.rand_policy_limits()
169
        r = self.qh.get_limits(get_limits=[p1, p2])
170
        self.assertEqual(r, [(p1,) + limits2])
171

    
172
    def test_007_get_set_holding(self):
173
        e, k = self.new_entity()
174
        resource = self.rand_resource()
175

    
176
        p0 = self.rand_policy()
177
        f0 = self.rand_flags()
178
        p1, _ = self.new_policy()
179
        f1 = self.rand_flags()
180
        p2, _ = self.new_policy()
181
        f2 = self.rand_flags()
182
        r = self.qh.set_holding(set_holding=[(e, resource, k, p0, f0),
183
                                             (e, resource, k, p1, f1),
184
                                             (e, resource, k, p2, f2)])
185
        self.assertEqual(r, [(e, resource, p0)])
186

    
187
        resource1 = self.rand_resource()
188
        r = self.qh.get_holding(get_holding=[(e, resource, k),
189
                                             (e, resource1, k)])
190
        self.assertEqual(r, [(e, resource, p2) + DEFAULT_HOLDING + (f2,)])
191

    
192
    def test_008_get_set_quota(self):
193
        e, k = self.new_entity()
194
        resource = self.rand_resource()
195
        limits = self.rand_limits()
196
        limits1 = self.rand_limits()
197
        f = self.rand_flags()
198
        r = self.qh.set_quota(set_quota=[(e, resource, k) + limits + (f,),
199
                                         (e, resource, k) + limits1 + (f,)])
200
        self.assertEqual(r, [])
201

    
202
        resource2 = self.rand_resource()
203
        r = self.qh.get_quota(get_quota=[(e, resource, k),
204
                                         (e, resource2, k)])
205
        self.assertEqual(r, [(e, resource) + limits1 +
206
                             DEFAULT_HOLDING + (f,)])
207

    
208
    def new_quota(self, entity, key, resource):
209
        limits = self.rand_limits()
210
        f = self.rand_flags()
211
        r = self.qh.set_quota(
212
            set_quota=[(entity, resource, key) + limits + (f,)])
213
        self.assertEqual(r, [])
214
        return limits
215

    
216
    def test_009_issue_commission(self):
217
        e0, k0 = self.new_entity()
218
        e1, k1 = self.new_entity()
219
        resource = self.rand_resource()
220
        q0, c0, il0, el0 = self.new_quota(e0, k0, resource)
221
        q1, c1, il1, el1 = self.new_quota(e1, k1, resource)
222

    
223
        most = max(0, min(c0, il0, q1, el1))
224
        print 'limits', (c0, il0, q1, el1)
225
        r = self.qh.issue_commission(clientkey=self.client, target=e0, key=k0,
226
                                     name='something',
227
                                     provisions=[(e1, resource, most)])
228
        self.assertEqual(r, 1)
229

    
230
        with self.assertRaises(CallError):
231
            self.qh.issue_commission(clientkey=self.client, target=e0, key=k0,
232
                                     name='something',
233
                                     provisions=[(e1, resource, 1)])
234

    
235

    
236
if __name__ == "__main__":
237
    import sys
238
    printf("Using {0}", sys.executable)
239
    run_test_case(QHAPITest)