images_tests.py 8.97 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 46
from synnefo_tools.burnin.common import BurninTests, Proper, \
    QPITHOS, QADD, QREMOVE
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
47 48


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

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

    def test_002_get_detailed_flavors(self):
        """Test detailed flavor list is the same length as list"""
63
        self.detailed_flavors = self._get_list_of_flavors(detail=True)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
64 65 66 67
        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"""
68 69
        names = sorted([flv['name'] for flv in self.simple_flavors])
        self.simple_names = names
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
70
        detailed_names = sorted([flv['name'] for flv in self.detailed_flavors])
71
        self.assertEqual(self.simple_names, detailed_names)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
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'])


# --------------------------------------------------------------------
93
# pylint: disable=too-many-public-methods
94
class ImagesTestSuite(BurninTests):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
95
    """Test image lists for consistency"""
96 97 98 99 100 101
    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
102 103 104

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

    def test_002_list_images_detailed(self):
        """Test detailed image list is the same length as simple list"""
110 111
        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
112 113 114 115 116 117 118 119 120 121

    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)
122
        self.system_images = images
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        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
139 140 141 142
        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
143 144 145 146 147 148 149 150
        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)
151
        self.temp_image_name = image_name
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
152 153 154 155 156

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

        # Create temp directory
157 158 159
        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
160 161

        # Write to file
162
        self.info("Downloading image to %s", self.temp_image_file)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
163 164 165 166 167 168 169
        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
170
        file_size = os.path.getsize(self.temp_image_file)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
171 172 173
        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
174
        # Verify quotas
175 176 177
        changes = \
            {self._get_uuid(): [(QPITHOS, QADD, file_size, None)]}
        self._check_quotas(changes)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
178

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    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
202 203
        # Verify quotas
        file_size = os.path.getsize(self.temp_image_file)
204 205 206
        changes = \
            {self._get_uuid(): [(QPITHOS, QREMOVE, file_size, None)]}
        self._check_quotas(changes)
207
        self.temp_image_name = None
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
208 209 210
        # Remove temp directory
        self.info("Deleting temp directory %s", self.temp_dir)
        self._remove_tmp_directory(self.temp_dir)
211
        self.temp_dir = None
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

    @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