Commit Graph

69 Commits

Author SHA1 Message Date
Zuul e8c5d7093a Merge "Replace find_module function" 2024-01-07 07:29:28 +00:00
songwenping a814032763 Remove usage of six
Change-Id: Ifce6c18e1da1bb3eb0cff9f03a91cdd1a84dea4c
Co-authored-by: Stephen Finucane <stephenfin@redhat.com>
2023-12-19 17:38:48 +00:00
Petr Vaněk 104cb896ea Replace find_module function
find_module function was deprecated in Python 3.4 [1] and later removed
in Python 3.12 [2], the find_spec function should be used instead. This
change is necessary for proper Python 3.12 support.

[1] https://docs.python.org/3.4/library/importlib.html#importlib.abc.MetaPathFinder.find_module
[2] https://github.com/python/cpython/issues/98040

Change-Id: I8354918d6548192dea3492eb2ee682d323559912
Signed-off-by: Petr Vaněk <arkamar@atlas.cz>
2023-06-21 17:36:53 +02:00
songwenping 49948dfad1 Remove unnecessary unicode prefixes
Change-Id: I554dee8e91468f8ea2dbcb99a925a2e9057ac378
2022-04-19 20:01:18 +08:00
Sorin Sbarnea 9cbf1963ea Document unit of measure for OS_TEST_TIMEOUT
Change-Id: Ie4b80481de69d0c9c376a1c21309ddeff9b5cef6
2021-04-01 11:43:31 +01:00
Ben Nemec 21beb1537f Revert "Revert "Switch to unittest.mock from mock""
This reverts commit c743e68f72af45b24f783a271368aa07923c00d9.

Change-Id: Ia5a87ec7a65703692cba2edf8aa01202dab68173
2020-04-03 14:24:43 +00:00
Ben Nemec cdee551ae6 Revert "Switch to unittest.mock from mock"
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
2020-04-03 14:24:35 +00:00
Monty Taylor ffdf6f10be Switch to unittest.mock from mock
In python3 mock is in the unittest package and doesn't need to
install mock directly.

Change-Id: I195ac9739936372042b5d7d3c0c3f3bd82b04107
2020-03-29 13:28:39 -05:00
Stephen Finucane 14bbcfb753 Remove 'oslotest.functional'
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>
2020-02-26 13:58:02 +00:00
Stephen Finucane 94f102e75f Remove 'oslotest.moxstubout' module
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>
2020-02-25 17:12:16 +00:00
Ben Nemec f8f6b14088 Stop testing mock functionality
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
2019-05-28 17:04:19 +00:00
Ben Nemec a381d7275b Fix nits in timeout change
Just a typo and comment update.

Change-Id: Idf29423516692da939f2bafb96018e35956ee9d4
2018-09-24 20:12:08 +00:00
Monty Taylor 42e8a69f28
Add DEFAULT_TIMEOUT and TIMEOUT_SCALING_FACTOR
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
2018-09-23 08:29:01 -05:00
Zuul 2dc5678f4f Merge "Deprecate MoxStubout class" 2018-05-20 21:49:05 +00:00
Zuul fa9706ed41 Merge "mock: Perform patch's autospec checks on __enter__" 2018-05-18 15:44:56 +00:00
Ben Nemec cae8c8d51a Deprecate MoxStubout class
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
2018-05-14 19:37:55 +00:00
Claudiu Belu 8241dd624f mock: Perform patch's autospec checks on __enter__
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
2018-05-07 10:58:45 +00:00
Zuul 325d0c17a4 Merge "Trivial: Update pypi url to new url" 2018-04-26 17:37:22 +00:00
melissaml 93d87e25ec Trivial: Update pypi url to new url
Pypi url changed from [1] to [2]

[1] https://pypi.python.org/pypi/<package>
[2] https://pypi.org/project/<package>

Change-Id: I5e106592e0df1d8cdb058e578cc423123f6a5b47
2018-04-21 12:12:20 +08:00
Claudiu Belu 6baff0eda4 mock: Apply autospec to a mock's return_value
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
2018-03-29 04:57:48 -07:00
Claudiu Belu bb78b84c3f mock: Properly patch mock.MagicMock
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
2018-03-28 01:14:44 -07:00
Doug Hellmann 039b03aa75 make the CaptureOutput fixture easier to control
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>
2018-03-19 16:35:17 -04:00
Claudiu Belu 5d3196781b mock: Fixes mock.patch.multiple autospec
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
2018-01-09 21:46:45 -08:00
Claudiu Belu 0bde35899a Adds mock autospec fixture
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
2018-01-06 12:21:14 -08:00
Zuul 507553bc46 Merge "Remove oslotest.mockpatch" 2017-10-31 01:49:58 +00:00
Thomas Bechtold 4688f76b21 Deprecate oslotest.functional
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
2017-08-21 12:01:40 +02:00
Thomas Bechtold 4350849eaf Remove oslotest.mockpatch
That was announced with commit 5e2a84d41. Use fixtures.Mock* classes instead.

