Maintenance support for latest Fenix, python3 and Fuel
[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 import urllib.request
11
12 from oslo_config import cfg
13
14 from doctor_tests.identity_auth import get_session
15 from doctor_tests.os_clients import glance_client
16
17 OPTS = [
18     cfg.StrOpt('image_name',
19                default=os.environ.get('IMAGE_NAME', 'cirros'),
20                help='the name of test image',
21                required=True),
22     cfg.StrOpt('image_format',
23                default='qcow2',
24                help='the format of test image',
25                required=True),
26     cfg.StrOpt('image_filename',
27                default='cirros.img',
28                help='the name of image file',
29                required=True),
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',
33                required=True),
34 ]
35
36
37 class Image(object):
38
39     def __init__(self, conf, log):
40         self.conf = conf
41         self.log = log
42         self.glance = \
43             glance_client(conf.glance_version, get_session())
44         self.use_existing_image = False
45         self.image = None
46
47     def create(self):
48         self.log.info('image create start......')
49
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())
56             self.image = \
57                 self.glance.images.create(
58                     name=self.conf.image_name,
59                     disk_format=self.conf.image_format,
60                     container_format="bare",
61                     visibility="public")
62             self.glance.images.upload(self.image['id'],
63                                       open(self.conf.image_filename, 'rb'))
64         else:
65             self.use_existing_image = True
66             self.image = images[self.conf.image_name]
67
68         self.log.info('image create end......')
69
70     def delete(self):
71         self.log.info('image delete start.......')
72
73         if not self.use_existing_image and self.image:
74             self.glance.images.delete(self.image['id'])
75
76         self.log.info('image delete end.......')