Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder / test / simpletests.py @ 04dcc30e

History | View | Annotate | Download (16.4 kB)

1
# Copyright 2012, 2013 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

    
38
from astakos.quotaholder.exception import (
39
                            QuotaholderError,
40
                            InvalidDataError,
41
                            NoCapacityError,
42
                            NoStockError,
43
                            NonExportedError,
44
                            CommissionValueException,
45
                            DuplicateError)
46

    
47
from astakos.quotaholder.api import QH_PRACTICALLY_INFINITE
48
from astakos.quotaholder.utils.rand import random_int, random_nat, random_name
49

    
50
DEFAULT_HOLDING = (0, 0, 0, 0)
51

    
52
class QHAPITest(QHTestCase):
53

    
54
    @classmethod
55
    def setUpClass(self):
56
        QHTestCase.setUpClass()
57
        self.client = self.rand_holder()
58

    
59
    @classmethod
60
    def rand_name(self, exclude=None):
61
        for i in range(1,100):
62
            r = random_name()
63
            if exclude is not None and 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_holder(self):
74
        return self.rand_name(self.used_entities)
75

    
76
    used_resources = []
77

    
78
    @classmethod
79
    def rand_resource(self):
80
        return self.rand_name(self.used_resources)
81

    
82
    def rand_limits(self):
83
        c = random_nat()
84
        return (c,)
85

    
86
    def rand_flags(self):
87
        return random_nat()
88

    
89
    def rand_counters(self):
90
        return tuple(random_nat() for i in range(4))
91

    
92
    def new_policy(self):
93
        p, limits = self.rand_policy_limits()
94
        self.qh.set_limits(set_limits=[(p,) + limits])
95
        return p, limits
96

    
97
    @transaction.commit_on_success
98
    def test_0080_get_set_quota(self):
99
        e = self.rand_holder()
100
        resource = self.rand_resource()
101
        limits = self.rand_limits()
102
        limits1 = self.rand_limits()
103
        f = self.rand_flags()
104
        self.qh.set_quota(set_quota=[(e, resource) + limits + (f,),
105
                                     (e, resource) + limits1 + (f,)])
106

    
107
        resource2 = self.rand_resource()
108
        r = self.qh.get_quota(get_quota=[(e, resource),
109
                                         (e, resource2)])
110
        self.assertEqual(r, [(e, resource) + limits1 +
111
                             DEFAULT_HOLDING + (f,)])
112

    
113
    def new_quota(self, holder, resource, limits=None):
114
        if limits is None:
115
            limits = self.rand_limits()
116
        f = self.rand_flags()
117
        self.qh.set_quota(
118
            set_quota=[(holder, resource) + limits + (f,)])
119
        return limits
120

    
121
    @transaction.commit_on_success
122
    def test_0081_add_quota(self):
123
        e0 = self.rand_holder()
124
        e1 = self.rand_holder()
125
        resource0 = self.rand_resource()
126
        resource1 = self.rand_resource()
127

    
128
        self.qh.set_quota(
129
            set_quota=[(e0, resource0) + (QH_PRACTICALLY_INFINITE, 0),
130
                       (e1, resource0) + (5, 0)])
131

    
132
        self.qh.add_quota(sub_quota=[(e0, resource0,
133
                                      QH_PRACTICALLY_INFINITE)],
134
                          add_quota=[(e0, resource0,
135
                                      3),
136
                                     # new holding
137
                                     (e0, resource1,
138
                                      QH_PRACTICALLY_INFINITE)])
139

    
140
        r = self.qh.get_quota(get_quota=[(e0, resource0),
141
                                         (e0, resource1)])
142
        self.assertEqual(r, [(e0, resource0, 3)
143
                             + DEFAULT_HOLDING + (0,),
144
                             (e0, resource1, QH_PRACTICALLY_INFINITE)
145
                             + DEFAULT_HOLDING + (0,)])
146

    
147
        with self.assertRaises(QuotaholderError) as cm:
148
            self.qh.add_quota(add_quota=[(e1, resource0,
149
                                          (-10)),
150
                                         (e0, resource1, 0)])
