DevStack support
[doctor.git] / doctor_tests / image.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
3 #
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 ##############################################################################
9 import os
10 try:
11     from urllib.request import urlopen
12 except Exception:
13     from urllib2 import urlopen
14
15
16 from oslo_config import cfg
17
18 from doctor_tests.identity_auth import get_session
19 from doctor_tests.os_clients import glance_client
20
21 OPTS = [
22     cfg.StrOpt('image_name',
23                default=os.environ.get('IMAGE_NAME', 'cirros'),
24                help='the name of test image',
25                required=True),
26     cfg.StrOpt('image_format',
27                default='qcow2',
28                help='the format of test image',
29                required=True),
30     cfg.StrOpt('image_filename',
31                default='cirros.img',
32                help='the name of image file',
33                required=True),
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',
37                required=True),
38 ]
39
40
41 class Image(object):
42
43     def __init__(self, conf, log):
44         self.conf = conf
45         self.log = log
46         self.glance = \
47             glance_client(conf.glance_version, get_session())
48         self.use_existing_image = False
49         self.image = None
50
51     def create(self):
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]
56             if cirros:
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())
63             self.image = \
64                 self.glance.images.create(
65                     name=self.conf.image_name,
66                     disk_format=self.conf.image_format,
67                     container_format="bare",
68                     visibility="public")
69             self.glance.images.upload(self.image['id'],
70                                       open(self.conf.image_filename, 'rb'))
71         else:
72             self.use_existing_image = True
73             self.image = images[self.conf.image_name]
74
75         self.log.info('image create end......')
76
77     def delete(self):
78         self.log.info('image delete start.......')
79
80         if not self.use_existing_image and self.image:
81             self.glance.images.delete(self.image['id'])
82
83         self.log.info('image delete end.......')