blob: 98ca12d188cd1ea4e5404d313a1aa4a280942a8c [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
27from tempest.tests import base
28
29DEVNULL = open(os.devnull, 'wb')
Divyansh Acharya1bc06aa2017-08-18 15:09:46 +000030atexit.register(DEVNULL.close)
Matthew Treinisha051c222016-05-23 15:48:22 -040031
32
33class TestTempestRun(base.TestCase):
34
35 def setUp(self):
36 super(TestTempestRun, self).setUp()
37 self.run_cmd = run.TempestRun(None, None)
38
Matthew Treinisha051c222016-05-23 15:48:22 -040039 def test__build_regex_default(self):
40 args = mock.Mock(spec=argparse.Namespace)
41 setattr(args, 'smoke', False)
42 setattr(args, 'regex', '')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053043 self.assertIsNone(None, self.run_cmd._build_regex(args))
Matthew Treinisha051c222016-05-23 15:48:22 -040044
45 def test__build_regex_smoke(self):
46 args = mock.Mock(spec=argparse.Namespace)
47 setattr(args, "smoke", True)
48 setattr(args, 'regex', '')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053049 self.assertEqual(['smoke'], self.run_cmd._build_regex(args))
Matthew Treinisha051c222016-05-23 15:48:22 -040050
51 def test__build_regex_regex(self):
52 args = mock.Mock(spec=argparse.Namespace)
53 setattr(args, 'smoke', False)
54 setattr(args, "regex", 'i_am_a_fun_little_regex')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053055 self.assertEqual(['i_am_a_fun_little_regex'],
Matthew Treinisha051c222016-05-23 15:48:22 -040056 self.run_cmd._build_regex(args))
57
58
59class TestRunReturnCode(base.TestCase):
60 def setUp(self):
61 super(TestRunReturnCode, self).setUp()
62 # Setup test dirs
63 self.directory = tempfile.mkdtemp(prefix='tempest-unit')
64 self.addCleanup(shutil.rmtree, self.directory)
65 self.test_dir = os.path.join(self.directory, 'tests')
66 os.mkdir(self.test_dir)
67 # Setup Test files
Chandan Kumar8a4396e2017-09-15 12:18:10 +053068 self.stestr_conf_file = os.path.join(self.directory, '.stestr.conf')
Matthew Treinisha051c222016-05-23 15:48:22 -040069 self.setup_cfg_file = os.path.join(self.directory, 'setup.cfg')
70 self.passing_file = os.path.join(self.test_dir, 'test_passing.py')
71 self.failing_file = os.path.join(self.test_dir, 'test_failing.py')
72 self.init_file = os.path.join(self.test_dir, '__init__.py')
73 self.setup_py = os.path.join(self.directory, 'setup.py')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053074 shutil.copy('tempest/tests/files/testr-conf', self.stestr_conf_file)
Matthew Treinisha051c222016-05-23 15:48:22 -040075 shutil.copy('tempest/tests/files/passing-tests', self.passing_file)
76 shutil.copy('tempest/tests/files/failing-tests', self.failing_file)
77 shutil.copy('setup.py', self.setup_py)
78 shutil.copy('tempest/tests/files/setup.cfg', self.setup_cfg_file)
79 shutil.copy('tempest/tests/files/__init__.py', self.init_file)
80 # Change directory, run wrapper and check result
81 self.addCleanup(os.chdir, os.path.abspath(os.curdir))
82 os.chdir(self.directory)
83
84 def assertRunExit(self, cmd, expected):
85 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
86 stderr=subprocess.PIPE)
87 out, err = p.communicate()
88 msg = ("Running %s got an unexpected returncode\n"
89 "Stdout: %s\nStderr: %s" % (' '.join(cmd), out, err))
90 self.assertEqual(p.returncode, expected, msg)
Matthew Treinishf9902ec2018-02-22 12:11:46 -050091 return out, err
Matthew Treinisha051c222016-05-23 15:48:22 -040092
93 def test_tempest_run_passes(self):
Matthew Treinisha051c222016-05-23 15:48:22 -040094 self.assertRunExit(['tempest', 'run', '--regex', 'passing'], 0)
95
Chandan Kumar8a4396e2017-09-15 12:18:10 +053096 def test_tempest_run_passes_with_stestr_repository(self):
97 subprocess.call(['stestr', 'init'])
Masayuki Igawafe2fa002016-06-22 12:58:34 +090098 self.assertRunExit(['tempest', 'run', '--regex', 'passing'], 0)
99
Matthew Treinisha051c222016-05-23 15:48:22 -0400100 def test_tempest_run_fails(self):
Matthew Treinisha051c222016-05-23 15:48:22 -0400101 self.assertRunExit(['tempest', 'run'], 1)
Brant Knudson6a090f42016-10-13 12:51:49 -0500102
Matthew Treinishf9902ec2018-02-22 12:11:46 -0500103 def test_run_list(self):
104 subprocess.call(['stestr', 'init'])
105 out, err = self.assertRunExit(['tempest', 'run', '-l'], 0)
106 tests = out.split()
107 tests = sorted([six.text_type(x.rstrip()) for x in tests if x])
108 result = [
109 six.text_type('tests.test_failing.FakeTestClass.test_pass'),
110 six.text_type('tests.test_failing.FakeTestClass.test_pass_list'),
111 six.text_type('tests.test_passing.FakeTestClass.test_pass'),
112 six.text_type('tests.test_passing.FakeTestClass.test_pass_list'),
113 ]
114 # NOTE(mtreinish): on python 3 the subprocess prints b'' around
115 # stdout.
116 if six.PY3:
117 result = ["b\'" + x + "\'" for x in result]
118 self.assertEqual(result, tests)
119
Matthew Treinish3c6b15d2018-02-22 11:37:52 -0500120 def test_tempest_run_with_whitelist(self):
121 fd, path = tempfile.mkstemp()
122 self.addCleanup(os.remove, path)
123 whitelist_file = os.fdopen(fd, 'wb', 0)
124 self.addCleanup(whitelist_file.close)
125 whitelist_file.write('passing'.encode('utf-8'))
126 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path], 0)
127
128 def test_tempest_run_with_whitelist_with_regex(self):
129 fd, path = tempfile.mkstemp()
130 self.addCleanup(os.remove, path)
131 whitelist_file = os.fdopen(fd, 'wb', 0)
132 self.addCleanup(whitelist_file.close)
133 whitelist_file.write('passing'.encode('utf-8'))
134 self.assertRunExit(['tempest', 'run', '--whitelist-file=%s' % path,
135 '--regex', 'fail'], 1)
136
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900137 def test_tempest_run_passes_with_config_file(self):
138 self.assertRunExit(['tempest', 'run',
139 '--config-file', self.stestr_conf_file,
140 '--regex', 'passing'], 0)
141
Brant Knudson6a090f42016-10-13 12:51:49 -0500142
143class TestTakeAction(base.TestCase):
144 def test_workspace_not_registered(self):
145 class Exception_(Exception):
146 pass
147
148 m_exit = self.useFixture(fixtures.MockPatch('sys.exit')).mock
149 # sys.exit must not continue (or exit)
150 m_exit.side_effect = Exception_
151
152 workspace = self.getUniqueString()
153
154 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
155 parsed_args = mock.Mock()
156 parsed_args.config_file = []
157
158 # Override $HOME so that empty workspace gets created in temp dir.
159 self.useFixture(fixtures.TempHomeDir())
160
161 # Force use of the temporary home directory.
162 parsed_args.workspace_path = None
163
164 # Simulate --workspace argument.
165 parsed_args.workspace = workspace
166
167 self.assertRaises(Exception_, tempest_run.take_action, parsed_args)
168 exit_msg = m_exit.call_args[0][0]
169 self.assertIn(workspace, exit_msg)
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900170
171 def test_config_file_specified(self):
172 # Setup test dirs
173 self.directory = tempfile.mkdtemp(prefix='tempest-unit')
174 self.addCleanup(shutil.rmtree, self.directory)
175 self.test_dir = os.path.join(self.directory, 'tests')
176 os.mkdir(self.test_dir)
177 # Change directory, run wrapper and check result
178 self.addCleanup(os.chdir, os.path.abspath(os.curdir))
179 os.chdir(self.directory)
180
181 tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
182 parsed_args = mock.Mock()
Masayuki Igawaff07eac2018-02-22 16:53:09 +0900183
184 parsed_args.workspace = None
185 parsed_args.state = None
186 parsed_args.list_tests = False
187 parsed_args.config_file = '.stestr.conf'
188
189 with mock.patch('stestr.commands.run_command') as m:
190 m.return_value = 0
191 self.assertEqual(0, tempest_run.take_action(parsed_args))
192 m.assert_called()