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 ##############################################################################
11 from urllib.request import urlopen
13 from urllib2 import urlopen
16 from oslo_config import cfg
18 from doctor_tests.identity_auth import get_session
19 from doctor_tests.os_clients import glance_client
22 cfg.StrOpt('image_name',
23 default=os.environ.get('IMAGE_NAME', 'cirros'),
24 help='the name of test image',
26 cfg.StrOpt('image_format',
28 help='the format of test image',
30 cfg.StrOpt('image_filename',
32 help='the name of image file',
34 cfg.StrOpt('image_download_url',
35 default='https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img', # noqa
36 help='the url where to get the image',
43 def __init__(self, conf, log):
47 glance_client(conf.glance_version, get_session())
48 self.use_existing_image = False
52 self.log.info('image create start......')
53 images = {image.name: image for image in self.glance.images.list()}
54 if self.conf.image_name == 'cirros':
55 cirros = [image for image in images if 'cirros' in image]
57 self.conf.image_name = cirros[0]
58 if self.conf.image_name not in images:
59 if not os.path.exists(self.conf.image_filename):
60 resp = urlopen(self.conf.image_download_url)
61 with open(self.conf.image_filename, "wb") as file:
62 file.write(resp.read())
64 self.glance.images.create(
65 name=self.conf.image_name,
66 disk_format=self.conf.image_format,
67 container_format="bare",
69 self.glance.images.upload(self.image['id'],
70 open(self.conf.image_filename, 'rb'))
72 self.use_existing_image = True
73 self.image = images[self.conf.image_name]
75 self.log.info('image create end......')
78 self.log.info('image delete start.......')
80 if not self.use_existing_image and self.image:
81 self.glance.images.delete(self.image['id'])
83 self.log.info('image delete end.......')