Statistics
| Branch: | Tag: | Revision:

root / snf-quotaholder-app / quotaholder_django / test / simpletests.py @ 18e54648

History | View | Annotate | Download (10.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 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(r, ['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
        r = self.qh.set_holding(set_holding=[(e, resource, k, p0, f0),
184
                                             (e, resource, k, p1, f1),
185
                                             (e, resource, k, p2, f2)])
186
        self.assertEqual(r, [(e, resource, p0)])
187

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

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

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

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

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

    
224
        most = max(0, min(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
    def test_010_pending_commissions(self):
237
        r = self.qh.get_pending_commissions(clientkey=self.client)
238
        self.assertEqual(r, [1])
239
        r = self.qh.resolve_pending_commissions(clientkey=self.client,
240
                                                max_serial=1, accept_set=[1])
241
        r = self.qh.get_pending_commissions(clientkey=self.client)
242
        self.assertEqual(r, [])
243

    
244
    def test_011_release_empty(self):
245
        e, k = self.new_entity()
246
        e0, k0 = self.rand_entity(), Key.random()
247
        r = self.qh.release_entity(release_entity=[(e, k), (e0, k0)])
248
        self.assertEqual(r, [e0])
249
        r = self.qh.get_entity(get_entity=[(e, k)])
250
        self.assertEqual(r, [])
251

    
252
    def test_012_release_nonempty(self):
253
        e, k = self.new_entity()
254
        e1, k1 = self.new_entity(e, k)
255
        r = self.qh.release_entity(release_entity=[(e, k), (e1, k1)])
256
        self.assertEqual(r, [e])
257
        r = self.qh.get_entity(get_entity=[(e1, k1)])
258
        self.assertEqual(r, [])
259
        r = self.qh.release_entity(release_entity=[(e, k)])
260
        self.assertEqual(r, [])
261

    
262
    def test_013_release_nonempty(self):
263
        e, k = self.new_entity()
264
        resource = self.rand_resource()
265
        limits = self.new_quota(e, k, resource)
266
        r = self.qh.release_entity(release_entity=[(e, k)])
267
        self.assertEqual(r, [e])
268
        r = self.qh.release_holding(release_holding=[(e, resource, k)])
269
        self.assertEqual(r, [])
270
        r = self.qh.release_entity(release_entity=[(e, k)])
271
        self.assertEqual(r, [])
272

    
273
    def test_014_reset_holding(self):
274
        e0, k0 = self.new_entity()
275
        e1, k1 = self.new_entity()
276
        resource = self.rand_resource()
277
        p, _ = self.new_policy()
278
        f = self.rand_flags()
279
        r = self.qh.set_holding(set_holding=[(e1, resource, k1, p, f)])
280

    
281
        counters = self.rand_counters()
282
        r = self.qh.reset_holding(
283
            reset_holding=[(e0, resource, k0) + counters,
284
                           (e1, resource, k1) + counters])
285
        self.assertEqual(r, [0])
286
        r = self.qh.get_holding(get_holding=[(e1, resource, k1)])
287
        self.assertEqual(r, [(e1, resource, p) + counters + (f,)])
288

    
289

    
290
if __name__ == "__main__":
291
    import sys
292
    printf("Using {0}", sys.executable)
293
    run_test_case(QHAPITest)