151

    
152
        err = cm.exception
153
        self.assertEqual(err.message, [(e1, resource0)])
154

    
155
        # r = self.qh.get_quota(get_quota=[(e1, resource0),
156
        #                                  (e0, resource1)])
157
        # self.assertEqual(r, [(e1, resource0, 5, 5)
158
        #                      + DEFAULT_HOLDING + (0,),
159
        #                      (e0, resource1, 0, QH_PRACTICALLY_INFINITE)
160
        #                      + DEFAULT_HOLDING + (0,)])
161

    
162
    @transaction.commit_on_success
163
    def test_0082_max_quota(self):
164
        e0 = self.rand_holder()
165
        e1 = self.rand_holder()
166
        resource0 = self.rand_resource()
167
        resource1 = self.rand_resource()
168

    
169
        self.qh.set_quota(
170
            set_quota=[(e0, resource0) +
171
                       (QH_PRACTICALLY_INFINITE,) + (0,)])
172

    
173
        self.qh.add_quota(add_quota=[(e0, resource0,
174
                                      QH_PRACTICALLY_INFINITE)])
175

    
176
        r = self.qh.get_quota(get_quota=[(e0, resource0)])
177
        self.assertEqual(r, [(e0, resource0, 2*QH_PRACTICALLY_INFINITE)
178
                             + DEFAULT_HOLDING + (0,)])
179

    
180
    @transaction.commit_on_success
181
    def initialize_holding(self, holder, resource, quantity):
182
        s = self.qh.issue_commission(clientkey=self.client, target=holder,
183
                                     name='initialize',
184
                                     provisions=[(None, resource, quantity)])
185
        self.qh.accept_commission(clientkey=self.client, serials=[s])
186

    
187
    @transaction.commit_on_success
188
    def issue_commission(self, target, provisions):
189
        return self.qh.issue_commission(clientkey=self.client, target=target,
190
                                        name='something',
191
                                        provisions=provisions)
192

    
193
    @transaction.commit_on_success
194
    def test_0090_commissions(self):
195
        e0 = self.rand_holder()
196
        e1 = self.rand_holder()
197
        resource = self.rand_resource()
198
        c0, = self.new_quota(e0, resource)
199
        c1, = self.new_quota(e1, resource)
200

    
201
        self.initialize_holding(e1, resource, c1)
202

    
203
        most = min(c0, c1)
204
        if most < 0:
205
            raise AssertionError("%s <= 0" % most)
206

    
207
        s1 = self.issue_commission(target=e0,
208
                                   provisions=[(e1, resource, most)])
209
        self.assertGreater(s1, 0)
210

    
211
        with self.assertRaises(CommissionValueException):
212
            self.issue_commission(target=e0,
213
                                  provisions=[(e1, resource, 1)])
214

    
215
        r = self.qh.get_pending_commissions(clientkey=self.client)
216
        self.assertEqual(list(r), [s1])
217
        r = self.qh.resolve_pending_commissions(clientkey=self.client,
218
                                                max_serial=s1, accept_set=[s1])
219
        r = self.qh.get_pending_commissions(clientkey=self.client)
220
        self.assertEqual(list(r), [])
221

    
222
    @transaction.commit_on_success
223
    def test_0091_commissions_exceptions(self):
224
        es1 = self.rand_holder()
225
        es2 = self.rand_holder()
226
        et1 = self.rand_holder()
227
        et2 = self.rand_holder()
228
        resource = self.rand_resource()
229
        self.new_quota(es1, resource, (10,))
230
        self.new_quota(es2, resource, (10,))
231
        self.new_quota(et1, resource, (15,))
232
        self.new_quota(et2, resource, (15,))
233

    
234
        self.initialize_holding(es1, resource, 10)
235
        self.initialize_holding(es2, resource, 10)
236

    
237
        with self.assertRaises(NoStockError) as cm:
238
            self.qh.issue_commission(clientkey=self.client, target=et1,
239
                                     name='something',
240
                                     provisions=[(es1, resource, 12)])
241
        e = cm.exception
242
        self.assertEqual(e.holder, es1)
