blob: e78f6b08df04f47940f9e011a6fa72e214f8301a [file] [log] [blame]
Matthew Treinisha051c222016-05-23 15:48:22 -04001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13"""
14Runs tempest tests
15
16This command is used for running the tempest tests
17
18Test Selection
19==============
20Tempest run has several options:
21
22 * **--regex/-r**: This is a selection regex like what testr uses. It will run
23 any tests that match on re.match() with the regex
24 * **--smoke**: Run all the tests tagged as smoke
25
26You can also use the **--list-tests** option in conjunction with selection
27arguments to list which tests will be run.
28
29Test Execution
30==============
31There are several options to control how the tests are executed. By default
32tempest will run in parallel with a worker for each CPU present on the machine.
33If you want to adjust the number of workers use the **--concurrency** option
34and if you want to run tests serially use **--serial**
35
36Test Output
37===========
38By default tempest run's output to STDOUT will be generated using the
39subunit-trace output filter. But, if you would prefer a subunit v2 stream be
40output to STDOUT use the **--subunit** flag
41
42"""
43
44import io
45import os
46import sys
47import threading
48
49from cliff import command
50from os_testr import subunit_trace
51from oslo_log import log as logging
52from testrepository.commands import run_argv
53
54from tempest import config
55
56
57LOG = logging.getLogger(__name__)
58CONF = config.CONF
59
60
61class TempestRun(command.Command):
62
63 def _set_env(self):
64 # NOTE(mtreinish): This is needed so that testr doesn't gobble up any
65 # stacktraces on failure.
66 if 'TESTR_PDB' in os.environ:
67 return
68 else:
69 os.environ["TESTR_PDB"] = ""
70
71 def take_action(self, parsed_args):
72 self._set_env()
zhuflbedb2ad2016-06-20 11:39:01 +080073 # Local execution mode
Matthew Treinisha051c222016-05-23 15:48:22 -040074 if os.path.isfile('.testr.conf'):
75 # If you're running in local execution mode and there is not a
76 # testrepository dir create one
77 if not os.path.isdir('.testrepository'):
78 returncode = run_argv(['testr', 'init'], sys.stdin, sys.stdout,
79 sys.stderr)
80 if returncode:
81 sys.exit(returncode)
82 else:
zhuflbedb2ad2016-06-20 11:39:01 +080083 print("No .testr.conf file was found for local execution")
Matthew Treinisha051c222016-05-23 15:48:22 -040084 sys.exit(2)
85
86 regex = self._build_regex(parsed_args)
87 if parsed_args.list_tests:
88 argv = ['tempest', 'list-tests', regex]
89 returncode = run_argv(argv, sys.stdin, sys.stdout, sys.stderr)
90 else:
91 options = self._build_options(parsed_args)
92 returncode = self._run(regex, options)
93 sys.exit(returncode)
94
95 def get_description(self):
96 return 'Run tempest'
97
98 def get_parser(self, prog_name):
99 parser = super(TempestRun, self).get_parser(prog_name)
100 parser = self._add_args(parser)
101 return parser
102
103 def _add_args(self, parser):
104 # test selection args
105 regex = parser.add_mutually_exclusive_group()
106 regex.add_argument('--smoke', action='store_true',
107 help="Run the smoke tests only")
108 regex.add_argument('--regex', '-r', default='',
109 help='A normal testr selection regex used to '
110 'specify a subset of tests to run')
111 # list only args
112 parser.add_argument('--list-tests', '-l', action='store_true',
113 help='List tests',
114 default=False)
115 # exectution args
116 parser.add_argument('--concurrency', '-w',
117 help="The number of workers to use, defaults to "
118 "the number of cpus")
119 parallel = parser.add_mutually_exclusive_group()
120 parallel.add_argument('--parallel', dest='parallel',
121 action='store_true',
122 help='Run tests in parallel (this is the'
123 ' default)')
124 parallel.add_argument('--serial', dest='parallel',
125 action='store_false',
126 help='Run tests serially')
127 # output args
128 parser.add_argument("--subunit", action='store_true',
129 help='Enable subunit v2 output')
130
131 parser.set_defaults(parallel=True)
132 return parser
133
134 def _build_regex(self, parsed_args):
135 regex = ''
136 if parsed_args.smoke:
137 regex = 'smoke'
138 elif parsed_args.regex:
139 regex = parsed_args.regex
140 return regex
141
142 def _build_options(self, parsed_args):
143 options = []
144 if parsed_args.subunit:
145 options.append("--subunit")
146 if parsed_args.parallel:
147 options.append("--parallel")
148 if parsed_args.concurrency:
149 options.append("--concurrency=%s" % parsed_args.concurrency)
150 return options
151
152 def _run(self, regex, options):
153 returncode = 0
154 argv = ['tempest', 'run', regex] + options
155 if '--subunit' in options:
156 returncode = run_argv(argv, sys.stdin, sys.stdout, sys.stderr)
157 else:
158 argv.append('--subunit')
159 stdin = io.StringIO()
160 stdout_r, stdout_w = os.pipe()
161 subunit_w = os.fdopen(stdout_w, 'wt')
162 subunit_r = os.fdopen(stdout_r)
163 returncodes = {}
164
165 def run_argv_thread():
166 returncodes['testr'] = run_argv(argv, stdin, subunit_w,
167 sys.stderr)
168 subunit_w.close()
169
170 run_thread = threading.Thread(target=run_argv_thread)
171 run_thread.start()
172 returncodes['subunit-trace'] = subunit_trace.trace(subunit_r,
173 sys.stdout)
174 run_thread.join()
175 subunit_r.close()
176 # python version of pipefail
177 if returncodes['testr']:
178 returncode = returncodes['testr']
179 elif returncodes['subunit-trace']:
180 returncode = returncodes['subunit-trace']
181 return returncode