Remove use of contextlib.nested (api-tests)

Removed use of contextlib.nested call from codebase, as it has been
deprecated since Python 2.7.

There are also known issues with contextlib.nested that were addressed
by the native support for multiple "with" variables. For instance, if
the first object is created but the second one throws an exception,
the first object's __exit__ is never called. For more information see
https://docs.python.org/2/library/contextlib.html#contextlib.nested
contextlib.nested is also not compatible with Python 3.

Multi-patch set for easier chunks. This one addresses the tests from
neutron/tests/unit/api directory.

Line continuation markers (e.g. '\') had to be used or syntax
errors were thrown. While using parentheses is the preferred way
for multiple line statements, but in case of long with statements
backslashes are acceptable.

Partial-Bug: 1428424
Change-Id: I09673f9d4c7f07d3043804676fef018905dd1239
This commit is contained in:
ankitagrawal 2015-05-14 05:19:44 -07:00
parent 2f8373aff6
commit 8ee3020c47
2 changed files with 2 additions and 8 deletions

View File

@ -146,7 +146,6 @@ def check_no_contextlib_nested(logical_line, filename):
# these issues. It should be removed completely
# when bug 1428424 is closed.
ignore_dirs = [
"neutron/tests/unit/api",
"neutron/tests/unit/db",
"neutron/tests/unit/extensions",
"neutron/tests/unit/plugins",

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import mock
from neutron.api.rpc.handlers import securitygroups_rpc
@ -24,12 +23,8 @@ class SecurityGroupServerRpcApiTestCase(base.BaseTestCase):
def test_security_group_rules_for_devices(self):
rpcapi = securitygroups_rpc.SecurityGroupServerRpcApi('fake_topic')
with contextlib.nested(
mock.patch.object(rpcapi.client, 'call'),
mock.patch.object(rpcapi.client, 'prepare'),
) as (
rpc_mock, prepare_mock
):
with mock.patch.object(rpcapi.client, 'call') as rpc_mock,\
mock.patch.object(rpcapi.client, 'prepare') as prepare_mock:
prepare_mock.return_value = rpcapi.client
rpcapi.security_group_rules_for_devices('context', ['fake_device'])