243
        self.assertEqual(e.resource, resource)
244
        self.assertEqual(int(e.limit), 10)
245
        self.assertEqual(int(e.requested), 12)
246

    
247
        r = self.qh.issue_commission(clientkey=self.client, target=et1,
248
                                     name='something',
249
                                     provisions=[(es1, resource, 2)])
250
        self.assertGreater(r, 0)
251

    
252

    
253

    
254
        r = self.qh.issue_commission(clientkey=self.client, target=et2,
255
                                     name='something',
256
                                     provisions=[(es2, resource, 9)])
257
        self.assertGreater(r, 0)
258

    
259
        with self.assertRaises(NoCapacityError) as cm:
260
            self.qh.issue_commission(clientkey=self.client, target=et2,
261
                                     name='something',
262
                                     provisions=[(es2, resource, 1),
263
                                                 (es1, resource, 6)])
264
        e = cm.exception
265
        self.assertEqual(e.holder, et2)
266
        self.assertEqual(e.resource, resource)
267
        self.assertEqual(int(e.limit), 15)
268
        self.assertEqual(int(e.requested), 6)
269
        # 9 actual + 1 from the first provision
270
        self.assertEqual(int(e.current), 10)
271

    
272
    @transaction.commit_on_success
273
    def test_010_list_holdings(self):
274
        e0 = 'list_holdings_one'
275
        e1 = 'list_holdings_two'
276
        resource = 'list_holdings_resource'
277
        sys = 'system'
278

    
279
        self.qh.set_quota(set_quota=[(sys, resource, 10, 0),
280
                                     (e0, resource, 10, 0),
281
                                     (e1, resource, 10, 0)])
282

    
283
        self.initialize_holding(sys, resource, 10)
284

    
285
        s0 = self.qh.issue_commission(clientkey=self.client, target=e0,
286
                                      name='a commission',
287
                                      provisions=[('system', resource, 3)])
288

    
289
        s1 = self.qh.issue_commission(clientkey=self.client, target=e1,
290
                                      name='a commission',
291
                                      provisions=[('system', resource, 4)])
292

    
293
        holdings_list, rejected = self.qh.list_holdings(
294
            list_holdings=[e0, e1, e0+e1])
295

    
296
        self.assertEqual(rejected, [e0+e1])
297
        self.assertEqual(holdings_list, [[(e0, resource, 0, 3, 0, 0)],
298
                                         [(e1, resource, 0, 4, 0, 0)]])
299

    
300
        self.qh.accept_commission(clientkey=self.client, serials=[s0, s1])
301

    
302
        holdings_list, rejected = self.qh.list_holdings(
303
            list_holdings=[e0, e1, e0+e1])
304

    
305
        self.assertEqual(rejected, [e0+e1])
306
        self.assertEqual(holdings_list, [[(e0, resource, 3, 3, 3, 3)],
307
                                         [(e1, resource, 4, 4, 4, 4)]])
308

    
309

    
310
    @transaction.commit_on_success
311
    def test_0130_release_holding(self):
312
        e = self.rand_holder()
313
        resource = self.rand_resource()
314
        limits = self.new_quota(e, resource, (2,))
315

    
316
        self.initialize_holding(e, resource, 1)
317

    
318
        with self.assertRaises(QuotaholderError) as cm:
319
            self.qh.release_holding(release_holding=[(e, resource)])
320

    
321
        err = cm.exception
322
        self.assertEqual(err.message, [0])
323

    
324
    @transaction.commit_on_success
325
    def test_0131_release_holding(self):
326
        e = self.rand_holder()
327
        resource = self.rand_resource()
328
        limits = self.new_quota(e, resource, (2,))
329

    
330
        self.qh.release_holding(release_holding=[(e, resource)])
331

    
332
    @transaction.commit_on_success
333
    def test_0132_release_holding(self):
334
        resource = self.rand_resource()
335

    
336
        es = self.rand_holder()
337
        limits_s = self.new_quota(es, resource, (3,))
338

    
339
        self.initialize_holding(es, resource, 3)
340

    
341
        e = self.rand_holder()
