050ef0c119590ccc4d1e0d080729714bb0d659ce
[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.functest_logger as ft_logger
18 import functest.utils.functest_utils as ft_utils
19 import functest.utils.openstack_clean as os_clean
20 import functest.utils.openstack_snapshot as os_snapshot
21 import functest.utils.openstack_utils as os_utils
22
23
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 generate_os_snapshot():
65     os_snapshot.main()
66
67
68 def cleanup():
69     os_clean.main()
70
71
72 def run_test(test):
73     test_name = test.get_name()
74     print_separator("=")
75     logger.info("Running test case '%s'..." % test_name)
76     print_separator("=")
77     logger.debug("\n%s" % test)
78
79     generate_os_snapshot()
80
81     flags = (" -t %s" % (test_name))
82     if REPORT_FLAG:
83         flags += " -r"
84
85     cmd = ("%s%s" % (EXEC_SCRIPT, flags))
86     logger.debug("Executing command '%s'" % cmd)
87
88     result = ft_utils.execute_command(cmd, logger, exit_on_error=False)
89
90     if result != 0:
91         logger.error("The test case '%s' failed. Cleaning and exiting."
92                      % test_name)
93         if CLEAN_FLAG:
94             cleanup()
95         sys.exit(1)
96
97     if CLEAN_FLAG:
98         cleanup()
99
100
101 def run_tier(tier):
102     print_separator("#")
103     logger.info("Running tier '%s'" % tier.get_name())
104     print_separator("#")
105     logger.debug("\n%s" % tier)
106     for test in tier.get_tests():
107         run_test(test)
108
109
110 def run_all(tiers):
111     summary = ""
112     BUILD_TAG = os.getenv('BUILD_TAG')
113     if BUILD_TAG is not None and re.search("daily", BUILD_TAG) is not None:
114         CI_LOOP = "daily"
115     else:
116         CI_LOOP = "weekly"
117
118     tiers_to_run = []
119
120     for tier in tiers.get_tiers():
121         if re.search(CI_LOOP, tier.get_ci_loop()) is not None:
122             tiers_to_run.append(tier)
123             summary += ("\n    - %s. %s:\n\t   %s"
124                         % (tier.get_order(),
125                            tier.get_name(),
126                            tier.get_test_names()))
127
128     logger.info("Tiers to be executed:%s" % summary)
129
130     for tier in tiers_to_run:
131         run_tier(tier)
132
133
134 def main():
135     global CLEAN_FLAG
136     global REPORT_FLAG
137
138     CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
139     CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
140
141     file = FUNCTEST_REPO + "/ci/testcases.yaml"
142     _tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, file)
143
144     if args.noclean:
145         CLEAN_FLAG = False
146
147     if args.report:
148         REPORT_FLAG = True
149
150     if args.test:
151         source_rc_file()
152         if _tiers.get_tier(args.test):
153             run_tier(_tiers.get_tier(args.test))
154
155         elif _tiers.get_test(args.test):
156             run_test(_tiers.get_test(args.test))
157
158         elif args.test == "all":
159             run_all(_tiers)
160
161         else:
162             logger.error("Unknown test case or tier '%s', or not supported by "
163                          "the given scenario '%s'."
164                          % (args.test, CI_SCENARIO))
165             logger.debug("Available tiers are:\n\n%s"
166                          % _tiers)
167     else:
168         run_all(_tiers)
169
170     sys.exit(0)
171
172 if __name__ == '__main__':
173     main()