Merge "Added Unit Tests for ci/prepare_env"
[functest.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
14 import click
15
16 import functest.ci.tier_builder as tb
17 from functest.utils.constants import CONST
18 import functest.utils.functest_utils as ft_utils
19 import functest.utils.functest_vacation as vacation
20
21
22 class CliTestcase(object):
23
24     def __init__(self):
25         self.tiers = tb.TierBuilder(CONST.INSTALLER_TYPE,
26                                     CONST.DEPLOY_SCENARIO,
27                                     CONST.functest_testcases_yaml)
28
29     def list(self):
30         summary = ""
31         for tier in self.tiers.get_tiers():
32             for test in tier.get_tests():
33                 summary += (" %s\n" % test.get_name())
34         click.echo(summary)
35
36     def show(self, testname):
37         description = self.tiers.get_test(testname)
38         if description is None:
39             click.echo("The test case '%s' does not exist or is not supported."
40                        % testname)
41
42         click.echo(description)
43
44     @staticmethod
45     def run(testname, noclean=False, report=False):
46
47         flags = ""
48         if noclean:
49             flags += "-n "
50         if report:
51             flags += "-r "
52
53         if testname == 'vacation':
54             vacation.main()
55         elif not os.path.isfile(CONST.env_active):
56             click.echo("Functest environment is not ready. "
57                        "Run first 'functest env prepare'")
58         else:
59             tests = testname.split(",")
60             for test in tests:
61                 cmd = ("python %s/functest/ci/run_tests.py "
62                        "%s -t %s" % (CONST.dir_repo_functest, flags, test))
63                 ft_utils.execute_command(cmd)