Commit b597c849 authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis
Browse files

burnin: Add ImagesTestSuite

parent 72d3b842
......@@ -42,7 +42,8 @@ import optparse
from synnefo_tools import version
from synnefo_tools.burnin import common
from synnefo_tools.burnin.astakos_tests import AstakosTestSuite
from synnefo_tools.burnin.cyclades_tests import FlavorsTestSuite
from synnefo_tools.burnin.images_tests import \
FlavorsTestSuite, ImagesTestSuite
from synnefo_tools.burnin.pithos_tests import PithosTestSuite
......@@ -51,6 +52,7 @@ from synnefo_tools.burnin.pithos_tests import PithosTestSuite
TESTSUITES = [
AstakosTestSuite,
FlavorsTestSuite,
ImagesTestSuite,
PithosTestSuite,
]
......@@ -130,6 +132,11 @@ def parse_arguments(args):
help="Force all server creations to use the specified IMAGE "
"instead of the default one (a Debian Base image). Just like the "
"--force-flavor option, it supports both search by name and id")
parser.add_option(
"--system-user", action="store",
type="string", default=None, dest="system_user",
help="Owner of system images (typed option in the form of "
"\"name:user_name\" or \"id:uuuid\")")
parser.add_option(
"--show-stale", action="store_true",
default=False, dest="show_stale",
......
......@@ -36,8 +36,12 @@ Common utils for burnin tests
"""
import os
import re
import sys
import shutil
import datetime
import tempfile
import traceback
# Use backported unittest functionality if Python < 2.7
try:
......@@ -50,6 +54,7 @@ except ImportError:
from kamaki.clients.astakos import AstakosClient
from kamaki.clients.compute import ComputeClient
from kamaki.clients.pithos import PithosClient
from kamaki.clients.image import ImageClient
from synnefo_tools.burnin.logger import Log
......@@ -59,6 +64,7 @@ from synnefo_tools.burnin.logger import Log
logger = None # Invalid constant name. pylint: disable-msg=C0103
SNF_TEST_PREFIX = "snf-test-"
CONNECTION_RETRY_LIMIT = 2
SYSTEM_USERS = ["images@okeanos.grnet.gr", "images@demo.synnefo.org"]
# --------------------------------------------------------------------
......@@ -121,6 +127,9 @@ class Clients(object):
# Pithos
pithos = None
pithos_url = None
# Image
image = None
image_url = None
# Too many public methods (45/20). pylint: disable-msg=R0904
......@@ -132,6 +141,7 @@ class BurninTests(unittest.TestCase):
action_timeout = None
action_warning = None
query_interval = None
system_user = None
@classmethod
def setUpClass(cls): # noqa
......@@ -175,6 +185,13 @@ class BurninTests(unittest.TestCase):
self.clients.pithos_url, self.clients.token)
self.clients.pithos.CONNECTION_RETRY_LIMIT = self.clients.retry
self.clients.image_url = \
self.clients.astakos.get_service_endpoints('image')['publicURL']
self.info("Image url is %s", self.clients.image_url)
self.clients.image = ImageClient(
self.clients.image_url, self.clients.token)
self.clients.image.CONNECTION_RETRY_LIMIT = self.clients.retry
# ----------------------------------
# Loggers helper functions
def log(self, msg, *args):
......@@ -213,6 +230,64 @@ class BurninTests(unittest.TestCase):
self.info("User's name is %s", username)
return username
def _create_tmp_directory(self):
"""Create a tmp directory
In my machine /tmp has not enough space for an image
to be saves, so we are going to use the current directory.
"""
temp_dir = tempfile.mkdtemp(dir=os.getcwd())
self.info("Temp directory %s created", temp_dir)
return temp_dir
def _remove_tmp_directory(self, tmp_dir):
"""Remove a tmp directory"""
try:
shutil.rmtree(tmp_dir)
self.info("Temp directory %s deleted", tmp_dir)
except OSError:
pass
def _get_uuid_of_system_user(self):
"""Get the uuid of the system user
This is the user that upload the 'official' images.
"""
self.info("Getting the uuid of the system user")
system_users = None
if self.system_user is not None:
parsed_su = parse_typed_option(self.system_user)
if parsed_su is None:
msg = "Invalid system-user format: %s. Must be [id|name]:.+"
self.warning(msg, self.system_user)
else:
su_type, su_value = parsed_su
if su_type == "name":
system_users = [su_value]
elif su_type == "id":
self.info("System user's uuid is %s", su_value)
return su_value
else:
self.error("Unrecognized system-user type %s", su_type)
self.fail("Unrecognized system-user type")
if system_users is None:
system_users = SYSTEM_USERS
uuids = self.clients.astakos.usernames2uuids(system_users)
for su_name in system_users:
self.info("Trying username %s", su_name)
if su_name in uuids:
self.info("System user's uuid is %s", uuids[su_name])
return uuids[su_name]
self.warning("No system user found")
return None
# ----------------------------------
# Flavors
def _get_list_of_flavors(self, detail=False):
"""Get (detailed) list of flavors"""
if detail:
......@@ -222,13 +297,95 @@ class BurninTests(unittest.TestCase):
flavors = self.clients.compute.list_flavors(detail=detail)
return flavors
# ----------------------------------
# Images
def _get_list_of_images(self, detail=False):
"""Get (detailed) list of images"""
if detail:
self.info("Getting detailed list of images")
else:
self.info("Getting simple list of images")
images = self.clients.image.list_public(detail=detail)
# Remove images registered by burnin
images = [img for img in images
if not img['name'].startswith(SNF_TEST_PREFIX)]
return images
def _get_list_of_sys_images(self, images=None):
"""Get (detailed) list of images registered by system user or by me"""
self.info("Getting list of images registered by system user or by me")
if images is None:
images = self._get_list_of_images(detail=True)
su_uuid = self._get_uuid_of_system_user()
my_uuid = self._get_uuid()
ret_images = [i for i in images
if i['owner'] == su_uuid or i['owner'] == my_uuid]
return ret_images
def _find_image(self, patterns, images=None):
"""Find a suitable image to use
The patterns is a list of `typed_options'. The first pattern to
match an image will be the one that will be returned.
"""
if images is None:
images = self._get_list_of_sys_images()
for ptrn in patterns:
parsed_ptrn = parse_typed_option(ptrn)
if parsed_ptrn is None:
msg = "Invalid image format: %s. Must be [id|name]:.+"
self.warning(msg, ptrn)
continue
img_type, img_value = parsed_ptrn
if img_type == "name":
# Filter image by name
msg = "Trying to find an image with name %s"
self.info(msg, img_value)
filtered_imgs = \
[i for i in images if
re.search(img_value, i['name'], flags=re.I) is not None]
elif img_type == "id":
# Filter images by id
msg = "Trying to find an image with id %s"
self.info(msg, img_value)
filtered_imgs = \
[i for i in images if
i['id'].lower() == img_value.lower()]
else:
self.error("Unrecognized image type %s", img_type)
self.fail("Unrecognized image type")
# Check if we found one
if filtered_imgs:
img = filtered_imgs[0]
self.info("Will use %s with id %s", img['name'], img['id'])
return img
# We didn't found one
err = "No matching image found"
self.error(err)
self.fail(err)
# ----------------------------------
# Pithos
def _set_pithos_account(self, account):
"""Set the pithos account"""
"""Set the Pithos account"""
assert account, "No pithos account was given"
self.info("Setting pithos account to %s", account)
self.info("Setting Pithos account to %s", account)
self.clients.pithos.account = account
def _set_pithos_container(self, container):
"""Set the Pithos container"""
assert container, "No pithos container was given"
self.info("Setting Pithos container to %s", container)
self.clients.pithos.container = container
def _get_list_of_containers(self, account=None):
"""Get list of containers"""
if account is not None:
......@@ -272,6 +429,7 @@ def initialize(opts, testsuites):
BurninTests.action_timeout = opts.action_timeout
BurninTests.action_warning = opts.action_warning
BurninTests.query_interval = opts.query_interval
BurninTests.system_user = opts.system_user
BurninTests.run_id = SNF_TEST_PREFIX + \
datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M%S")
......@@ -321,3 +479,18 @@ def was_successful(tsuite, success):
else:
logger.testsuite_failure(tsuite)
return False
def parse_typed_option(value):
"""Parse typed options (flavors and images)
The options are in the form 'id:123-345' or 'name:^Debian Base$'
"""
try:
[type_, val] = value.strip().split(':')
if type_ not in ["id", "name"]:
raise ValueError
return type_, val
except ValueError:
return None
# 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
from synnefo_tools.burnin import common
# Too many public methods. pylint: disable-msg=R0904
class FlavorsTestSuite(common.BurninTests):
"""Test flavor lists for consistency"""
simple_flavors = None
detailed_flavors = None
simple_names = None
def test_001_simple_flavors(self):
"""Test flavor list actually returns flavors"""
simple_flavors = self._get_list_of_flavors(detail=False)
self._setattr("simple_flavors", simple_flavors)
self.assertGreater(len(self.simple_flavors), 0)
def test_002_get_detailed_flavors(self):
"""Test detailed flavor list is the same length as list"""
detailed_flavors = self._get_list_of_flavors(detail=True)
self._setattr("detailed_flavors", detailed_flavors)
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"""
simple_names = sorted([flv['name'] for flv in self.simple_flavors])
self._setattr("simple_names", simple_names)
detailed_names = sorted([flv['name'] for flv in self.detailed_flavors])
self.assertEqual(simple_names, detailed_names)
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
class ImagesTestSuite(common.BurninTests):
"""Test image lists for consistency"""
simple_images = None
detailed_images = None
system_images = None
temp_dir = None
temp_image_name = None
temp_image_file = None
def test_001_list_images(self):
"""Test simple image list actually returns images"""
images = self._get_list_of_images(detail=False)
self._setattr("simple_images", images)
self.assertGreater(len(images), 0)
def test_002_list_images_detailed(self):
"""Test detailed image list is the same length as simple list"""
images = self._get_list_of_images(detail=True)
self._setattr("detailed_images", images)
self.assertEqual(len(self.simple_images), len(images))
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)
self._setattr("system_images", images)
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
image = self._find_image(["name:^Debian Base$"],
images=self.system_images)
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)
self._setattr("temp_image_name", image_name)
self._set_pithos_account(image_owner)
self._set_pithos_container(image_container)
# Create temp directory
temp_dir = self._create_tmp_directory()
self._setattr("temp_dir", temp_dir)
self._setattr("temp_image_file",
os.path.join(self.temp_dir, self.temp_image_name))
# Write to file
self.info("Download image to %s", self.temp_image_file)
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")
with open(self.temp_image_file, "r+b") as fin:
self.clients.pithos.upload_object(self.temp_image_name, fin)
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)
self._setattr("temp_image_name", None)
# Remove temp directory
self.info("Deleting temp directory %s", self.temp_dir)
self._remove_tmp_directory(self.temp_dir)
self._setattr("temp_dir", None)
@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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment