5b930982847146a13fbd8590d977105486947a6b
[functest.git] / ci / run_tests.py
1 #!/bin/bash
2 #
3 # Author: Jose Lausuch (jose.lausuch@ericsson.com)
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11 import argparse
12 import os
13 import subprocess
14 import sys
15
16 import functest.ci.tier_builder as tb
17 import functest.utils.clean_openstack as clean_os
18 import functest.utils.functest_logger as ft_logger
19 import functest.utils.openstack_utils as os_utils
20
21
22 """ arguments """
23 parser = argparse.ArgumentParser()
24 parser.add_argument("-t", "--test", dest="test", action='store',
25                     help="Test case or tier (group of tests) to be executed. "
26                     "It will run all the test if not specified.")
27 parser.add_argument("-n", "--noclean", help="Do not clean OpenStack resources"
28                     " after running each test (default=false).",
29                     action="store_true")
30 parser.add_argument("-r", "--report", help="Push results to database "
31                     "(default=false).", action="store_true")
32 args = parser.parse_args()
33
34
35 """ logging configuration """
36 logger = ft_logger.Logger("run_tests").getLogger()
37
38
39 """ global variables """
40 REPOS_DIR = os.getenv('repos_dir')
41 FUNCTEST_REPO = ("%s/functest/" % REPOS_DIR)
42 EXEC_SCRIPT = ("%sci/exec_test.sh" % FUNCTEST_REPO)
43 CLEAN_FLAG = True
44 REPORT_FLAG = False
45
46
47 def print_separator(str, count=45):
48     line = ""
49     for i in range(0, count - 1):
50         line += str
51     logger.info("%s" % line)
52
53
54 def source_rc_file():
55     rc_file = os.getenv('creds')
56     if not os.path.isfile(rc_file):
57         logger.error("RC file %s does not exist..." % rc_file)
58         sys.exit(1)
59     logger.info("Sourcing the OpenStack RC file...")
60     os_utils.source_credentials(rc_file)
61
62
63 def cleanup():
64     clean_os.main()
65
66
67 def run_test(test):
68     test_name = test.get_name()
69     print_separator("=")
70     logger.info("Running test case '%s'..." % test_name)
71     print_separator("=")
72     logger.debug("\n%s" % test)
73     flags = (" -t %s" % (test_name))
74     if REPORT_FLAG:
75         flags += " -r"
76
77     cmd = ("%s%s" % (EXEC_SCRIPT, flags))
78     logger.debug("Executing command '%s'" % cmd)
79
80     p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
81
82     while p.poll() is None:
83         line = p.stdout.readline().rstrip()
84         logger.debug(line)
85
86     if p.returncode != 0:
87         logger.error("The test case '%s' failed. Cleaning and exiting."
88                      % test_name)
89         if CLEAN_FLAG:
90             cleanup()
91         sys.exit(1)
92
93     if CLEAN_FLAG:
94         cleanup()
95
96
97 def run_tier(tier):
98     print_separator("#")
99     logger.info("Running tier '%s'" % tier.get_name())
100     print_separator("#")
101     logger.debug("\n%s" % tier)
102     for test in tier.get_tests():
103         run_test(test)
104
105
106 def run_all(tiers):
107     summary = ""
108     for tier in tiers.get_tiers():
109         summary += ("\n    - %s. %s:\n\t   %s"
110                     % (tier.get_order(),
111                        tier.get_name(),
112                        tier.get_test_names()))
113
114     logger.info("Tiers to be executed:%s" % summary)
115
116     for tier in tiers.get_tiers():
117         run_tier(tier)
118
119
120 def main():
121     global CLEAN_FLAG
122     global REPORT_FLAG
123
124     CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
125     CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
126
127     file = FUNCTEST_REPO + "/ci/testcases.yaml"
128     _tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, file)
129
130     if args.noclean:
131         CLEAN_FLAG = False
132
133     if args.report:
134         REPORT_FLAG = True
135
136     if args.test:
137         source_rc_file()
138         if _tiers.get_tier(args.test):
139             run_tier(_tiers.get_tier(args.test))
140
141         elif _tiers.get_test(args.test):
142             run_test(_tiers.get_test(args.test))
143
144         elif args.test == "all":
145             run_all(_tiers)
146
147         else:
148             logger.error("Unknown test case or tier '%s', or not supported by "
149                          "the given scenario '%s'."
150                          % (args.test, CI_SCENARIO))
151             logger.debug("Available tiers are:\n\n%s"
152                          % _tiers)
153     else:
154         run_all(_tiers)
155
156     sys.exit(0)
157
158 if __name__ == '__main__':
159     main()