blob: 3c99bbe572764acd7f6f1f5a3f97b105c08e869f [file] [log] [blame]
Matthew Treinisha051c222016-05-23 15:48:22 -04001# 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
15import argparse
Divyansh Acharya1bc06aa2017-08-18 15:09:46 +000016import atexit
Matthew Treinisha051c222016-05-23 15:48:22 -040017import os
18import shutil
19import subprocess
20import tempfile
Sean McGinniseed80742020-04-18 12:01:03 -050021from unittest import mock
Matthew Treinisha051c222016-05-23 15:48:22 -040022
Brant Knudson6a090f42016-10-13 12:51:49 -050023import fixtures
Matthew Treinishf9902ec2018-02-22 12:11:46 -050024import six
Matthew Treinisha051c222016-05-23 15:48:22 -040025
26from tempest.cmd import run
Manik Bindlish087d4d02018-08-01 10:10:22 +000027from tempest.cmd import workspace
Manik Bindlish321c85c2018-07-30 06:48:24 +000028from tempest import config
Manik Bindlish087d4d02018-08-01 10:10:22 +000029from tempest.lib.common.utils import data_utils
Matthew Treinisha051c222016-05-23 15:48:22 -040030from tempest.tests import base
31
32DEVNULL = open(os.devnull, 'wb')
Divyansh Acharya1bc06aa2017-08-18 15:09:46 +000033atexit.register(DEVNULL.close)
Matthew Treinisha051c222016-05-23 15:48:22 -040034
Manik Bindlish321c85c2018-07-30 06:48:24 +000035CONF = config.CONF
36
Matthew Treinisha051c222016-05-23 15:48:22 -040037
38class TestTempestRun(base.TestCase):
39
40 def setUp(self):
41 super(TestTempestRun, self).setUp()
42 self.run_cmd = run.TempestRun(None, None)
43
Matthew Treinisha051c222016-05-23 15:48:22 -040044 def test__build_regex_default(self):
45 args = mock.Mock(spec=argparse.Namespace)
46 setattr(args, 'smoke', False)
47 setattr(args, 'regex', '')
zhufle1afe4e2019-06-28 17:43:01 +080048 self.assertIsNone(self.run_cmd._build_regex(args))
Matthew Treinisha051c222016-05-23 15:48:22 -040049
50 def test__build_regex_smoke(self):
51 args = mock.Mock(spec=argparse.Namespace)
52 setattr(args, "smoke", True)
53 setattr(args, 'regex', '')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053054 self.assertEqual(['smoke'], self.run_cmd._build_regex(args))
Matthew Treinisha051c222016-05-23 15:48:22 -040055
56 def test__build_regex_regex(self):
57 args = mock.Mock(spec=argparse.Namespace)
58 setattr(args, 'smoke', False)
59 setattr(args, "regex", 'i_am_a_fun_little_regex')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053060 self.assertEqual(['i_am_a_fun_little_regex'],
Matthew Treinisha051c222016-05-23 15:48:22 -040061 self.run_cmd._build_regex(args))
62
Manik Bindlish087d4d02018-08-01 10:10:22 +000063 def test__build_regex_smoke_regex(self):
64 args = mock.Mock(spec=argparse.Namespace)
65 setattr(args, "smoke", True)
66 setattr(args, 'regex', 'i_am_a_fun_little_regex')
67 self.assertEqual(['smoke'], self.run_cmd._build_regex(args))
68
Matthew Treinisha051c222016-05-23 15:48:22 -040069
70class TestRunReturnCode(base.TestCase):
71 def setUp(self):
72 super(TestRunReturnCode, self).setUp()
73 # Setup test dirs
74 self.directory = tempfile.mkdtemp(prefix='tempest-unit')
75 self.addCleanup(shutil.rmtree, self.directory)
76 self.test_dir = os.path.join(self.directory, 'tests')
77 os.mkdir(self.test_dir)
78 # Setup Test files
Chandan Kumar8a4396e2017-09-15 12:18:10 +053079 self.stestr_conf_file = os.path.join(self.directory, '.stestr.conf')
Matthew Treinisha051c222016-05-23 15:48:22 -040080 self.setup_cfg_file = os.path.join(self.directory, 'setup.cfg')
81 self.passing_file = os.path.join(self.test_dir, 'test_passing.py')
82 self.failing_file = os.path.join(self.test_dir, 'test_failing.py')
83 self.init_file = os.path.join(self.test_dir, '__init__.py')
84 self.setup_py = os.path.join(self.directory, 'setup.py')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053085 shutil.copy('tempest/tests/files/testr-conf', self.stestr_conf_file)
Matthew Treinisha051c222016-05-23 15:48:22 -040086 shutil.copy('tempest/tests/files/passing-tests', self.passing_file)
87 shutil.copy('tempest/tests/files/failing-tests', self.failing_file)
88 shutil.copy('setup.py', self.setup_py)
89 shutil.copy('tempest/tests/files/setup.cfg', self.setup_cfg_file)
90 shutil.copy('tempest/tests/files/__init__.py', self.init_file)
91 # Change directory, run wrapper and check result
92 self.addCleanup(os.chdir, os.path.abspath(os.curdir))
93 os.chdir(self.directory)
94
95 def assertRunExit(self, cmd, expected):
96 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
97 stderr=subprocess.PIPE)
98 out, err = p.communicate()
99 msg = ("Running %s got an unexpected returncode\n"
100 "Stdout: %s\nStderr: %s" % (' '.join(cmd), out, err))
101 self.assertEqual(p.returncode, expected, msg)
Matthew Treinishf9902ec2018-02-22 12:11:46 -0500102 return out, err
Matthew Treinisha051c222016-05-23 15:48:22 -0400103
104 def test_tempest_run_passes(self):
Matthew Treinisha051c222016-05-23 15:48:22 -0400105 self.assertRunExit(['tempest', 'run', '--regex', 'passing'], 0)
106
Chandan Kumar8a4396e2017-09-15 12:18:10 +0530107 def test_tempest_run_passes_with_stestr_repository(self):
108 subprocess.call(['stestr', 'init'])
Masayuki Igawafe2fa002016-06-22 12:58:34 +0900109 self.assertRunExit(['tempest', 'run', '--regex', 'passing'], 0)
110
Manik Bindlish71c82372019-01-29 10:52:27 +0000111 def test_tempest_run_failing(self):
112 self.assertRunExit(['tempest', 'run', '--regex', 'failing'], 1)
113
114 def test_tempest_run_failing_with_stestr_repository(self):
115 subprocess.call(['stestr', 'init'])
116 self.assertRunExit(['tempest', 'run', '--regex', 'failing'], 1)
117
118 def test_tempest_run_blackregex_failing(self):
119 self.assertRunExit(['tempest', 'run', '--black-regex', 'failing'], 0)
120
121 def test_tempest_run_blackregex_failing_with_stestr_repository(self):
122 subprocess.call(['stestr', 'init'])
123 self.assertRunExit(['tempest', 'run', '--black-regex', 'failing'], 0)
124
125 def test_tempest_run_blackregex_passing(self):
126 self.assertRunExit(['tempest', 'run', '--black-regex', 'passing'], 1)
127
128 def test_tempest_run_blackregex_passing_with_stestr_repository(self):
129 subprocess.call(['stestr', 'init'])
130 self.assertRunExit(['tempest', 'run', '--black-regex', 'passing'], 1)
131
Matthew Treinisha051c222016-05-23 15:48:22 -0400132 def test_tempest_run_fails(self):
Matthew Treinisha051c222016-05-23 15:48:22 -0400133 self.assertRunExit(['tempest', 'run'], 1)
Brant Knudson6a090f42016-10-13 12:51:49 -0500134
Matthew Treinishf9902ec2018-02-22 12:11:46 -0500135 def test_run_list(self):
136 subprocess.call(['stestr', 'init'])
137 out, err = self.assertRunExit(['tempest', 'run', '-l'], 0)
138 tests = out.split()
139 tests = sorted([six.text_type(x.rstrip()) for x in tests if x])
140 result = [
141 six.text_type('tests.test_failing.FakeTestClass.test_pass'),
142 six.text_type('tests.test_failing.FakeTestClass.test_pass_list'),
143 six.text_type('tests.test_passing.FakeTestClass.test_pass'),
144 six.text_type('tests.test_passing.FakeTestClass.test_pass_list'),
145 ]
146 # NOTE(mtreinish): on python 3 the subprocess prints b'' around
147 # stdout.
likui7d91c872020-09-22 12:29:16 +0800148 result = ["b\'" + x + "\'" for x in result]
Matthew Treinishf9902ec2018-02-22 12:11:46 -0500149 self.assertEqual(result, tests)
150
Arx Cruzc06c3712020-02-20 11:03:52 +0100151 def test_tempest_run_with_worker_file(self):
152 fd, path = tempfile.mkstemp()
153 self.addCleanup(os.remove, path)
154 worker_file = os.fdopen(fd, 'wb', 0)
155 self.addCleanup(worker_file.close)
156 worker_file.write(
157 '- worker:\n - passing\n concurrency: 3'.encode('utf-8'))
158 self.assertRunExit(['tempest', 'run', '--worker-file=%s' % path], 0)
159
Matthew Treinish3c6b15d2018-02-22 11:37:52 -0500160 def test_tempest_run_with_whitelist(self):
161 fd, path = tempfile.mkstemp()
162 self.addCleanup(os.remove, path)
163 whitelist_file = os.fdopen(fd, 'wb', 0)
164 self.addCleanup(whitelist_file.close)
165 whitelist_file.write('passing'.encode('utf-8'))
166 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path], 0)
167
Manik Bindlish5a276ea2019-01-29 07:43:52 +0000168 def test_tempest_run_with_whitelist_regex_include_pass_check_fail(self):
Matthew Treinish3c6b15d2018-02-22 11:37:52 -0500169 fd, path = tempfile.mkstemp()
170 self.addCleanup(os.remove, path)
171 whitelist_file = os.fdopen(fd, 'wb', 0)
172 self.addCleanup(whitelist_file.close)
173 whitelist_file.write('passing'.encode('utf-8'))
174 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path,
175 '--regex', 'fail'], 1)
176
Manik Bindlish5a276ea2019-01-29 07:43:52 +0000177 def test_tempest_run_with_whitelist_regex_include_pass_check_pass(self):
178 fd, path = tempfile.mkstemp()
179 self.addCleanup(os.remove, path)
180 whitelist_file = os.fdopen(fd, 'wb', 0)
181 self.addCleanup(whitelist_file.close)
182 whitelist_file.write('passing'.encode('utf-8'))
183 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path,
184 '--regex', 'passing'], 0)
185
186 def test_tempest_run_with_whitelist_regex_include_fail_check_pass(self):
187 fd, path = tempfile.mkstemp()
188 self.addCleanup(os.remove, path)
189 whitelist_file = os.fdopen(fd, 'wb', 0)
190 self.addCleanup(whitelist_file.close)
191 whitelist_file.write('failing'.encode('utf-8'))
192 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path,
193 '--regex', 'pass'], 1)
194
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900195 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
Manik Bindlish9334ddb2019-01-29 10:26:43 +0000200 def test_tempest_run_with_blacklist_failing(self):
201 fd, path = tempfile.mkstemp()
202 self.addCleanup(os.remove, path)
203 blacklist_file = os.fdopen(fd, 'wb', 0)
204 self.addCleanup(blacklist_file.close)
205 blacklist_file.write('failing'.encode('utf-8'))
206 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path], 0)
207
208 def test_tempest_run_with_blacklist_passing(self):
209 fd, path = tempfile.mkstemp()
210 self.addCleanup(os.remove, path)
211 blacklist_file = os.fdopen(fd, 'wb', 0)
212 self.addCleanup(blacklist_file.close)
213 blacklist_file.write('passing'.encode('utf-8'))
214 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path], 1)
215
216 def test_tempest_run_with_blacklist_regex_exclude_fail_check_pass(self):
217 fd, path = tempfile.mkstemp()
218 self.addCleanup(os.remove, path)
219 blacklist_file = os.fdopen(fd, 'wb', 0)
220 self.addCleanup(blacklist_file.close)
221 blacklist_file.write('failing'.encode('utf-8'))
222 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path,
223 '--regex', 'pass'], 0)
224
225 def test_tempest_run_with_blacklist_regex_exclude_pass_check_pass(self):
226 fd, path = tempfile.mkstemp()
227 self.addCleanup(os.remove, path)
228 blacklist_file = os.fdopen(fd, 'wb', 0)
229 self.addCleanup(blacklist_file.close)
230 blacklist_file.write('passing'.encode('utf-8'))
231 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path,
232 '--regex', 'pass'], 1)
233
234 def test_tempest_run_with_blacklist_regex_exclude_pass_check_fail(self):
235 fd, path = tempfile.mkstemp()
236 self.addCleanup(os.remove, path)
237 blacklist_file = os.fdopen(fd, 'wb', 0)
238 self.addCleanup(blacklist_file.close)
239 blacklist_file.write('passing'.encode('utf-8'))
240 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path,
241 '--regex', 'fail'], 1)
242
Brant Knudson6a090f42016-10-13 12:51:49 -0500243
Manik Bindlish321c85c2018-07-30 06:48:24 +0000244class TestConfigPathCheck(base.TestCase):
245 def setUp(self):
246 super(TestConfigPathCheck, self).setUp()
247 self.run_cmd = run.TempestRun(None, None)
248
249 def test_tempest_run_set_config_path(self):
250 # Note: (mbindlish) This test is created for the bug id: 1783751
251 # Checking TEMPEST_CONFIG_DIR and TEMPEST_CONFIG is actually
252 # getting set in os environment when some data has passed to
253 # set the environment.
254
Manik Bindlish21491df2018-12-14 06:58:42 +0000255 _, path = tempfile.mkstemp()
256 self.addCleanup(os.remove, path)
Manik Bindlish321c85c2018-07-30 06:48:24 +0000257
Manik Bindlish21491df2018-12-14 06:58:42 +0000258 self.run_cmd._set_env(path)
259 self.assertEqual(path, CONF._path)
260 self.assertIn('TEMPEST_CONFIG_DIR', os.environ)
261 self.assertEqual(path, os.path.join(os.environ['TEMPEST_CONFIG_DIR'],
262 os.environ['TEMPEST_CONFIG']))
263
264 def test_tempest_run_set_config_no_exist_path(self):
265 path = "fake/path"
266 self.assertRaisesRegex(FileNotFoundError,
267 'Config file: .* doesn\'t exist',
268 self.run_cmd._set_env, path)
269
270 def test_tempest_run_no_config_path(self):
Manik Bindlish321c85c2018-07-30 06:48:24 +0000271 # Note: (mbindlish) This test is created for the bug id: 1783751
272 # Checking TEMPEST_CONFIG_DIR and TEMPEST_CONFIG should have no value
273 # in os environment when no data has passed to set the environment.
274
275 self.run_cmd._set_env("")
276 self.assertFalse(CONF._path)
277 self.assertNotIn('TEMPEST_CONFIG_DIR', os.environ)
278 self.assertNotIn('TEMPEST_CONFIG', os.environ)
279
280
Brant Knudson6a090f42016-10-13 12:51:49 -0500281class TestTakeAction(base.TestCase):
Manik Bindlish087d4d02018-08-01 10:10:22 +0000282 def setUp(self):
283 super(TestTakeAction, self).setUp()
284 self.name = data_utils.rand_name('workspace')
285 self.path = tempfile.mkdtemp()
286 self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
287 store_dir = tempfile.mkdtemp()
288 self.addCleanup(shutil.rmtree, store_dir, ignore_errors=True)
289 self.store_file = os.path.join(store_dir, 'workspace.yaml')
290 self.workspace_manager = workspace.WorkspaceManager(
291 path=self.store_file)
292 self.workspace_manager.register_new_workspace(self.name, self.path)
293
294 def _setup_test_dirs(self):
295 self.directory = tempfile.mkdtemp(prefix='tempest-unit')
296 self.addCleanup(shutil.rmtree, self.directory, ignore_errors=True)
297 self.test_dir = os.path.join(self.directory, 'tests')
298 os.mkdir(self.test_dir)
299 # Change directory, run wrapper and check result
300 self.addCleanup(os.chdir, os.path.abspath(os.curdir))
301 os.chdir(self.directory)
302
Brant Knudson6a090f42016-10-13 12:51:49 -0500303 def test_workspace_not_registered(self):
304 class Exception_(Exception):
305 pass
306
307 m_exit = self.useFixture(fixtures.MockPatch('sys.exit')).mock
308 # sys.exit must not continue (or exit)
309 m_exit.side_effect = Exception_
310
311 workspace = self.getUniqueString()
312
313 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
314 parsed_args = mock.Mock()
315 parsed_args.config_file = []
316
317 # Override $HOME so that empty workspace gets created in temp dir.
318 self.useFixture(fixtures.TempHomeDir())
319
320 # Force use of the temporary home directory.
321 parsed_args.workspace_path = None
322
323 # Simulate --workspace argument.
324 parsed_args.workspace = workspace
325
326 self.assertRaises(Exception_, tempest_run.take_action, parsed_args)
327 exit_msg = m_exit.call_args[0][0]
328 self.assertIn(workspace, exit_msg)
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900329
330 def test_config_file_specified(self):
Manik Bindlish087d4d02018-08-01 10:10:22 +0000331 self._setup_test_dirs()
Manik Bindlish21491df2018-12-14 06:58:42 +0000332 _, path = tempfile.mkstemp()
333 self.addCleanup(os.remove, path)
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900334 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
335 parsed_args = mock.Mock()
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900336
337 parsed_args.workspace = None
338 parsed_args.state = None
339 parsed_args.list_tests = False
Manik Bindlish21491df2018-12-14 06:58:42 +0000340 parsed_args.config_file = path
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900341
342 with mock.patch('stestr.commands.run_command') as m:
343 m.return_value = 0
344 self.assertEqual(0, tempest_run.take_action(parsed_args))
345 m.assert_called()
Manik Bindlish087d4d02018-08-01 10:10:22 +0000346
347 def test_no_config_file_no_workspace_no_state(self):
348 self._setup_test_dirs()
349 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
350 parsed_args = mock.Mock()
351
352 parsed_args.workspace = None
353 parsed_args.state = None
354 parsed_args.list_tests = False
355 parsed_args.config_file = ''
356
357 with mock.patch('stestr.commands.run_command'):
358 self.assertRaises(SystemExit, tempest_run.take_action, parsed_args)
359
360 def test_config_file_workspace_registered(self):
361 self._setup_test_dirs()
Manik Bindlish21491df2018-12-14 06:58:42 +0000362 _, path = tempfile.mkstemp()
363 self.addCleanup(os.remove, path)
Manik Bindlish087d4d02018-08-01 10:10:22 +0000364 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
365 parsed_args = mock.Mock()
366 parsed_args.workspace = self.name
367 parsed_args.workspace_path = self.store_file
368 parsed_args.state = None
369 parsed_args.list_tests = False
Manik Bindlish21491df2018-12-14 06:58:42 +0000370 parsed_args.config_file = path
Manik Bindlish087d4d02018-08-01 10:10:22 +0000371
372 with mock.patch('stestr.commands.run_command') as m:
373 m.return_value = 0
374 self.assertEqual(0, tempest_run.take_action(parsed_args))
375 m.assert_called()
376
377 @mock.patch('tempest.cmd.run.TempestRun._init_state')
378 def test_workspace_registered_no_config_no_state(self, mock_init_state):
379 self._setup_test_dirs()
380 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
381 parsed_args = mock.Mock()
382 parsed_args.workspace = self.name
383 parsed_args.workspace_path = self.store_file
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') as m:
389 m.return_value = 0
390 self.assertEqual(0, tempest_run.take_action(parsed_args))
391 m.assert_called()
392 mock_init_state.assert_not_called()
393
394 @mock.patch('tempest.cmd.run.TempestRun._init_state')
395 def test_no_config_file_no_workspace_state_true(self, mock_init_state):
396 self._setup_test_dirs()
397 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
398 parsed_args = mock.Mock()
399
400 parsed_args.workspace = None
401 parsed_args.state = True
402 parsed_args.list_tests = False
403 parsed_args.config_file = ''
404
405 with mock.patch('stestr.commands.run_command'):
406 self.assertRaises(SystemExit, tempest_run.take_action, parsed_args)
407 mock_init_state.assert_not_called()
408
409 @mock.patch('tempest.cmd.run.TempestRun._init_state')
410 def test_workspace_registered_no_config_state_true(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 = True
417 parsed_args.list_tests = False
418 parsed_args.config_file = ''
419
420 with mock.patch('stestr.commands.run_command') as m:
421 m.return_value = 0
422 self.assertEqual(0, tempest_run.take_action(parsed_args))
423 m.assert_called()
424 mock_init_state.assert_called()
425
426 @mock.patch('tempest.cmd.run.TempestRun._init_state')
427 def test_no_workspace_config_file_state_true(self, mock_init_state):
428 self._setup_test_dirs()
Manik Bindlish21491df2018-12-14 06:58:42 +0000429 _, path = tempfile.mkstemp()
430 self.addCleanup(os.remove, path)
Manik Bindlish087d4d02018-08-01 10:10:22 +0000431 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
432 parsed_args = mock.Mock()
433 parsed_args.workspace = None
434 parsed_args.workspace_path = self.store_file
435 parsed_args.state = True
436 parsed_args.list_tests = False
Manik Bindlish21491df2018-12-14 06:58:42 +0000437 parsed_args.config_file = path
Manik Bindlish087d4d02018-08-01 10:10:22 +0000438
439 with mock.patch('stestr.commands.run_command') as m:
440 m.return_value = 0
441 self.assertEqual(0, tempest_run.take_action(parsed_args))
442 m.assert_called()
443 mock_init_state.assert_called()