Closes-Bug: #1712017
Change-Id: I367e59e7f9040cd95cb4ed8a2c3e0f526c2825ab
2017-08-21 10:57:02 +02:00
lingyongxu 31803fe815 Use assertIsNone(...) instead of assertIs(None,...)
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
2017-07-18 14:49:07 +08:00
Luong Anh Tuan 08bdc31572 Using fixtures instead of deprecated mockpatch module
This module mockpatch of oslotest[1] is deprecated since version 1.13
and may be removed in version 2.0. Use fixtures.Mock* classes instead[2]

[1]OpenStack Testing Framework and Utilities
[2]https://docs.openstack.org/oslotest/latest/api/oslotest.mockpatch.html#module-oslotest.mockpatch

Change-Id: I21306034427447221d2a27f5ed52a0f1fdb6a95e
2017-07-12 23:47:58 +07:00
Jenkins 0fad88599e Merge "A DisableModules fixture that removes modules from path" 2016-08-23 07:17:27 +00:00
Danek Duvall 425d465122 Import mock so that it works on Python 3.x
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
2016-07-29 14:19:07 -07:00
yan.haifeng 9c03983d78 Fix parameters of assertEqual are misplaced
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
2016-07-22 11:51:16 +08:00
Jamie Lennox f3786742ed A DisableModules fixture that removes modules from path
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
2016-07-06 15:48:24 +10:00
Brant Knudson 9581cd0206 Remove mockpatch re-implementations
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
2016-04-13 14:27:28 -05:00
Doug Hellmann 936a99dce0 move unit tests into the oslotest package
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>
2016-02-18 12:44:16 +00:00
Davanum Srinivas 7f098afd2d Hack to get back stopall cleanup behavior feature
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
2016-02-17 06:54:22 -05:00
Julien Danjou 5e2a84d417 mockpatch: deprecate in favor of native fixtures
Change-Id: I3efb3c64ebce396e756a47d110a74fe8604b6b72
2015-11-16 18:40:21 +01:00
Brant Knudson 4c164868c4 Fixup docstrings
A few docstring cleanups to make the library easier to use.

Change-Id: Icd3c6c11834e6488fd5782fbea666b4c8b74337c
2015-09-24 08:25:08 -05:00
Joshua Harlow 358f5b3e03 Allow TRACE and integer logging levels for 'OS_DEBUG'
Change-Id: Id977e71dc64d0e13b2c1077e1dd17c0a38d7354d
2015-07-27 17:58:30 -07:00
Jon Grimm bcd8446a8e Allow ``OS_DEBUG`` environment variable to specify log level.
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
2015-06-17 18:43:47 +00:00
Doug Hellmann cc57067355 Add CreateFileWithContent fixture
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
2015-06-12 19:15:18 +00:00
Doug Hellmann 5f63439ee7 Create ConfigureLogging fixture
Add a fixture for controlling how logging is configured.

Blueprint oslotest-refactor-test-base-class

Change-Id: I5f3f353781a6036f21c4cdb577792bceab242ea4
2015-06-12 19:14:45 +00:00
Doug Hellmann 9e7c3e99eb Create CaptureOutput fixture
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
2015-06-12 14:35:28 +00:00
Doug Hellmann a7cd68519b 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
2015-06-12 14:34:51 +00:00
Brant Knudson ecad0651b1 Document the mock attribute for mockpatch
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
2015-06-09 20:20:57 -05:00
Jenkins 7dcdbc5d5a Merge "Add class to deal with clouds.yaml support" 2015-06-03 12:44:45 +00:00
Jenkins 1654fffeb2 Merge "Remove six.moves call" 2015-05-21 22:13:36 +00:00
Monty Taylor 960e4441b8 Add class to deal with clouds.yaml support
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
2015-05-14 20:43:32 -07:00
Julien Danjou 247817014d mockpatch: factorize code
This creates a base class and simplify the rest of the inheriting
classes code.

Change-Id: I010e8720766a3cbb5e3832c1e15385962c576721
2015-04-03 16:43:21 +02:00
Julien Danjou fd60b9c025 mockpatch: fix a potential race condition
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
2015-03-12 14:31:54 +01:00