106214400a2653d0a54d74625954dd5ec2436877
[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_test").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
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     print_separator("+")
66     logger.info("Cleaning OpenStack resources...")
67     print_separator("+")
68     clean_os.main()
69     print_separator("")
70
71
72 def run_test(test):
73     test_name = test.get_name()
74     print_separator("")
75     print_separator("=")
76     logger.info("Running test case '%s'..." % test_name)
77     print_separator("=")
78     logger.debug("\n%s" % test)
79     flags = (" -t %s" % (test_name))
80     if REPORT_FLAG:
81         flags += " -r"
82
83     cmd = ("%s%s" % (EXEC_SCRIPT, flags))
84     logger.debug("Executing command '%s'" % cmd)
85     print_separator("")
86
87     p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
88
89     while p.poll() is None:
90         line = p.stdout.readline().rstrip()
91         logger.debug(line)
92
93     if p != 0:
94         logger.error("The command '%s' failed. Cleaning and exiting." % cmd)
95         if CLEAN_FLAG:
96             cleanup()
97         sys.exit(1)
98
99     if CLEAN_FLAG:
100         cleanup()
101
102
103
104 def run_tier(tier):
105     print_separator("#")
106     logger.info("Running tier '%s'" % tier.get_name())
107     print_separator("#")
108     logger.debug("\n%s" % tier)
109     for test in tier.get_tests():
110         run_test(test)
111
112
113 def run_all(tiers):
114     logger.debug("Tiers to be executed:")
115     for tier in tiers.get_tiers():
116         logger.info("\n    - %s. %s:\n\t%s"
117                     % (tier.get_order(), tier.get_name(), tier.get_tests()))
118
119     for tier in tiers.get_tiers():
120         run_tier(tier)
121
122
123 def main():
124     global CLEAN_FLAG
125     global REPORT_FLAG
126
127     CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
128     CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
129
130     file = FUNCTEST_REPO + "/ci/testcases.yaml"
131     _tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, file)
132
133     if args.noclean:
134         CLEAN_FLAG = False
135
136     if args.report:
137         REPORT_FLAG = True
138
139     if args.test:
140         source_rc_file()
141         if _tiers.get_tier(args.test):
142             run_tier(_tiers.get_tier(args.test))
143
144         elif _tiers.get_test(args.test):
145             run_test(_tiers.get_test(args.test))
146
147         elif args.test == "all":
148             run_all(_tiers)
149
150         else:
151             logger.error("Unknown test case or tier '%s', or not supported by "
152                          "the given scenario '%s'."
153                          % (args.test, CI_SCENARIO))
154             logger.debug("Available tiers are:\n\n%s"
155                          % _tiers)
156     else:
157         run_all(_tiers)
158
159     sys.exit(0)
160
161 if __name__ == '__main__':
162     main()