Statistics
| Branch: | Tag: | Revision:

root / snf-quotaholder-app / quotaholder_django / test / simpletests.py @ 2fd11a01

History | View | Annotate | Download (15.8 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
    Imported, Exported, Returned, Released)
44

    
45
DEFAULT_HOLDING = (0, 0, 0, 0)
46

    
47
class QHAPITest(QHTestCase):
48

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

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

    
70
    used_entities = ['system']
71

    
72
    @classmethod
73
    def rand_entity(self):
74
        return self.rand_name(self.used_entities)
75

    
76
    used_policies = []
77

    
78
    @classmethod
79
    def rand_policy(self):
80
        return self.rand_name(self.used_policies)
81

    
82
    used_resources = []
83

    
84
    @classmethod
85
    def rand_resource(self):
86
        return self.rand_name(self.used_resources)
87

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

    
95
    def rand_policy_limits(self):
96
        p = self.rand_policy()
97
        limits = self.rand_limits()
98
        return p, limits
99

    
100
    def rand_flags(self):
101
        return Flags.random()
102

    
103
    def rand_counters(self):
104
        return (Imported.random(), Exported.random(),
105
                Returned.random(), Released.random())
106

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

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

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

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

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

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

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

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

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

    
161
    def test_006_get_set_limits(self):
162

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

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

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

    
177
        p0 = self.rand_policy()
178
        f0 = self.rand_flags()
179
        p1, _ = self.new_policy()
180
        f1 = self.rand_flags()
181
        p2, _ = self.new_policy()
182
        f2 = self.rand_flags()
183

    
184
        # none is committed
185
        r = self.qh.set_holding(set_holding=[(e, resource, k, p0, f0),
186
                                             (e, resource, k, p1, f1),
187
                                             (e, resource, k, p2, f2)])
188
        self.assertEqual(r, [(e, resource, p0)])
189

    
190
        r = self.qh.get_holding(get_holding=[(e, resource, k)])
191
        self.assertEqual(r, [])
192

    
193
        r = self.qh.set_holding(set_holding=[(e, resource, k, p1, f1),
194
                                             (e, resource, k, p2, f2)])
195
        self.assertEqual(r, [])
196

    
197
        resource1 = self.rand_resource()
198
        r = self.qh.get_holding(get_holding=[(e, resource, k),
199
                                             (e, resource1, k)])
200
        self.assertEqual(r, [(e, resource, p2) + DEFAULT_HOLDING + (f2,)])
201

    
202
    def test_008_get_set_quota(self):
203
        e, k = self.new_entity()
204
        resource = self.rand_resource()
205
        limits = self.rand_limits()
206
        limits1 = self.rand_limits()
207
        f = self.rand_flags()
208
        r = self.qh.set_quota(set_quota=[(e, resource, k) + limits + (f,),
209
                                         (e, resource, k) + limits1 + (f,)])
210
        self.assertEqual(r, [])
211

    
212
        resource2 = self.rand_resource()
213
        r = self.qh.get_quota(get_quota=[(e, resource, k),
214
                                         (e, resource2, k)])
215
        self.assertEqual(r, [(e, resource) + limits1 +
216
                             DEFAULT_HOLDING + (f,)])
217

    
218
    def new_quota(self, entity, key, resource):
219
        limits = self.rand_limits()
220
        f = self.rand_flags()
221
        r = self.qh.set_quota(
222
            set_quota=[(entity, resource, key) + limits + (f,)])
223
        self.assertEqual(r, [])
224
        return limits
225

    
226
    def test_0081_add_quota(self):
227
        e0, k0 = self.new_entity()
228
        e1, k1 = self.new_entity()
229
        resource0 = self.rand_resource()
230
        resource1 = self.rand_resource()
231

    
232
        r = self.qh.set_quota(
233
            set_quota=[(e0, resource0, k0) + (5, 5, 5, 5) + (0,),
234
                       (e1, resource0, k1) + (5, 5, 5, 5) + (0,)])
235
        self.assertEqual(r, [])
236

    
237
        r = self.qh.add_quota(clientkey=self.client,
238
                              serial=1,
239
                              add_quota=[(e0, resource0, k0, 0, (-2), None, 0),
240
                                         (e0, resource1, k0, 0, None, 5, 5)])
241
        self.assertEqual(r, [])
