blob: 0e00d94ce1bbe05453aa73cc3d8947910ee632f3 [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
21
Brant Knudson6a090f42016-10-13 12:51:49 -050022import fixtures
Matthew Treinisha051c222016-05-23 15:48:22 -040023import mock
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
Manik Bindlish21491df2018-12-14 06:58:42 +000032if six.PY2:
33 # Python 2 has not FileNotFoundError exception
34 FileNotFoundError = IOError
35
Matthew Treinisha051c222016-05-23 15:48:22 -040036DEVNULL = open(os.devnull, 'wb')
Divyansh Acharya1bc06aa2017-08-18 15:09:46 +000037atexit.register(DEVNULL.close)
Matthew Treinisha051c222016-05-23 15:48:22 -040038
Manik Bindlish321c85c2018-07-30 06:48:24 +000039CONF = config.CONF
40
Matthew Treinisha051c222016-05-23 15:48:22 -040041
42class TestTempestRun(base.TestCase):
43
44 def setUp(self):
45 super(TestTempestRun, self).setUp()
46 self.run_cmd = run.TempestRun(None, None)
47
Matthew Treinisha051c222016-05-23 15:48:22 -040048 def test__build_regex_default(self):
49 args = mock.Mock(spec=argparse.Namespace)
50 setattr(args, 'smoke', False)
51 setattr(args, 'regex', '')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053052 self.assertIsNone(None, self.run_cmd._build_regex(args))
Matthew Treinisha051c222016-05-23 15:48:22 -040053
54 def test__build_regex_smoke(self):
55 args = mock.Mock(spec=argparse.Namespace)
56 setattr(args, "smoke", True)
57 setattr(args, 'regex', '')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053058 self.assertEqual(['smoke'], self.run_cmd._build_regex(args))
Matthew Treinisha051c222016-05-23 15:48:22 -040059
60 def test__build_regex_regex(self):
61 args = mock.Mock(spec=argparse.Namespace)
62 setattr(args, 'smoke', False)
63 setattr(args, "regex", 'i_am_a_fun_little_regex')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053064 self.assertEqual(['i_am_a_fun_little_regex'],
Matthew Treinisha051c222016-05-23 15:48:22 -040065 self.run_cmd._build_regex(args))
66
Manik Bindlish087d4d02018-08-01 10:10:22 +000067 def test__build_regex_smoke_regex(self):
68 args = mock.Mock(spec=argparse.Namespace)
69 setattr(args, "smoke", True)
70 setattr(args, 'regex', 'i_am_a_fun_little_regex')
71 self.assertEqual(['smoke'], self.run_cmd._build_regex(args))
72
Matthew Treinisha051c222016-05-23 15:48:22 -040073
74class TestRunReturnCode(base.TestCase):
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 Kumar8a4396e2017-09-15 12:18:10 +053083 self.stestr_conf_file = os.path.join(self.directory, '.stestr.conf')
Matthew Treinisha051c222016-05-23 15:48:22 -040084 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 Kumar8a4396e2017-09-15 12:18:10 +053089 shutil.copy('tempest/tests/files/testr-conf', self.stestr_conf_file)
Matthew Treinisha051c222016-05-23 15:48:22 -040090 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
99 def assertRunExit(self, cmd, expected):
100 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
101 stderr=subprocess.PIPE)
102 out, err = p.communicate()
103 msg = ("Running %s got an unexpected returncode\n"
104 "Stdout: %s\nStderr: %s" % (' '.join(cmd), out, err))
105 self.assertEqual(p.returncode, expected, msg)
Matthew Treinishf9902ec2018-02-22 12:11:46 -0500106 return out, err
Matthew Treinisha051c222016-05-23 15:48:22 -0400107
108 def test_tempest_run_passes(self):
Matthew Treinisha051c222016-05-23 15:48:22 -0400109 self.assertRunExit(['tempest', 'run', '--regex', 'passing'], 0)
110
Chandan Kumar8a4396e2017-09-15 12:18:10 +0530111 def test_tempest_run_passes_with_stestr_repository(self):
112 subprocess.call(['stestr', 'init'])
Masayuki Igawafe2fa002016-06-22 12:58:34 +0900113 self.assertRunExit(['tempest', 'run', '--regex', 'passing'], 0)
114
Manik Bindlish71c82372019-01-29 10:52:27 +0000115 def test_tempest_run_failing(self):
116 self.assertRunExit(['tempest', 'run', '--regex', 'failing'], 1)
117
118 def test_tempest_run_failing_with_stestr_repository(self):
119 subprocess.call(['stestr', 'init'])
120 self.assertRunExit(['tempest', 'run', '--regex', 'failing'], 1)
121
122 def test_tempest_run_blackregex_failing(self):
123 self.assertRunExit(['tempest', 'run', '--black-regex', 'failing'], 0)
124
125 def test_tempest_run_blackregex_failing_with_stestr_repository(self):
126 subprocess.call(['stestr', 'init'])
127 self.assertRunExit(['tempest', 'run', '--black-regex', 'failing'], 0)
128
129 def test_tempest_run_blackregex_passing(self):
130 self.assertRunExit(['tempest', 'run', '--black-regex', 'passing'], 1)
131
132 def test_tempest_run_blackregex_passing_with_stestr_repository(self):
133 subprocess.call(['stestr', 'init'])
134 self.assertRunExit(['tempest', 'run', '--black-regex', 'passing'], 1)
135
Matthew Treinisha051c222016-05-23 15:48:22 -0400136 def test_tempest_run_fails(self):
Matthew Treinisha051c222016-05-23 15:48:22 -0400137 self.assertRunExit(['tempest', 'run'], 1)
Brant Knudson6a090f42016-10-13 12:51:49 -0500138
Matthew Treinishf9902ec2018-02-22 12:11:46 -0500139 def test_run_list(self):
140 subprocess.call(['stestr', 'init'])
141 out, err = self.assertRunExit(['tempest', 'run', '-l'], 0)
142 tests = out.split()
143 tests = sorted([six.text_type(x.rstrip()) for x in tests if x])
144 result = [
145 six.text_type('tests.test_failing.FakeTestClass.test_pass'),
146 six.text_type('tests.test_failing.FakeTestClass.test_pass_list'),
147 six.text_type('tests.test_passing.FakeTestClass.test_pass'),
148 six.text_type('tests.test_passing.FakeTestClass.test_pass_list'),
149 ]
150 # NOTE(mtreinish): on python 3 the subprocess prints b'' around
151 # stdout.
152 if six.PY3:
153 result = ["b\'" + x + "\'" for x in result]
154 self.assertEqual(result, tests)
155
Matthew Treinish3c6b15d2018-02-22 11:37:52 -0500156 def test_tempest_run_with_whitelist(self):
157 fd, path = tempfile.mkstemp()
158 self.addCleanup(os.remove, path)
159 whitelist_file = os.fdopen(fd, 'wb', 0)
160 self.addCleanup(whitelist_file.close)
161 whitelist_file.write('passing'.encode('utf-8'))
162 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path], 0)
163
Manik Bindlish5a276ea2019-01-29 07:43:52 +0000164 def test_tempest_run_with_whitelist_regex_include_pass_check_fail(self):
Matthew Treinish3c6b15d2018-02-22 11:37:52 -0500165 fd, path = tempfile.mkstemp()
166 self.addCleanup(os.remove, path)
167 whitelist_file = os.fdopen(fd, 'wb', 0)
168 self.addCleanup(whitelist_file.close)
169 whitelist_file.write('passing'.encode('utf-8'))
170 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path,
171 '--regex', 'fail'], 1)
172
Manik Bindlish5a276ea2019-01-29 07:43:52 +0000173 def test_tempest_run_with_whitelist_regex_include_pass_check_pass(self):
174 fd, path = tempfile.mkstemp()
175 self.addCleanup(os.remove, path)
176 whitelist_file = os.fdopen(fd, 'wb', 0)
177 self.addCleanup(whitelist_file.close)
178 whitelist_file.write('passing'.encode('utf-8'))
179 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path,
180 '--regex', 'passing'], 0)
181
182 def test_tempest_run_with_whitelist_regex_include_fail_check_pass(self):
183 fd, path = tempfile.mkstemp()
184 self.addCleanup(os.remove, path)
185 whitelist_file = os.fdopen(fd, 'wb', 0)
186 self.addCleanup(whitelist_file.close)
187 whitelist_file.write('failing'.encode('utf-8'))
188 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path,
189 '--regex', 'pass'], 1)
190
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900191 def test_tempest_run_passes_with_config_file(self):
192 self.assertRunExit(['tempest', 'run',
193 '--config-file', self.stestr_conf_file,
194 '--regex', 'passing'], 0)
195
Manik Bindlish9334ddb2019-01-29 10:26:43 +0000196 def test_tempest_run_with_blacklist_failing(self):
197 fd, path = tempfile.mkstemp()
198 self.addCleanup(os.remove, path)
199 blacklist_file = os.fdopen(fd, 'wb', 0)
200 self.addCleanup(blacklist_file.close)
201 blacklist_file.write('failing'.encode('utf-8'))
202 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path], 0)
203
204 def test_tempest_run_with_blacklist_passing(self):
205 fd, path = tempfile.mkstemp()
206 self.addCleanup(os.remove, path)
207 blacklist_file = os.fdopen(fd, 'wb', 0)
208 self.addCleanup(blacklist_file.close)
209 blacklist_file.write('passing'.encode('utf-8'))
210 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path], 1)
211
212 def test_tempest_run_with_blacklist_regex_exclude_fail_check_pass(self):
213 fd, path = tempfile.mkstemp()
214 self.addCleanup(os.remove, path)
215 blacklist_file = os.fdopen(fd, 'wb', 0)
216 self.addCleanup(blacklist_file.close)
217 blacklist_file.write('failing'.encode('utf-8'))
218 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path,
219 '--regex', 'pass'], 0)
220
221 def test_tempest_run_with_blacklist_regex_exclude_pass_check_pass(self):
222 fd, path = tempfile.mkstemp()
223 self.addCleanup(os.remove, path)
224 blacklist_file = os.fdopen(fd, 'wb', 0)
225 self.addCleanup(blacklist_file.close)
226 blacklist_file.write('passing'.encode('utf-8'))
227 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path,
228 '--regex', 'pass'], 1)
229
230 def test_tempest_run_with_blacklist_regex_exclude_pass_check_fail(self):
231 fd, path = tempfile.mkstemp()
232 self.addCleanup(os.remove, path)
233 blacklist_file = os.fdopen(fd, 'wb', 0)
234 self.addCleanup(blacklist_file.close)
235 blacklist_file.write('passing'.encode('utf-8'))
236 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path,
237 '--regex', 'fail'], 1)
238
Brant Knudson6a090f42016-10-13 12:51:49 -0500239
Manik Bindlish321c85c2018-07-30 06:48:24 +0000240class TestConfigPathCheck(base.TestCase):
241 def setUp(self):
242 super(TestConfigPathCheck, self).setUp()
243 self.run_cmd = run.TempestRun(None, None)
244
245 def test_tempest_run_set_config_path(self):
246 # Note: (mbindlish) This test is created for the bug id: 1783751
247 # Checking TEMPEST_CONFIG_DIR and TEMPEST_CONFIG is actually
248 # getting set in os environment when some data has passed to
249 # set the environment.
250
Manik Bindlish21491df2018-12-14 06:58:42 +0000251 _, path = tempfile.mkstemp()
252 self.addCleanup(os.remove, path)
Manik Bindlish321c85c2018-07-30 06:48:24 +0000253
Manik Bindlish21491df2018-12-14 06:58:42 +0000254 self.run_cmd._set_env(path)
255 self.assertEqual(path, CONF._path)
256 self.assertIn('TEMPEST_CONFIG_DIR', os.environ)
257 self.assertEqual(path, os.path.join(os.environ['TEMPEST_CONFIG_DIR'],
258 os.environ['TEMPEST_CONFIG']))
259
260 def test_tempest_run_set_config_no_exist_path(self):
261 path = "fake/path"
262 self.assertRaisesRegex(FileNotFoundError,
263 'Config file: .* doesn\'t exist',
264 self.run_cmd._set_env, path)
265
266 def test_tempest_run_no_config_path(self):
Manik Bindlish321c85c2018-07-30 06:48:24 +0000267 # Note: (mbindlish) This test is created for the bug id: 1783751
268 # Checking TEMPEST_CONFIG_DIR and TEMPEST_CONFIG should have no value
269 # in os environment when no data has passed to set the environment.
270
271 self.run_cmd._set_env("")
272 self.assertFalse(CONF._path)
273 self.assertNotIn('TEMPEST_CONFIG_DIR', os.environ)
274 self.assertNotIn('TEMPEST_CONFIG', os.environ)
275
276
Brant Knudson6a090f42016-10-13 12:51:49 -0500277class TestTakeAction(base.TestCase):
Manik Bindlish087d4d02018-08-01 10:10:22 +0000278 def setUp(self):
279 super(TestTakeAction, self).setUp()
280 self.name = data_utils.rand_name('workspace')
281 self.path = tempfile.mkdtemp()
282 self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
283 store_dir = tempfile.mkdtemp()
284 self.addCleanup(shutil.rmtree, store_dir, ignore_errors=True)
285 self.store_file = os.path.join(store_dir, 'workspace.yaml')
286 self.workspace_manager = workspace.WorkspaceManager(
287 path=self.store_file)
288 self.workspace_manager.register_new_workspace(self.name, self.path)
289
290 def _setup_test_dirs(self):
291 self.directory = tempfile.mkdtemp(prefix='tempest-unit')
292 self.addCleanup(shutil.rmtree, self.directory, ignore_errors=True)
293 self.test_dir = os.path.join(self.directory, 'tests')
294 os.mkdir(self.test_dir)
295 # Change directory, run wrapper and check result
296 self.addCleanup(os.chdir, os.path.abspath(os.curdir))
297 os.chdir(self.directory)
298
Brant Knudson6a090f42016-10-13 12:51:49 -0500299 def test_workspace_not_registered(self):
300 class Exception_(Exception):
301 pass
302
303 m_exit = self.useFixture(fixtures.MockPatch('sys.exit')).mock
304 # sys.exit must not continue (or exit)
305 m_exit.side_effect = Exception_
306
307 workspace = self.getUniqueString()
308
309 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
310 parsed_args = mock.Mock()
311 parsed_args.config_file = []
312
313 # Override $HOME so that empty workspace gets created in temp dir.
314 self.useFixture(fixtures.TempHomeDir())
315
316 # Force use of the temporary home directory.
317 parsed_args.workspace_path = None
318
319 # Simulate --workspace argument.
320 parsed_args.workspace = workspace
321
322 self.assertRaises(Exception_, tempest_run.take_action, parsed_args)
323 exit_msg = m_exit.call_args[0][0]
324 self.assertIn(workspace, exit_msg)
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900325
326 def test_config_file_specified(self):
Manik Bindlish087d4d02018-08-01 10:10:22 +0000327 self._setup_test_dirs()
Manik Bindlish21491df2018-12-14 06:58:42 +0000328 _, path = tempfile.mkstemp()
329 self.addCleanup(os.remove, path)
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900330 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
331 parsed_args = mock.Mock()
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900332
333 parsed_args.workspace = None
334 parsed_args.state = None
335 parsed_args.list_tests = False
Manik Bindlish21491df2018-12-14 06:58:42 +0000336 parsed_args.config_file = path
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900337
338 with mock.patch('stestr.commands.run_command') as m:
339 m.return_value = 0
340 self.assertEqual(0, tempest_run.take_action(parsed_args))
341 m.assert_called()
Manik Bindlish087d4d02018-08-01 10:10:22 +0000342
343 def test_no_config_file_no_workspace_no_state(self):
344 self._setup_test_dirs()
345 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
346 parsed_args = mock.Mock()
347
348 parsed_args.workspace = None
349 parsed_args.state = None
350 parsed_args.list_tests = False
351 parsed_args.config_file = ''
352
353 with mock.patch('stestr.commands.run_command'):
354 self.assertRaises(SystemExit, tempest_run.take_action, parsed_args)
355
356 def test_config_file_workspace_registered(self):
357 self._setup_test_dirs()
Manik Bindlish21491df2018-12-14 06:58:42 +0000358 _, path = tempfile.mkstemp()
359 self.addCleanup(os.remove, path)
Manik Bindlish087d4d02018-08-01 10:10:22 +0000360 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
361 parsed_args = mock.Mock()
362 parsed_args.workspace = self.name
363 parsed_args.workspace_path = self.store_file
364 parsed_args.state = None
365 parsed_args.list_tests = False
Manik Bindlish21491df2018-12-14 06:58:42 +0000366 parsed_args.config_file = path
Manik Bindlish087d4d02018-08-01 10:10:22 +0000367
368 with mock.patch('stestr.commands.run_command') as m:
369 m.return_value = 0
370 self.assertEqual(0, tempest_run.take_action(parsed_args))
371 m.assert_called()
372
373 @mock.patch('tempest.cmd.run.TempestRun._init_state')
374 def test_workspace_registered_no_config_no_state(self, mock_init_state):
375 self._setup_test_dirs()
376 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
377 parsed_args = mock.Mock()
378 parsed_args.workspace = self.name
379 parsed_args.workspace_path = self.store_file
380 parsed_args.state = None
381 parsed_args.list_tests = False
382 parsed_args.config_file = ''
383
384 with mock.patch('stestr.commands.run_command') as m:
385 m.return_value = 0
386 self.assertEqual(0, tempest_run.take_action(parsed_args))
387 m.assert_called()
388 mock_init_state.assert_not_called()
389
390 @mock.patch('tempest.cmd.run.TempestRun._init_state')
391 def test_no_config_file_no_workspace_state_true(self, mock_init_state):
392 self._setup_test_dirs()
393 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
394 parsed_args = mock.Mock()
395
396 parsed_args.workspace = None
397 parsed_args.state = True
398 parsed_args.list_tests = False
399 parsed_args.config_file = ''
400
401 with mock.patch('stestr.commands.run_command'):
402 self.assertRaises(SystemExit, tempest_run.take_action, parsed_args)
403 mock_init_state.assert_not_called()
404
405 @mock.patch('tempest.cmd.run.TempestRun._init_state')
406 def test_workspace_registered_no_config_state_true(self, mock_init_state):
407 self._setup_test_dirs()
408 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
409 parsed_args = mock.Mock()
410 parsed_args.workspace = self.name
411 parsed_args.workspace_path = self.store_file
412 parsed_args.state = True
413 parsed_args.list_tests = False
414 parsed_args.config_file = ''
415
416 with mock.patch('stestr.commands.run_command') as m:
417 m.return_value = 0
418 self.assertEqual(0, tempest_run.take_action(parsed_args))
419 m.assert_called()
420 mock_init_state.assert_called()
421
422 @mock.patch('tempest.cmd.run.TempestRun._init_state')
423 def test_no_workspace_config_file_state_true(self, mock_init_state):
424 self._setup_test_dirs()
Manik Bindlish21491df2018-12-14 06:58:42 +0000425 _, path = tempfile.mkstemp()
426 self.addCleanup(os.remove, path)
Manik Bindlish087d4d02018-08-01 10:10:22 +0000427 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
428 parsed_args = mock.Mock()
429 parsed_args.workspace = None
430 parsed_args.workspace_path = self.store_file
431 parsed_args.state = True
432 parsed_args.list_tests = False
Manik Bindlish21491df2018-12-14 06:58:42 +0000433 parsed_args.config_file = path
Manik Bindlish087d4d02018-08-01 10:10:22 +0000434
435 with mock.patch('stestr.commands.run_command') as m:
436 m.return_value = 0
437 self.assertEqual(0, tempest_run.take_action(parsed_args))
438 m.assert_called()
439 mock_init_state.assert_called()