Merge "Cleanup CGNAPT unit tests"
[yardstick.git] / yardstick / benchmark / scenarios / lib / create_volume.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 import time
11 import logging
12
13 from yardstick.benchmark.scenarios import base
14 from yardstick.common import openstack_utils
15 from yardstick.common import exceptions
16
17 LOG = logging.getLogger(__name__)
18
19
20 class CreateVolume(base.Scenario):
21     """Create an OpenStack volume"""
22
23     __scenario_type__ = "CreateVolume"
24
25     def __init__(self, scenario_cfg, context_cfg):
26         self.scenario_cfg = scenario_cfg
27         self.context_cfg = context_cfg
28         self.options = self.scenario_cfg["options"]
29
30         self.size = self.options["size_gb"]
31         self.wait = self.options.get("wait", True)
32         self.timeout = self.options.get("timeout")
33         self.image = self.options.get("image")
34         self.name = self.options.get("name")
35         self.description = self.options.get("description")
36
37         self.shade_client = openstack_utils.get_shade_client()
38
39         self.setup_done = False
40
41     def setup(self):
42         """scenario setup"""
43
44         self.setup_done = True
45
46     def run(self, result):
47         """execute the test"""
48
49         if not self.setup_done:
50             self.setup()
51
52         volume = openstack_utils.create_volume(
53             self.shade_client, self.size, wait=self.wait, timeout=self.timeout,
54             image=self.image, name=self.name, description=self.description)
55
56         if not volume:
57             result.update({"volume_create": 0})
58             LOG.error("Create volume failed!")
59             raise exceptions.ScenarioCreateVolumeError
60
61         status = volume["status"]
62         while status == "creating" or status == "downloading":
63             LOG.info("Volume status is: %s", status)
64             time.sleep(5)
65             volume = openstack_utils.get_volume(self.shade_client, self.name)
66             status = volume["status"]
67         result.update({"volume_create": 1})
68         LOG.info("Create volume successful!")
69         values = [volume["id"]]
70         keys = self.scenario_cfg.get("output", '').split()
71         return self._push_to_outputs(keys, values)