images_tests.py 8.81 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
39
40
41
42
43
44

# 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 Flavors/Images functionality

"""

import os
import shutil

from kamaki.clients import ClientError

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


# Too many public methods. pylint: disable-msg=R0904
49
class FlavorsTestSuite(BurninTests):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
50
    """Test flavor lists for consistency"""
51
52
53
    simple_flavors = Proper(value=None)
    detailed_flavors = Proper(value=None)
    simple_names = Proper(value=None)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
54
55
56

    def test_001_simple_flavors(self):
        """Test flavor list actually returns flavors"""
57
        self.simple_flavors = self._get_list_of_flavors(detail=False)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
58
59
60
61
        self.assertGreater(len(self.simple_flavors), 0)

    def test_002_get_detailed_flavors(self):
        """Test detailed flavor list is the same length as list"""
62
        self.detailed_flavors = self._get_list_of_flavors(detail=True)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
63
64
65
66
        self.assertEquals(len(self.simple_flavors), len(self.detailed_flavors))

    def test_003_same_flavor_names(self):
        """Test detailed and simple flavor list contain same names"""
67
68
        names = sorted([flv['name'] for flv in self.simple_flavors])
        self.simple_names = names
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
69
        detailed_names = sorted([flv['name'] for flv in self.detailed_flavors])
70
        self.assertEqual(self.simple_names, detailed_names)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

    def test_004_unique_flavor_names(self):
        """Test flavors have unique names"""
        self.assertEqual(sorted(list(set(self.simple_names))),
                         self.simple_names)

    def test_005_well_formed_names(self):
        """Test flavors have well formed names

        Test flavors have names of the form CxxRyyDzz, where xx is vCPU count,
        yy is RAM in MiB, zz is Disk in GiB

        """
        for flv in self.detailed_flavors:
            flavor = (flv['vcpus'], flv['ram'], flv['disk'],
                      flv['SNF:disk_template'])
            self.assertEqual("C%dR%dD%d%s" % flavor, flv['name'],
                             "Flavor %s doesn't match its specs" % flv['name'])


# --------------------------------------------------------------------
# Too many public methods. pylint: disable-msg=R0904
93
class ImagesTestSuite(BurninTests):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
94
    """Test image lists for consistency"""
95
96
97
98
99
100
    simple_images = Proper(value=None)
    detailed_images = Proper(value=None)
    system_images = Proper(value=None)
    temp_dir = Proper(value=None)
    temp_image_name = Proper(value=None)
    temp_image_file = Proper(value=None)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
101
102
103

    def test_001_list_images(self):
        """Test simple image list actually returns images"""
104
105
        self.simple_images = self._get_list_of_images(detail=False)
        self.assertGreater(len(self.simple_images), 0)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
106
107
108

    def test_002_list_images_detailed(self):
        """Test detailed image list is the same length as simple list"""
109
110
        self.detailed_images = self._get_list_of_images(detail=True)
        self.assertEqual(len(self.simple_images), len(self.detailed_images))
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
111
112
113
114
115
116
117
118
119
120

    def test_003_same_image_names(self):
        """Test detailed and simple image list contain the same names"""
        snames = sorted([i['name'] for i in self.simple_images])
        dnames = sorted([i['name'] for i in self.detailed_images])
        self.assertEqual(snames, dnames)

    def test_004_system_images(self):
        """Test that there are system images registered"""
        images = self._get_list_of_sys_images(images=self.detailed_images)
121
        self.system_images = images
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
        self.assertGreater(len(images), 0)

    def test_005_unique_image_names(self):
        """Test system images have unique names"""
        names = sorted([i['name'] for i in self.system_images])
        self.assertEqual(sorted(list(set(names))), names)

    def test_006_image_metadata(self):
        """Test every system image has specific metadata defined"""
        keys = frozenset(["osfamily", "root_partition"])
        for i in self.system_images:
            self.assertTrue(keys.issubset(i['properties'].keys()))

    def test_007_download_image(self):
        """Download image from Pithos"""
        # Find the 'Debian Base' image
138
139
140
141
        images = self._find_images(["name:^Debian Base$"],
                                   images=self.system_images)
        image = images[0]
        self.info("Will use %s with id %s", image['name'], image['id'])
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
142
143
144
145
146
147
148
149
        image_location = \
            image['location'].replace("://", " ").replace("/", " ").split()
        image_owner = image_location[1]
        self.info("Image's owner is %s", image_owner)
        image_container = image_location[2]
        self.info("Image's container is %s", image_container)
        image_name = image_location[3]
        self.info("Image's name is %s", image_name)
150
        self.temp_image_name = image_name
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
151
152
153
154
155

        self._set_pithos_account(image_owner)
        self._set_pithos_container(image_container)

        # Create temp directory
156
157
158
        self.temp_dir = self._create_tmp_directory()
        self.temp_image_file = \
            os.path.join(self.temp_dir, self.temp_image_name)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
159
160

        # Write to file
161
        self.info("Downloading image to %s", self.temp_image_file)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
162
163
164
165
166
167
168
        with open(self.temp_image_file, "w+b") as fout:
            self.clients.pithos.download_object(image_name, fout)

    def test_008_upload_image(self):
        """Upload the image to Pithos"""
        self._set_pithos_account(self._get_uuid())
        self._create_pithos_container("burnin-images")
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
169
        file_size = os.path.getsize(self.temp_image_file)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
170
171
172
        with open(self.temp_image_file, "r+b") as fin:
            self.clients.pithos.upload_object(self.temp_image_name, fin)

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
173
174
175
        # Verify quotas
        self._check_quotas(diskspace=file_size)

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    def test_009_register_image(self):
        """Register image to Plankton"""
        location = "pithos://" + self._get_uuid() + \
            "/burnin-images/" + self.temp_image_name
        self.info("Registering image %s", location)

        params = {'is_public': False}
        properties = {'OSFAMILY': "linux", 'ROOT_PARTITION': 1}
        self.clients.image.register(self.temp_image_name, location,
                                    params, properties)

        # Check that image is registered
        self.info("Checking that image has been registered")
        images = self._get_list_of_images(detail=True)
        images = [i for i in images if i['location'] == location]
        self.assertEqual(len(images), 1)
        self.info("Image registered with id %s", images[0]['id'])

    def test_010_cleanup_image(self):
        """Remove uploaded image from Pithos"""
        # Remove uploaded image
        self.info("Deleting uploaded image %s", self.temp_image_name)
        self.clients.pithos.del_object(self.temp_image_name)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
199
200
201
        # Verify quotas
        file_size = os.path.getsize(self.temp_image_file)
        self._check_quotas(diskspace=-file_size)
202
        self.temp_image_name = None
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
203
204
205
        # Remove temp directory
        self.info("Deleting temp directory %s", self.temp_dir)
        self._remove_tmp_directory(self.temp_dir)
206
        self.temp_dir = None
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

    @classmethod
    def tearDownClass(cls):  # noqa
        """Clean up"""
        if cls.temp_image_name is not None:
            try:
                cls.clients.pithos.del_object(cls.temp_image_name)
            except ClientError:
                pass

        if cls.temp_dir is not None:
            try:
                shutil.rmtree(cls.temp_dir)
            except OSError:
                pass