Update base test case to use base from oslotest

We can get rid of our custom stdout/stderr handling by doing this. We
should push our 'attach-on-exception' code up to oslotest so we can get
rid of it locally.

Remove a direct mock of time.sleep. This is already handled in
the base test case. Also remove a time.sleep(40) - because of all of the
reasons.

Set the default for timeout to 3 seconds. Sometimes an error will cause
a requests-mock test to spin in an infinite loop. All of the unit tests
run in under a second and should stay that way.

Change-Id: I7f9ea357a85754a6d3f79955d3ffb2a68a0ff621
This commit is contained in:
Monty Taylor 2018-02-02 06:24:45 -06:00
parent 821af87bb8
commit 18a178456c
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
8 changed files with 42 additions and 44 deletions

View File

@ -276,11 +276,14 @@ class OpenStackCloud(_normalize.Normalizer):
that do not want to be overridden can be ommitted.
"""
# TODO(mordred) Replace this with from_session
config = openstack.config.OpenStackConfig(
app_name=self.config._app_name,
app_version=self.config._app_version,
load_yaml_config=False)
if self.config._openstack_config:
config = self.config._openstack_config
else:
# TODO(mordred) Replace this with from_session
config = openstack.config.OpenStackConfig(
app_name=self.config._app_name,
app_version=self.config._app_version,
load_yaml_config=False)
params = copy.deepcopy(self.config.config)
# Remove profile from current cloud so that overridding works
params.pop('profile', None)
@ -322,9 +325,7 @@ class OpenStackCloud(_normalize.Normalizer):
session=self.session,
discovery_cache=self.config._discovery_cache)
# Use cloud='defaults' so that we overlay settings properly
cloud_config = config.get_one(
cloud='defaults',
session_constructor=session_constructor,
**params)
# Override the cloud name so that logging/location work right

View File

@ -103,7 +103,8 @@ def _get_os_environ(envvar_prefix=None):
environkeys = [k for k in os.environ.keys()
if (k.startswith('OS_') or k.startswith(envvar_prefix))
and not k.startswith('OS_TEST') # infra CI var
and not k.startswith('OS_STD') # infra CI var
and not k.startswith('OS_STD') # oslotest var
and not k.startswith('OS_LOG') # oslotest var
]
for k in environkeys:
newkey = k.split('_', 1)[-1].lower()

View File

@ -14,19 +14,20 @@
# under the License.
import os
import sys
import fixtures
import logging
import munch
from oslotest import base
import pprint
from six import StringIO
import testtools
import testtools.content
_TRUE_VALUES = ('true', '1', 'yes')
class TestCase(testtools.TestCase):
class TestCase(base.BaseTestCase):
"""Test case base class for all tests."""
@ -35,32 +36,25 @@ class TestCase(testtools.TestCase):
def setUp(self):
"""Run before each test method to initialize test environment."""
# No openstacksdk unit tests should EVER run longer than a second.
# Set this to 3 by default just to give us some fudge.
# Do this before super setUp so that we intercept the default value
# in oslotest. TODO(mordred) Make the default timeout configurable
# in oslotest.
self.useFixture(
fixtures.EnvironmentVariable(
'OS_TEST_TIMEOUT', os.environ.get('OS_TEST_TIMEOUT', '3')))
super(TestCase, self).setUp()
test_timeout = int(os.environ.get('OS_TEST_TIMEOUT', 0))
try:
test_timeout = int(test_timeout * self.TIMEOUT_SCALING_FACTOR)
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(fixtures.NestedTempfile())
self.useFixture(fixtures.TempHomeDir())
if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES:
stdout = self.useFixture(fixtures.StringStream('stdout')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
if os.environ.get('OS_STDERR_CAPTURE') in _TRUE_VALUES:
stderr = self.useFixture(fixtures.StringStream('stderr')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
self._log_stream = StringIO()
if os.environ.get('OS_ALWAYS_LOG') in _TRUE_VALUES:
self.addCleanup(self.printLogs)
if os.environ.get('OS_LOG_CAPTURE') in _TRUE_VALUES:
self._log_stream = StringIO()
if os.environ.get('OS_ALWAYS_LOG') in _TRUE_VALUES:
self.addCleanup(self.printLogs)
else:
self.addOnException(self.attachLogs)
else:
self.addOnException(self.attachLogs)
self._log_stream = sys.stdout
handler = logging.StreamHandler(self._log_stream)
formatter = logging.Formatter('%(asctime)s %(name)-32s %(message)s')
@ -76,6 +70,11 @@ class TestCase(testtools.TestCase):
logger.addHandler(handler)
logger.propagate = False
def _fake_logs(self):
# Override _fake_logs in oslotest until we can get our
# attach-on-exception logic added
pass
def assertEqual(self, first, second, *args, **kwargs):
'''Munch aware wrapper'''
if isinstance(first, munch.Munch):

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import time
from openstack.compute.v2 import server
from openstack.tests.functional import base
from openstack.tests.functional.network.v2 import test_network
@ -50,8 +48,6 @@ class TestServer(base.BaseFunctionalTest):
self.assertIsNone(sot)
# Need to wait for the stack to go away before network delete
self.conn.compute.wait_for_delete(self.server)
# TODO(shade) sleeping in tests is bad mmkay?
time.sleep(40)
test_network.delete_network(self.conn, self.network, self.subnet)
super(TestServer, self).tearDown()

View File

@ -10,7 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import time
import unittest
from openstack import exceptions
@ -68,8 +67,6 @@ class TestStack(base.BaseFunctionalTest):
self.stack, 'DELETE_COMPLETE')
except exceptions.NotFoundException:
pass
# TODO(shade) sleeping in tests is bad mmkay?
time.sleep(40)
test_network.delete_network(self.conn, self.network, self.subnet)
super(TestStack, self).tearDown()

View File

@ -484,9 +484,8 @@ class TestCreateServer(base.RequestsMockTestCase):
self.assert_calls()
@mock.patch.object(openstack.cloud.OpenStackCloud, 'add_ips_to_server')
@mock.patch('time.sleep')
def test_create_server_no_addresses(
self, mock_sleep, mock_add_ips_to_server):
self, mock_add_ips_to_server):
"""
Test that create_server with a wait throws an exception if the
server doesn't have addresses.

