From a7cd68519b28cdff0d27f29b7b1ae44094585dbe Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Thu, 14 May 2015 20:56:26 +0000 Subject: [PATCH] Create Timeout wrapper fixture Add a fixture to replace the code in the test base class that was looking at OS_TEST_TIMEOUT and setting up a fixtures.Timeout instance. Blueprint oslotest-refactor-test-base-class Change-Id: I0432ac28772395be15db539f3797b257eb8933ca --- doc/source/api.rst | 7 ++++++ oslotest/base.py | 11 +++------- oslotest/timeout.py | 34 ++++++++++++++++++++++++++++ tests/unit/test_base.py | 2 +- tests/unit/test_timeout.py | 45 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 oslotest/timeout.py create mode 100644 tests/unit/test_timeout.py diff --git a/doc/source/api.rst b/doc/source/api.rst index d64c037..8fabfff 100644 --- a/doc/source/api.rst +++ b/doc/source/api.rst @@ -22,3 +22,10 @@ oslotest.moxstubout .. automodule:: oslotest.moxstubout :members: + +oslotest.timeout +================ + +.. automodule:: oslotest.timeout + :members: + :special-members: diff --git a/oslotest/base.py b/oslotest/base.py index 6860ae3..8ed270c 100644 --- a/oslotest/base.py +++ b/oslotest/base.py @@ -20,6 +20,8 @@ import os import tempfile import fixtures +from oslotest import timeout + import six from six.moves import mock import testtools @@ -96,14 +98,7 @@ class BaseTestCase(testtools.TestCase): self.useFixture(fixtures.TempHomeDir()) def _set_timeout(self): - test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) - try: - test_timeout = int(test_timeout) - except ValueError: - # If timeout value is invalid do not set a timeout. - test_timeout = 0 - if test_timeout > 0: - self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) + self.useFixture(timeout.Timeout()) def _fake_output(self): if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES: diff --git a/oslotest/timeout.py b/oslotest/timeout.py new file mode 100644 index 0000000..a9286d3 --- /dev/null +++ b/oslotest/timeout.py @@ -0,0 +1,34 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import fixtures + +import os + + +class Timeout(fixtures.Fixture): + """Set the maximum length of time for the test to run. + + Uses OS_TEST_TIMEOUT to get the timeout. + + """ + + def setUp(self): + super(Timeout, self).setUp() + test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) + try: + test_timeout = int(test_timeout) + except ValueError: + # If timeout value is invalid do not set a timeout. + test_timeout = 0 + if test_timeout > 0: + self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) diff --git a/tests/unit/test_base.py b/tests/unit/test_base.py index 3ba0fa7..96f83fd 100644 --- a/tests/unit/test_base.py +++ b/tests/unit/test_base.py @@ -33,7 +33,7 @@ class TestBaseTestCase(testtools.TestCase): pass @mock.patch('os.environ.get') - @mock.patch.object(FakeTestCase, 'useFixture') + @mock.patch('oslotest.timeout.Timeout.useFixture') @mock.patch('fixtures.Timeout') def test_timeout(self, fixture_timeout_mock, fixture_mock, env_get_mock): env_get_mock.return_value = 1 diff --git a/tests/unit/test_timeout.py b/tests/unit/test_timeout.py new file mode 100644 index 0000000..aa266a6 --- /dev/null +++ b/tests/unit/test_timeout.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock +import testtools + +from oslotest import timeout + + +class TimeoutTestCase(testtools.TestCase): + + @mock.patch('os.environ.get') + @mock.patch.object(timeout.Timeout, 'useFixture') + @mock.patch('fixtures.Timeout') + def test_timeout(self, fixture_timeout_mock, fixture_mock, env_get_mock): + env_get_mock.return_value = 1 + tc = timeout.Timeout() + tc.setUp() + env_get_mock.assert_called_once_with('OS_TEST_TIMEOUT', 0) + fixture_timeout_mock.assert_called_once_with(1, gentle=True) + self.assertEqual(fixture_mock.call_count, 1) + + @mock.patch('os.environ.get') + @mock.patch.object(timeout.Timeout, 'useFixture') + @mock.patch('fixtures.Timeout') + def test_no_timeout(self, fixture_timeout_mock, fixture_mock, + env_get_mock): + # Returning 0 means we don't install the timeout + env_get_mock.return_value = 0 + tc = timeout.Timeout() + tc.setUp() + env_get_mock.assert_called_once_with('OS_TEST_TIMEOUT', 0) + self.assertEqual(fixture_timeout_mock.call_count, 0) + self.assertEqual(fixture_mock.call_count, 0)