Merge "Add common openstack opertation scenarios: network"
[yardstick.git] / yardstick / benchmark / scenarios / lib / create_image.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd 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
10 from __future__ import print_function
11 from __future__ import absolute_import
12
13 import logging
14
15 from yardstick.benchmark.scenarios import base
16 import yardstick.common.openstack_utils as op_utils
17
18 LOG = logging.getLogger(__name__)
19
20
21 class CreateImage(base.Scenario):
22     """Create an OpenStack image"""
23
24     __scenario_type__ = "CreateImage"
25
26     def __init__(self, scenario_cfg, context_cfg):
27         self.scenario_cfg = scenario_cfg
28         self.context_cfg = context_cfg
29         self.options = self.scenario_cfg['options']
30
31         self.image_name = self.options.get("image_name", "TestImage")
32         self.file_path = self.options.get("file_path", None)
33         self.disk_format = self.options.get("disk_format", "qcow2")
34         self.container_format = self.options.get("container_format", "bare")
35         self.min_disk = self.options.get("min_disk", 0)
36         self.min_ram = self.options.get("min_ram", 0)
37         self.protected = self.options.get("protected", False)
38         self.public = self.options.get("public", "public")
39         self.tags = self.options.get("tags", [])
40         self.custom_property = self.options.get("property", {})
41
42         self.glance_client = op_utils.get_glance_client()
43
44         self.setup_done = False
45
46     def setup(self):
47         """scenario setup"""
48
49         self.setup_done = True
50
51     def run(self, result):
52         """execute the test"""
53
54         if not self.setup_done:
55             self.setup()
56
57         image_id = op_utils.create_image(self.glance_client, self.image_name,
58                                          self.file_path, self.disk_format,
59                                          self.container_format, self.min_disk,
60                                          self.min_ram, self.protected, self.tags,
61                                          self.public, **self.custom_property)
62
63         if image_id:
64             LOG.info("Create image successful!")
65             values = [image_id]
66
67         else:
68             LOG.info("Create image failed!")
69             values = []
70
71         keys = self.scenario_cfg.get('output', '').split()
72         return self._push_to_outputs(keys, values)