add image code 87/29387/14
authordongwenjuan <dong.wenjuan@zte.com.cn>
Thu, 13 Apr 2017 01:09:41 +0000 (09:09 +0800)
committerdongwenjuan <dong.wenjuan@zte.com.cn>
Tue, 2 May 2017 03:01:56 +0000 (11:01 +0800)
JIRA: DOCTOR-93

Change-Id: I6551d5e31799b967ce442cb05a664c0d64a3dcf8
Signed-off-by: dongwenjuan <dong.wenjuan@zte.com.cn>
test-requirements.txt
tests/config.py
tests/identity_auth.py
tests/image.py [new file with mode: 0644]
tests/main.py
tests/os_clients.py [new file with mode: 0644]
tox.ini

index a2ce1d9..2928e0f 100644 (file)
@@ -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
index 2a062c2..2288d36 100644 (file)
@@ -8,9 +8,15 @@
 ##############################################################################\r
 from oslo_config import cfg\r
 \r
+import image\r
+import os_clients\r
+\r
 \r
 def list_opts():\r
-    return []\r
+    return [\r
+        ('os_clients', os_clients.OPTS),\r
+        ('image', image.IMAGE_OPTS),\r
+    ]\r
 \r
 \r
 def prepare_conf(conf=None):\r
index 4726ca3..0031cae 100644 (file)
@@ -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 (file)
index 0000000..0b4a3d7
--- /dev/null
@@ -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.......')
index e36bb4f..50e0821 100644 (file)
@@ -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 (file)
index 0000000..2eb406e
--- /dev/null
@@ -0,0 +1,21 @@
+##############################################################################\r
+# Copyright (c) 2017 ZTE Corporation and others.\r
+#\r
+# All rights reserved. This program and the accompanying materials\r
+# are made available under the terms of the Apache License, Version 2.0\r
+# which accompanies this distribution, and is available at\r
+# http://www.apache.org/licenses/LICENSE-2.0\r
+##############################################################################\r
+from oslo_config import cfg\r
+\r
+import glanceclient.client as glanceclient\r
+\r
+\r
+OPTS = [\r
+    cfg.StrOpt('glance_version', default='2', help='glance version'),\r
+]\r
+\r
+\r
+def glance_client(version, session):\r
+    return glanceclient.Client(version=version,\r
+                               session=session)\r
diff --git a/tox.ini b/tox.ini
index c859582..2f74083 100644 (file)
--- 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