0b4a3d723e8cf87c7e1dec15c93e8413fce9fa09
[doctor.git] / 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 urllib2
11
12 from oslo_config import cfg
13
14 from identity_auth import get_session
15 from os_clients import glance_client
16 import logger as doctor_log
17
18 IMAGE_OPTS = [
19     cfg.StrOpt('name',
20                default=os.environ.get('IMAGE_NAME', 'cirros'),
21                help='the name of test image',
22                required=True),
23     cfg.StrOpt('format',
24                default='qcow2',
25                help='the format of test image',
26                required=True),
27     cfg.StrOpt('file_name',
28                default='cirros.img',
29                help='the name of image file',
30                required=True),
31     cfg.StrOpt('url',
32                default='https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img',
33                help='the url where to get the image',
34                required=True),
35 ]
36
37 LOG = doctor_log.Logger('doctor').getLogger()
38
39
40 class Image(object):
41
42     def __init__(self, conf):
43         self.conf = conf
44         self.glance = \
45             glance_client(conf.os_clients.glance_version,
46                           get_session())
47         self.use_existing_image = False
48         self.image = None
49
50     def create(self):
51         LOG.info('image create start......')
52
53         images = {image.name: image for image in self.glance.images.list()}
54         if self.conf.image.name not in images:
55             if not os.path.exists(self.conf.image.file_name):
56                 resp = urllib2.urlopen(self.conf.image.url)
57                 with open(self.conf.image.file_name, "wb") as file:
58                     file.write(resp.read())
59             self.image = self.glance.images.create(name=self.conf.image.name,
60                                                    disk_format=self.conf.image.format,
61                                                    container_format="bare",
62                                                    visibility="public")
63             self.glance.images.upload(self.image['id'],
64                                       open(self.conf.image.file_name, 'rb'))
65         else:
66             self.use_existing_image = True
67             self.image = images[self.conf.image.name]
68
69         LOG.info('image create end......')
70
71     def delete(self):
72         LOG.info('image delete start.......')
73
74         if not self.use_existing_image and self.image:
75             self.glance.images.delete(self.image['id'])
76
77         LOG.info('image delete end.......')