Statistics
| Branch: | Tag: | Revision:

root / snf-tools / synnefo_tools / burnin / pithos_tests.py @ c2f037ff

History | View | Annotate | Download (5.8 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

    
43
from synnefo_tools.burnin.common import BurninTests, Proper, \
44
    QPITHOS, QADD, QREMOVE
45

    
46

    
47
# pylint: disable=too-many-public-methods
48
class PithosTestSuite(BurninTests):
49
    """Test Pithos functionality"""
50
    containers = Proper(value=None)
51
    created_container = Proper(value=None)
52

    
53
    def test_001_list_containers(self):
54
        """Test container list actually returns containers"""
55
        self._set_pithos_account(self._get_uuid())
56
        self.containers = self._get_list_of_containers()
57
        self.assertGreater(len(self.containers), 0)
58

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

    
65
    def test_003_create_container(self):
66
        """Test creating a new container"""
67
        names = [n['name'] for n in self.containers]
68
        while True:
69
            rand_num = random.randint(1000, 9999)
70
            rand_name = "%s%s" % (self.run_id, rand_num)
71
            self.info("Trying container name %s", rand_name)
72
            if rand_name not in names:
73
                break
74
            self.info("Container name %s already exists", rand_name)
75
        # Create container
76
        self._create_pithos_container(rand_name)
77
        # Verify that container is created
78
        containers = self._get_list_of_containers()
79
        self.info("Verify that container %s is created", rand_name)
80
        names = [n['name'] for n in containers]
81
        self.assertIn(rand_name, names)
82
        # Keep the name of the container so we can remove it
83
        # at cleanup phase, if something goes wrong.
84
        self.created_container = rand_name
85

    
86
    def test_004_upload_file(self):
87
        """Test uploading a txt file to Pithos"""
88
        # Create a tmp file
89
        with tempfile.TemporaryFile(dir=self.temp_directory) as fout:
90
            fout.write("This is a temp file")
91
            fout.seek(0, 0)
92
            # Upload the file,
93
            # The container is the one choosen during the `create_container'
94
            self.clients.pithos.upload_object("test.txt", fout)
95
            # Verify quotas
96
            size = os.fstat(fout.fileno()).st_size
97
            changes = \
98
                {self._get_uuid(): [(QPITHOS, QADD, size, None)]}
99
            self._check_quotas(changes)
100

    
101
    def test_005_download_file(self):
102
        """Test downloading the file from Pithos"""
103
        # Create a tmp directory to save the file
104
        with tempfile.TemporaryFile(dir=self.temp_directory) as fout:
105
            self.clients.pithos.download_object("test.txt", fout)
106
            # Now read the file
107
            fout.seek(0, 0)
108
            contents = fout.read()
109
            # Compare results
110
            self.info("Comparing contents with the uploaded file")
111
            self.assertEqual(contents, "This is a temp file")
112

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

    
122
        # Verify quotas
123
        changes = \
124
            {self._get_uuid(): [(QPITHOS, QREMOVE, content_length, None)]}
125
        self._check_quotas(changes)
126

    
127
        self.info("Removing the container %s", self.created_container)
128
        self.clients.pithos.purge_container()
129

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

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