Revision f772699c

b/snf-tools/synnefo_tools/burnin/__init__.py
42 42
from synnefo_tools import version
43 43
from synnefo_tools.burnin import common
44 44
from synnefo_tools.burnin.astakos_tests import AstakosTestSuite
45
from synnefo_tools.burnin.cyclades_tests import FlavorsTestSuite
45
from synnefo_tools.burnin.images_tests import \
46
    FlavorsTestSuite, ImagesTestSuite
46 47
from synnefo_tools.burnin.pithos_tests import PithosTestSuite
47 48

  
48 49

  
......
51 52
TESTSUITES = [
52 53
    AstakosTestSuite,
53 54
    FlavorsTestSuite,
55
    ImagesTestSuite,
54 56
    PithosTestSuite,
55 57
    ]
56 58

  
......
131 133
             "instead of the default one (a Debian Base image). Just like the "
132 134
             "--force-flavor option, it supports both search by name and id")
133 135
    parser.add_option(
136
        "--system-user", action="store",
137
        type="string", default=None, dest="system_user",
138
        help="Owner of system images (typed option in the form of "
139
             "\"name:user_name\" or \"id:uuuid\")")
140
    parser.add_option(
134 141
        "--show-stale", action="store_true",
135 142
        default=False, dest="show_stale",
136 143
        help="Show stale servers from previous runs. A server is considered "
b/snf-tools/synnefo_tools/burnin/common.py
36 36

  
37 37
"""
38 38

  
39
import os
40
import re
39 41
import sys
42
import shutil
40 43
import datetime
44
import tempfile
41 45
import traceback
42 46
# Use backported unittest functionality if Python < 2.7
43 47
try:
......
50 54
from kamaki.clients.astakos import AstakosClient
51 55
from kamaki.clients.compute import ComputeClient
52 56
from kamaki.clients.pithos import PithosClient
57
from kamaki.clients.image import ImageClient
53 58

  
54 59
from synnefo_tools.burnin.logger import Log
55 60

  
......
59 64
logger = None  # Invalid constant name. pylint: disable-msg=C0103
60 65
SNF_TEST_PREFIX = "snf-test-"
61 66
CONNECTION_RETRY_LIMIT = 2
67
SYSTEM_USERS = ["images@okeanos.grnet.gr", "images@demo.synnefo.org"]
62 68

  
63 69

  
64 70
# --------------------------------------------------------------------
......
121 127
    # Pithos
122 128
    pithos = None
123 129
    pithos_url = None
130
    # Image
131
    image = None
132
    image_url = None
124 133

  
125 134

  
126 135
# Too many public methods (45/20). pylint: disable-msg=R0904
......
132 141
    action_timeout = None
133 142
    action_warning = None
134 143
    query_interval = None
144
    system_user = None
135 145

  
136 146
    @classmethod
137 147
    def setUpClass(cls):  # noqa
......
175 185
            self.clients.pithos_url, self.clients.token)
176 186
        self.clients.pithos.CONNECTION_RETRY_LIMIT = self.clients.retry
177 187

  
188
        self.clients.image_url = \
189
            self.clients.astakos.get_service_endpoints('image')['publicURL']
190
        self.info("Image url is %s", self.clients.image_url)
191
        self.clients.image = ImageClient(
192
            self.clients.image_url, self.clients.token)
193
        self.clients.image.CONNECTION_RETRY_LIMIT = self.clients.retry
194

  
178 195
    # ----------------------------------
179 196
    # Loggers helper functions
180 197
    def log(self, msg, *args):
......
213 230
        self.info("User's name is %s", username)
214 231
        return username
215 232

  
233
    def _create_tmp_directory(self):
234
        """Create a tmp directory
235

  
236
        In my machine /tmp has not enough space for an image
237
        to be saves, so we are going to use the current directory.
238

  
239
        """
240
        temp_dir = tempfile.mkdtemp(dir=os.getcwd())
241
        self.info("Temp directory %s created", temp_dir)
242
        return temp_dir
243

  
244
    def _remove_tmp_directory(self, tmp_dir):
245
        """Remove a tmp directory"""
246
        try:
247
            shutil.rmtree(tmp_dir)
248
            self.info("Temp directory %s deleted", tmp_dir)
249
        except OSError:
250
            pass
251

  
252
    def _get_uuid_of_system_user(self):
253
        """Get the uuid of the system user
254

  
255
        This is the user that upload the 'official' images.
256

  
257
        """
258
        self.info("Getting the uuid of the system user")
259
        system_users = None
260
        if self.system_user is not None:
261
            parsed_su = parse_typed_option(self.system_user)
262
            if parsed_su is None:
263
                msg = "Invalid system-user format: %s. Must be [id|name]:.+"
264
                self.warning(msg, self.system_user)
265
            else:
266
                su_type, su_value = parsed_su
267
                if su_type == "name":
268
                    system_users = [su_value]
269
                elif su_type == "id":
270
                    self.info("System user's uuid is %s", su_value)
271
                    return su_value
272
                else:
273
                    self.error("Unrecognized system-user type %s", su_type)
274
                    self.fail("Unrecognized system-user type")
275

  
276
        if system_users is None:
277
            system_users = SYSTEM_USERS
278

  
279
        uuids = self.clients.astakos.usernames2uuids(system_users)
280
        for su_name in system_users:
281
            self.info("Trying username %s", su_name)
282
            if su_name in uuids:
283
                self.info("System user's uuid is %s", uuids[su_name])
284
                return uuids[su_name]
285

  
286
        self.warning("No system user found")
287
        return None
288

  
289
    # ----------------------------------
290
    # Flavors
216 291
    def _get_list_of_flavors(self, detail=False):
217 292
        """Get (detailed) list of flavors"""
218 293
        if detail:
......
222 297
        flavors = self.clients.compute.list_flavors(detail=detail)
223 298
        return flavors
224 299

  
300
    # ----------------------------------
301
    # Images
302
    def _get_list_of_images(self, detail=False):
303
        """Get (detailed) list of images"""
304
        if detail:
305
            self.info("Getting detailed list of images")
306
        else:
307
            self.info("Getting simple list of images")
308
        images = self.clients.image.list_public(detail=detail)
309
        # Remove images registered by burnin
310
        images = [img for img in images
311
                  if not img['name'].startswith(SNF_TEST_PREFIX)]
312
        return images
313

  
314
    def _get_list_of_sys_images(self, images=None):
315
        """Get (detailed) list of images registered by system user or by me"""
316
        self.info("Getting list of images registered by system user or by me")
317
        if images is None:
318
            images = self._get_list_of_images(detail=True)
319

  
320
        su_uuid = self._get_uuid_of_system_user()
321
        my_uuid = self._get_uuid()
322
        ret_images = [i for i in images
323
                      if i['owner'] == su_uuid or i['owner'] == my_uuid]
324

  
325
        return ret_images
326

  
327
    def _find_image(self, patterns, images=None):
328
        """Find a suitable image to use
329

  
330
        The patterns is a list of `typed_options'. The first pattern to
331
        match an image will be the one that will be returned.
332

  
333
        """
334
        if images is None:
335
            images = self._get_list_of_sys_images()
336

  
337
        for ptrn in patterns:
338
            parsed_ptrn = parse_typed_option(ptrn)
339
            if parsed_ptrn is None:
340
                msg = "Invalid image format: %s. Must be [id|name]:.+"
341
                self.warning(msg, ptrn)
342
                continue
343
            img_type, img_value = parsed_ptrn
344
            if img_type == "name":
345
                # Filter image by name
346
                msg = "Trying to find an image with name %s"
347
                self.info(msg, img_value)
348
                filtered_imgs = \
349
                    [i for i in images if
350
                     re.search(img_value, i['name'], flags=re.I) is not None]
351
            elif img_type == "id":
352
                # Filter images by id
353
                msg = "Trying to find an image with id %s"
354
                self.info(msg, img_value)
355
                filtered_imgs = \
356
                    [i for i in images if
357
                     i['id'].lower() == img_value.lower()]
358
            else:
359
                self.error("Unrecognized image type %s", img_type)
360
                self.fail("Unrecognized image type")
361

  
362
            # Check if we found one
363
            if filtered_imgs:
364
                img = filtered_imgs[0]
365
                self.info("Will use %s with id %s", img['name'], img['id'])
366
                return img
367

  
368
        # We didn't found one
369
        err = "No matching image found"
370
        self.error(err)
371
        self.fail(err)
372

  
373
    # ----------------------------------
374
    # Pithos
225 375
    def _set_pithos_account(self, account):
226
        """Set the pithos account"""
376
        """Set the Pithos account"""
227 377
        assert account, "No pithos account was given"
228 378

  
229
        self.info("Setting pithos account to %s", account)
379
        self.info("Setting Pithos account to %s", account)
230 380
        self.clients.pithos.account = account
231 381

  
382
    def _set_pithos_container(self, container):
383
        """Set the Pithos container"""
384
        assert container, "No pithos container was given"
385

  
386
        self.info("Setting Pithos container to %s", container)
387
        self.clients.pithos.container = container
388

  
232 389
    def _get_list_of_containers(self, account=None):
233 390
        """Get list of containers"""
234 391
        if account is not None:
......
272 429
    BurninTests.action_timeout = opts.action_timeout
273 430
    BurninTests.action_warning = opts.action_warning
274 431
    BurninTests.query_interval = opts.query_interval
432
    BurninTests.system_user = opts.system_user
275 433
    BurninTests.run_id = SNF_TEST_PREFIX + \
276 434
        datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M%S")
277 435

  
......
321 479
    else:
322 480
        logger.testsuite_failure(tsuite)
323 481
        return False
482

  
483

  
484
def parse_typed_option(value):
485
    """Parse typed options (flavors and images)
486

  
487
    The options are in the form 'id:123-345' or 'name:^Debian Base$'
488

  
489
    """
490
    try:
491
        [type_, val] = value.strip().split(':')
492
        if type_ not in ["id", "name"]:
493
            raise ValueError
494
        return type_, val
495
    except ValueError:
496
        return None
b/snf-tools/synnefo_tools/burnin/images_tests.py
1

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

  
35
"""
36
This is the burnin class that tests the Flavors/Images functionality
37

  
38
"""
39

  
40
import os
41
import shutil
42

  
43
from kamaki.clients import ClientError
44

  
45
from synnefo_tools.burnin import common
46

  
47

  
48
# Too many public methods. pylint: disable-msg=R0904
49
class FlavorsTestSuite(common.BurninTests):
50
    """Test flavor lists for consistency"""
51
    simple_flavors = None
52
    detailed_flavors = None
53
    simple_names = None
54

  
55
    def test_001_simple_flavors(self):
56
        """Test flavor list actually returns flavors"""
57
        simple_flavors = self._get_list_of_flavors(detail=False)
58
        self._setattr("simple_flavors", simple_flavors)
59
        self.assertGreater(len(self.simple_flavors), 0)
60

  
61
    def test_002_get_detailed_flavors(self):
62
        """Test detailed flavor list is the same length as list"""
63
        detailed_flavors = self._get_list_of_flavors(detail=True)
64
        self._setattr("detailed_flavors", detailed_flavors)
65
        self.assertEquals(len(self.simple_flavors), len(self.detailed_flavors))
66

  
67
    def test_003_same_flavor_names(self):
68
        """Test detailed and simple flavor list contain same names"""
69
        simple_names = sorted([flv['name'] for flv in self.simple_flavors])
70
        self._setattr("simple_names", simple_names)
71
        detailed_names = sorted([flv['name'] for flv in self.detailed_flavors])
72
        self.assertEqual(simple_names, detailed_names)
73

  
74
    def test_004_unique_flavor_names(self):
75
        """Test flavors have unique names"""
76
        self.assertEqual(sorted(list(set(self.simple_names))),
77
                         self.simple_names)
78

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

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

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

  
92

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

  
104
    def test_001_list_images(self):
105
        """Test simple image list actually returns images"""
106
        images = self._get_list_of_images(detail=False)
107
        self._setattr("simple_images", images)
108
        self.assertGreater(len(images), 0)
109

  
110
    def test_002_list_images_detailed(self):
111
        """Test detailed image list is the same length as simple list"""
112
        images = self._get_list_of_images(detail=True)
113
        self._setattr("detailed_images", images)
114
        self.assertEqual(len(self.simple_images), len(images))
115

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

  
122
    def test_004_system_images(self):
123
        """Test that there are system images registered"""
124
        images = self._get_list_of_sys_images(images=self.detailed_images)
125
        self._setattr("system_images", images)
126
        self.assertGreater(len(images), 0)
127

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

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

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

  
154
        self._set_pithos_account(image_owner)
155
        self._set_pithos_container(image_container)
156

  
157
        # Create temp directory
158
        temp_dir = self._create_tmp_directory()
159
        self._setattr("temp_dir", temp_dir)
160
        self._setattr("temp_image_file",
161
                      os.path.join(self.temp_dir, self.temp_image_name))
162

  
163
        # Write to file
164
        self.info("Download image to %s", self.temp_image_file)
165
        with open(self.temp_image_file, "w+b") as fout:
166
            self.clients.pithos.download_object(image_name, fout)
167

  
168
    def test_008_upload_image(self):
169
        """Upload the image to Pithos"""
170
        self._set_pithos_account(self._get_uuid())
171
        self._create_pithos_container("burnin-images")
172
        with open(self.temp_image_file, "r+b") as fin:
173
            self.clients.pithos.upload_object(self.temp_image_name, fin)
174

  
175
    def test_009_register_image(self):
176
        """Register image to Plankton"""
177
        location = "pithos://" + self._get_uuid() + \
178
            "/burnin-images/" + self.temp_image_name
179
        self.info("Registering image %s", location)
180

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

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

  
193
    def test_010_cleanup_image(self):
194
        """Remove uploaded image from Pithos"""
195
        # Remove uploaded image
196
        self.info("Deleting uploaded image %s", self.temp_image_name)
197
        self.clients.pithos.del_object(self.temp_image_name)
198
        self._setattr("temp_image_name", None)
199
        # Remove temp directory
200
        self.info("Deleting temp directory %s", self.temp_dir)
201
        self._remove_tmp_directory(self.temp_dir)
202
        self._setattr("temp_dir", None)
203

  
204
    @classmethod
205
    def tearDownClass(cls):  # noqa
206
        """Clean up"""
207
        if cls.temp_image_name is not None:
208
            try:
209
                cls.clients.pithos.del_object(cls.temp_image_name)
210
            except ClientError:
211
                pass
212

  
213
        if cls.temp_dir is not None:
214
            try:
215
                shutil.rmtree(cls.temp_dir)
216
            except OSError:
217
                pass

Also available in: Unified diff