blob: edb9061512d155ddc029d8eea0469564c96e5545 [file] [log] [blame]
Matthew Treinish32d35702013-08-13 11:59:06 -04001# Copyright 2013 IBM Corp.
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 os
16import shutil
17import subprocess
18import tempfile
Matthew Treinish87086212013-10-28 20:21:54 +000019
Matthew Treinishb0c65f22015-04-23 09:09:41 -040020import six
21
Jordan Pittier41129042016-03-29 21:21:16 +020022from tempest.tests.lib import base
Matthew Treinish32d35702013-08-13 11:59:06 -040023
Matthew Treinish32d35702013-08-13 11:59:06 -040024DEVNULL = open(os.devnull, 'wb')
25
26
Matthew Treinish87086212013-10-28 20:21:54 +000027class TestWrappers(base.TestCase):
Matthew Treinish32d35702013-08-13 11:59:06 -040028 def setUp(self):
29 super(TestWrappers, self).setUp()
30 # Setup test dirs
31 self.directory = tempfile.mkdtemp(prefix='tempest-unit')
Sean Dague86d3c422014-01-30 07:06:10 -050032 self.addCleanup(shutil.rmtree, self.directory)
Matthew Treinish32d35702013-08-13 11:59:06 -040033 self.test_dir = os.path.join(self.directory, 'tests')
34 os.mkdir(self.test_dir)
35 # Setup Test files
36 self.testr_conf_file = os.path.join(self.directory, '.testr.conf')
37 self.setup_cfg_file = os.path.join(self.directory, 'setup.cfg')
38 self.passing_file = os.path.join(self.test_dir, 'test_passing.py')
39 self.failing_file = os.path.join(self.test_dir, 'test_failing.py')
40 self.init_file = os.path.join(self.test_dir, '__init__.py')
41 self.setup_py = os.path.join(self.directory, 'setup.py')
42 shutil.copy('tempest/tests/files/testr-conf', self.testr_conf_file)
43 shutil.copy('tempest/tests/files/passing-tests', self.passing_file)
44 shutil.copy('tempest/tests/files/failing-tests', self.failing_file)
45 shutil.copy('setup.py', self.setup_py)
46 shutil.copy('tempest/tests/files/setup.cfg', self.setup_cfg_file)
47 shutil.copy('tempest/tests/files/__init__.py', self.init_file)
Sean Daguefb5a3e22014-05-07 14:05:14 -040048 # copy over the pretty_tox scripts
49 shutil.copy('tools/pretty_tox.sh',
50 os.path.join(self.directory, 'pretty_tox.sh'))
51 shutil.copy('tools/pretty_tox_serial.sh',
52 os.path.join(self.directory, 'pretty_tox_serial.sh'))
53
Matthew Treinishb0c65f22015-04-23 09:09:41 -040054 self.stdout = six.StringIO()
55 self.stderr = six.StringIO()
Sean Daguefb5a3e22014-05-07 14:05:14 -040056 # Change directory, run wrapper and check result
57 self.addCleanup(os.chdir, os.path.abspath(os.curdir))
58 os.chdir(self.directory)
59
60 def assertRunExit(self, cmd, expected):
61 p = subprocess.Popen(
62 "bash %s" % cmd, shell=True,
63 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Aaron Rosen4fd95092014-09-22 16:10:46 -070064 out, err = p.communicate()
Sean Daguefb5a3e22014-05-07 14:05:14 -040065
66 self.assertEqual(
67 p.returncode, expected,
Aaron Rosen4fd95092014-09-22 16:10:46 -070068 "Stdout: %s; Stderr: %s" % (out, err))
Matthew Treinish32d35702013-08-13 11:59:06 -040069
Matthew Treinish32d35702013-08-13 11:59:06 -040070 def test_pretty_tox(self):
Matthew Treinish32d35702013-08-13 11:59:06 -040071 # Git init is required for the pbr testr command. pbr requires a git
72 # version or an sdist to work. so make the test directory a git repo
73 # too.
Sean Daguefb5a3e22014-05-07 14:05:14 -040074 subprocess.call(['git', 'init'], stderr=DEVNULL)
Matthew Treinish178cc4a2014-09-12 18:54:34 -040075 self.assertRunExit('pretty_tox.sh passing', 0)
Matthew Treinish32d35702013-08-13 11:59:06 -040076
Matthew Treinish32d35702013-08-13 11:59:06 -040077 def test_pretty_tox_fails(self):
Matthew Treinish32d35702013-08-13 11:59:06 -040078 # Git init is required for the pbr testr command. pbr requires a git
79 # version or an sdist to work. so make the test directory a git repo
80 # too.
Sean Daguefb5a3e22014-05-07 14:05:14 -040081 subprocess.call(['git', 'init'], stderr=DEVNULL)
82 self.assertRunExit('pretty_tox.sh', 1)
Matthew Treinish32d35702013-08-13 11:59:06 -040083
Matthew Treinish32d35702013-08-13 11:59:06 -040084 def test_pretty_tox_serial(self):
Matthew Treinish178cc4a2014-09-12 18:54:34 -040085 self.assertRunExit('pretty_tox_serial.sh passing', 0)
Matthew Treinish32d35702013-08-13 11:59:06 -040086
Matthew Treinish32d35702013-08-13 11:59:06 -040087 def test_pretty_tox_serial_fails(self):
Sean Daguefb5a3e22014-05-07 14:05:14 -040088 self.assertRunExit('pretty_tox_serial.sh', 1)