pithos_tests.py 5.54 KB
Newer Older
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

"""
This is the burnin class that tests the Pithos functionality

"""

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
39
import os
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
40
41
42
import random
import tempfile

43
from synnefo_tools.burnin.common import BurninTests, Proper
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
44
45
46


# Too many public methods. pylint: disable-msg=R0904
47
class PithosTestSuite(BurninTests):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
48
    """Test Pithos functionality"""
49
50
    containers = Proper(value=None)
    created_container = Proper(value=None)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
51
52
53
54

    def test_001_list_containers(self):
        """Test container list actually returns containers"""
        self._set_pithos_account(self._get_uuid())
55
        self.containers = self._get_list_of_containers()
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        self.assertGreater(len(self.containers), 0)

    def test_002_unique_containers(self):
        """Test if containers have unique names"""
        names = [n['name'] for n in self.containers]
        names = sorted(names)
        self.assertEqual(sorted(list(set(names))), names)

    def test_003_create_container(self):
        """Test creating a new container"""
        names = [n['name'] for n in self.containers]
        while True:
            rand_num = random.randint(1000, 9999)
            rand_name = "%s%s" % (self.run_id, rand_num)
            self.info("Trying container name %s", rand_name)
            if rand_name not in names:
                break
            self.info("Container name %s already exists", rand_name)
        # Create container
        self._create_pithos_container(rand_name)
        # Verify that container is created
        containers = self._get_list_of_containers()
        self.info("Verify that container %s is created", rand_name)
        names = [n['name'] for n in containers]
        self.assertIn(rand_name, names)
        # Keep the name of the container so we can remove it
        # at cleanup phase, if something goes wrong.
83
        self.created_container = rand_name
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
84
85
86
87
88
89
90
91
92
93

    def test_004_upload_file(self):
        """Test uploading a txt file to Pithos"""
        # Create a tmp file
        with tempfile.TemporaryFile() as fout:
            fout.write("This is a temp file")
            fout.seek(0, 0)
            # Upload the file,
            # The container is the one choosen during the `create_container'
            self.clients.pithos.upload_object("test.txt", fout)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
94
95
            # Verify quotas
            self._check_quotas(diskspace=+os.fstat(fout.fileno()).st_size)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

    def test_005_download_file(self):
        """Test downloading the file from Pithos"""
        # Create a tmp directory to save the file
        with tempfile.TemporaryFile() as fout:
            self.clients.pithos.download_object("test.txt", fout)
            # Now read the file
            fout.seek(0, 0)
            contents = fout.read()
            # Compare results
            self.info("Comparing contents with the uploaded file")
            self.assertEqual(contents, "This is a temp file")

    def test_006_remove(self):
        """Test removing files and containers from Pithos"""
        self.info("Removing the file %s from container %s",
                  "test.txt", self.created_container)
        # The container is the one choosen during the `create_container'
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
114
115
        content_length = \
            self.clients.pithos.get_object_info("test.txt")['content-length']
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
116
117
        self.clients.pithos.del_object("test.txt")

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
118
119
120
        # Verify quotas
        self._check_quotas(diskspace=-int(content_length))

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
121
122
123
124
125
126
127
128
129
130
131
        self.info("Removing the container %s", self.created_container)
        self.clients.pithos.purge_container()

        # List containers
        containers = self._get_list_of_containers()
        self.info("Check that the container %s has been deleted",
                  self.created_container)
        names = [n['name'] for n in containers]
        self.assertNotIn(self.created_container, names)
        # We successfully deleted our container, no need to do it
        # in our clean up phase
132
        self.created_container = None
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
133
134
135
136
137
138
139

    @classmethod
    def tearDownClass(cls):  # noqa
        """Clean up"""
        if cls.created_container is not None:
            cls.clients.pithos.del_container(delimiter='/')
            cls.clients.pithos.purge_container()