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