Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 1 | # 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 | """ |
| 14 | Runs tempest tests |
| 15 | |
| 16 | This command is used for running the tempest tests |
| 17 | |
| 18 | Test Selection |
| 19 | ============== |
| 20 | Tempest 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 | |
Matthew Treinish | a6b4da9 | 2016-05-23 17:24:12 -0400 | [diff] [blame] | 26 | There are also the **--blacklist_file** and **--whitelist_file** options that |
| 27 | let you pass a filepath to tempest run with the file format being a line |
| 28 | seperated regex, with '#' used to signify the start of a comment on a line. |
| 29 | For example:: |
| 30 | |
| 31 | # Regex file |
| 32 | ^regex1 # Match these tests |
| 33 | .*regex2 # Match those tests |
| 34 | |
| 35 | The blacklist file will be used to construct a negative lookahead regex and |
| 36 | the whitelist file will simply OR all the regexes in the file. The whitelist |
| 37 | and blacklist file options are mutually exclusive so you can't use them |
| 38 | together. However, you can combine either with a normal regex or the *--smoke* |
| 39 | flag. When used with a blacklist file the generated regex will be combined to |
| 40 | something like:: |
| 41 | |
| 42 | ^((?!black_regex1|black_regex2).)*$cli_regex1 |
| 43 | |
| 44 | When combined with a whitelist file all the regexes from the file and the CLI |
| 45 | regexes will be ORed. |
| 46 | |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 47 | You can also use the **--list-tests** option in conjunction with selection |
| 48 | arguments to list which tests will be run. |
| 49 | |
| 50 | Test Execution |
| 51 | ============== |
| 52 | There are several options to control how the tests are executed. By default |
| 53 | tempest will run in parallel with a worker for each CPU present on the machine. |
| 54 | If you want to adjust the number of workers use the **--concurrency** option |
| 55 | and if you want to run tests serially use **--serial** |
| 56 | |
Matthew Treinish | c89a951 | 2016-06-09 17:43:35 -0400 | [diff] [blame^] | 57 | Running with Workspaces |
| 58 | ----------------------- |
| 59 | Tempest run enables you to run your tempest tests from any setup tempest |
| 60 | workspace it relies on you having setup a tempest workspace with either the |
| 61 | ``tempest init`` or ``tempest workspace`` commands. Then using the |
| 62 | ``--workspace`` CLI option you can specify which one of your workspaces you |
| 63 | want to run tempest from. Using this option you don't have to run Tempest |
| 64 | directly with you current working directory being the workspace, Tempest will |
| 65 | take care of managing everything to be executed from there. |
| 66 | |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 67 | Test Output |
| 68 | =========== |
| 69 | By default tempest run's output to STDOUT will be generated using the |
| 70 | subunit-trace output filter. But, if you would prefer a subunit v2 stream be |
| 71 | output to STDOUT use the **--subunit** flag |
| 72 | |
| 73 | """ |
| 74 | |
| 75 | import io |
| 76 | import os |
| 77 | import sys |
| 78 | import threading |
| 79 | |
| 80 | from cliff import command |
Matthew Treinish | a6b4da9 | 2016-05-23 17:24:12 -0400 | [diff] [blame] | 81 | from os_testr import regex_builder |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 82 | from os_testr import subunit_trace |
| 83 | from oslo_log import log as logging |
| 84 | from testrepository.commands import run_argv |
| 85 | |
Matthew Treinish | c89a951 | 2016-06-09 17:43:35 -0400 | [diff] [blame^] | 86 | from tempest.cmd import workspace |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 87 | from tempest import config |
| 88 | |
| 89 | |
| 90 | LOG = logging.getLogger(__name__) |
| 91 | CONF = config.CONF |
| 92 | |
| 93 | |
| 94 | class TempestRun(command.Command): |
| 95 | |
| 96 | def _set_env(self): |
| 97 | # NOTE(mtreinish): This is needed so that testr doesn't gobble up any |
| 98 | # stacktraces on failure. |
| 99 | if 'TESTR_PDB' in os.environ: |
| 100 | return |
| 101 | else: |
| 102 | os.environ["TESTR_PDB"] = "" |
| 103 | |
Matthew Treinish | c89a951 | 2016-06-09 17:43:35 -0400 | [diff] [blame^] | 104 | def _create_testrepository(self): |
| 105 | if not os.path.isdir('.testrepository'): |
| 106 | returncode = run_argv(['testr', 'init'], sys.stdin, sys.stdout, |
| 107 | sys.stderr) |
| 108 | if returncode: |
| 109 | sys.exit(returncode) |
| 110 | |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 111 | def take_action(self, parsed_args): |
| 112 | self._set_env() |
Masayuki Igawa | fe2fa00 | 2016-06-22 12:58:34 +0900 | [diff] [blame] | 113 | returncode = 0 |
Matthew Treinish | c89a951 | 2016-06-09 17:43:35 -0400 | [diff] [blame^] | 114 | # Workspace execution mode |
| 115 | if parsed_args.workspace: |
| 116 | workspace_mgr = workspace.WorkspaceManager( |
| 117 | parsed_args.workspace_path) |
| 118 | path = workspace_mgr.get_workspace(parsed_args.workspace) |
| 119 | os.chdir(path) |
| 120 | # NOTE(mtreinish): tempest init should create a .testrepository dir |
| 121 | # but since workspaces can be imported let's sanity check and |
| 122 | # ensure that one is created |
| 123 | self._create_testrepository() |
zhufl | bedb2ad | 2016-06-20 11:39:01 +0800 | [diff] [blame] | 124 | # Local execution mode |
Matthew Treinish | c89a951 | 2016-06-09 17:43:35 -0400 | [diff] [blame^] | 125 | elif os.path.isfile('.testr.conf'): |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 126 | # If you're running in local execution mode and there is not a |
| 127 | # testrepository dir create one |
Matthew Treinish | c89a951 | 2016-06-09 17:43:35 -0400 | [diff] [blame^] | 128 | self._create_testrepository() |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 129 | else: |
zhufl | bedb2ad | 2016-06-20 11:39:01 +0800 | [diff] [blame] | 130 | print("No .testr.conf file was found for local execution") |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 131 | sys.exit(2) |
| 132 | |
| 133 | regex = self._build_regex(parsed_args) |
| 134 | if parsed_args.list_tests: |
| 135 | argv = ['tempest', 'list-tests', regex] |
| 136 | returncode = run_argv(argv, sys.stdin, sys.stdout, sys.stderr) |
| 137 | else: |
| 138 | options = self._build_options(parsed_args) |
| 139 | returncode = self._run(regex, options) |
| 140 | sys.exit(returncode) |
| 141 | |
| 142 | def get_description(self): |
| 143 | return 'Run tempest' |
| 144 | |
| 145 | def get_parser(self, prog_name): |
| 146 | parser = super(TempestRun, self).get_parser(prog_name) |
| 147 | parser = self._add_args(parser) |
| 148 | return parser |
| 149 | |
| 150 | def _add_args(self, parser): |
Matthew Treinish | c89a951 | 2016-06-09 17:43:35 -0400 | [diff] [blame^] | 151 | # workspace args |
| 152 | parser.add_argument('--workspace', default=None, |
| 153 | help='Name of tempest workspace to use for running' |
| 154 | ' tests. You can see a list of workspaces ' |
| 155 | 'with tempest workspace list') |
| 156 | parser.add_argument('--workspace-path', default=None, |
| 157 | dest='workspace_path', |
| 158 | help="The path to the workspace file, the default " |
| 159 | "is ~/.tempest/workspace.yaml") |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 160 | # test selection args |
| 161 | regex = parser.add_mutually_exclusive_group() |
| 162 | regex.add_argument('--smoke', action='store_true', |
| 163 | help="Run the smoke tests only") |
| 164 | regex.add_argument('--regex', '-r', default='', |
| 165 | help='A normal testr selection regex used to ' |
| 166 | 'specify a subset of tests to run') |
Matthew Treinish | a6b4da9 | 2016-05-23 17:24:12 -0400 | [diff] [blame] | 167 | list_selector = parser.add_mutually_exclusive_group() |
| 168 | list_selector.add_argument('--whitelist_file', |
| 169 | help="Path to a whitelist file, this file " |
| 170 | "contains a seperate regex on each " |
| 171 | "newline.") |
| 172 | list_selector.add_argument('--blacklist_file', |
| 173 | help='Path to a blacklist file, this file ' |
| 174 | 'contains a separate regex exclude on ' |
| 175 | 'each newline') |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 176 | # list only args |
| 177 | parser.add_argument('--list-tests', '-l', action='store_true', |
| 178 | help='List tests', |
| 179 | default=False) |
| 180 | # exectution args |
| 181 | parser.add_argument('--concurrency', '-w', |
| 182 | help="The number of workers to use, defaults to " |
| 183 | "the number of cpus") |
| 184 | parallel = parser.add_mutually_exclusive_group() |
| 185 | parallel.add_argument('--parallel', dest='parallel', |
| 186 | action='store_true', |
| 187 | help='Run tests in parallel (this is the' |
| 188 | ' default)') |
| 189 | parallel.add_argument('--serial', dest='parallel', |
| 190 | action='store_false', |
| 191 | help='Run tests serially') |
| 192 | # output args |
| 193 | parser.add_argument("--subunit", action='store_true', |
| 194 | help='Enable subunit v2 output') |
| 195 | |
| 196 | parser.set_defaults(parallel=True) |
| 197 | return parser |
| 198 | |
| 199 | def _build_regex(self, parsed_args): |
| 200 | regex = '' |
| 201 | if parsed_args.smoke: |
| 202 | regex = 'smoke' |
| 203 | elif parsed_args.regex: |
| 204 | regex = parsed_args.regex |
Matthew Treinish | a6b4da9 | 2016-05-23 17:24:12 -0400 | [diff] [blame] | 205 | if parsed_args.whitelist_file or parsed_args.blacklist_file: |
| 206 | regex = regex_builder.construct_regex(parsed_args.blacklist_file, |
| 207 | parsed_args.whitelist_file, |
| 208 | regex, False) |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 209 | return regex |
| 210 | |
| 211 | def _build_options(self, parsed_args): |
| 212 | options = [] |
| 213 | if parsed_args.subunit: |
| 214 | options.append("--subunit") |
| 215 | if parsed_args.parallel: |
| 216 | options.append("--parallel") |
| 217 | if parsed_args.concurrency: |
| 218 | options.append("--concurrency=%s" % parsed_args.concurrency) |
| 219 | return options |
| 220 | |
| 221 | def _run(self, regex, options): |
| 222 | returncode = 0 |
| 223 | argv = ['tempest', 'run', regex] + options |
| 224 | if '--subunit' in options: |
| 225 | returncode = run_argv(argv, sys.stdin, sys.stdout, sys.stderr) |
| 226 | else: |
| 227 | argv.append('--subunit') |
| 228 | stdin = io.StringIO() |
| 229 | stdout_r, stdout_w = os.pipe() |
| 230 | subunit_w = os.fdopen(stdout_w, 'wt') |
| 231 | subunit_r = os.fdopen(stdout_r) |
| 232 | returncodes = {} |
| 233 | |
| 234 | def run_argv_thread(): |
| 235 | returncodes['testr'] = run_argv(argv, stdin, subunit_w, |
| 236 | sys.stderr) |
| 237 | subunit_w.close() |
| 238 | |
| 239 | run_thread = threading.Thread(target=run_argv_thread) |
| 240 | run_thread.start() |
| 241 | returncodes['subunit-trace'] = subunit_trace.trace(subunit_r, |
| 242 | sys.stdout) |
| 243 | run_thread.join() |
| 244 | subunit_r.close() |
| 245 | # python version of pipefail |
| 246 | if returncodes['testr']: |
| 247 | returncode = returncodes['testr'] |
| 248 | elif returncodes['subunit-trace']: |
| 249 | returncode = returncodes['subunit-trace'] |
| 250 | return returncode |