Merge "Move tests: functional/"
[yardstick.git] / yardstick / tests / functional / utils.py
1 ##############################################################################
2 # Copyright (c) 2015 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 copy
11 import os
12
13 from oslo_serialization import jsonutils
14
15 from yardstick.common import process
16
17
18 class Yardstick(object):
19     """Create and represent separate yardstick installation.
20
21     Usage:
22         yardstick = yardstick()
23         output = yardstick("runner list")
24
25     """
26
27     def __init__(self):
28         self._args = ["yardstick"]
29         self.env = copy.deepcopy(os.environ)
30
31     def __call__(self, cmd, getjson=False):
32         """Call yardstick in the shell
33
34         :param cmd: Yardstick command.
35         :param getjson: If the output is a JSON object, it's deserialized.
36         :return Command output string.
37         """
38
39         if not isinstance(cmd, list):
40             cmd = cmd.split(" ")
41         cmd = self._args + cmd
42         output = process.execute(cmd=cmd)
43         if getjson:
44             return jsonutils.loads(output)
45         return output