1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
12 from oslo_config import cfg
14 from doctor_tests.identity_auth import get_session
15 from doctor_tests.os_clients import glance_client
18 cfg.StrOpt('image_name',
19 default=os.environ.get('IMAGE_NAME', 'cirros'),
20 help='the name of test image',
22 cfg.StrOpt('image_format',
24 help='the format of test image',
26 cfg.StrOpt('image_filename',
28 help='the name of image file',
30 cfg.StrOpt('image_download_url',
31 default='https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img', # noqa
32 help='the url where to get the image',
39 def __init__(self, conf, log):
43 glance_client(conf.glance_version, get_session())
44 self.use_existing_image = False
48 self.log.info('image create start......')
50 images = {image.name: image for image in self.glance.images.list()}
51 if self.conf.image_name not in images:
52 if not os.path.exists(self.conf.image_filename):
53 resp = urllib.request.urlopen(self.conf.image_download_url)
54 with open(self.conf.image_filename, "wb") as file:
55 file.write(resp.read())
57 self.glance.images.create(
58 name=self.conf.image_name,
59 disk_format=self.conf.image_format,
60 container_format="bare",
62 self.glance.images.upload(self.image['id'],
63 open(self.conf.image_filename, 'rb'))
65 self.use_existing_image = True
66 self.image = images[self.conf.image_name]
68 self.log.info('image create end......')
71 self.log.info('image delete start.......')
73 if not self.use_existing_image and self.image:
74 self.glance.images.delete(self.image['id'])
76 self.log.info('image delete end.......')