View File

@ -1824,7 +1824,7 @@ class TestWaitForStatus(base.TestCase):
self.assertRaises(exceptions.ResourceTimeout,
resource.wait_for_status,
"session", res, status, None, 1, 3)
"session", res, status, None, 0.01, 0.1)
def test_no_sleep(self):
res = mock.Mock()

View File

@ -6,11 +6,15 @@ skipsdist = True
[testenv]
usedevelop = True
install_command = pip install {opts} {packages}
passenv = OS_* OPENSTACKSDK_*
setenv =
VIRTUAL_ENV={envdir}
LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_ALL=C
OS_LOG_CAPTURE={env:OS_LOG_CAPTURE:true}
OS_STDOUT_CAPTURE={env:OS_STDOUT_CAPTURE:true}
OS_STDERR_CAPTURE={env:OS_STDERR_CAPTURE:true}
deps =
-c{env:UPPER_CONSTRAINTS_FILE:https://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt}
-r{toxinidir}/test-requirements.txt
@ -19,13 +23,14 @@ commands = stestr run {posargs}
stestr slowest
[testenv:examples]
passenv = OS_* OPENSTACKSDK_*
commands = stestr --test-path ./openstack/tests/examples run {posargs}
stestr slowest
[testenv:functional]
basepython = {env:OPENSTACKSDK_TOX_PYTHON:python2}
passenv = OS_* OPENSTACKSDK_*
setenv =
{[testenv]setenv}
OS_TEST_TIMEOUT=60
commands = stestr --test-path ./openstack/tests/functional run --serial {posargs}
stestr slowest