Merge "Added traffic update capability to Ixload TG"
[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 import logging
10
11 from yardstick.benchmark.scenarios import base
12 from yardstick.common import openstack_utils
13 from yardstick.common import exceptions
14
15 LOG = logging.getLogger(__name__)
16
17
18 class CreateImage(base.Scenario):
19     """Create an OpenStack image"""
20
21     __scenario_type__ = "CreateImage"
22
23     def __init__(self, scenario_cfg, context_cfg):
24         self.scenario_cfg = scenario_cfg
25         self.context_cfg = context_cfg
26         self.options = self.scenario_cfg["options"]
27
28         self.name = self.options["image_name"]
29         self.file_name = self.options.get("file_name")
30         self.container = self.options.get("container", 'images')
31         self.md5 = self.options.get("md5")
32         self.sha256 = self.options.get("sha256")
33         self.disk_format = self.options.get("disk_format")
34         self.container_format = self.options.get("container_format",)
35         self.disable_vendor_agent = self.options.get("disable_vendor_agent", True)
36         self.wait = self.options.get("wait", True)
37         self.timeout = self.options.get("timeout", 3600)
38         self.allow_duplicates = self.options.get("allow_duplicates", False)
39         self.meta = self.options.get("meta")
40         self.volume = self.options.get("volume")
41
42         self.shade_client = openstack_utils.get_shade_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 = openstack_utils.create_image(
58             self.shade_client, self.name, filename=self.file_name,
59             container=self.container, md5=self.md5, sha256=self.sha256,
60             disk_format=self.disk_format,
61             container_format=self.container_format,
62             disable_vendor_agent=self.disable_vendor_agent, wait=self.wait,
63             timeout=self.timeout, allow_duplicates=self.allow_duplicates,
64             meta=self.meta, volume=self.volume)
65
66         if not image_id:
67             result.update({"image_create": 0})
68             LOG.error("Create image failed!")
69             raise exceptions.ScenarioCreateImageError
70
71         result.update({"image_create": 1})
72         LOG.info("Create image successful!")
73         keys = self.scenario_cfg.get("output", '').split()
74         values = [image_id]
75         return self._push_to_outputs(keys, values)