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,
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)

View File

@ -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()

View File

@ -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):

View File

@ -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)