API proposal for functest
[functest-xtesting.git] / functest / 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 import pkg_resources
14
15 import click
16
17 import functest.ci.tier_builder as tb
18 from functest.utils.constants import CONST
19 import functest.utils.functest_utils as ft_utils
20 import functest.utils.functest_vacation as vacation
21
22
23 class Testcase(object):
24
25     def __init__(self):
26         self.tiers = tb.TierBuilder(
27             CONST.__getattribute__('INSTALLER_TYPE'),
28             CONST.__getattribute__('DEPLOY_SCENARIO'),
29             pkg_resources.resource_filename('functest', 'ci/testcases.yaml'))
30
31     def list(self):
32         summary = ""
33         for tier in self.tiers.get_tiers():
34             for test in tier.get_tests():
35                 summary += (" %s\n" % test.get_name())
36         return summary
37
38     def show(self, testname):
39         description = self.tiers.get_test(testname)
40         return description
41
42     @staticmethod
43     def run(testname, noclean=False, report=False):
44
45         flags = ""
46         if noclean:
47             flags += "-n "
48         if report:
49             flags += "-r "
50
51         if testname == 'vacation':
52             vacation.main()
53         elif not os.path.isfile(CONST.__getattribute__('env_active')):
54             click.echo("Functest environment is not ready. "
55                        "Run first 'functest env prepare'")
56         else:
57             tests = testname.split(",")
58             for test in tests:
59                 cmd = "run_tests {}-t {}".format(flags, test)
60                 ft_utils.execute_command(cmd)
61
62
63 class CliTestcase(Testcase):
64
65     def __init__(self):
66         super(CliTestcase, self).__init__()
67
68     def list(self):
69         click.echo(super(CliTestcase, self).list())
70
71     def show(self, testname):
72         testcase_show = super(CliTestcase, self).show(testname)
73         if testcase_show:
74             click.echo(testcase_show)
75         else:
76             click.echo("The test case '%s' does not exist or is not supported."
77                        % testname)