242

    
243
        r = self.qh.get_quota(get_quota=[(e0, resource0, k0),
244
                                         (e0, resource1, k0)])
245
        self.assertEqual(r, [(e0, resource0, 5, 5 - 2, None, 5)
246
                             + DEFAULT_HOLDING + (0,),
247
                             (e0, resource1, 0, None, 5, 5)
248
                             + DEFAULT_HOLDING + (0,)])
249

    
250
        # repeated serial
251
        r = self.qh.add_quota(clientkey=self.client,
252
                              serial=1,
253
                              add_quota=[(e0, resource0, k0, 0, 2, None, 0),
254
                                         (e0, resource1, k0, 0, None, (-5), 0)])
255
        self.assertEqual(r, [(e0, resource0), (e0, resource1)])
256

    
257
        r = self.qh.query_serials(clientkey=self.client, serials=[1, 2])
258
        self.assertEqual(r, [1])
259

    
260
        r = self.qh.query_serials(clientkey=self.client, serials=[])
261
        self.assertEqual(r, [1])
262

    
263
        r = self.qh.query_serials(clientkey=self.client, serials=[2])
264
        self.assertEqual(r, [])
265

    
266
        r = self.qh.ack_serials(clientkey=self.client, serials=[1])
267

    
268
        r = self.qh.query_serials(clientkey=self.client, serials=[1, 2])
269
        self.assertEqual(r, [])
270

    
271
        # idempotent
272
        r = self.qh.ack_serials(clientkey=self.client, serials=[1])
273

    
274
        # serial has been deleted
275
        r = self.qh.add_quota(clientkey=self.client,
276
                              serial=1,
277
                              add_quota=[(e0, resource0, k0, 0, 2, None, 0)])
278
        self.assertEqual(r, [])
279

    
280
        # none is committed
281
        r = self.qh.add_quota(clientkey=self.client,
282
                              serial=2,
283
                              add_quota=[(e1, resource0, k1, 0, (-10), None, 0),
284
                                         (e0, resource1, k0, 1, 0, 0, 0)])
285
        self.assertEqual(r, [(e1, resource0)])
286

    
287
        r = self.qh.get_quota(get_quota=[(e1, resource0, k1),
288
                                         (e0, resource1, k0)])
289
        self.assertEqual(r, [(e1, resource0, 5, 5 , 5, 5)
290
                             + DEFAULT_HOLDING + (0,),
291
                             (e0, resource1, 0, None, 5, 5)
292
                             + DEFAULT_HOLDING + (0,)])
293

    
294
    def test_009_commissions(self):
295
        e0, k0 = self.new_entity()
296
        e1, k1 = self.new_entity()
297
        resource = self.rand_resource()
298
        q0, c0, il0, el0 = self.new_quota(e0, k0, resource)
299
        q1, c1, il1, el1 = self.new_quota(e1, k1, resource)
300

    
301
        most = max(0, min(c0, il0, q1, el1))
302
        r = self.qh.issue_commission(clientkey=self.client, target=e0, key=k0,
303
                                     name='something',
304
                                     provisions=[(e1, resource, most)])
305
        self.assertEqual(r, 1)
306

    
307
        with self.assertRaises(CallError):
308
            self.qh.issue_commission(clientkey=self.client, target=e0, key=k0,
309
                                     name='something',
310
                                     provisions=[(e1, resource, 1)])
311

    
312
        r = self.qh.get_pending_commissions(clientkey=self.client)
313
        self.assertEqual(r, [1])
314
        r = self.qh.resolve_pending_commissions(clientkey=self.client,
315
                                                max_serial=1, accept_set=[1])
316
        r = self.qh.get_pending_commissions(clientkey=self.client)
317
        self.assertEqual(r, [])
318

    
319
    def test_010_list_holdings(self):
320
        e0, k0 = ('list_holdings_one', '1')
321
        e1, k1 = ('list_holdings_two', '1')
322
        resource = 'list_holdings_resource'
323
        sys = 'system'
324

    
325
        r = self.qh.create_entity(create_entity=[(e0, sys, k0, ''),
326
                                                 (e1, sys, k1, '')])
327
        if r:
328
            raise AssertionError("cannot create entities")
329

    
330
        self.qh.set_quota(set_quota=[(sys, resource, '', 10, 0, None, None, 0),
331
                                     (e0, resource, k0, 0, 10, None, None, 0),
332
                                     (e1, resource, k1, 0, 10, None, None, 0)])
