diff --git a/os_vif/internal/command/ip/__init__.py b/os_vif/internal/command/ip/__init__.py index ad0339d9..c9d6fc44 100644 --- a/os_vif/internal/command/ip/__init__.py +++ b/os_vif/internal/command/ip/__init__.py @@ -16,24 +16,22 @@ from os_vif.internal.command.ip import api def set(device, check_exit_code=None, state=None, mtu=None, address=None, promisc=None): """Method to set a parameter in an interface.""" - return api._get_impl().set(device, check_exit_code=check_exit_code, - state=state, mtu=mtu, address=address, - promisc=promisc) + return api.ip.set(device, check_exit_code=check_exit_code, state=state, + mtu=mtu, address=address, promisc=promisc) def add(device, dev_type, check_exit_code=None, peer=None, link=None, vlan_id=None): """Method to add an interface.""" - return api._get_impl().add(device, dev_type, - check_exit_code=check_exit_code, peer=peer, - link=link, vlan_id=vlan_id) + return api.ip.add(device, dev_type, check_exit_code=check_exit_code, + peer=peer, link=link, vlan_id=vlan_id) def delete(device, check_exit_code=None): """Method to delete an interface.""" - return api._get_impl().delete(device, check_exit_code=check_exit_code) + return api.ip.delete(device, check_exit_code=check_exit_code) def exists(device): """Method to check if an interface exists.""" - return api._get_impl().exists(device) + return api.ip.exists(device) diff --git a/os_vif/internal/command/ip/api.py b/os_vif/internal/command/ip/api.py index f65b0898..cfb42c0d 100644 --- a/os_vif/internal/command/ip/api.py +++ b/os_vif/internal/command/ip/api.py @@ -13,16 +13,15 @@ import os from oslo_log import log as logging -from oslo_utils import importutils + +if os.name == 'nt': + from os_vif.internal.command.ip.windows.impl_netifaces import \ + Netifaces as ip_lib_class +else: + from os_vif.internal.command.ip.linux.impl_pyroute2 import \ + PyRoute2 as ip_lib_class LOG = logging.getLogger(__name__) - -def _get_impl(): - if os.name == 'nt': - impl = 'os_vif.internal.command.ip.windows.impl_netifaces.Netifaces' - else: - impl = 'os_vif.internal.command.ip.linux.impl_pyroute2.PyRoute2' - - return importutils.import_object(impl) +ip = ip_lib_class() diff --git a/os_vif/tests/functional/internal/command/ip/test_impl_pyroute2.py b/os_vif/tests/functional/internal/command/ip/test_impl_pyroute2.py index 8f59f679..373816d1 100644 --- a/os_vif/tests/functional/internal/command/ip/test_impl_pyroute2.py +++ b/os_vif/tests/functional/internal/command/ip/test_impl_pyroute2.py @@ -15,7 +15,7 @@ import re from oslo_concurrency import processutils from oslo_utils import excutils -from os_vif.internal.command.ip.linux import impl_pyroute2 +from os_vif.internal.command import ip as ip_lib from os_vif.tests.functional import base from os_vif.tests.functional import privsep @@ -96,17 +96,17 @@ class ShellIpCommands(object): @privsep.os_vif_pctxt.entrypoint def _ip_cmd_set(*args, **kwargs): - impl_pyroute2.PyRoute2().set(*args, **kwargs) + ip_lib.set(*args, **kwargs) @privsep.os_vif_pctxt.entrypoint def _ip_cmd_add(*args, **kwargs): - impl_pyroute2.PyRoute2().add(*args, **kwargs) + ip_lib.add(*args, **kwargs) @privsep.os_vif_pctxt.entrypoint def _ip_cmd_delete(*args, **kwargs): - impl_pyroute2.PyRoute2().delete(*args, **kwargs) + ip_lib.delete(*args, **kwargs) class TestIpCommand(ShellIpCommands, base.BaseFunctionalTestCase): diff --git a/os_vif/tests/unit/internal/command/ip/test_api.py b/os_vif/tests/unit/internal/command/ip/test_api.py index cabaa688..434e58e1 100644 --- a/os_vif/tests/unit/internal/command/ip/test_api.py +++ b/os_vif/tests/unit/internal/command/ip/test_api.py @@ -11,22 +11,28 @@ # under the License. import mock - -from os_vif.tests.unit import base +from six import moves from os_vif.internal.command.ip import api -from os_vif.internal.command.ip.linux import impl_pyroute2 as linux_ip_lib -from os_vif.internal.command.ip.windows import impl_netifaces as win_ip_lib +from os_vif.tests.unit import base class TestIpApi(base.TestCase): - @mock.patch("os.name", "nt") - def test_get_impl_windows(self): - ip_lib = api._get_impl() - self.assertIsInstance(ip_lib, win_ip_lib.Netifaces) + @staticmethod + def _reload_original_os_module(): + moves.reload_module(api) + + def test_get_impl_windows(self): + self.addCleanup(self._reload_original_os_module) + with mock.patch('os.name', 'nt'): + moves.reload_module(api) + from os_vif.internal.command.ip.windows import impl_netifaces + self.assertIsInstance(api.ip, impl_netifaces.Netifaces) - @mock.patch("os.name", "posix") def test_get_impl_linux(self): - ip_lib = api._get_impl() - self.assertIsInstance(ip_lib, linux_ip_lib.PyRoute2) + self.addCleanup(self._reload_original_os_module) + with mock.patch('os.name', 'posix'): + moves.reload_module(api) + from os_vif.internal.command.ip.linux import impl_pyroute2 + self.assertIsInstance(api.ip, impl_pyroute2.PyRoute2)