Merge "Add ODL HA testcase"
[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 from __future__ import print_function
11 from __future__ import absolute_import
12
13 import time
14 import logging
15
16 from yardstick.benchmark.scenarios import base
17 import yardstick.common.openstack_utils as op_utils
18
19 LOG = logging.getLogger(__name__)
20
21
22 class CreateVolume(base.Scenario):
23     """Create an OpenStack volume"""
24
25     __scenario_type__ = "CreateVolume"
26
27     def __init__(self, scenario_cfg, context_cfg):
28         self.scenario_cfg = scenario_cfg
29         self.context_cfg = context_cfg
30         self.options = self.scenario_cfg['options']
31
32         self.volume_name = self.options.get("volume_name", "TestVolume")
33         self.volume_size = self.options.get("size", 100)
34         self.image_name = self.options.get("image", None)
35         self.image_id = None
36
37         self.glance_client = op_utils.get_glance_client()
38         self.cinder_client = op_utils.get_cinder_client()
39
40         self.setup_done = False
41
42     def setup(self):
43         """scenario setup"""
44
45         self.setup_done = True
46
47     def run(self):
48         """execute the test"""
49
50         if not self.setup_done:
51             self.setup()
52
53         self.image_id = op_utils.get_image_id(self.glance_client,
54                                               self.image_name)
55
56         volume = op_utils.create_volume(self.cinder_client, self.volume_name,
57                                         self.volume_size, self.image_id)
58
59         status = volume.status
60         while(status == 'creating' or status == 'downloading'):
61             LOG.info("Volume status is: %s", status)
62             time.sleep(5)
63             volume = op_utils.get_volume_by_name(self.volume_name)
64             status = volume.status
65
66         LOG.info("Create volume successful!")
67
68         values = [volume.id]
69         keys = self.scenario_cfg.get('output', '').split()
70         return self._push_to_outputs(keys, values)