This reverts commit ffdf6f10be.
Switching oslotest to unittest.mock has broken a bunch of other
projects because they were (incorrectly) relying on oslotest to
pull in third-party mock for them. Until we get everyone migrated,
let's hold off on doing this for oslotest.
Change-Id: I0bdca39686f3653a56f9e4a554662727e2dbaafd
Another one that should be removed now that we're releasing a major
version bump.
Change-Id: I3754a7fe607bcd093e5c87a8b0592301b353e416
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This has been deprecated for some time and codesearch.o.o suggests there
are only three or so 'openstack/xxx' projects still using this, all of
which should drop the usage shortly. Time to drop this module.
Change-Id: I315254ede8353a0cd88828f520c596b1f02a56a7
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
There's no particular reason for us to be verifying mock's behavior.
They have their own unit tests for doing that.
This is specifically a problem right now because mock changed its
behavior in 3.0.0 and broke this test: "Issue #36366: Calling stop()
on an unstarted or stopped unittest.mock.patch() object will now
return None instead of raising RuntimeError, making the method
idempotent."
Change-Id: I57db9bad15cab343e152615e115e2ba3ac4d87fe
Closes-Bug: 1830429
The openstacksdk test suite sets a default value for OS_TEST_TIMEOUT to
enforce that no test in the suite should ever run long. It has to do
that by using the EnvironmentVariable fixture before running the super
setUp to get the value set before oslotest reads the environment
variable. Add a class variable that can be overridden to allow doing
that more cleanly.
Additionally, in the openstacksdk functional test suite, there is a
variable called "TIMEOUT_SCALING_FACTOR" that lets us mark that some of
the tests (I'm looking at you volume functional tests) naturally run a
bit longer than other tests in the suite. It's been invaluable for us
for a couple of ugly cases, so it seemed like we should upstream it into
oslotest.
Change-Id: I2ef5d0194185bf58c0945efb0725202e6d177e3f
As part of the mox removal goal, we need to deprecate this class so
it can be removed once everyone has stopped using it.
Story 2001546
Change-Id: I8864ab0c475b63a7549137d9ea0514b7e8154e79
Currently we're doing the autospec checks on __init__ (if the
target is autospecable), which will require importing modules
in some cases.
The mock.patch's constructor is called when a test module is being
loaded, before any set up could have been run, which can be problematic
in some cases (e.g.: some tested modules are importing platform specific
modules, and they couldn't have been mocked yet).
This patch moves the autospec checks to __enter__, which is executed
after the setUp.
Related-Bug: #1735588
Change-Id: I9e10b34092ad795c7f9e58596fcccf4f37856225
When creating a mock with a class as an autospec:
mock_foo = mock.Mock(autospec=FooClass)
The autospec is only applied to mock_foo itself, and it will
behave as expected. However, the autospec is not applied to its
return_value and thus, the method validation is not enforced:
mock_foo().lish(some_argument_which_doesnt_exist=42)
This patch addresses this issue, and adds necessary unit tests to
test this behaviour.
Change-Id: Icd96beba5a32001cf33f075b801471c6e7c75898
Related-Bug: #1735588
MockAutospecFixture should patch both internal and external usages
of MagicMock (mock.MagicMock and mock.mock.MagicMock). However, only
the internal one is patched properly.
Related-Bug: #1735588
Change-Id: Ib9709f1cf5dbed4792f5dd7c49d8f9c77f04419f
The default behavior of the CaptureOutput fixture relies on
environment variables that many projects may not set. This change
allows the behavior to be controlled by passing parameters to the
fixture when it is initialized.
Change-Id: I110a9062210b0bc92a2f102e3be4558eea8b9f05
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
mock.patch.multiple causes the patch result to be a dictionary,
which causes the autospec to fail. This patch will autospec
properly in this scenario as well.
Change-Id: I3aa97459bf53059422c8fa696cb09df811c1701b
Related-Bug: #1735588
In typical unit tests, almost all of the dependencies are mocked or
patched (mock.patch), without any guarantee that the mocked methods
actually exist, or if their signatures are respected (see below). Because
of this, actual issues can easily be overlooked and missed, as the unit
tests are wrongfully passing.
The mock.Mock class accepts a spec as an argument, which only solves half
the problem: it only checks if an attribute exists, based on the given
spec. It does not guarantee that the given attribute is actually a method,
or if its signature is respected. Some unit tests may pass the autospec
argument, but mock doesn't support it at the moment.
mock.patch, mock.patch.object, mock.patch.multiple accept an autospec
argument, but because of a bug, it cannot be used properly.
Adds a fixture which replaces mock.Mock and mock.MagicMock with
subclass which accepts the autospec argument, and on call, it will
check the signature of the called method / function.
Adds a function which replaces mock.mock._patch with a subclass, which
treats the autospec argument properly (consumes the self / cls argument),
and sets autospec=True by default, unless otherwise specified.
WARNING: this function is not a fixture, and in order to benefit from it,
it will have to be called as EARLY as possible, before any test classes
are loaded, otherwise the original mock.mock._patch is used instead.
Needed-By: I3636833962c905faa0f144c7fdc4833037324d31
Needed-By: I4484e63c97bd1cdde3d88855eabe7545784f365e
Closes-Bug: #1735588
Change-Id: I0e4a55fbf4c1d175726ca22b664e240849a99856
oslotest is a very basic module and should depend on as less as
possible things. Currently oslotest.functional requires
os-cloud-config which test-requires oslotest. Removing
oslotest.functional solves this.
Also oslotest.functional is not used anywhere.
Change-Id: I553fc81a8ec55435004af27c4a34707cff9cf38d
That was announced with commit 5e2a84d41. Use fixtures.Mock* classes instead.
Closes-Bug: #1712017
Change-Id: I367e59e7f9040cd95cb4ed8a2c3e0f526c2825ab
Following OpenStack Style Guidelines: [1]
Unit test assertions tend to give better messages for more
specific assertions. As a result, assertIsNone(...) is
preferred over assertIs(None, ...) and assertIsNotNone(...)
is preferred over assertIsNot(None, ...).
[1] http://docs.openstack.org/developer/hacking/#unit-tests-and-assertraises
Change-Id: Ib2eea3c4fba81849338a3aad538dd58a56bc3f11
Running oslotest's tests fail on Python 3.x because of the "import mock"
lines in test_output.py and test_timeout.py. That works for 2.x because
"mock" is an independent package, but in 3.x, it's a part of unittest in
the standard library. This difference is covered in six.moves, which is
where mock is imported from in other test modules.
Change-Id: I3882c2a9be8abc93cf95942579cb9562c6377c01
Closes-Bug: #1607963
Many assertEqual sentences don't follow assertEqual(expected, actual),
These misplaces have 2 impacts:
1, giving confusing messages when some tests failed.
2, mislead other developers, new test modules may follow these wrong pattern.
This patch fix all of them.
Change-Id: Ie9da006cc561e5266812db2bb1c03a624b5e844b
Closes-Bug: #1604213
This is a refactor of a fixture we use in keystonemiddleware for
removing modules from the python path.
Because of the way python works this typically means it only works on
modules that are imported inline or are reloaded manually.
Change-Id: Ib384e1910ca292fce81ccdd4ff857b64ee8d5a21
Rather than carry implementations of the moved classes in mockpatch,
just reference the symbols. Less maintenance for us.
The tests are removed since fixtures has its own tests.
Change-Id: Iab382a604321e09a01c3d795cd7bcd480dc249ce
Move the tests directory into the package to be consistent with the way
we handle other libraries.
Change-Id: I9527242fe1d6aa0f7177309b05d8d1f5c8ccd396
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
So, calling addCleanup in __init__ is not supported anymore in
latest testtools. This breaks the support we used to have in
the base class that registered mock.patch.stopall right at the
beginning, so classes inheriting would not have to worry about
the cleanup. For Mitaka, we add a hack to make it work by
looking at the underlying _cleanups variable. This clearly needs
to be removed as soon as possible.
Closes-Bug: #1545576
Change-Id: I29a77224bfd9d106c711155e0cfc10eac0e6fe36
Preserve original behavior when ``OS_DEBUG`` is ``True``, but if variable
is set to a valid log level, such as 'WARNING' use that level instead to
allow finer grained control over verbosity of log messages with the
FakeLogger.
Change-Id: I6594cec8de6fa221fe81f663a9126b2df8762ef3
Closes-Bug: 1280454
Add a fixture for creating real files on the filesystem with contents,
to replace the implementation inside the test base class.
Blueprint oslotest-refactor-test-base-class
Change-Id: Ibca716d1054da973583e5bb4694ef1d64750d50a
Add a fixture for controlling how logging is configured.
Blueprint oslotest-refactor-test-base-class
Change-Id: I5f3f353781a6036f21c4cdb577792bceab242ea4
Add a fixture for controlling whether or not stdout and stderr are
captured as the test runs.
Blueprint oslotest-refactor-test-base-class
Change-Id: Ifa8d991c1a8e3ec643e5350d09fcb3895ac8378e
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
When using mockpatch.Patch/PatchObject you need access to the
mock in order to verify that it's getting called (e.g.,
assert_called_once_with), but this attribute wasn't documented so
nobody can tell how to do it.
While adding the attribute, other issues with the docs are
corrected.
Change-Id: I7df083998adcee710cf1c5b87a1b8f091e7ad522
For running functional tests on devstack nodes, it's important to be
able to get the appropriate credentials. Use the clouds.yaml that is
newly written by devstack and os-client-config to pull a few well
defined sets of credentials.
Change-Id: Ibd2d473fce878424e8a6d1c76e81088e575e619b
If start() fails at some point, we may have a partial mock applied, and
no cleanup function. So let's start by adding the cleanup function
before starting anything.
Change-Id: Ic41d65b2da826f982ff020ebd269b7f749dc63a4