Revision 508ed0e1

b/snf-astakos-app/astakos/quotaholder/test/__init__.py
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
# placeholder
/dev/null
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 sys import argv,executable
35
from os.path import dirname
36
from config import run_test_cases
37
from config import printf
38
from limits import LimitsTest
39
from createrelease import CreateReleaseListAPITest
40

  
41
HERE = dirname(__file__)
42

  
43
# Enumerate all test cases to run.
44
# In the command line use
45
#   $ python test
46
# to run them all
47

  
48
all_cases = [
49
    CreateReleaseListAPITest,
50
    LimitsTest
51
]
52

  
53
printf("=======================================================")
54
printf("Using {0} {1}", executable, ' '.join(argv))
55
printf("Running tests from {0}", HERE)
56
printf("=======================================================")
57
printf("All tests are:")
58
for test_case in all_cases:
59
    printf("  {0}", test_case.__name__)
60
run_test_cases(all_cases)
61
printf("=======================================================")
62
printf("")
/dev/null
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
import os
39

  
40
#def create_entity      OK
41
#def set_entity_key     OK
42
#def list_entities      OK
43
#def get_entity         OK 
44
#def get_limits         LOVERDOS
45
#def set_limits         LOVERDOS
46
#def release_entity     OK
47
#def get_holding
48
#def set_holding
49
#def list_resources     
50
#def get_quota
51
#def set_quota
52
#def issue_commission
53
#def accept_commission
54
#def reject_commission
55
#def get_pending_commissions
56
#def resolve_pending_commissions
57
#def get_timeline
58

  
59
class Context(object): 
60
        entityName = rand_string()
61
        entityKey = "key1" 
62
        parentName = "pgerakios"
63
        parentKey = "key1"
64

  
65
class create_release(object):
66

  
67
    def __init__(self, f):
68
        """
69
        If there are no decorator arguments, the function
70
        to be decorated is passed to the constructor.
71
        """
72
        print "Inside __init__()"
73
        self.f = f
74

  
75
    def __call__(self, *args):
76
        """
77
        The __call__ method is not called until the
78
        decorated function is called.
79
        """
80
        print "Inside __call__()"
81
        self.f(*args)
82
        print "After self.f(*args)"
83

  
84

  
85
class CreateReleaseListAPITest(QHTestCase):
86

  
87
    entityName = rand_string()
88
    entityKey = "key1" 
89
    parentName = "pgerakios"
90
    parentKey = "key1"
91

  
92
    def createEntity(self):
93
        printf("Creating entity: {0}", self.entityName)
94
        rejected = self.qh.create_entity(context={},
95
                                        create_entity=[(self.entityName,
96
                                                        self.parentName,
97
                                                        self.entityKey,
98
                                                        self.parentKey)])
99
        self.assertEqual(rejected,[])
100

  
101
    def releaseEntity(self):        
102
        printf("Releasing entity: {0}", self.entityName)
103
        rejected = self.qh.release_entity(context={},release_entity=[(self.entityName,
104
                                                                      self.entityKey)])
105
        self.assertEqual(rejected,[])
106

  
107
    def checkEntityList(self,exists):
108
        entityList = self.qh.list_entities(context={},entity=self.parentName,key=self.parentKey)
109
        if(exists):
110
            self.assertTrue(self.entityName in entityList)
111
        else:
112
            self.assertFalse(self.entityName in entityList)
113

  
114
    def setNewEntityKey(self):
115
         entityKey2 = rand_string()
116
         rejected = self.qh.set_entity_key(context={},set_entity_key=[(self.entityName,
117
                                                                       self.entityKey,
118
                                                                       entityKey2)])
119
         self.assertEqual(rejected,[])
120
         self.entityKey = entityKey2
121
           
122
    def checkGetEntity(self,exists):
123
        entityList = self.qh.get_entity(context={},get_entity=[(self.entityName,
124
                                                                self.entityKey)])
125
        if(exists):
126
            self.assertEqual([(self.entityName,self.parentName)],entityList)
127
        else:
128
            self.assertEqual(entityList,[])
129

  
130
    def listResources(self,expected):
131
        resList = self.qh.list_resources(context={},entity=self.entityName,key=self.entityKey)
132
        self.assertEqual(expected,resList)
133

  
134
    def setQuota(self,r,q,c,i,e,f):
135
        rejected = self.qh.set_quota(context={},set_quota=[(self.entityName,r,self.entityKey,q,c,i,e,f)])
136
        self.assertEqual(rejected,[])
137
        resList = self.qh.get_quota(context={},get_quota=[(self.entityName,r,self.entityKey)])
138
        (e0,r1,q1,c1,i1,e1,t0,t1,t2,t3,f1),tail = resList[0],resList[1:]
139
        self.assertEqual(tail,[])
140
        self.assertEqual((self.entityName,r,q,c,i,e,f),
141
                         (e0,r1,q1,c1,i1,e1,f1))
142

  
143
        #    def issueCommission(self):
144
        # self.qh.issue_commission
145
    def setUp(self):
146
        self.qh.create_entity(create_entity=[("pgerakios", "system", "key1", "")])
147
        self.parentName = "pgerakios"
148
        self.parentKey = "key1"
149

  
150

  
151

  
152
    #BUG: max empty name <= 72 
153
    def test_001(self):
154
        self.createEntity()
155
        self.releaseEntity()
156

  
157
    # Test create, list and release
158
    def test_002(self):
159
        self.checkEntityList(False)
160
        self.createEntity()
161
        self.checkEntityList(True)
162
        self.releaseEntity()
163
        self.checkEntityList(False)
164

  
165

  
166
    # Test create,set key and release
167
    def test_003(self):
168
        self.createEntity()
169
        self.setNewEntityKey()
170
        self.setNewEntityKey()
171
        self.releaseEntity()
172

  
173
    # test get_entity
174
    def test_004(self):
175
        self.checkGetEntity(False)
176
        self.createEntity()
177
        self.checkGetEntity(True)
178
        self.releaseEntity()
179
        self.checkGetEntity(False)
180

  
181
    def test_005(self):
182
        self.createEntity()
183
        self.setQuota("res1",10,100,10,10,0)
184
#        self.listResources([])
185
        self.releaseEntity()
186

  
187
if __name__ == "__main__":
188
    import sys
189
    printf("Using {0}", sys.executable)
190
    run_test_case(CreateReleaseListAPITest)
/dev/null
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)

Also available in: Unified diff