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 --- test-requirements.txt | 11 +------- tests/config.py | 8 +++++- tests/identity_auth.py | 8 ++++++ tests/image.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/main.py | 10 +++++-- tests/os_clients.py | 21 ++++++++++++++ tox.ini | 13 +++++++++ 7 files changed, 135 insertions(+), 13 deletions(-) create mode 100644 tests/image.py create mode 100644 tests/os_clients.py diff --git a/test-requirements.txt b/test-requirements.txt index a2ce1d9b..2928e0f7 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -5,18 +5,9 @@ requests>=2.8.0 oslo.config==3.22.0 # Apache-2.0 python-openstackclient==2.3.0 python-ceilometerclient==2.6.2 -python-heatclient==1.7.0 python-keystoneclient==3.5.0 python-neutronclient==6.0.0 python-novaclient==6.0.0 python-congressclient==1.5.0 +python-glanceclient==2.5.0 virtualenv==15.1.0 -coverage>=4.0 # Apache-2.0 -oslotest>=1.10.0 # Apache-2.0 -python-subunit>=0.0.18 # Apache-2.0/BSD -testtools>=1.4.0 # MIT -oslotest>=1.10.0 # Apache-2.0 -testrepository>=0.0.18 # Apache-2.0/BSD -testresources>=0.2.4 # Apache-2.0/BSD -testscenarios>=0.4 # Apache-2.0/BSD -oslosphinx>=4.7.0 # Apache-2.0 \ No newline at end of file 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) diff --git a/tox.ini b/tox.ini index c859582e..2f74083f 100644 --- a/tox.ini +++ b/tox.ini @@ -7,6 +7,19 @@ skipsdist = True install_command = pip install -U {opts} {packages} setenv = VIRTUAL_ENV={envdir} deps = -r{toxinidir}/test-requirements.txt +passenv = + OS_AUTH_URL + OS_USERNAME + OS_PASSWORD + OS_USER_DOMAIN_NAME + OS_PROJECT_NAME + OS_TENANT_NAME + OS_PROJECT_DOMAIN_NAME + IMAGE_NAME + VM_COUNT + PROFILER_TYPE + PYTHON_ENABLE + CI_DEBUG [testenv:verify] changedir = {toxinidir}/tests -- cgit 1.2.3-korg