9b2e60baa52179bfd4d68b2202a3c972e3ab7fdc
[functest-xtesting.git] / functest / cli / commands / cli_tier.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
21
22 class CliTier(object):
23
24     def __init__(self):
25         self.tiers = tb.TierBuilder(
26             CONST.__getattribute__('INSTALLER_TYPE'),
27             CONST.__getattribute__('DEPLOY_SCENARIO'),
28             pkg_resources.resource_filename('functest', 'ci/testcases.yaml'))
29
30     def list(self):
31         summary = ""
32         for tier in self.tiers.get_tiers():
33             summary += ("    - %s. %s:\n\t   %s\n"
34                         % (tier.get_order(),
35                            tier.get_name(),
36                            tier.get_test_names()))
37         click.echo(summary)
38
39     def show(self, tiername):
40         tier = self.tiers.get_tier(tiername)
41         if tier is None:
42             tier_names = self.tiers.get_tier_names()
43             click.echo("The tier with name '%s' does not exist. "
44                        "Available tiers are:\n  %s\n" % (tiername, tier_names))
45         else:
46             click.echo(self.tiers.get_tier(tiername))
47
48     def gettests(self, tiername):
49         tier = self.tiers.get_tier(tiername)
50         if tier is None:
51             tier_names = self.tiers.get_tier_names()
52             click.echo("The tier with name '%s' does not exist. "
53                        "Available tiers are:\n  %s\n" % (tiername, tier_names))
54         else:
55             tests = tier.get_test_names()
56             click.echo("Test cases in tier '%s':\n %s\n" % (tiername, tests))
57
58     @staticmethod
59     def run(tiername, noclean=False, report=False):
60
61         flags = ""
62         if noclean:
63             flags += "-n "
64         if report:
65             flags += "-r "
66
67         if not os.path.isfile(CONST.__getattribute__('env_active')):
68             click.echo("Functest environment is not ready. "
69                        "Run first 'functest env prepare'")
70         else:
71             cmd = "run_tests {}-t {}".format(flags, tiername)
72             ft_utils.execute_command(cmd)