Statistics
| Branch: | Tag: | Revision:

root / snf-tools / synnefo_tools / burnin / pithos_tests.py @ 1ff50540

History | View | Annotate | Download (11.6 kB)

1
# Copyright 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
"""
35
This is the burnin class that tests the Pithos functionality
36

37
"""
38

    
39
import os
40
import random
41
import tempfile
42
from datetime import datetime
43

    
44
from synnefo_tools.burnin.common import BurninTests, Proper
45
from kamaki.clients import ClientError
46

    
47

    
48
# Too many public methods. pylint: disable-msg=R0904
49
class PithosTestSuite(BurninTests):
50
    """Test Pithos functionality"""
51
    containers = Proper(value=None)
52
    created_container = Proper(value=None)
53
    now_unformated = Proper(value=datetime.utcnow())
54

    
55
    def test_005_account_head(self):
56
        """HEAD on pithos account"""
57
        self._set_pithos_account(self._get_uuid())
58
        pithos = self.clients.pithos
59
        r = pithos.account_head()
60
        self.assertEqual(r.status_code, 204)
61
        self.info('Returns 204')
62

    
63
        r = pithos.account_head(until='1000000000')
64
        self.assertEqual(r.status_code, 204)
65
        datestring = unicode(r.headers['x-account-until-timestamp'])
66
        self.assertEqual(u'Sun, 09 Sep 2001 01:46:40 GMT', datestring)
67
        self.assertTrue('x-account-policy-quota' in r.headers)
68
        self.info('Until and policy quota exist')
69

    
70
        for format in pithos.DATE_FORMATS:
71
            now_formated = self.now_unformated.strftime(format)
72
            r1 = pithos.account_head(
73
                if_modified_since=now_formated, success=(204, 304, 412))
74
            r2 = pithos.account_head(
75
                if_unmodified_since=now_formated, success=(204, 304, 412))
76
            self.assertNotEqual(r1.status_code, r2.status_code)
77
        self.info('If_(un)modified_since is OK')
78

    
79
    def test_010_account_get(self):
80
        """Test account_get"""
81
        self.info('Preparation')
82
        pithos = self.clients.pithos
83
        for i in range(1, 3):
84
            cont_name = "cont%s_%s%s" % (
85
                i, self.run_id or 0, random.randint(1000, 9999))
86
            self._create_pithos_container(cont_name)
87
        pithos.container, obj = cont_name, 'shared_file'
88
        pithos.create_object(obj)
89
        pithos.set_object_sharing(obj, read_permission='*')
90
        self.info('Created object /%s/%s' % (cont_name, obj))
91

    
92
        r = pithos.list_containers()
93
        fullLen = len(r)
94
        self.assertTrue(fullLen > 2)
95
        self.info('Normal use is OK')
96

    
97
        r = pithos.account_get(limit=1)
98
        self.assertEqual(len(r.json), 1)
99
        self.info('Limit works')
100

    
101
        r = pithos.account_get(marker='cont')
102
        cont1, cont3 = r.json[0], r.json[2]
103
        self.info('Marker works')
104

    
105
        r = pithos.account_get(limit=2, marker='cont')
106
        conames = [container['name'] for container in r.json if (
107
            container['name'].lower().startswith('cont'))]
108
        self.assertTrue(cont1['name'] in conames)
109
        self.assertFalse(cont3['name'] in conames)
110
        self.info('Marker-limit combination works')
111

    
112
        r = pithos.account_get(show_only_shared=True)
113
        self.assertTrue(cont_name in [c['name'] for c in r.json])
114
        self.info('Show-only-shared works')
115

    
116
        r = pithos.account_get(until=1342609206.0)
117
        self.assertTrue(len(r.json) <= fullLen)
118
        self.info('Until works')
119

    
120
        for format in pithos.DATE_FORMATS:
121
            now_formated = self.now_unformated.strftime(format)
122
            r1 = pithos.account_get(
123
                if_modified_since=now_formated, success=(200, 304, 412))
124
            r2 = pithos.account_get(
125
                if_unmodified_since=now_formated, success=(200, 304, 412))
126
            self.assertNotEqual(r1.status_code, r2.status_code)
127
        self.info('If_(un)modified_since is OK')
128

    
129
    def test_015_account_post(self):
130
        """Test account_post"""
131
        pithos = self.clients.pithos
132
        r = pithos.account_post()
133
        self.assertEqual(r.status_code, 202)
134
        self.info('Status code is OK')
135

    
136
        rand_num = '%s%s' % (self.run_id or 0, random.randint(1000, 9999))
137
        grpName = 'grp%s' % rand_num
138

    
139
        u1, u2 = pithos.account, 'invalid-user-uuid-%s' % rand_num
140
        self.assertRaises(
141
            ClientError, pithos.set_account_group, grpName, [u1, u2])
142
        self.info('Invalid uuid is handled correctly')
143

    
144
        pithos.set_account_group(grpName, [u1])
145
        r = pithos.get_account_group()
146
        self.assertEqual(r['x-account-group-' + grpName], '%s' % u1)
147
        self.info('Account group is OK')
148
        pithos.del_account_group(grpName)
149
        r = pithos.get_account_group()
150
        self.assertTrue('x-account-group-' + grpName not in r)
151
        self.info('Removed account group')
152

    
153
        mprefix = 'meta%s' % rand_num
154
        pithos.set_account_meta({
155
            mprefix + '1': 'v1', mprefix + '2': 'v2'})
156
        r = pithos.get_account_meta()
157
        self.assertEqual(r['x-account-meta-' + mprefix + '1'], 'v1')
