Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 1 | # 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 | |
| 15 | import os |
| 16 | import shutil |
Sean Dague | fb5a3e2 | 2014-05-07 14:05:14 -0400 | [diff] [blame] | 17 | import StringIO |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 18 | import subprocess |
| 19 | import tempfile |
Matthew Treinish | 8708621 | 2013-10-28 20:21:54 +0000 | [diff] [blame] | 20 | |
| 21 | from tempest.tests import base |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 22 | |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 23 | DEVNULL = open(os.devnull, 'wb') |
| 24 | |
| 25 | |
Matthew Treinish | 8708621 | 2013-10-28 20:21:54 +0000 | [diff] [blame] | 26 | class TestWrappers(base.TestCase): |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 27 | def setUp(self): |
| 28 | super(TestWrappers, self).setUp() |
| 29 | # Setup test dirs |
| 30 | self.directory = tempfile.mkdtemp(prefix='tempest-unit') |
Sean Dague | 86d3c42 | 2014-01-30 07:06:10 -0500 | [diff] [blame] | 31 | self.addCleanup(shutil.rmtree, self.directory) |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 32 | self.test_dir = os.path.join(self.directory, 'tests') |
| 33 | os.mkdir(self.test_dir) |
| 34 | # Setup Test files |
| 35 | self.testr_conf_file = os.path.join(self.directory, '.testr.conf') |
| 36 | self.setup_cfg_file = os.path.join(self.directory, 'setup.cfg') |
Sean Dague | 50af5d5 | 2014-05-02 14:48:44 -0400 | [diff] [blame] | 37 | self.subunit_trace = os.path.join(self.directory, 'subunit-trace.py') |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 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 Dague | 50af5d5 | 2014-05-02 14:48:44 -0400 | [diff] [blame] | 48 | shutil.copy('tools/subunit-trace.py', self.subunit_trace) |
Sean Dague | fb5a3e2 | 2014-05-07 14:05:14 -0400 | [diff] [blame] | 49 | # copy over the pretty_tox scripts |
| 50 | shutil.copy('tools/pretty_tox.sh', |
| 51 | os.path.join(self.directory, 'pretty_tox.sh')) |
| 52 | shutil.copy('tools/pretty_tox_serial.sh', |
| 53 | os.path.join(self.directory, 'pretty_tox_serial.sh')) |
| 54 | |
| 55 | self.stdout = StringIO.StringIO() |
| 56 | self.stderr = StringIO.StringIO() |
| 57 | # Change directory, run wrapper and check result |
| 58 | self.addCleanup(os.chdir, os.path.abspath(os.curdir)) |
| 59 | os.chdir(self.directory) |
| 60 | |
| 61 | def assertRunExit(self, cmd, expected): |
| 62 | p = subprocess.Popen( |
| 63 | "bash %s" % cmd, shell=True, |
| 64 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Aaron Rosen | 4fd9509 | 2014-09-22 16:10:46 -0700 | [diff] [blame] | 65 | out, err = p.communicate() |
Sean Dague | fb5a3e2 | 2014-05-07 14:05:14 -0400 | [diff] [blame] | 66 | |
| 67 | self.assertEqual( |
| 68 | p.returncode, expected, |
Aaron Rosen | 4fd9509 | 2014-09-22 16:10:46 -0700 | [diff] [blame] | 69 | "Stdout: %s; Stderr: %s" % (out, err)) |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 70 | |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 71 | def test_pretty_tox(self): |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 72 | # Git init is required for the pbr testr command. pbr requires a git |
| 73 | # version or an sdist to work. so make the test directory a git repo |
| 74 | # too. |
Sean Dague | fb5a3e2 | 2014-05-07 14:05:14 -0400 | [diff] [blame] | 75 | subprocess.call(['git', 'init'], stderr=DEVNULL) |
Matthew Treinish | 178cc4a | 2014-09-12 18:54:34 -0400 | [diff] [blame] | 76 | self.assertRunExit('pretty_tox.sh passing', 0) |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 77 | |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 78 | def test_pretty_tox_fails(self): |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 79 | # Git init is required for the pbr testr command. pbr requires a git |
| 80 | # version or an sdist to work. so make the test directory a git repo |
| 81 | # too. |
Sean Dague | fb5a3e2 | 2014-05-07 14:05:14 -0400 | [diff] [blame] | 82 | subprocess.call(['git', 'init'], stderr=DEVNULL) |
| 83 | self.assertRunExit('pretty_tox.sh', 1) |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 84 | |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 85 | def test_pretty_tox_serial(self): |
Matthew Treinish | 178cc4a | 2014-09-12 18:54:34 -0400 | [diff] [blame] | 86 | self.assertRunExit('pretty_tox_serial.sh passing', 0) |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 87 | |
Matthew Treinish | 32d3570 | 2013-08-13 11:59:06 -0400 | [diff] [blame] | 88 | def test_pretty_tox_serial_fails(self): |
Sean Dague | fb5a3e2 | 2014-05-07 14:05:14 -0400 | [diff] [blame] | 89 | self.assertRunExit('pretty_tox_serial.sh', 1) |