remove use of contextlib and with nested

- The use of contextlib and "with nested" statements are deprecated.
- This change remove the deprecation warning by refactoring all
  unit test that used "with neted" statements to create mocks via
  with the decorator syntax.
- This chage removes all depencies on contexlib.

trivial_fix

Change-Id: I6d929e59306f9289f717fff3fb79542e804396b6
This commit is contained in:
Sean Mooney 2016-10-06 05:36:38 +00:00
parent 4cdd962bc3
commit 54172168e2
5 changed files with 104 additions and 154 deletions

View File

@ -0,0 +1,6 @@
---
fixes:
- The use of contextlib and with nested statements is deprecated.
"with nested" statements are not python 3 compatible as with statement now directly
support context managers. The use of contextlib and "with nested" statements has
been removed from all unittests in favor of the @mock decorator syntax.

View File

@ -10,10 +10,8 @@
# 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
import os.path import os.path
import six
import testtools import testtools
import fixtures import fixtures
@ -28,14 +26,6 @@ from vif_plug_linux_bridge import privsep
CONF = cfg.CONF CONF = cfg.CONF
if six.PY2:
nested = contextlib.nested
else:
@contextlib.contextmanager
def nested(*contexts):
with contextlib.ExitStack() as stack:
yield [stack.enter_context(c) for c in contexts]
class LinuxNetTest(testtools.TestCase): class LinuxNetTest(testtools.TestCase):

View File

@ -10,9 +10,7 @@
# 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
import six
import testtools import testtools
from os_vif import objects from os_vif import objects
@ -21,15 +19,6 @@ from vif_plug_linux_bridge import linux_bridge
from vif_plug_linux_bridge import linux_net from vif_plug_linux_bridge import linux_net
if six.PY2:
nested = contextlib.nested
else:
@contextlib.contextmanager
def nested(*contexts):
with contextlib.ExitStack() as stack:
yield [stack.enter_context(c) for c in contexts]
class PluginTest(testtools.TestCase): class PluginTest(testtools.TestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -41,7 +30,10 @@ class PluginTest(testtools.TestCase):
name='demo', name='demo',
uuid='f0000000-0000-0000-0000-000000000001') uuid='f0000000-0000-0000-0000-000000000001')
def test_plug_bridge(self): @mock.patch.object(linux_net, 'ensure_vlan_bridge')
@mock.patch.object(linux_net, 'ensure_bridge')
def test_plug_bridge(self, mock_ensure_bridge,
mock_ensure_vlan_bridge):
network = objects.network.Network( network = objects.network.Network(
id='437c6db5-4e6f-4b43-b64b-ed6a11ee5ba7', id='437c6db5-4e6f-4b43-b64b-ed6a11ee5ba7',
bridge='br0') bridge='br0')
@ -53,17 +45,16 @@ class PluginTest(testtools.TestCase):
dev_name='tap-xxx-yyy-zzz', dev_name='tap-xxx-yyy-zzz',
bridge_name="br0") bridge_name="br0")
with nested(
mock.patch.object(linux_net, 'ensure_bridge'),
mock.patch.object(linux_net, 'ensure_vlan_bridge')
) as (mock_ensure_bridge, mock_ensure_vlan_bridge):
plugin = linux_bridge.LinuxBridgePlugin.load("linux_bridge") plugin = linux_bridge.LinuxBridgePlugin.load("linux_bridge")
plugin.plug(vif, self.instance) plugin.plug(vif, self.instance)
self.assertEqual(len(mock_ensure_bridge.calls), 0) self.assertEqual(len(mock_ensure_bridge.calls), 0)
self.assertEqual(len(mock_ensure_vlan_bridge.calls), 0) self.assertEqual(len(mock_ensure_vlan_bridge.calls), 0)
def test_plug_bridge_create_br(self): @mock.patch.object(linux_net, 'ensure_vlan_bridge')
@mock.patch.object(linux_net, 'ensure_bridge')
def test_plug_bridge_create_br(self, mock_ensure_bridge,
mock_ensure_vlan_bridge):
network = objects.network.Network( network = objects.network.Network(
id='437c6db5-4e6f-4b43-b64b-ed6a11ee5ba7', id='437c6db5-4e6f-4b43-b64b-ed6a11ee5ba7',
bridge='br0', bridge='br0',
@ -77,10 +68,6 @@ class PluginTest(testtools.TestCase):
dev_name='tap-xxx-yyy-zzz', dev_name='tap-xxx-yyy-zzz',
bridge_name="br0") bridge_name="br0")
with nested(
mock.patch.object(linux_net, 'ensure_bridge'),
mock.patch.object(linux_net, 'ensure_vlan_bridge')
) as (mock_ensure_bridge, mock_ensure_vlan_bridge):
plugin = linux_bridge.LinuxBridgePlugin.load("linux_bridge") plugin = linux_bridge.LinuxBridgePlugin.load("linux_bridge")
plugin.plug(vif, self.instance) plugin.plug(vif, self.instance)
@ -93,7 +80,11 @@ class PluginTest(testtools.TestCase):
def test_plug_bridge_create_br_vlan_mtu_from_config(self): def test_plug_bridge_create_br_vlan_mtu_from_config(self):
self._test_plug_bridge_create_br_vlan() self._test_plug_bridge_create_br_vlan()
def _test_plug_bridge_create_br_vlan(self, mtu=None): @mock.patch.object(linux_net, 'ensure_vlan_bridge')
@mock.patch.object(linux_net, 'ensure_bridge')
def _test_plug_bridge_create_br_vlan(self, mock_ensure_bridge,
mock_ensure_vlan_bridge,
mtu=None):
network = objects.network.Network( network = objects.network.Network(
id='437c6db5-4e6f-4b43-b64b-ed6a11ee5ba7', id='437c6db5-4e6f-4b43-b64b-ed6a11ee5ba7',
bridge='br0', bridge='br0',
@ -110,10 +101,6 @@ class PluginTest(testtools.TestCase):
dev_name='tap-xxx-yyy-zzz', dev_name='tap-xxx-yyy-zzz',
bridge_name="br0") bridge_name="br0")
with nested(
mock.patch.object(linux_net, 'ensure_bridge'),
mock.patch.object(linux_net, 'ensure_vlan_bridge')
) as (mock_ensure_bridge, mock_ensure_vlan_bridge):
plugin = linux_bridge.LinuxBridgePlugin.load("linux_bridge") plugin = linux_bridge.LinuxBridgePlugin.load("linux_bridge")
plugin.plug(vif, self.instance) plugin.plug(vif, self.instance)

