Port test_agent and test_agent_api to Python 3

* Add neutron_lbaas.tests.nested(): contextlib.nested() for Python 3
  based on contextlib.ExitStack
* Use tests.nested() in test_agent.py and test_agent_api.py
* tests-py3.txt: add test_agent and test_agent_api

Blueprint: neutron-python3
Change-Id: Ic34fbbc41389a5fc94aaa788fc17d9701422e03b
This commit is contained in:
Victor Stinner 2016-03-04 12:40:25 +01:00
parent 4d93cf3c15
commit 1f557006dc
4 changed files with 35 additions and 4 deletions

View File

@ -0,0 +1,26 @@
# Copyright 2016 Red Hat, Inc.
#
# 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 contextlib
import six
if six.PY3:
@contextlib.contextmanager
def nested(*contexts):
with contextlib.ExitStack() as stack:
yield [stack.enter_context(c) for c in contexts]
else:
nested = contextlib.nested

View File

@ -13,12 +13,12 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import contextlib
import mock import mock
from oslo_config import cfg from oslo_config import cfg
from neutron_lbaas.agent import agent from neutron_lbaas.agent import agent
from neutron_lbaas.tests import base from neutron_lbaas.tests import base
from neutron_lbaas.tests import nested
class TestLbaasService(base.BaseTestCase): class TestLbaasService(base.BaseTestCase):
@ -36,7 +36,7 @@ class TestLbaasService(base.BaseTestCase):
def test_main(self): def test_main(self):
logging_str = 'neutron.agent.common.config.setup_logging' logging_str = 'neutron.agent.common.config.setup_logging'
with contextlib.nested( with nested(
mock.patch(logging_str), mock.patch(logging_str),
mock.patch.object(agent.service, 'launch'), mock.patch.object(agent.service, 'launch'),
mock.patch('sys.argv'), mock.patch('sys.argv'),

View File

@ -13,12 +13,12 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import contextlib
import copy import copy
import mock import mock
from neutron_lbaas.agent import agent_api as api from neutron_lbaas.agent import agent_api as api
from neutron_lbaas.tests import base from neutron_lbaas.tests import base
from neutron_lbaas.tests import nested
class TestApiCache(base.BaseTestCase): class TestApiCache(base.BaseTestCase):
@ -37,7 +37,7 @@ class TestApiCache(base.BaseTestCase):
if method in add_host: if method in add_host:
expected_kwargs['host'] = self.api.host expected_kwargs['host'] = self.api.host
with contextlib.nested( with nested(
mock.patch.object(self.api.client, 'call'), mock.patch.object(self.api.client, 'call'),
mock.patch.object(self.api.client, 'prepare'), mock.patch.object(self.api.client, 'prepare'),
) as ( ) as (

View File

@ -1,3 +1,8 @@
# Use "$" in the regex to not run
# neutron_lbaas.tests.unit.agent.test_agent_manager which doesn't pass on
# Python 3 yet
neutron_lbaas.tests.unit.agent.test_agent$
neutron_lbaas.tests.unit.agent.test_agent_api
neutron_lbaas.tests.unit.common.cert_manager.barbican_auth.test_barbican_acl neutron_lbaas.tests.unit.common.cert_manager.barbican_auth.test_barbican_acl
neutron_lbaas.tests.unit.common.cert_manager.test_barbican neutron_lbaas.tests.unit.common.cert_manager.test_barbican
neutron_lbaas.tests.unit.common.cert_manager.test_local neutron_lbaas.tests.unit.common.cert_manager.test_local