158
        self.assertEqual(r['x-account-meta-' + mprefix + '2'], 'v2')
159
        self.info('Account meta is OK')
160

    
161
        pithos.del_account_meta(mprefix + '1')
162
        r = pithos.get_account_meta()
163
        self.assertTrue('x-account-meta-' + mprefix + '1' not in r)
164
        self.assertTrue('x-account-meta-' + mprefix + '2' in r)
165
        self.info('Selective removal of account meta is OK')
166

    
167
        pithos.del_account_meta(mprefix + '2')
168
        r = pithos.get_account_meta()
169
        self.assertTrue('x-account-meta-' + mprefix + '2' not in r)
170
        self.info('Temporary account meta are removed')
171

    
172
    def test_020_container_head(self):
173
        """Test container HEAD"""
174
        pithos = self.clients.pithos
175
        r = pithos.container_head()
176
        self.assertEqual(r.status_code, 204)
177
        self.info('Status code is OK')
178

    
179
        r = pithos.container_head(until=1000000, success=(204, 404))
180
        self.assertEqual(r.status_code, 404)
181
        self.info('Until works')
182

    
183
        for format in pithos.DATE_FORMATS:
184
            now_formated = self.now_unformated.strftime(format)
185
            r1 = pithos.container_head(
186
                if_modified_since=now_formated, success=(204, 304, 412))
187
            r2 = pithos.container_head(
188
                if_unmodified_since=now_formated, success=(204, 304, 412))
189
            self.assertNotEqual(r1.status_code, r2.status_code)
190

    
191
        k = 'metakey%s' % random.randint(1000, 9999)
192
        pithos.set_container_meta({k: 'our value'})
193
        r = pithos.get_container_meta()
194
        k = 'x-container-meta-%s' % k
195
        self.assertIn(k, r)
196
        self.assertEqual('our value', r[k])
197
        self.info('Container meta exists')
198

    
199
    # def test_050_stop_test(self):
200
    #     """STOP TESTING ALREADY"""
201
    #     self.assertTrue(False)
202

    
203
    def test_051_list_containers(self):
204
        """Test container list actually returns containers"""
205
        self.containers = self._get_list_of_containers()
206
        self.assertGreater(len(self.containers), 0)
207
        self.lala = 1
208

    
209
    def test_052_unique_containers(self):
210
        """Test if containers have unique names"""
211
        names = [n['name'] for n in self.containers]
212
        names = sorted(names)
213
        self.assertEqual(sorted(list(set(names))), names)
214

    
215
    def test_053_create_container(self):
216
        """Test creating a new container"""
217
        names = [n['name'] for n in self.containers]
218
        while True:
219
            rand_num = random.randint(1000, 9999)
220
            rand_name = "%s%s" % (self.run_id or 0, rand_num)
221
            self.info("Trying container name %s", rand_name)
222
            if rand_name not in names:
223
                break
224
            self.info("Container name %s already exists", rand_name)
225
        # Create container
226
        self._create_pithos_container(rand_name)
227
        # Verify that container is created
228
        containers = self._get_list_of_containers()
229
        self.info("Verify that container %s is created", rand_name)
230
        names = [n['name'] for n in containers]
231
        self.assertIn(rand_name, names)
232
        # Keep the name of the container so we can remove it
233
        # at cleanup phase, if something goes wrong.
234
        self.created_container = rand_name
235

    
236
    def test_054_upload_file(self):
237
        """Test uploading a txt file to Pithos"""
238
        # Create a tmp file
239
        with tempfile.TemporaryFile(dir=self.temp_directory) as fout:
240
            fout.write("This is a temp file")
241
            fout.seek(0, 0)
242
            # Upload the file,
243
            # The container is the one choosen during the `create_container'
244
            self.clients.pithos.upload_object("test.txt", fout)
245
            # Verify quotas
246
            self._check_quotas(diskspace=+os.fstat(fout.fileno()).st_size)
247

    
248
    def test_055_download_file(self):
249
        """Test downloading the file from Pithos"""
250
        # Create a tmp directory to save the file
251
        with tempfile.TemporaryFile(dir=self.temp_directory) as fout:
252
            self.clients.pithos.download_object("test.txt", fout)
253
            # Now read the file
254
            fout.seek(0, 0)
255
            contents = fout.read()
256
            # Compare results
257
            self.info("Comparing contents with the uploaded file")
258
            self.assertEqual(contents, "This is a temp file")
259

    
260
    def test_056_remove(self):
261
        """Test removing files and containers from Pithos"""
262
        self.info("Removing the file %s from container %s",
263
                  "test.txt", self.created_container)
264
        # The container is the one choosen during the `create_container'
265
        content_length = \
266
            self.clients.pithos.get_object_info("test.txt")['content-length']
267
        self.clients.pithos.del_object("test.txt")
268

    
269
        # Verify quotas
270
        self._check_quotas(diskspace=-int(content_length))
271

    
272
        self.info("Removing the container %s", self.created_container)
273
        self.clients.pithos.purge_container()
274

    
275
        # List containers
276
        containers = self._get_list_of_containers()
277
        self.info("Check that the container %s has been deleted",
278
                  self.created_container)
279
        names = [n['name'] for n in containers]
280
        self.assertNotIn(self.created_container, names)
281
        # We successfully deleted our container, no need to do it
282
        # in our clean up phase
283
        self.created_container = None
284

    
285
    @classmethod
286
    def tearDownClass(cls):  # noqa
287
        """Clean up"""
288
        print cls.created_container
289
        if cls.created_container is not None:
290
            cls.clients.pithos.del_container(delimiter='/')
291
            cls.clients.pithos.purge_container()