Merge "Added 60 sec delay before launching instances in healthcheck"
[functest.git] / ci / run_tests.py
1 #!/usr/bin/env python
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 re
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.functest_utils as ft_utils
20 import functest.utils.openstack_utils as os_utils
21
22
23 """ arguments """
24 parser = argparse.ArgumentParser()
25 parser.add_argument("-t", "--test", dest="test", action='store',
26                     help="Test case or tier (group of tests) to be executed. "
27                     "It will run all the test if not specified.")
28 parser.add_argument("-n", "--noclean", help="Do not clean OpenStack resources"
29                     " after running each test (default=false).",
30                     action="store_true")
31 parser.add_argument("-r", "--report", help="Push results to database "
32                     "(default=false).", action="store_true")
33 args = parser.parse_args()
34
35
36 """ logging configuration """
37 logger = ft_logger.Logger("run_tests").getLogger()
38
39
40 """ global variables """
41 REPOS_DIR = os.getenv('repos_dir')
42 FUNCTEST_REPO = ("%s/functest/" % REPOS_DIR)
43 EXEC_SCRIPT = ("%sci/exec_test.sh" % FUNCTEST_REPO)
44 CLEAN_FLAG = True
45 REPORT_FLAG = False
46
47
48 def print_separator(str, count=45):
49     line = ""
50     for i in range(0, count - 1):
51         line += str
52     logger.info("%s" % line)
53
54
55 def source_rc_file():
56     rc_file = os.getenv('creds')
57     if not os.path.isfile(rc_file):
58         logger.error("RC file %s does not exist..." % rc_file)
59         sys.exit(1)
60     logger.info("Sourcing the OpenStack RC file...")
61     os_utils.source_credentials(rc_file)
62
63
64 def cleanup():
65     clean_os.main()
66
67
68 def run_test(test):
69     test_name = test.get_name()
70     print_separator("=")
71     logger.info("Running test case '%s'..." % test_name)
72     print_separator("=")
73     logger.debug("\n%s" % test)
74     flags = (" -t %s" % (test_name))
75     if REPORT_FLAG:
76         flags += " -r"
77
78     cmd = ("%s%s" % (EXEC_SCRIPT, flags))
79     logger.debug("Executing command '%s'" % cmd)
80
81     result = ft_utils.execute_command(cmd, logger, exit_on_error=False)
82
83     if result != 0:
84         logger.error("The test case '%s' failed. Cleaning and exiting."
85                      % test_name)
86         if CLEAN_FLAG:
87             cleanup()
88         sys.exit(1)
89
90     if CLEAN_FLAG:
91         cleanup()
92
93
94 def run_tier(tier):
95     print_separator("#")
96     logger.info("Running tier '%s'" % tier.get_name())
97     print_separator("#")
98     logger.debug("\n%s" % tier)
99     for test in tier.get_tests():
100         run_test(test)
101
102
103 def run_all(tiers):
104     summary = ""
105     BUILD_TAG = os.getenv('BUILD_TAG')
106     if BUILD_TAG is not None and re.search("daily", BUILD_TAG) is not None:
107         CI_LOOP = "daily"
108     else:
109         CI_LOOP = "weekly"
110
111     tiers_to_run = []
112
113     for tier in tiers.get_tiers():
114         if re.search(CI_LOOP, tier.get_ci_loop()) is not None:
115             tiers_to_run.append(tier)
116             summary += ("\n    - %s. %s:\n\t   %s"
117                         % (tier.get_order(),
118                            tier.get_name(),
119                            tier.get_test_names()))
120
121     logger.info("Tiers to be executed:%s" % summary)
122
123     for tier in tiers_to_run:
124         run_tier(tier)
125
126
127 def main():
128     global CLEAN_FLAG
129     global REPORT_FLAG
130
131     CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
132     CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
133
134     file = FUNCTEST_REPO + "/ci/testcases.yaml"
135     _tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, file)
136
137     if args.noclean:
138         CLEAN_FLAG = False
139
140     if args.report:
141         REPORT_FLAG = True
142
143     if args.test:
144         source_rc_file()
145         if _tiers.get_tier(args.test):
146             run_tier(_tiers.get_tier(args.test))
147
148         elif _tiers.get_test(args.test):
149             run_test(_tiers.get_test(args.test))
150
151         elif args.test == "all":
152             run_all(_tiers)
153
154         else:
155             logger.error("Unknown test case or tier '%s', or not supported by "
156                          "the given scenario '%s'."
157                          % (args.test, CI_SCENARIO))
158             logger.debug("Available tiers are:\n\n%s"
159                          % _tiers)
160     else:
161         run_all(_tiers)
162
163     sys.exit(0)
164
165 if __name__ == '__main__':
166     main()