summaryrefslogtreecommitdiffstats
path: root/dovetail/cli/commands/cli_testcase.py
blob: 6711381c35ee871b4ae585665f8a5f289cd66aa3 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
##############################################################################
# Copyright (c) 2016 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 os

import click
import yaml

from dovetail import constants
from dovetail.testcase import Testsuite
from dovetail.utils.dovetail_config import DovetailConfig as dt_cfg
import dovetail.utils.dovetail_utils as dt_utils


class CliTestcase(object):

    @staticmethod
    def testsuite_load():
        dt_cfg.load_config_files(constants.CONF_PATH)
        Testsuite.load()

    @staticmethod
    def list_one_testsuite(testsuite):
        testsuite_stream = Testsuite.get(testsuite)
        if testsuite_stream:
            mandatory = dt_utils.get_value_from_dict(
                'testcases_list.mandatory', testsuite_stream)
            optional = dt_utils.get_value_from_dict(
                'testcases_list.optional', testsuite_stream)
            if mandatory:
                click.echo("- mandatory")
                for testcase in mandatory:
                    click.echo("    {}".format(testcase))
            if optional:
                click.echo("- optional")
                for testcase in optional:
                    click.echo("    {}".format(testcase))
            if not (mandatory or optional):
                click.echo("No testcase in testsuite {}".format(testsuite))
        else:
            click.echo("testsuite {} does not exist".format(testsuite))

    def list_testsuites(self, testsuite):
        self.testsuite_load()
        if testsuite:
            self.list_one_testsuite(testsuite)
        else:
            testsuite_json = Testsuite.get_all()
            if testsuite_json:
                for key in testsuite_json.keys():
                    click.echo("--------------------------")
                    click.echo("Test Suite {}".format(key))
                    click.echo("--------------------------")
                    self.list_one_testsuite(key)
            else:
                click.echo("No testsuite defined yet in dovetail!!!")

    @staticmethod
    def show_testcase(name):
        tc_path = os.path.join(constants.TESTCASE_PATH, "{}.yml".format(name))
        if os.path.isfile(tc_path):
            with open(tc_path, 'r') as stream:
                try:
                    click.echo(stream.read())
                except yaml.YAMLError as exc:
                    click.echo(exc)
        else:
            click.echo("testcase %s not exist or not supported" % name)

    @staticmethod
    def run(args_str):
        options = ''
        if args_str:
            options = options + args_str

        repo_dir = os.path.abspath(
            os.path.join(os.path.dirname(__file__),
                         os.pardir, os.pardir))

        cmd = ("python3 %s/run.py"
               " %s" % (repo_dir, options))
        dt_utils.exec_cmd(cmd, exit_on_error=True,
                          exec_msg_on=False, info=True)