d1b24829738937b090d02303692be230c0e19f17
[functest.git] / cli / commands / cli_testcase.py
1 #!/usr/bin/env python
2 #
3 # jose.lausuch@ericsson.com
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 """ global variables """
11
12 import os
13
14 import click
15 import functest.ci.tier_builder as tb
16 import functest.utils.functest_utils as ft_utils
17 import functest.utils.functest_vacation as vacation
18 import yaml
19
20
21 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
22     functest_yaml = yaml.safe_load(f)
23
24 FUNCTEST_CONF_DIR = functest_yaml.get("general").get(
25     "directories").get("dir_functest_conf")
26 ENV_FILE = FUNCTEST_CONF_DIR + "/env_active"
27
28
29 class CliTestcase:
30
31     def __init__(self):
32         CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
33         CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
34         testcases = ft_utils.get_testcases_file()
35         self.tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, testcases)
36
37     def list(self):
38         summary = ""
39         for tier in self.tiers.get_tiers():
40             for test in tier.get_tests():
41                 summary += (" %s\n" % test.get_name())
42         click.echo(summary)
43
44     def show(self, testname):
45         description = self.tiers.get_test(testname)
46         if description is None:
47             click.echo("The test case '%s' does not exist or is not supported."
48                        % testname)
49
50         click.echo(description)
51
52     def run(self, testname, noclean=False):
53         if testname == 'vacation':
54             vacation.main()
55         elif not os.path.isfile(ENV_FILE):
56             click.echo("Functest environment is not ready. "
57                        "Run first 'functest env prepare'")
58         else:
59             if noclean:
60                 cmd = ("python /home/opnfv/repos/functest/ci/run_tests.py "
61                        "-n -t %s" % testname)
62             else:
63                 cmd = ("python /home/opnfv/repos/functest/ci/run_tests.py "
64                        "-t %s" % testname)
65             ft_utils.execute_command(cmd)