blob: 00f8bc57e8e74345273695a0b3525e9d0e90a867 [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
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', '')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053048 self.assertIsNone(None, 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.
148 if six.PY3:
149 result = ["b\'" + x + "\'" for x in result]
150 self.assertEqual(result, tests)
151
Matthew Treinish3c6b15d2018-02-22 11:37:52 -0500152 def test_tempest_run_with_whitelist(self):
153 fd, path = tempfile.mkstemp()
154 self.addCleanup(os.remove, path)
155 whitelist_file = os.fdopen(fd, 'wb', 0)
156 self.addCleanup(whitelist_file.close)
157 whitelist_file.write('passing'.encode('utf-8'))
158 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path], 0)
159
Manik Bindlish5a276ea2019-01-29 07:43:52 +0000160 def test_tempest_run_with_whitelist_regex_include_pass_check_fail(self):
Matthew Treinish3c6b15d2018-02-22 11:37:52 -0500161 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,
167 '--regex', 'fail'], 1)
168
Manik Bindlish5a276ea2019-01-29 07:43:52 +0000169 def test_tempest_run_with_whitelist_regex_include_pass_check_pass(self):
170 fd, path = tempfile.mkstemp()
171 self.addCleanup(os.remove, path)
172 whitelist_file = os.fdopen(fd, 'wb', 0)
173 self.addCleanup(whitelist_file.close)
174 whitelist_file.write('passing'.encode('utf-8'))
175 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path,
176 '--regex', 'passing'], 0)
177
178 def test_tempest_run_with_whitelist_regex_include_fail_check_pass(self):
179 fd, path = tempfile.mkstemp()
180 self.addCleanup(os.remove, path)
181 whitelist_file = os.fdopen(fd, 'wb', 0)
182 self.addCleanup(whitelist_file.close)
183 whitelist_file.write('failing'.encode('utf-8'))
184 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path,
185 '--regex', 'pass'], 1)
186
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900187 def test_tempest_run_passes_with_config_file(self):
188 self.assertRunExit(['tempest', 'run',
189 '--config-file', self.stestr_conf_file,
190 '--regex', 'passing'], 0)
191
Manik Bindlish9334ddb2019-01-29 10:26:43 +0000192 def test_tempest_run_with_blacklist_failing(self):
193 fd, path = tempfile.mkstemp()
194 self.addCleanup(os.remove, path)
195 blacklist_file = os.fdopen(fd, 'wb', 0)
196 self.addCleanup(blacklist_file.close)
197 blacklist_file.write('failing'.encode('utf-8'))
198 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path], 0)
199
200 def test_tempest_run_with_blacklist_passing(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('passing'.encode('utf-8'))
206 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path], 1)
207
208 def test_tempest_run_with_blacklist_regex_exclude_fail_check_pass(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('failing'.encode('utf-8'))
214 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path,
215 '--regex', 'pass'], 0)
216
217 def test_tempest_run_with_blacklist_regex_exclude_pass_check_pass(self):
218 fd, path = tempfile.mkstemp()
219 self.addCleanup(os.remove, path)
220 blacklist_file = os.fdopen(fd, 'wb', 0)
221 self.addCleanup(blacklist_file.close)
222 blacklist_file.write('passing'.encode('utf-8'))
223 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path,
224 '--regex', 'pass'], 1)
225
226 def test_tempest_run_with_blacklist_regex_exclude_pass_check_fail(self):
227 fd, path = tempfile.mkstemp()
228 self.addCleanup(os.remove, path)
229 blacklist_file = os.fdopen(fd, 'wb', 0)
230 self.addCleanup(blacklist_file.close)
231 blacklist_file.write('passing'.encode('utf-8'))
232 self.assertRunExit(['tempest', 'run', '--blacklist-file=%s' % path,
233 '--regex', 'fail'], 1)
234
Brant Knudson6a090f42016-10-13 12:51:49 -0500235
Manik Bindlish321c85c2018-07-30 06:48:24 +0000236class TestConfigPathCheck(base.TestCase):
237 def setUp(self):
238 super(TestConfigPathCheck, self).setUp()
239 self.run_cmd = run.TempestRun(None, None)
240
241 def test_tempest_run_set_config_path(self):
242 # Note: (mbindlish) This test is created for the bug id: 1783751
243 # Checking TEMPEST_CONFIG_DIR and TEMPEST_CONFIG is actually
244 # getting set in os environment when some data has passed to
245 # set the environment.
246
247 self.run_cmd._set_env("/fakedir/fakefile")
248 self.assertEqual("/fakedir/fakefile", CONF._path)
249 self.assertIn('TEMPEST_CONFIG_DIR', os.environ)
250 self.assertEqual("/fakedir/fakefile",
251 os.path.join(os.environ['TEMPEST_CONFIG_DIR'],
252 os.environ['TEMPEST_CONFIG']))
253
254 def test_tempest_run_set_config_no_path(self):
255 # Note: (mbindlish) This test is created for the bug id: 1783751
256 # Checking TEMPEST_CONFIG_DIR and TEMPEST_CONFIG should have no value
257 # in os environment when no data has passed to set the environment.
258
259 self.run_cmd._set_env("")
260 self.assertFalse(CONF._path)
261 self.assertNotIn('TEMPEST_CONFIG_DIR', os.environ)
262 self.assertNotIn('TEMPEST_CONFIG', os.environ)
263
264
Brant Knudson6a090f42016-10-13 12:51:49 -0500265class TestTakeAction(base.TestCase):
Manik Bindlish087d4d02018-08-01 10:10:22 +0000266 def setUp(self):
267 super(TestTakeAction, self).setUp()
268 self.name = data_utils.rand_name('workspace')
269 self.path = tempfile.mkdtemp()
270 self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
271 store_dir = tempfile.mkdtemp()
272 self.addCleanup(shutil.rmtree, store_dir, ignore_errors=True)
273 self.store_file = os.path.join(store_dir, 'workspace.yaml')
274 self.workspace_manager = workspace.WorkspaceManager(
275 path=self.store_file)
276 self.workspace_manager.register_new_workspace(self.name, self.path)
277
278 def _setup_test_dirs(self):
279 self.directory = tempfile.mkdtemp(prefix='tempest-unit')
280 self.addCleanup(shutil.rmtree, self.directory, ignore_errors=True)
281 self.test_dir = os.path.join(self.directory, 'tests')
282 os.mkdir(self.test_dir)
283 # Change directory, run wrapper and check result
284 self.addCleanup(os.chdir, os.path.abspath(os.curdir))
285 os.chdir(self.directory)
286
Brant Knudson6a090f42016-10-13 12:51:49 -0500287 def test_workspace_not_registered(self):
288 class Exception_(Exception):
289 pass
290
291 m_exit = self.useFixture(fixtures.MockPatch('sys.exit')).mock
292 # sys.exit must not continue (or exit)
293 m_exit.side_effect = Exception_
294
295 workspace = self.getUniqueString()
296
297 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
298 parsed_args = mock.Mock()
299 parsed_args.config_file = []
300
301 # Override $HOME so that empty workspace gets created in temp dir.
302 self.useFixture(fixtures.TempHomeDir())
303
304 # Force use of the temporary home directory.
305 parsed_args.workspace_path = None
306
307 # Simulate --workspace argument.
308 parsed_args.workspace = workspace
309
310 self.assertRaises(Exception_, tempest_run.take_action, parsed_args)
311 exit_msg = m_exit.call_args[0][0]
312 self.assertIn(workspace, exit_msg)
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900313
314 def test_config_file_specified(self):
Manik Bindlish087d4d02018-08-01 10:10:22 +0000315 self._setup_test_dirs()
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900316 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
317 parsed_args = mock.Mock()
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900318
319 parsed_args.workspace = None
320 parsed_args.state = None
321 parsed_args.list_tests = False
322 parsed_args.config_file = '.stestr.conf'
323
324 with mock.patch('stestr.commands.run_command') as m:
325 m.return_value = 0
326 self.assertEqual(0, tempest_run.take_action(parsed_args))
327 m.assert_called()
Manik Bindlish087d4d02018-08-01 10:10:22 +0000328
329 def test_no_config_file_no_workspace_no_state(self):
330 self._setup_test_dirs()
331 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
332 parsed_args = mock.Mock()
333
334 parsed_args.workspace = None
335 parsed_args.state = None
336 parsed_args.list_tests = False
337 parsed_args.config_file = ''
338
339 with mock.patch('stestr.commands.run_command'):
340 self.assertRaises(SystemExit, tempest_run.take_action, parsed_args)
341
342 def test_config_file_workspace_registered(self):
343 self._setup_test_dirs()
344 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
345 parsed_args = mock.Mock()
346 parsed_args.workspace = self.name
347 parsed_args.workspace_path = self.store_file
348 parsed_args.state = None
349 parsed_args.list_tests = False
350 parsed_args.config_file = '.stestr.conf'
351
352 with mock.patch('stestr.commands.run_command') as m:
353 m.return_value = 0
354 self.assertEqual(0, tempest_run.take_action(parsed_args))
355 m.assert_called()
356
357 @mock.patch('tempest.cmd.run.TempestRun._init_state')
358 def test_workspace_registered_no_config_no_state(self, mock_init_state):
359 self._setup_test_dirs()
360 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
366 parsed_args.config_file = ''
367
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 mock_init_state.assert_not_called()
373
374 @mock.patch('tempest.cmd.run.TempestRun._init_state')
375 def test_no_config_file_no_workspace_state_true(self, mock_init_state):
376 self._setup_test_dirs()
377 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
378 parsed_args = mock.Mock()
379
380 parsed_args.workspace = None
381 parsed_args.state = True
382 parsed_args.list_tests = False
383 parsed_args.config_file = ''
384
385 with mock.patch('stestr.commands.run_command'):
386 self.assertRaises(SystemExit, tempest_run.take_action, parsed_args)
387 mock_init_state.assert_not_called()
388
389 @mock.patch('tempest.cmd.run.TempestRun._init_state')
390 def test_workspace_registered_no_config_state_true(self, mock_init_state):
391 self._setup_test_dirs()
392 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
393 parsed_args = mock.Mock()
394 parsed_args.workspace = self.name
395 parsed_args.workspace_path = self.store_file
396 parsed_args.state = True
397 parsed_args.list_tests = False
398 parsed_args.config_file = ''
399
400 with mock.patch('stestr.commands.run_command') as m:
401 m.return_value = 0
402 self.assertEqual(0, tempest_run.take_action(parsed_args))
403 m.assert_called()
404 mock_init_state.assert_called()
405
406 @mock.patch('tempest.cmd.run.TempestRun._init_state')
407 def test_no_workspace_config_file_state_true(self, mock_init_state):
408 self._setup_test_dirs()
409 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
410 parsed_args = mock.Mock()
411 parsed_args.workspace = None
412 parsed_args.workspace_path = self.store_file
413 parsed_args.state = True
414 parsed_args.list_tests = False
415 parsed_args.config_file = '.stestr.conf'
416
417 with mock.patch('stestr.commands.run_command') as m:
418 m.return_value = 0
419 self.assertEqual(0, tempest_run.take_action(parsed_args))
420 m.assert_called()
421 mock_init_state.assert_called()