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
This commit is contained in:
parent
ecad0651b1
commit
a7cd68519b
@ -22,3 +22,10 @@ oslotest.moxstubout
|
||||
|
||||
.. automodule:: oslotest.moxstubout
|
||||
:members:
|
||||
|
||||
oslotest.timeout
|
||||
================
|
||||
|
||||
.. automodule:: oslotest.timeout
|
||||
:members:
|
||||
:special-members:
|
||||
|
@ -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:
|
||||
|
34
oslotest/timeout.py
Normal file
34
oslotest/timeout.py
Normal file
@ -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))
|
@ -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
|
||||
|
45
tests/unit/test_timeout.py
Normal file
45
tests/unit/test_timeout.py
Normal file
@ -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)
|
Loading…
x
Reference in New Issue
Block a user