fix bug of tc075 \t format error in yaml
[yardstick.git] / 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 json
12 import os
13 import shutil
14 import subprocess
15
16
17 from oslo_utils import encodeutils
18
19
20 class Yardstick(object):
21     """Create and represent separate yardstick installation.
22
23     Usage:
24         yardstick = yardstick()
25         output = yardstick("runner list")
26
27     """
28
29     def __init__(self, fake=False):
30
31         self.args = ["yardstick"]
32         self.env = copy.deepcopy(os.environ)
33
34     def __del__(self):
35         pass
36
37     def __call__(self, cmd, getjson=False, report_path=None, raw=False,
38                  suffix=None, extension=None, keep_old=False,
39                  write_report=False):
40         """Call yardstick in the shell
41
42         :param cmd: yardstick command
43         :param getjson: in cases, when yardstick prints JSON, you can catch output
44             deserialized
45         TO DO:
46         :param report_path: if present, yardstick command and its output will be
47             written to file with passed file name
48         :param raw: don't write command itself to report file. Only output
49             will be written
50         """
51
52         if not isinstance(cmd, list):
53             cmd = cmd.split(" ")
54         try:
55             output = encodeutils.safe_decode(subprocess.check_output(
56                 self.args + cmd, stderr=subprocess.STDOUT, env=self.env))
57
58             if getjson:
59                 return json.loads(output)
60             return output
61         except subprocess.CalledProcessError as e:
62             raise e
63