Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 1 | # 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 | |
| 15 | import argparse |
| 16 | import os |
| 17 | import shutil |
| 18 | import subprocess |
| 19 | import tempfile |
| 20 | |
| 21 | import mock |
| 22 | |
| 23 | from tempest.cmd import run |
| 24 | from tempest.tests import base |
| 25 | |
| 26 | DEVNULL = open(os.devnull, 'wb') |
| 27 | |
| 28 | |
| 29 | class TestTempestRun(base.TestCase): |
| 30 | |
| 31 | def setUp(self): |
| 32 | super(TestTempestRun, self).setUp() |
| 33 | self.run_cmd = run.TempestRun(None, None) |
| 34 | |
| 35 | def test_build_options(self): |
| 36 | args = mock.Mock(spec=argparse.Namespace) |
| 37 | setattr(args, "subunit", True) |
| 38 | setattr(args, "parallel", False) |
| 39 | setattr(args, "concurrency", 10) |
| 40 | options = self.run_cmd._build_options(args) |
| 41 | self.assertEqual(['--subunit', |
| 42 | '--concurrency=10'], |
| 43 | options) |
| 44 | |
| 45 | def test__build_regex_default(self): |
| 46 | args = mock.Mock(spec=argparse.Namespace) |
| 47 | setattr(args, 'smoke', False) |
| 48 | setattr(args, 'regex', '') |
| 49 | self.assertEqual('', self.run_cmd._build_regex(args)) |
| 50 | |
| 51 | def test__build_regex_smoke(self): |
| 52 | args = mock.Mock(spec=argparse.Namespace) |
| 53 | setattr(args, "smoke", True) |
| 54 | setattr(args, 'regex', '') |
| 55 | self.assertEqual('smoke', self.run_cmd._build_regex(args)) |
| 56 | |
| 57 | def test__build_regex_regex(self): |
| 58 | args = mock.Mock(spec=argparse.Namespace) |
| 59 | setattr(args, 'smoke', False) |
| 60 | setattr(args, "regex", 'i_am_a_fun_little_regex') |
| 61 | self.assertEqual('i_am_a_fun_little_regex', |
| 62 | self.run_cmd._build_regex(args)) |
| 63 | |
| 64 | |
| 65 | class TestRunReturnCode(base.TestCase): |
| 66 | def setUp(self): |
| 67 | super(TestRunReturnCode, self).setUp() |
| 68 | # Setup test dirs |
| 69 | self.directory = tempfile.mkdtemp(prefix='tempest-unit') |
| 70 | self.addCleanup(shutil.rmtree, self.directory) |
| 71 | self.test_dir = os.path.join(self.directory, 'tests') |
| 72 | os.mkdir(self.test_dir) |
| 73 | # Setup Test files |
| 74 | self.testr_conf_file = os.path.join(self.directory, '.testr.conf') |
| 75 | self.setup_cfg_file = os.path.join(self.directory, 'setup.cfg') |
| 76 | self.passing_file = os.path.join(self.test_dir, 'test_passing.py') |
| 77 | self.failing_file = os.path.join(self.test_dir, 'test_failing.py') |
| 78 | self.init_file = os.path.join(self.test_dir, '__init__.py') |
| 79 | self.setup_py = os.path.join(self.directory, 'setup.py') |
| 80 | shutil.copy('tempest/tests/files/testr-conf', self.testr_conf_file) |
| 81 | shutil.copy('tempest/tests/files/passing-tests', self.passing_file) |
| 82 | shutil.copy('tempest/tests/files/failing-tests', self.failing_file) |
| 83 | shutil.copy('setup.py', self.setup_py) |
| 84 | shutil.copy('tempest/tests/files/setup.cfg', self.setup_cfg_file) |
| 85 | shutil.copy('tempest/tests/files/__init__.py', self.init_file) |
| 86 | # Change directory, run wrapper and check result |
| 87 | self.addCleanup(os.chdir, os.path.abspath(os.curdir)) |
| 88 | os.chdir(self.directory) |
| 89 | |
| 90 | def assertRunExit(self, cmd, expected): |
| 91 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 92 | stderr=subprocess.PIPE) |
| 93 | out, err = p.communicate() |
| 94 | msg = ("Running %s got an unexpected returncode\n" |
| 95 | "Stdout: %s\nStderr: %s" % (' '.join(cmd), out, err)) |
| 96 | self.assertEqual(p.returncode, expected, msg) |
| 97 | |
| 98 | def test_tempest_run_passes(self): |
| 99 | # Git init is required for the pbr testr command. pbr requires a git |
| 100 | # version or an sdist to work. so make the test directory a git repo |
| 101 | # too. |
| 102 | subprocess.call(['git', 'init'], stderr=DEVNULL) |
| 103 | self.assertRunExit(['tempest', 'run', '--regex', 'passing'], 0) |
| 104 | |
Masayuki Igawa | fe2fa00 | 2016-06-22 12:58:34 +0900 | [diff] [blame^] | 105 | def test_tempest_run_passes_with_testrepository(self): |
| 106 | # Git init is required for the pbr testr command. pbr requires a git |
| 107 | # version or an sdist to work. so make the test directory a git repo |
| 108 | # too. |
| 109 | subprocess.call(['git', 'init'], stderr=DEVNULL) |
| 110 | subprocess.call(['testr', 'init']) |
| 111 | self.assertRunExit(['tempest', 'run', '--regex', 'passing'], 0) |
| 112 | |
Matthew Treinish | a051c22 | 2016-05-23 15:48:22 -0400 | [diff] [blame] | 113 | def test_tempest_run_fails(self): |
| 114 | # Git init is required for the pbr testr command. pbr requires a git |
| 115 | # version or an sdist to work. so make the test directory a git repo |
| 116 | # too. |
| 117 | subprocess.call(['git', 'init'], stderr=DEVNULL) |
| 118 | self.assertRunExit(['tempest', 'run'], 1) |