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