333

    
334
        s0 = self.qh.issue_commission(clientkey=self.client, target=e0, key=k0,
335
                                      name='a commission',
336
                                      provisions=[('system', resource, 3)])
337

    
338
        s1 = self.qh.issue_commission(clientkey=self.client, target=e1, key=k1,
339
                                      name='a commission',
340
                                      provisions=[('system', resource, 4)])
341

    
342
        self.qh.accept_commission(clientkey=self.client, serials=[s0, s1])
343

    
344
        holdings_list, rejected = self.qh.list_holdings(list_holdings=[
345
                                                        (e0, k0),
346
                                                        (e1, k1),
347
                                                        (e0+e1, k0+k1)])
348

    
349
        self.assertEqual(rejected, [e0+e1])
350
        self.assertEqual(holdings_list, [[(e0, resource, 3, 0, 0, 0)],
351
                                         [(e1, resource, 4, 0, 0, 0)]])
352

    
353

    
354
    def test_011_release_empty(self):
355
        e, k = self.new_entity()
356
        e0, k0 = self.rand_entity(), Key.random()
357

    
358
        # none is committed
359
        r = self.qh.release_entity(release_entity=[(e, k), (e0, k0)])
360
        self.assertEqual(r, [e0])
361

    
362
        r = self.qh.get_entity(get_entity=[(e, k)])
363
        self.assertEqual(r, [(e, 'system')])
364

    
365
        r = self.qh.release_entity(release_entity=[(e, k)])
366
        self.assertEqual(r, [])
367

    
368
        r = self.qh.get_entity(get_entity=[(e, k)])
369
        self.assertEqual(r, [])
370

    
371
    def test_012_release_nonempty(self):
372
        e, k = self.new_entity()
373
        e1, k1 = self.new_entity(e, k)
374

    
375
        # none is committed
376
        r = self.qh.release_entity(release_entity=[(e, k), (e1, k1)])
377
        self.assertEqual(r, [e])
378

    
379
        r = self.qh.get_entity(get_entity=[(e1, k1)])
380
        self.assertEqual(r, [(e1, e)])
381

    
382
        r = self.qh.release_entity(release_entity=[(e1, k1), (e, k)])
383
        self.assertEqual(r, [])
384

    
385
        r = self.qh.get_entity(get_entity=[(e1, k1)])
386
        self.assertEqual(r, [])
387

    
388
    def test_013_release_nonempty(self):
389
        e, k = self.new_entity()
390
        resource = self.rand_resource()
391
        limits = self.new_quota(e, k, resource)
392
        r = self.qh.release_entity(release_entity=[(e, k)])
393
        self.assertEqual(r, [e])
394
        r = self.qh.release_holding(release_holding=[(e, resource, k)])
395
        self.assertEqual(r, [])
396
        r = self.qh.release_entity(release_entity=[(e, k)])
397
        self.assertEqual(r, [])
398

    
399
    def test_014_reset_holding(self):
400
        e0, k0 = self.new_entity()
401
        e1, k1 = self.new_entity()
402
        resource = self.rand_resource()
403
        p, _ = self.new_policy()
404
        f = self.rand_flags()
405
        r = self.qh.set_holding(set_holding=[(e1, resource, k1, p, f)])
406

    
407
        counters = self.rand_counters()
408

    
409
        # none is committed
410
        r = self.qh.reset_holding(
411
            reset_holding=[(e0, resource, k0) + counters,
412
                           (e1, resource, k1) + counters])
413
        self.assertEqual(r, [0])
414

    
415
        r = self.qh.get_holding(get_holding=[(e1, resource, k1)])
416
        self.assertEqual(r, [(e1, resource, p) + DEFAULT_HOLDING + (f,)])
417

    
418
        r = self.qh.reset_holding(
419
            reset_holding=[(e1, resource, k1) + counters])
420
        self.assertEqual(r, [])
421

    
422
        r = self.qh.get_holding(get_holding=[(e1, resource, k1)])
423
        self.assertEqual(r, [(e1, resource, p) + counters + (f,)])
424

    
425

    
426
if __name__ == "__main__":
427
    import sys
428
    printf("Using {0}", sys.executable)
429
    run_test_case(QHAPITest)