Import IP implementation modules outside privsep context

If the IP module is loaded inside the privsep context, the following
error will occur:
  return getattr(sys.modules[mod_str], class_str)
  "AttributeError: 'module' object has no attribute 'PyRoute2'

Change-Id: I1852038b8d40d4aa52c4f8e661bc78e54e0fc966
Closes-Bug: #1812090
This commit is contained in:
Rodolfo Alonso Hernandez 2019-01-07 18:20:28 +00:00
parent bf8ad81638
commit 843d84c819
4 changed files with 35 additions and 32 deletions

View File

@ -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, def set(device, check_exit_code=None, state=None, mtu=None, address=None,
promisc=None): promisc=None):
"""Method to set a parameter in an interface.""" """Method to set a parameter in an interface."""
return api._get_impl().set(device, check_exit_code=check_exit_code, return api.ip.set(device, check_exit_code=check_exit_code, state=state,
state=state, mtu=mtu, address=address, mtu=mtu, address=address, promisc=promisc)
promisc=promisc)
def add(device, dev_type, check_exit_code=None, peer=None, link=None, def add(device, dev_type, check_exit_code=None, peer=None, link=None,
vlan_id=None): vlan_id=None):
"""Method to add an interface.""" """Method to add an interface."""
return api._get_impl().add(device, dev_type, return api.ip.add(device, dev_type, check_exit_code=check_exit_code,
check_exit_code=check_exit_code, peer=peer, peer=peer, link=link, vlan_id=vlan_id)
link=link, vlan_id=vlan_id)
def delete(device, check_exit_code=None): def delete(device, check_exit_code=None):
"""Method to delete an interface.""" """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): def exists(device):
"""Method to check if an interface exists.""" """Method to check if an interface exists."""
return api._get_impl().exists(device) return api.ip.exists(device)

View File

@ -13,16 +13,15 @@
import os import os
from oslo_log import log as logging 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__) LOG = logging.getLogger(__name__)
ip = ip_lib_class()
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)

View File

@ -15,7 +15,7 @@ import re
from oslo_concurrency import processutils from oslo_concurrency import processutils
from oslo_utils import excutils 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 base
from os_vif.tests.functional import privsep from os_vif.tests.functional import privsep
@ -96,17 +96,17 @@ class ShellIpCommands(object):
@privsep.os_vif_pctxt.entrypoint @privsep.os_vif_pctxt.entrypoint
def _ip_cmd_set(*args, **kwargs): def _ip_cmd_set(*args, **kwargs):
impl_pyroute2.PyRoute2().set(*args, **kwargs) ip_lib.set(*args, **kwargs)
@privsep.os_vif_pctxt.entrypoint @privsep.os_vif_pctxt.entrypoint
def _ip_cmd_add(*args, **kwargs): def _ip_cmd_add(*args, **kwargs):
impl_pyroute2.PyRoute2().add(*args, **kwargs) ip_lib.add(*args, **kwargs)
@privsep.os_vif_pctxt.entrypoint @privsep.os_vif_pctxt.entrypoint
def _ip_cmd_delete(*args, **kwargs): def _ip_cmd_delete(*args, **kwargs):
impl_pyroute2.PyRoute2().delete(*args, **kwargs) ip_lib.delete(*args, **kwargs)
class TestIpCommand(ShellIpCommands, base.BaseFunctionalTestCase): class TestIpCommand(ShellIpCommands, base.BaseFunctionalTestCase):

View File

@ -11,22 +11,28 @@
# under the License. # under the License.
import mock import mock
from six import moves
from os_vif.tests.unit import base
from os_vif.internal.command.ip import api 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.tests.unit import base
from os_vif.internal.command.ip.windows import impl_netifaces as win_ip_lib
class TestIpApi(base.TestCase): class TestIpApi(base.TestCase):
@mock.patch("os.name", "nt") @staticmethod
def test_get_impl_windows(self): def _reload_original_os_module():
ip_lib = api._get_impl() moves.reload_module(api)
self.assertIsInstance(ip_lib, win_ip_lib.Netifaces)
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): def test_get_impl_linux(self):
ip_lib = api._get_impl() self.addCleanup(self._reload_original_os_module)
self.assertIsInstance(ip_lib, linux_ip_lib.PyRoute2) 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)