Merge "unify functest_yaml obtain process"
[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
19 functest_yaml = ft_utils.get_functest_yaml()
20
21 FUNCTEST_CONF_DIR = functest_yaml.get("general").get(
22     "directories").get("dir_functest_conf")
23 ENV_FILE = FUNCTEST_CONF_DIR + "/env_active"
24
25
26 class CliTestcase:
27
28     def __init__(self):
29         CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
30         CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
31         testcases = ft_utils.get_testcases_file()
32         self.tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, testcases)
33
34     def list(self):
35         summary = ""
36         for tier in self.tiers.get_tiers():
37             for test in tier.get_tests():
38                 summary += (" %s\n" % test.get_name())
39         click.echo(summary)
40
41     def show(self, testname):
42         description = self.tiers.get_test(testname)
43         if description is None:
44             click.echo("The test case '%s' does not exist or is not supported."
45                        % testname)
46
47         click.echo(description)
48
49     def run(self, testname, noclean=False):
50         if testname == 'vacation':
51             vacation.main()
52         elif not os.path.isfile(ENV_FILE):
53             click.echo("Functest environment is not ready. "
54                        "Run first 'functest env prepare'")
55         else:
56             if noclean:
57                 cmd = ("python /home/opnfv/repos/functest/ci/run_tests.py "
58                        "-n -t %s" % testname)
59             else:
60                 cmd = ("python /home/opnfv/repos/functest/ci/run_tests.py "
61                        "-t %s" % testname)
62             ft_utils.execute_command(cmd)