summaryrefslogtreecommitdiffstats
path: root/tests/functional/utils.py
blob: d889c0dfa2578b0b948ee3da6b5acfbf30e9e1ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import copy
import os

from oslo_serialization import jsonutils

from yardstick.common import process


class Yardstick(object):
    """Create and represent separate yardstick installation.

    Usage:
        yardstick = yardstick()
        output = yardstick("runner list")

    """

    def __init__(self):
        self._args = ["yardstick"]
        self.env = copy.deepcopy(os.environ)

    def __call__(self, cmd, getjson=False):
        """Call yardstick in the shell

        :param cmd: Yardstick command.
        :param getjson: If the output is a JSON object, it's deserialized.
        :return Command output string.
        """

        if not isinstance(cmd, list):
            cmd = cmd.split(" ")
        cmd = self._args + cmd
        output = process.execute(cmd=cmd)
        if getjson:
            return jsonutils.loads(output)
        return output