Merge "unify functest_yaml obtain process"
[functest.git] / 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
14 import click
15 import functest.ci.tier_builder as tb
16 import functest.utils.functest_utils as ft_utils
17
18 functest_yaml = ft_utils.get_functest_yaml()
19
20 FUNCTEST_CONF_DIR = functest_yaml.get("general").get(
21     "directories").get("dir_functest_conf")
22 ENV_FILE = FUNCTEST_CONF_DIR + "/env_active"
23
24
25 class CliTier:
26
27     def __init__(self):
28         CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
29         CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
30         testcases = ft_utils.get_testcases_file()
31         self.tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, testcases)
32
33     def list(self):
34         summary = ""
35         for tier in self.tiers.get_tiers():
36             summary += ("    - %s. %s:\n\t   %s\n"
37                         % (tier.get_order(),
38                            tier.get_name(),
39                            tier.get_test_names()))
40         click.echo(summary)
41
42     def show(self, tiername):
43         tier = self.tiers.get_tier(tiername)
44         if tier is None:
45             tier_names = self.tiers.get_tier_names()
46             click.echo("The tier with name '%s' does not exist. "
47                        "Available tiers are:\n  %s\n" % (tiername, tier_names))
48         else:
49             click.echo(self.tiers.get_tier(tiername))
50
51     def gettests(self, tiername):
52         tier = self.tiers.get_tier(tiername)
53         if tier is None:
54             tier_names = self.tiers.get_tier_names()
55             click.echo("The tier with name '%s' does not exist. "
56                        "Available tiers are:\n  %s\n" % (tiername, tier_names))
57         else:
58             tests = tier.get_test_names()
59             click.echo("Test cases in tier '%s':\n %s\n" % (tiername, tests))
60
61     def run(self, tiername, noclean=False):
62         if not os.path.isfile(ENV_FILE):
63             click.echo("Functest environment is not ready. "
64                        "Run first 'functest env prepare'")
65         else:
66             if noclean:
67                 cmd = ("python /home/opnfv/repos/functest/ci/run_tests.py "
68                        "-n -t %s" % tiername)
69             else:
70                 cmd = ("python /home/opnfv/repos/functest/ci/run_tests.py "
71                        "-t %s" % tiername)
72             ft_utils.execute_command(cmd)