Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 1 | # Copyright 2015 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import argparse |
Divyansh Acharya | 1bc06aa | 2017-08-18 15:09:46 +0000 | [diff] [blame] | 16 | import atexit |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 17 | import os |
| 18 | import shutil |
| 19 | import subprocess |
| 20 | import tempfile |
Sean McGinnis | eed8074 | 2020-04-18 12:01:03 -0500 | [diff] [blame] | 21 | from unittest import mock |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 22 | |
Brant Knudson | 6a090f4 | 2016-10-13 12:51:49 -0500 | [diff] [blame] | 23 | import fixtures |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 24 | |
| 25 | from tempest.cmd import run |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 26 | from tempest.cmd import workspace |
Manik Bindlish | 321c85c | 2018-07-30 06:48:24 +0000 | [diff] [blame] | 27 | from tempest import config |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 28 | from tempest.lib.common.utils import data_utils |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 29 | from tempest.tests import base |
| 30 | |
| 31 | DEVNULL = open(os.devnull, 'wb') |
Divyansh Acharya | 1bc06aa | 2017-08-18 15:09:46 +0000 | [diff] [blame] | 32 | atexit.register(DEVNULL.close) |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 33 | |
Manik Bindlish | 321c85c | 2018-07-30 06:48:24 +0000 | [diff] [blame] | 34 | CONF = config.CONF |
| 35 | |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 36 | |
| 37 | class TestTempestRun(base.TestCase): |
| 38 | |
| 39 | def setUp(self): |
| 40 | super(TestTempestRun, self).setUp() |
| 41 | self.run_cmd = run.TempestRun(None, None) |
| 42 | |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 43 | def test__build_regex_default(self): |
| 44 | args = mock.Mock(spec=argparse.Namespace) |
| 45 | setattr(args, 'smoke', False) |
| 46 | setattr(args, 'regex', '') |
zhufl | e1afe4e | 2019-06-28 17:43:01 +0800 | [diff] [blame] | 47 | self.assertIsNone(self.run_cmd._build_regex(args)) |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 48 | |
| 49 | def test__build_regex_smoke(self): |
| 50 | args = mock.Mock(spec=argparse.Namespace) |
| 51 | setattr(args, "smoke", True) |
| 52 | setattr(args, 'regex', '') |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 53 | self.assertEqual(['smoke'], self.run_cmd._build_regex(args)) |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 54 | |
| 55 | def test__build_regex_regex(self): |
| 56 | args = mock.Mock(spec=argparse.Namespace) |
| 57 | setattr(args, 'smoke', False) |
| 58 | setattr(args, "regex", 'i_am_a_fun_little_regex') |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 59 | self.assertEqual(['i_am_a_fun_little_regex'], |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 60 | self.run_cmd._build_regex(args)) |
| 61 | |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 62 | def test__build_regex_smoke_regex(self): |
| 63 | args = mock.Mock(spec=argparse.Namespace) |
| 64 | setattr(args, "smoke", True) |
| 65 | setattr(args, 'regex', 'i_am_a_fun_little_regex') |
| 66 | self.assertEqual(['smoke'], self.run_cmd._build_regex(args)) |
| 67 | |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 68 | |
| 69 | class TestRunReturnCode(base.TestCase): |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 70 | |
| 71 | exclude_regex = '--exclude-regex' |
| 72 | exclude_list = '--exclude-list' |
| 73 | include_list = '--include-list' |
| 74 | |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 75 | def setUp(self): |
| 76 | super(TestRunReturnCode, self).setUp() |
| 77 | # Setup test dirs |
| 78 | self.directory = tempfile.mkdtemp(prefix='tempest-unit') |
| 79 | self.addCleanup(shutil.rmtree, self.directory) |
| 80 | self.test_dir = os.path.join(self.directory, 'tests') |
| 81 | os.mkdir(self.test_dir) |
| 82 | # Setup Test files |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 83 | self.stestr_conf_file = os.path.join(self.directory, '.stestr.conf') |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 84 | self.setup_cfg_file = os.path.join(self.directory, 'setup.cfg') |
| 85 | self.passing_file = os.path.join(self.test_dir, 'test_passing.py') |
| 86 | self.failing_file = os.path.join(self.test_dir, 'test_failing.py') |
| 87 | self.init_file = os.path.join(self.test_dir, '__init__.py') |
| 88 | self.setup_py = os.path.join(self.directory, 'setup.py') |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 89 | shutil.copy('tempest/tests/files/testr-conf', self.stestr_conf_file) |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 90 | shutil.copy('tempest/tests/files/passing-tests', self.passing_file) |
| 91 | shutil.copy('tempest/tests/files/failing-tests', self.failing_file) |
| 92 | shutil.copy('setup.py', self.setup_py) |
| 93 | shutil.copy('tempest/tests/files/setup.cfg', self.setup_cfg_file) |
| 94 | shutil.copy('tempest/tests/files/__init__.py', self.init_file) |
| 95 | # Change directory, run wrapper and check result |
| 96 | self.addCleanup(os.chdir, os.path.abspath(os.curdir)) |
| 97 | os.chdir(self.directory) |
| 98 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 99 | def _get_test_list_file(self, content): |
| 100 | fd, path = tempfile.mkstemp() |
| 101 | self.addCleanup(os.remove, path) |
| 102 | test_file = os.fdopen(fd, 'wb', 0) |
| 103 | self.addCleanup(test_file.close) |
| 104 | test_file.write(content.encode('utf-8')) |
| 105 | return path |
| 106 | |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 107 | def assertRunExit(self, cmd, expected): |
| 108 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 109 | stderr=subprocess.PIPE) |
| 110 | out, err = p.communicate() |
| 111 | msg = ("Running %s got an unexpected returncode\n" |
| 112 | "Stdout: %s\nStderr: %s" % (' '.join(cmd), out, err)) |
| 113 | self.assertEqual(p.returncode, expected, msg) |
Matthew Treinish | f9902ec | 2018-02-22 12:11:46 -0500 | [diff] [blame] | 114 | return out, err |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 115 | |
| 116 | def test_tempest_run_passes(self): |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 117 | self.assertRunExit(['tempest', 'run', '--regex', 'passing'], 0) |
| 118 | |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 119 | def test_tempest_run_passes_with_stestr_repository(self): |
| 120 | subprocess.call(['stestr', 'init']) |
Masayuki Igawa | fe2fa00 | 2016-06-22 12:58:34 +0900 | [diff] [blame] | 121 | self.assertRunExit(['tempest', 'run', '--regex', 'passing'], 0) |
| 122 | |
Manik Bindlish | 71c8237 | 2019-01-29 10:52:27 +0000 | [diff] [blame] | 123 | def test_tempest_run_failing(self): |
| 124 | self.assertRunExit(['tempest', 'run', '--regex', 'failing'], 1) |
| 125 | |
| 126 | def test_tempest_run_failing_with_stestr_repository(self): |
| 127 | subprocess.call(['stestr', 'init']) |
| 128 | self.assertRunExit(['tempest', 'run', '--regex', 'failing'], 1) |
| 129 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 130 | def test_tempest_run_exclude_regex_failing(self): |
| 131 | self.assertRunExit(['tempest', 'run', |
| 132 | self.exclude_regex, 'failing'], 0) |
Manik Bindlish | 71c8237 | 2019-01-29 10:52:27 +0000 | [diff] [blame] | 133 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 134 | def test_tempest_run_exclude_regex_failing_with_stestr_repository(self): |
Manik Bindlish | 71c8237 | 2019-01-29 10:52:27 +0000 | [diff] [blame] | 135 | subprocess.call(['stestr', 'init']) |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 136 | self.assertRunExit(['tempest', 'run', |
| 137 | self.exclude_regex, 'failing'], 0) |
Manik Bindlish | 71c8237 | 2019-01-29 10:52:27 +0000 | [diff] [blame] | 138 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 139 | def test_tempest_run_exclude_regex_passing(self): |
| 140 | self.assertRunExit(['tempest', 'run', |
| 141 | self.exclude_regex, 'passing'], 1) |
Manik Bindlish | 71c8237 | 2019-01-29 10:52:27 +0000 | [diff] [blame] | 142 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 143 | def test_tempest_run_exclude_regex_passing_with_stestr_repository(self): |
Manik Bindlish | 71c8237 | 2019-01-29 10:52:27 +0000 | [diff] [blame] | 144 | subprocess.call(['stestr', 'init']) |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 145 | self.assertRunExit(['tempest', 'run', |
| 146 | self.exclude_regex, 'passing'], 1) |
Manik Bindlish | 71c8237 | 2019-01-29 10:52:27 +0000 | [diff] [blame] | 147 | |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 148 | def test_tempest_run_fails(self): |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 149 | self.assertRunExit(['tempest', 'run'], 1) |
Brant Knudson | 6a090f4 | 2016-10-13 12:51:49 -0500 | [diff] [blame] | 150 | |
Matthew Treinish | f9902ec | 2018-02-22 12:11:46 -0500 | [diff] [blame] | 151 | def test_run_list(self): |
| 152 | subprocess.call(['stestr', 'init']) |
| 153 | out, err = self.assertRunExit(['tempest', 'run', '-l'], 0) |
| 154 | tests = out.split() |
likui | 19b70a3 | 2020-12-02 13:25:18 +0800 | [diff] [blame] | 155 | tests = sorted([str(x.rstrip()) for x in tests if x]) |
Matthew Treinish | f9902ec | 2018-02-22 12:11:46 -0500 | [diff] [blame] | 156 | result = [ |
likui | 19b70a3 | 2020-12-02 13:25:18 +0800 | [diff] [blame] | 157 | str('tests.test_failing.FakeTestClass.test_pass'), |
| 158 | str('tests.test_failing.FakeTestClass.test_pass_list'), |
| 159 | str('tests.test_passing.FakeTestClass.test_pass'), |
| 160 | str('tests.test_passing.FakeTestClass.test_pass_list'), |
Matthew Treinish | f9902ec | 2018-02-22 12:11:46 -0500 | [diff] [blame] | 161 | ] |
| 162 | # NOTE(mtreinish): on python 3 the subprocess prints b'' around |
| 163 | # stdout. |
likui | 7d91c87 | 2020-09-22 12:29:16 +0800 | [diff] [blame] | 164 | result = ["b\'" + x + "\'" for x in result] |
Matthew Treinish | f9902ec | 2018-02-22 12:11:46 -0500 | [diff] [blame] | 165 | self.assertEqual(result, tests) |
| 166 | |
Arx Cruz | c06c371 | 2020-02-20 11:03:52 +0100 | [diff] [blame] | 167 | def test_tempest_run_with_worker_file(self): |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 168 | path = self._get_test_list_file( |
| 169 | '- worker:\n - passing\n concurrency: 3') |
Arx Cruz | c06c371 | 2020-02-20 11:03:52 +0100 | [diff] [blame] | 170 | self.assertRunExit(['tempest', 'run', '--worker-file=%s' % path], 0) |
| 171 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 172 | def test_tempest_run_with_include_list(self): |
| 173 | path = self._get_test_list_file('passing') |
| 174 | self.assertRunExit(['tempest', 'run', |
| 175 | '%s=%s' % (self.include_list, path)], 0) |
Matthew Treinish | 3c6b15d | 2018-02-22 11:37:52 -0500 | [diff] [blame] | 176 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 177 | def test_tempest_run_with_include_regex_include_pass_check_fail(self): |
| 178 | path = self._get_test_list_file('passing') |
| 179 | self.assertRunExit(['tempest', 'run', |
| 180 | '%s=%s' % (self.include_list, path), |
Matthew Treinish | 3c6b15d | 2018-02-22 11:37:52 -0500 | [diff] [blame] | 181 | '--regex', 'fail'], 1) |
| 182 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 183 | def test_tempest_run_with_include_regex_include_pass_check_pass(self): |
| 184 | path = self._get_test_list_file('passing') |
| 185 | self.assertRunExit(['tempest', 'run', |
| 186 | '%s=%s' % (self.include_list, path), |
Manik Bindlish | 5a276ea | 2019-01-29 07:43:52 +0000 | [diff] [blame] | 187 | '--regex', 'passing'], 0) |
| 188 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 189 | def test_tempest_run_with_include_regex_include_fail_check_pass(self): |
| 190 | path = self._get_test_list_file('failing') |
| 191 | self.assertRunExit(['tempest', 'run', |
| 192 | '%s=%s' % (self.include_list, path), |
Manik Bindlish | 5a276ea | 2019-01-29 07:43:52 +0000 | [diff] [blame] | 193 | '--regex', 'pass'], 1) |
| 194 | |
Masayuki Igawa | ff07eac | 2018-02-22 16:53:09 +0900 | [diff] [blame] | 195 | def test_tempest_run_passes_with_config_file(self): |
| 196 | self.assertRunExit(['tempest', 'run', |
| 197 | '--config-file', self.stestr_conf_file, |
| 198 | '--regex', 'passing'], 0) |
| 199 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 200 | def test_tempest_run_with_exclude_list_failing(self): |
| 201 | path = self._get_test_list_file('failing') |
| 202 | self.assertRunExit(['tempest', 'run', |
| 203 | '%s=%s' % (self.exclude_list, path)], 0) |
Manik Bindlish | 9334ddb | 2019-01-29 10:26:43 +0000 | [diff] [blame] | 204 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 205 | def test_tempest_run_with_exclude_list_passing(self): |
| 206 | path = self._get_test_list_file('passing') |
| 207 | self.assertRunExit(['tempest', 'run', |
| 208 | '%s=%s' % (self.exclude_list, path)], 1) |
Manik Bindlish | 9334ddb | 2019-01-29 10:26:43 +0000 | [diff] [blame] | 209 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 210 | def test_tempest_run_with_exclude_list_regex_exclude_fail_check_pass(self): |
| 211 | path = self._get_test_list_file('failing') |
| 212 | self.assertRunExit(['tempest', 'run', |
| 213 | '%s=%s' % (self.exclude_list, path), |
Manik Bindlish | 9334ddb | 2019-01-29 10:26:43 +0000 | [diff] [blame] | 214 | '--regex', 'pass'], 0) |
| 215 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 216 | def test_tempest_run_with_exclude_list_regex_exclude_pass_check_pass(self): |
| 217 | path = self._get_test_list_file('passing') |
| 218 | self.assertRunExit(['tempest', 'run', |
| 219 | '%s=%s' % (self.exclude_list, path), |
Manik Bindlish | 9334ddb | 2019-01-29 10:26:43 +0000 | [diff] [blame] | 220 | '--regex', 'pass'], 1) |
| 221 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 222 | def test_tempest_run_with_exclude_list_regex_exclude_pass_check_fail(self): |
| 223 | path = self._get_test_list_file('passing') |
| 224 | self.assertRunExit(['tempest', 'run', |
| 225 | '%s=%s' % (self.exclude_list, path), |
Manik Bindlish | 9334ddb | 2019-01-29 10:26:43 +0000 | [diff] [blame] | 226 | '--regex', 'fail'], 1) |
| 227 | |
melanie witt | 0b330af | 2023-12-07 01:44:40 +0000 | [diff] [blame] | 228 | def test_tempest_run_with_slowest(self): |
| 229 | out, err = self.assertRunExit(['tempest', 'run', '--regex', 'passing', |
| 230 | '--slowest'], 0) |
| 231 | self.assertRegex(str(out), r'Test id\s+Runtime \(s\)') |
| 232 | |
Brant Knudson | 6a090f4 | 2016-10-13 12:51:49 -0500 | [diff] [blame] | 233 | |
Martin Kopec | dc84423 | 2020-12-24 15:57:53 +0000 | [diff] [blame] | 234 | class TestOldArgRunReturnCode(TestRunReturnCode): |
| 235 | """A class for testing deprecated but still supported args. |
| 236 | |
| 237 | This class will be removed once we remove the following arguments: |
| 238 | * --black-regex |
| 239 | * --blacklist-file |
| 240 | * --whitelist-file |
| 241 | """ |
| 242 | exclude_regex = '--black-regex' |
| 243 | exclude_list = '--blacklist-file' |
| 244 | include_list = '--whitelist-file' |
| 245 | |
| 246 | def _test_args_passing(self, args): |
| 247 | self.assertRunExit(['tempest', 'run'] + args, 0) |
| 248 | |
| 249 | def test_tempest_run_new_old_arg_comb(self): |
| 250 | path = self._get_test_list_file('failing') |
| 251 | self._test_args_passing(['--black-regex', 'failing', |
| 252 | '--exclude-regex', 'failing']) |
| 253 | self._test_args_passing(['--blacklist-file=' + path, |
| 254 | '--exclude-list=' + path]) |
| 255 | path = self._get_test_list_file('passing') |
| 256 | self._test_args_passing(['--whitelist-file=' + path, |
| 257 | '--include-list=' + path]) |
| 258 | |
| 259 | def _test_args_passing_with_stestr_repository(self, args): |
| 260 | subprocess.call(['stestr', 'init']) |
| 261 | self.assertRunExit(['tempest', 'run'] + args, 0) |
| 262 | |
| 263 | def test_tempest_run_new_old_arg_comb_with_stestr_repository(self): |
| 264 | path = self._get_test_list_file('failing') |
| 265 | self._test_args_passing_with_stestr_repository( |
| 266 | ['--black-regex', 'failing', '--exclude-regex', 'failing']) |
| 267 | self._test_args_passing_with_stestr_repository( |
| 268 | ['--blacklist-file=' + path, '--exclude-list=' + path]) |
| 269 | path = self._get_test_list_file('passing') |
| 270 | self._test_args_passing_with_stestr_repository( |
| 271 | ['--whitelist-file=' + path, '--include-list=' + path]) |
| 272 | |
| 273 | |
Manik Bindlish | 321c85c | 2018-07-30 06:48:24 +0000 | [diff] [blame] | 274 | class TestConfigPathCheck(base.TestCase): |
| 275 | def setUp(self): |
| 276 | super(TestConfigPathCheck, self).setUp() |
| 277 | self.run_cmd = run.TempestRun(None, None) |
| 278 | |
| 279 | def test_tempest_run_set_config_path(self): |
| 280 | # Note: (mbindlish) This test is created for the bug id: 1783751 |
| 281 | # Checking TEMPEST_CONFIG_DIR and TEMPEST_CONFIG is actually |
| 282 | # getting set in os environment when some data has passed to |
| 283 | # set the environment. |
| 284 | |
Manik Bindlish | 21491df | 2018-12-14 06:58:42 +0000 | [diff] [blame] | 285 | _, path = tempfile.mkstemp() |
| 286 | self.addCleanup(os.remove, path) |
Manik Bindlish | 321c85c | 2018-07-30 06:48:24 +0000 | [diff] [blame] | 287 | |
Manik Bindlish | 21491df | 2018-12-14 06:58:42 +0000 | [diff] [blame] | 288 | self.run_cmd._set_env(path) |
| 289 | self.assertEqual(path, CONF._path) |
| 290 | self.assertIn('TEMPEST_CONFIG_DIR', os.environ) |
| 291 | self.assertEqual(path, os.path.join(os.environ['TEMPEST_CONFIG_DIR'], |
| 292 | os.environ['TEMPEST_CONFIG'])) |
| 293 | |
| 294 | def test_tempest_run_set_config_no_exist_path(self): |
| 295 | path = "fake/path" |
| 296 | self.assertRaisesRegex(FileNotFoundError, |
| 297 | 'Config file: .* doesn\'t exist', |
| 298 | self.run_cmd._set_env, path) |
| 299 | |
| 300 | def test_tempest_run_no_config_path(self): |
Manik Bindlish | 321c85c | 2018-07-30 06:48:24 +0000 | [diff] [blame] | 301 | # Note: (mbindlish) This test is created for the bug id: 1783751 |
| 302 | # Checking TEMPEST_CONFIG_DIR and TEMPEST_CONFIG should have no value |
| 303 | # in os environment when no data has passed to set the environment. |
| 304 | |
| 305 | self.run_cmd._set_env("") |
| 306 | self.assertFalse(CONF._path) |
| 307 | self.assertNotIn('TEMPEST_CONFIG_DIR', os.environ) |
| 308 | self.assertNotIn('TEMPEST_CONFIG', os.environ) |
| 309 | |
| 310 | |
Brant Knudson | 6a090f4 | 2016-10-13 12:51:49 -0500 | [diff] [blame] | 311 | class TestTakeAction(base.TestCase): |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 312 | def setUp(self): |
| 313 | super(TestTakeAction, self).setUp() |
| 314 | self.name = data_utils.rand_name('workspace') |
| 315 | self.path = tempfile.mkdtemp() |
| 316 | self.addCleanup(shutil.rmtree, self.path, ignore_errors=True) |
| 317 | store_dir = tempfile.mkdtemp() |
| 318 | self.addCleanup(shutil.rmtree, store_dir, ignore_errors=True) |
| 319 | self.store_file = os.path.join(store_dir, 'workspace.yaml') |
| 320 | self.workspace_manager = workspace.WorkspaceManager( |
| 321 | path=self.store_file) |
| 322 | self.workspace_manager.register_new_workspace(self.name, self.path) |
| 323 | |
| 324 | def _setup_test_dirs(self): |
| 325 | self.directory = tempfile.mkdtemp(prefix='tempest-unit') |
| 326 | self.addCleanup(shutil.rmtree, self.directory, ignore_errors=True) |
| 327 | self.test_dir = os.path.join(self.directory, 'tests') |
| 328 | os.mkdir(self.test_dir) |
| 329 | # Change directory, run wrapper and check result |
| 330 | self.addCleanup(os.chdir, os.path.abspath(os.curdir)) |
| 331 | os.chdir(self.directory) |
| 332 | |
Brant Knudson | 6a090f4 | 2016-10-13 12:51:49 -0500 | [diff] [blame] | 333 | def test_workspace_not_registered(self): |
| 334 | class Exception_(Exception): |
| 335 | pass |
| 336 | |
| 337 | m_exit = self.useFixture(fixtures.MockPatch('sys.exit')).mock |
| 338 | # sys.exit must not continue (or exit) |
| 339 | m_exit.side_effect = Exception_ |
| 340 | |
| 341 | workspace = self.getUniqueString() |
| 342 | |
| 343 | tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock()) |
| 344 | parsed_args = mock.Mock() |
| 345 | parsed_args.config_file = [] |
| 346 | |
| 347 | # Override $HOME so that empty workspace gets created in temp dir. |
| 348 | self.useFixture(fixtures.TempHomeDir()) |
| 349 | |
| 350 | # Force use of the temporary home directory. |
| 351 | parsed_args.workspace_path = None |
| 352 | |
| 353 | # Simulate --workspace argument. |
| 354 | parsed_args.workspace = workspace |
| 355 | |
| 356 | self.assertRaises(Exception_, tempest_run.take_action, parsed_args) |
| 357 | exit_msg = m_exit.call_args[0][0] |
| 358 | self.assertIn(workspace, exit_msg) |
Masayuki Igawa | ff07eac | 2018-02-22 16:53:09 +0900 | [diff] [blame] | 359 | |
| 360 | def test_config_file_specified(self): |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 361 | self._setup_test_dirs() |
Manik Bindlish | 21491df | 2018-12-14 06:58:42 +0000 | [diff] [blame] | 362 | _, path = tempfile.mkstemp() |
| 363 | self.addCleanup(os.remove, path) |
Masayuki Igawa | ff07eac | 2018-02-22 16:53:09 +0900 | [diff] [blame] | 364 | tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock()) |
| 365 | parsed_args = mock.Mock() |
Masayuki Igawa | ff07eac | 2018-02-22 16:53:09 +0900 | [diff] [blame] | 366 | |
| 367 | parsed_args.workspace = None |
| 368 | parsed_args.state = None |
| 369 | parsed_args.list_tests = False |
Manik Bindlish | 21491df | 2018-12-14 06:58:42 +0000 | [diff] [blame] | 370 | parsed_args.config_file = path |
melanie witt | 0b330af | 2023-12-07 01:44:40 +0000 | [diff] [blame] | 371 | parsed_args.slowest = False |
Masayuki Igawa | ff07eac | 2018-02-22 16:53:09 +0900 | [diff] [blame] | 372 | |
| 373 | with mock.patch('stestr.commands.run_command') as m: |
| 374 | m.return_value = 0 |
| 375 | self.assertEqual(0, tempest_run.take_action(parsed_args)) |
| 376 | m.assert_called() |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 377 | |
| 378 | def test_no_config_file_no_workspace_no_state(self): |
| 379 | self._setup_test_dirs() |
| 380 | tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock()) |
| 381 | parsed_args = mock.Mock() |
| 382 | |
| 383 | parsed_args.workspace = None |
| 384 | parsed_args.state = None |
| 385 | parsed_args.list_tests = False |
| 386 | parsed_args.config_file = '' |
| 387 | |
| 388 | with mock.patch('stestr.commands.run_command'): |
| 389 | self.assertRaises(SystemExit, tempest_run.take_action, parsed_args) |
| 390 | |
| 391 | def test_config_file_workspace_registered(self): |
| 392 | self._setup_test_dirs() |
Manik Bindlish | 21491df | 2018-12-14 06:58:42 +0000 | [diff] [blame] | 393 | _, path = tempfile.mkstemp() |
| 394 | self.addCleanup(os.remove, path) |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 395 | tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock()) |
| 396 | parsed_args = mock.Mock() |
| 397 | parsed_args.workspace = self.name |
| 398 | parsed_args.workspace_path = self.store_file |
| 399 | parsed_args.state = None |
| 400 | parsed_args.list_tests = False |
Manik Bindlish | 21491df | 2018-12-14 06:58:42 +0000 | [diff] [blame] | 401 | parsed_args.config_file = path |
melanie witt | 0b330af | 2023-12-07 01:44:40 +0000 | [diff] [blame] | 402 | parsed_args.slowest = False |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 403 | |
| 404 | with mock.patch('stestr.commands.run_command') as m: |
| 405 | m.return_value = 0 |
| 406 | self.assertEqual(0, tempest_run.take_action(parsed_args)) |
| 407 | m.assert_called() |
| 408 | |
| 409 | @mock.patch('tempest.cmd.run.TempestRun._init_state') |
| 410 | def test_workspace_registered_no_config_no_state(self, mock_init_state): |
| 411 | self._setup_test_dirs() |
| 412 | tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock()) |
| 413 | parsed_args = mock.Mock() |
| 414 | parsed_args.workspace = self.name |
| 415 | parsed_args.workspace_path = self.store_file |
| 416 | parsed_args.state = None |
| 417 | parsed_args.list_tests = False |
| 418 | parsed_args.config_file = '' |
melanie witt | 0b330af | 2023-12-07 01:44:40 +0000 | [diff] [blame] | 419 | parsed_args.slowest = False |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 420 | |
| 421 | with mock.patch('stestr.commands.run_command') as m: |
| 422 | m.return_value = 0 |
| 423 | self.assertEqual(0, tempest_run.take_action(parsed_args)) |
| 424 | m.assert_called() |
| 425 | mock_init_state.assert_not_called() |
| 426 | |
| 427 | @mock.patch('tempest.cmd.run.TempestRun._init_state') |
| 428 | def test_no_config_file_no_workspace_state_true(self, mock_init_state): |
| 429 | self._setup_test_dirs() |
| 430 | tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock()) |
| 431 | parsed_args = mock.Mock() |
| 432 | |
| 433 | parsed_args.workspace = None |
| 434 | parsed_args.state = True |
| 435 | parsed_args.list_tests = False |
| 436 | parsed_args.config_file = '' |
| 437 | |
| 438 | with mock.patch('stestr.commands.run_command'): |
| 439 | self.assertRaises(SystemExit, tempest_run.take_action, parsed_args) |
| 440 | mock_init_state.assert_not_called() |
| 441 | |
| 442 | @mock.patch('tempest.cmd.run.TempestRun._init_state') |
| 443 | def test_workspace_registered_no_config_state_true(self, mock_init_state): |
| 444 | self._setup_test_dirs() |
| 445 | tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock()) |
| 446 | parsed_args = mock.Mock() |
| 447 | parsed_args.workspace = self.name |
| 448 | parsed_args.workspace_path = self.store_file |
| 449 | parsed_args.state = True |
| 450 | parsed_args.list_tests = False |
| 451 | parsed_args.config_file = '' |
melanie witt | 0b330af | 2023-12-07 01:44:40 +0000 | [diff] [blame] | 452 | parsed_args.slowest = False |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 453 | |
| 454 | with mock.patch('stestr.commands.run_command') as m: |
| 455 | m.return_value = 0 |
| 456 | self.assertEqual(0, tempest_run.take_action(parsed_args)) |
| 457 | m.assert_called() |
| 458 | mock_init_state.assert_called() |
| 459 | |
| 460 | @mock.patch('tempest.cmd.run.TempestRun._init_state') |
| 461 | def test_no_workspace_config_file_state_true(self, mock_init_state): |
| 462 | self._setup_test_dirs() |
Manik Bindlish | 21491df | 2018-12-14 06:58:42 +0000 | [diff] [blame] | 463 | _, path = tempfile.mkstemp() |
| 464 | self.addCleanup(os.remove, path) |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 465 | tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock()) |
| 466 | parsed_args = mock.Mock() |
| 467 | parsed_args.workspace = None |
| 468 | parsed_args.workspace_path = self.store_file |
| 469 | parsed_args.state = True |
| 470 | parsed_args.list_tests = False |
Manik Bindlish | 21491df | 2018-12-14 06:58:42 +0000 | [diff] [blame] | 471 | parsed_args.config_file = path |
melanie witt | 0b330af | 2023-12-07 01:44:40 +0000 | [diff] [blame] | 472 | parsed_args.slowest = False |
Manik Bindlish | 087d4d0 | 2018-08-01 10:10:22 +0000 | [diff] [blame] | 473 | |
| 474 | with mock.patch('stestr.commands.run_command') as m: |
| 475 | m.return_value = 0 |
| 476 | self.assertEqual(0, tempest_run.take_action(parsed_args)) |
| 477 | m.assert_called() |
| 478 | mock_init_state.assert_called() |