9a4ce967c01301066d64b32e9ac783cd4936897d
[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 sys
14
15 import functest.ci.tier_builder as tb
16 import functest.utils.clean_openstack as clean_os
17 import functest.utils.functest_logger as ft_logger
18 import functest.utils.functest_utils as ft_utils
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     result = ft_utils.execute_command(cmd, logger, exit_on_error=False)
81
82     if result != 0:
83         logger.error("The test case '%s' failed. Cleaning and exiting."
84                      % test_name)
85         if CLEAN_FLAG:
86             cleanup()
87         sys.exit(1)
88
89     if CLEAN_FLAG:
90         cleanup()
91
92
93 def run_tier(tier):
94     print_separator("#")
95     logger.info("Running tier '%s'" % tier.get_name())
96     print_separator("#")
97     logger.debug("\n%s" % tier)
98     for test in tier.get_tests():
99         run_test(test)
100
101
102 def run_all(tiers):
103     summary = ""
104     for tier in tiers.get_tiers():
105         summary += ("\n    - %s. %s:\n\t   %s"
106                     % (tier.get_order(),
107                        tier.get_name(),
108                        tier.get_test_names()))
109
110     logger.info("Tiers to be executed:%s" % summary)
111
112     for tier in tiers.get_tiers():
113         run_tier(tier)
114
115
116 def main():
117     global CLEAN_FLAG
118     global REPORT_FLAG
119
120     CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
121     CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
122
123     file = FUNCTEST_REPO + "/ci/testcases.yaml"
124     _tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, file)
125
126     if args.noclean:
127         CLEAN_FLAG = False
128
129     if args.report:
130         REPORT_FLAG = True
131
132     if args.test:
133         source_rc_file()
134         if _tiers.get_tier(args.test):
135             run_tier(_tiers.get_tier(args.test))
136
137         elif _tiers.get_test(args.test):
138             run_test(_tiers.get_test(args.test))
139
140         elif args.test == "all":
141             run_all(_tiers)
142
143         else:
144             logger.error("Unknown test case or tier '%s', or not supported by "
145                          "the given scenario '%s'."
146                          % (args.test, CI_SCENARIO))
147             logger.debug("Available tiers are:\n\n%s"
148                          % _tiers)
149     else:
150         run_all(_tiers)
151
152     sys.exit(0)
153
154 if __name__ == '__main__':
155     main()