d5e33947e90b688c7f9851b80551dc6a586f36de
[yardstick.git] / yardstick / benchmark / scenarios / lib / get_flavor.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 GetFlavor(base.Scenario):
22     """Get an OpenStack flavor by name"""
23
24     __scenario_type__ = "GetFlavor"
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         self.flavor_name = self.options.get("flavor_name", "TestFlavor")
31         self.setup_done = False
32
33     def setup(self):
34         """scenario setup"""
35
36         self.setup_done = True
37
38     def run(self, result):
39         """execute the test"""
40
41         if not self.setup_done:
42             self.setup()
43
44         LOG.info("Querying flavor: %s", self.flavor_name)
45         flavor = op_utils.get_flavor_by_name(self.flavor_name)
46         if flavor:
47             LOG.info("Get flavor successful!")
48             values = [self._change_obj_to_dict(flavor)]
49         else:
50             LOG.info("Get flavor: no flavor matched!")
51             values = []
52
53         keys = self.scenario_cfg.get('output', '').split()
54         return self._push_to_outputs(keys, values)