Files
os-api-ref/os_api_ref/tests/base.py
Takashi Kajinami 561ed1cb5f Series of job fixes
The time passes and things stop to work which worked before.
Unfortunately now we landed in the situation that we can't fix issues
independently due to their collision so we need to address few things in
one change:

- Replace deprecated sphinx.testing.path

  The sphinx.testing.path moudle was deprecated in sphinx v7.2.0[1].
  Also remove the colgroup section causing the assertion failures.

  The changes made in unit tests require Sphinx v7.2.0 or later, but
  Sphinx is capped to 7.1.2 in Python 3.8. So unit tests may no longer
  pass in Python 3.8 once this change is merged.

  [1] https://github.com/sphinx-doc/sphinx/pull/11526

- Since we stop testing py38 update classifiers

- py312 dropped assertRegexpMatches (replace with AssertRegex)

- py312 changed how OrderedDict is serialized and as such test asserting
  certain serialization form is now failing. Address this by using
  OrderedDict directly in the comparison rather then hardcoding the form
  since onlt that would work in different python versions.

Change-Id: I01a89777e18fb6f21f92a297f605099c5971583c
2024-10-10 17:03:11 +02:00

123 lines
3.9 KiB
Python

# Copyright 2010-2011 OpenStack Foundation
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# 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 os
import pathlib
import shutil
import fixtures
import tempfile
import testtools
from sphinx.testing.util import SphinxTestApp
def example_dir(name=""):
return os.path.join(os.path.dirname(__file__), 'examples', name)
_TRUE_VALUES = ('True', 'true', '1', 'yes')
class with_app:
def __init__(self, **kwargs):
self.srcdir = pathlib.Path(kwargs['srcdir'])
self.sphinx_app_args = kwargs
def __call__(self, f):
def newf(*args, **kwargs):
with tempfile.TemporaryDirectory() as tmpdirname:
tmpdir = pathlib.Path(tmpdirname)
tmproot = tmpdir.joinpath(self.srcdir.name)
shutil.copytree(self.srcdir, tmproot)
self.sphinx_app_args['srcdir'] = tmproot
self.builddir = tmproot.joinpath('_build')
app = SphinxTestApp(freshenv=True, **self.sphinx_app_args)
f(*args, app, app._status, app._warning, **kwargs)
app.cleanup()
return newf
class OutputStreamCapture(fixtures.Fixture):
"""Capture output streams during tests.
This fixture captures errant printing to stderr / stdout during
the tests and lets us see those streams at the end of the test
runs instead. Useful to see what was happening during failed
tests.
"""
def setUp(self):
super(OutputStreamCapture, self).setUp()
if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES:
self.out = self.useFixture(fixtures.StringStream('stdout'))
self.useFixture(
fixtures.MonkeyPatch('sys.stdout', self.out.stream))
if os.environ.get('OS_STDERR_CAPTURE') in _TRUE_VALUES:
self.err = self.useFixture(fixtures.StringStream('stderr'))
self.useFixture(
fixtures.MonkeyPatch('sys.stderr', self.err.stream))
@property
def stderr(self):
return self.err._details["stderr"].as_text()
@property
def stdout(self):
return self.out._details["stdout"].as_text()
class Timeout(fixtures.Fixture):
"""Setup per test timeouts.
In order to avoid test deadlocks we support setting up a test
timeout parameter read from the environment. In almost all
cases where the timeout is reached this means a deadlock.
A class level TIMEOUT_SCALING_FACTOR also exists, which allows
extremely long tests to specify they need more time.
"""
def __init__(self, timeout, scaling=1):
super(Timeout, self).__init__()
try:
self.test_timeout = int(timeout)
except ValueError:
# If timeout value is invalid do not set a timeout.
self.test_timeout = 0
if scaling >= 1:
self.test_timeout *= scaling
else:
raise ValueError('scaling value must be >= 1')
def setUp(self):
super(Timeout, self).setUp()
if self.test_timeout > 0:
self.useFixture(fixtures.Timeout(self.test_timeout, gentle=True))
class TestCase(testtools.TestCase):
"""Test case base class for all unit tests."""
def setUp(self):
"""Run before each test method to initialize test environment."""
super(TestCase, self).setUp()
self.useFixture(Timeout(
os.environ.get('OS_TEST_TIMEOUT', 0)))
self.useFixture(OutputStreamCapture())