View File

@ -10,10 +10,8 @@
# 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
import os.path import os.path
import six
import testtools import testtools
from oslo_concurrency import processutils from oslo_concurrency import processutils
@ -22,14 +20,6 @@ from vif_plug_ovs import constants
from vif_plug_ovs import linux_net from vif_plug_ovs import linux_net
from vif_plug_ovs import privsep from vif_plug_ovs import privsep
if six.PY2:
nested = contextlib.nested
else:
@contextlib.contextmanager
def nested(*contexts):
with contextlib.ExitStack() as stack:
yield [stack.enter_context(c) for c in contexts]
class LinuxNetTest(testtools.TestCase): class LinuxNetTest(testtools.TestCase):

View File

@ -10,9 +10,7 @@
# 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
import six
import testtools import testtools
from os_vif import objects from os_vif import objects
@ -22,15 +20,6 @@ from vif_plug_ovs import linux_net
from vif_plug_ovs import ovs from vif_plug_ovs import ovs
if six.PY2:
nested = contextlib.nested
else:
@contextlib.contextmanager
def nested(*contexts):
with contextlib.ExitStack() as stack:
yield [stack.enter_context(c) for c in contexts]
class PluginTest(testtools.TestCase): class PluginTest(testtools.TestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -124,11 +113,9 @@ class PluginTest(testtools.TestCase):
timeout=plugin.config.ovs_vsctl_timeout, timeout=plugin.config.ovs_vsctl_timeout,
interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE) interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE)
def test_plug_ovs(self): @mock.patch.object(ovs, 'sys')
with nested( @mock.patch.object(linux_net, 'ensure_ovs_bridge')
mock.patch.object(ovs, 'sys'), def test_plug_ovs(self, ensure_ovs_bridge, mock_sys):
mock.patch.object(linux_net, 'ensure_ovs_bridge')
) as (mock_sys, ensure_ovs_bridge):
mock_sys.platform = 'linux' mock_sys.platform = 'linux'
plug_bridge_mock = mock.Mock() plug_bridge_mock = mock.Mock()
plugin = ovs.OvsPlugin.load("ovs") plugin = ovs.OvsPlugin.load("ovs")
@ -138,7 +125,17 @@ class PluginTest(testtools.TestCase):
ensure_ovs_bridge.assert_called_once_with( ensure_ovs_bridge.assert_called_once_with(
self.vif_ovs.network.bridge, constants.OVS_DATAPATH_SYSTEM) self.vif_ovs.network.bridge, constants.OVS_DATAPATH_SYSTEM)
def test_plug_ovs_bridge(self): @mock.patch.object(linux_net, 'ensure_ovs_bridge')
@mock.patch.object(ovs.OvsPlugin, '_create_vif_port')
@mock.patch.object(linux_net, 'add_bridge_port')
@mock.patch.object(linux_net, 'create_veth_pair')
@mock.patch.object(linux_net, 'device_exists', return_value=False)
@mock.patch.object(linux_net, 'ensure_bridge')
@mock.patch.object(ovs, 'sys')
def test_plug_ovs_bridge(self, mock_sys, ensure_bridge,
device_exists, create_veth_pair,
add_bridge_port, _create_vif_port,
ensure_ovs_bridge):
calls = { calls = {
'device_exists': [mock.call('qvob679325f-ca')], 'device_exists': [mock.call('qvob679325f-ca')],
'create_veth_pair': [mock.call('qvbb679325f-ca', 'create_veth_pair': [mock.call('qvbb679325f-ca',
@ -154,17 +151,6 @@ class PluginTest(testtools.TestCase):
constants.OVS_DATAPATH_SYSTEM)] constants.OVS_DATAPATH_SYSTEM)]
} }
with nested(
mock.patch.object(ovs, 'sys'),
mock.patch.object(linux_net, 'ensure_bridge'),
mock.patch.object(linux_net, 'device_exists',
return_value=False),
mock.patch.object(linux_net, 'create_veth_pair'),
mock.patch.object(linux_net, 'add_bridge_port'),
mock.patch.object(ovs.OvsPlugin, '_create_vif_port'),
mock.patch.object(linux_net, 'ensure_ovs_bridge')
) as (mock_sys, ensure_bridge, device_exists, create_veth_pair,
add_bridge_port, _create_vif_port, ensure_ovs_bridge):
mock_sys.platform = 'linux' mock_sys.platform = 'linux'
plugin = ovs.OvsPlugin.load("ovs") plugin = ovs.OvsPlugin.load("ovs")
plugin.plug(self.vif_ovs_hybrid, self.instance) plugin.plug(self.vif_ovs_hybrid, self.instance)
@ -175,7 +161,12 @@ class PluginTest(testtools.TestCase):
_create_vif_port.assert_has_calls(calls['_create_vif_port']) _create_vif_port.assert_has_calls(calls['_create_vif_port'])
ensure_ovs_bridge.assert_has_calls(calls['ensure_ovs_bridge']) ensure_ovs_bridge.assert_has_calls(calls['ensure_ovs_bridge'])
def _check_plug_ovs_windows(self, vif): @mock.patch.object(linux_net, 'ensure_ovs_bridge')
@mock.patch.object(ovs.OvsPlugin, '_create_vif_port')
@mock.patch.object(linux_net, 'device_exists', return_value=False)
@mock.patch.object(ovs, 'sys')
def _check_plug_ovs_windows(self, vif, mock_sys, device_exists,
_create_vif_port, ensure_ovs_bridge):
calls = { calls = {
'device_exists': [mock.call(vif.id)], 'device_exists': [mock.call(vif.id)],
'_create_vif_port': [mock.call(vif, vif.id, self.instance)], '_create_vif_port': [mock.call(vif, vif.id, self.instance)],
@ -183,13 +174,6 @@ class PluginTest(testtools.TestCase):
constants.OVS_DATAPATH_SYSTEM)] constants.OVS_DATAPATH_SYSTEM)]
} }
with nested(
mock.patch.object(ovs, 'sys'),
mock.patch.object(linux_net, 'device_exists',
return_value=False),
mock.patch.object(ovs.OvsPlugin, '_create_vif_port'),
mock.patch.object(linux_net, 'ensure_ovs_bridge')
) as (mock_sys, device_exists, _create_vif_port, ensure_ovs_bridge):
mock_sys.platform = constants.PLATFORM_WIN32 mock_sys.platform = constants.PLATFORM_WIN32
plugin = ovs.OvsPlugin.load("ovs") plugin = ovs.OvsPlugin.load("ovs")
plugin.plug(vif, self.instance) plugin.plug(vif, self.instance)
@ -210,33 +194,29 @@ class PluginTest(testtools.TestCase):
plugin.unplug(self.vif_ovs, self.instance) plugin.unplug(self.vif_ovs, self.instance)
self.assertFalse(unplug_bridge_mock.called) self.assertFalse(unplug_bridge_mock.called)
def test_unplug_ovs_bridge(self): @mock.patch.object(linux_net, 'delete_ovs_vif_port')
@mock.patch.object(linux_net, 'delete_bridge')
@mock.patch.object(ovs, 'sys')
def test_unplug_ovs_bridge(self, mock_sys, delete_bridge,
delete_ovs_vif_port):
calls = { calls = {
'delete_bridge': [mock.call('qbrvif-xxx-yyy', 'qvbb679325f-ca')], 'delete_bridge': [mock.call('qbrvif-xxx-yyy', 'qvbb679325f-ca')],
'delete_ovs_vif_port': [mock.call('br0', 'qvob679325f-ca', 'delete_ovs_vif_port': [mock.call('br0', 'qvob679325f-ca',
timeout=120)] timeout=120)]
} }
with nested(
mock.patch.object(ovs, 'sys'),
mock.patch.object(linux_net, 'delete_bridge'),
mock.patch.object(linux_net, 'delete_ovs_vif_port')
) as (mock_sys, delete_bridge, delete_ovs_vif_port):
mock_sys.platform = 'linux' mock_sys.platform = 'linux'
plugin = ovs.OvsPlugin.load("ovs") plugin = ovs.OvsPlugin.load("ovs")
plugin.unplug(self.vif_ovs_hybrid, self.instance) plugin.unplug(self.vif_ovs_hybrid, self.instance)
delete_bridge.assert_has_calls(calls['delete_bridge']) delete_bridge.assert_has_calls(calls['delete_bridge'])
delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port']) delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port'])
def _check_unplug_ovs_windows(self, vif): @mock.patch.object(linux_net, 'delete_ovs_vif_port')
with nested( @mock.patch.object(ovs, 'sys')
mock.patch.object(ovs, 'sys'), def _check_unplug_ovs_windows(self, vif, mock_sys, delete_ovs_vif_port):
mock.patch.object(linux_net, 'delete_ovs_vif_port')
) as (mock_sys, delete_ovs_vif_port):
mock_sys.platform = constants.PLATFORM_WIN32 mock_sys.platform = constants.PLATFORM_WIN32
plugin = ovs.OvsPlugin.load("ovs") plugin = ovs.OvsPlugin.load("ovs")
plugin.unplug(vif, self.instance) plugin.unplug(vif, self.instance)
delete_ovs_vif_port.assert_called_once_with( delete_ovs_vif_port.assert_called_once_with('br0', vif.id, timeout=120)
'br0', vif.id, timeout=120)
def test_unplug_ovs_windows(self): def test_unplug_ovs_windows(self):
self._check_unplug_ovs_windows(self.vif_ovs) self._check_unplug_ovs_windows(self.vif_ovs)
@ -244,7 +224,9 @@ class PluginTest(testtools.TestCase):
def test_unplug_ovs_bridge_windows(self): def test_unplug_ovs_bridge_windows(self):
self._check_unplug_ovs_windows(self.vif_ovs_hybrid) self._check_unplug_ovs_windows(self.vif_ovs_hybrid)
def test_plug_ovs_vhostuser(self): @mock.patch.object(linux_net, 'ensure_ovs_bridge')
@mock.patch.object(ovs.OvsPlugin, '_create_vif_port')
def test_plug_ovs_vhostuser(self, _create_vif_port, ensure_ovs_bridge):
calls = { calls = {
'_create_vif_port': [mock.call( '_create_vif_port': [mock.call(
@ -255,22 +237,17 @@ class PluginTest(testtools.TestCase):
constants.OVS_DATAPATH_NETDEV)] constants.OVS_DATAPATH_NETDEV)]
} }
with nested(
mock.patch.object(ovs.OvsPlugin, '_create_vif_port'),
mock.patch.object(linux_net, 'ensure_ovs_bridge')
) as (_create_vif_port, ensure_ovs_bridge):
plugin = ovs.OvsPlugin.load("ovs") plugin = ovs.OvsPlugin.load("ovs")
plugin.plug(self.vif_vhostuser, self.instance) plugin.plug(self.vif_vhostuser, self.instance)
_create_vif_port.assert_has_calls(calls['_create_vif_port']) _create_vif_port.assert_has_calls(calls['_create_vif_port'])
ensure_ovs_bridge.assert_has_calls(calls['ensure_ovs_bridge']) ensure_ovs_bridge.assert_has_calls(calls['ensure_ovs_bridge'])
def test_unplug_ovs_vhostuser(self): @mock.patch.object(linux_net, 'delete_ovs_vif_port')
def test_unplug_ovs_vhostuser(self, delete_ovs_vif_port):
calls = { calls = {
'delete_ovs_vif_port': [mock.call('br0', 'vhub679325f-ca', 'delete_ovs_vif_port': [mock.call('br0', 'vhub679325f-ca',
timeout=120)] timeout=120)]
} }
with mock.patch.object(linux_net, 'delete_ovs_vif_port') \
as delete_ovs_vif_port:
plugin = ovs.OvsPlugin.load("ovs") plugin = ovs.OvsPlugin.load("ovs")
plugin.unplug(self.vif_vhostuser, self.instance) plugin.unplug(self.vif_vhostuser, self.instance)
delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port']) delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port'])