342
        limits = self.new_quota(e, resource, (2,))
343

    
344
        r = self.qh.issue_commission(clientkey=self.client, target=e,
345
                                     name='something',
346
                                     provisions=[(es, resource, 1)])
347
        self.assertGreater(r, 0)
348

    
349
        with self.assertRaises(QuotaholderError) as cm:
350
            self.qh.release_holding(release_holding=[(e, resource)])
351

    
352
        err = cm.exception
353
        self.assertEqual(err.message, [0])
354

    
355
    @transaction.commit_on_success
356
    def test_014_reset_holding(self):
357
        e0 = self.rand_holder()
358
        e1 = self.rand_holder()
359
        resource = self.rand_resource()
360
        c, = self.rand_limits()
361
        f = self.rand_flags()
362
        r = self.qh.set_quota(set_quota=[(e1, resource, c, f)])
363

    
364
        counters = self.rand_counters()
365

    
366
        with self.assertRaises(QuotaholderError) as cm:
367
            self.qh.reset_holding(
368
                reset_holding=[(e0, resource) + counters,
369
                               (e1, resource) + counters])
370

    
371
        err = cm.exception
372
        self.assertEqual(err.message, [0])
373

    
374
        r = self.qh.get_quota(get_quota=[(e1, resource)])
375
        self.assertEqual(r, [(e1, resource, c) + counters + (f,)])
376

    
377
    @transaction.commit_on_success
378
    def test_015_release_nocapacity(self):
379
        qh = self.qh
380
        owner = "system"
381
        source = "test_015_release_nocapacity_source"
382
        resource = "resource"
383
        target = "test_015_release_nocapacity_target"
384
        flags = 0
385

    
386
        qh.set_quota(set_quota=[(source, resource, 6, 0)])
387
        self.initialize_holding(source, resource, 6)
388

    
389
        qh.set_quota(set_quota=[(target, resource, 5, 0)])
390

    
391
        serial = qh.issue_commission(clientkey=self.client, target=target,
392
                                     name="something",
393
                                     provisions=[(source, resource, 5)])
394
        qh.accept_commission(clientkey=self.client, serials=[serial])
395

    
396
        holding = qh.get_quota(get_quota=[[source, resource]])
397
        self.assertEqual(tuple(holding[0]),
398
                         (source, resource, 6, 6, 6, 1, 1, flags))
399
        holding = qh.get_quota(get_quota=[[target, resource]])
400
        self.assertEqual(tuple(holding[0]),
401
                         (target, resource, 5, 5, 5, 5, 5, flags))
402

    
403
        if qh.reset_holding(
404
            reset_holding=[[target, resource, 10, 10, 10, 10]]):
405
            raise failed
406

    
407
        with self.assertRaises(NoCapacityError):
408
            qh.issue_commission(clientkey=self.client, target=target,
409
                                name="something",
410
                                provisions=[(source, resource, 1)])
411

    
412
        with self.assertRaises(NonExportedError):
413
            qh.issue_commission(clientkey=self.client, target=target,
414
                                name="something",
415
                                provisions=[(source, resource, -7)])
416

    
417
        serial = qh.issue_commission(clientkey=self.client, target=target,
418
                                     name="something",
419
                                     provisions=[(source, resource, -1)])
420
        qh.accept_commission(clientkey=self.client, serials=[serial])
421

    
422
        holding = qh.get_quota(get_quota=[[source, resource]])
423
        self.assertEqual(tuple(holding[0]),
424
                         (source, resource, 6, 6, 6, 2, 2, flags))
425
        holding = qh.get_quota(get_quota=[[target, resource]])
426
        self.assertEqual(tuple(holding[0]),
427
                         (target, resource, 5, 9, 9, 9, 9, flags))
428

    
429
        with self.assertRaises(NonExportedError):
430
            qh.issue_commission(clientkey=self.client, target=target,
431
                                name="something",
432
                                provisions=[(source, resource, -10)])
433

    
434

    
435
if __name__ == "__main__":
436
    import sys
437
    printf("Using {0}", sys.executable)
438
    run_test_case(QHAPITest)