From d5918a87f12fdef9bd7408c4554c42606c4896d8 Mon Sep 17 00:00:00 2001 From: dongwenjuan Date: Thu, 13 Apr 2017 09:09:41 +0800 Subject: add image code JIRA: DOCTOR-93 Change-Id: I6551d5e31799b967ce442cb05a664c0d64a3dcf8 Signed-off-by: dongwenjuan --- tests/config.py | 8 +++++- tests/identity_auth.py | 8 ++++++ tests/image.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/main.py | 10 +++++-- tests/os_clients.py | 21 ++++++++++++++ 5 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 tests/image.py create mode 100644 tests/os_clients.py (limited to 'tests') diff --git a/tests/config.py b/tests/config.py index 2a062c22..2288d36e 100644 --- a/tests/config.py +++ b/tests/config.py @@ -8,9 +8,15 @@ ############################################################################## from oslo_config import cfg +import image +import os_clients + def list_opts(): - return [] + return [ + ('os_clients', os_clients.OPTS), + ('image', image.IMAGE_OPTS), + ] def prepare_conf(conf=None): diff --git a/tests/identity_auth.py b/tests/identity_auth.py index 4726ca37..0031caec 100644 --- a/tests/identity_auth.py +++ b/tests/identity_auth.py @@ -11,6 +11,7 @@ import os from keystoneauth1.identity import v2 from keystoneauth1.identity import v3 +from keystoneauth1 import session def get_identity_auth(): @@ -32,3 +33,10 @@ def get_identity_auth(): username=username, password=password, tenant_name=project_name) + + +def get_session(auth=None): + """Get a user credentials auth session.""" + if auth is None: + auth = get_identity_auth() + return session.Session(auth=auth) diff --git a/tests/image.py b/tests/image.py new file mode 100644 index 00000000..0b4a3d72 --- /dev/null +++ b/tests/image.py @@ -0,0 +1,77 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corporation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import os +import urllib2 + +from oslo_config import cfg + +from identity_auth import get_session +from os_clients import glance_client +import logger as doctor_log + +IMAGE_OPTS = [ + cfg.StrOpt('name', + default=os.environ.get('IMAGE_NAME', 'cirros'), + help='the name of test image', + required=True), + cfg.StrOpt('format', + default='qcow2', + help='the format of test image', + required=True), + cfg.StrOpt('file_name', + default='cirros.img', + help='the name of image file', + required=True), + cfg.StrOpt('url', + default='https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img', + help='the url where to get the image', + required=True), +] + +LOG = doctor_log.Logger('doctor').getLogger() + + +class Image(object): + + def __init__(self, conf): + self.conf = conf + self.glance = \ + glance_client(conf.os_clients.glance_version, + get_session()) + self.use_existing_image = False + self.image = None + + def create(self): + LOG.info('image create start......') + + images = {image.name: image for image in self.glance.images.list()} + if self.conf.image.name not in images: + if not os.path.exists(self.conf.image.file_name): + resp = urllib2.urlopen(self.conf.image.url) + with open(self.conf.image.file_name, "wb") as file: + file.write(resp.read()) + self.image = self.glance.images.create(name=self.conf.image.name, + disk_format=self.conf.image.format, + container_format="bare", + visibility="public") + self.glance.images.upload(self.image['id'], + open(self.conf.image.file_name, 'rb')) + else: + self.use_existing_image = True + self.image = images[self.conf.image.name] + + LOG.info('image create end......') + + def delete(self): + LOG.info('image delete start.......') + + if not self.use_existing_image and self.image: + self.glance.images.delete(self.image['id']) + + LOG.info('image delete end.......') diff --git a/tests/main.py b/tests/main.py index e36bb4f0..50e0821b 100644 --- a/tests/main.py +++ b/tests/main.py @@ -9,16 +9,18 @@ import sys import config +from image import Image import logger as doctor_log -LOG = doctor_log.Logger(__name__).getLogger() +LOG = doctor_log.Logger('doctor').getLogger() class DoctorTest(object): def __init__(self, conf): self.conf = conf + self.image = Image(self.conf) def run(self): """run doctor test""" @@ -27,6 +29,7 @@ class DoctorTest(object): # prepare the cloud env # preparing VM image... + self.image.create() # creating test user... @@ -40,7 +43,10 @@ class DoctorTest(object): # verify the test results except Exception as e: - LOG.error('doctor test failed: %s ', e) + LOG.error('doctor test failed, Exception=%s' % e) + sys.exit(1) + finally: + self.image.delete() def main(): diff --git a/tests/os_clients.py b/tests/os_clients.py new file mode 100644 index 00000000..2eb406e0 --- /dev/null +++ b/tests/os_clients.py @@ -0,0 +1,21 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corporation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +from oslo_config import cfg + +import glanceclient.client as glanceclient + + +OPTS = [ + cfg.StrOpt('glance_version', default='2', help='glance version'), +] + + +def glance_client(version, session): + return glanceclient.Client(version=version, + session=session) -- cgit 1.2.3-korg