clean up ip_command interface
- In change I16b180a298e104f393b5f665409cdf4ba5bff203 an error was introduced i how _get_impl was defiend in os_vif/internal/command/ip/api.py. This change correct the error by returning a instance of the the PyRoute2 class instead of a reference to the enclosing module. - This change adds exists to the ip command interfece. - This change makes the windows version also inherit from the ip_command abstract base class. Closes-Bug: #1797182 Change-Id: I82377297db9e9ff7fad055dadb73864964f427d9
This commit is contained in:
parent
512e64d794
commit
409455d16c
|
@ -23,6 +23,6 @@ LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
def _get_impl():
|
def _get_impl():
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
return win_ip_lib
|
return win_ip_lib.Netifaces()
|
||||||
else:
|
else:
|
||||||
return linux_ip_lib
|
return linux_ip_lib.PyRoute2()
|
||||||
|
|
|
@ -59,3 +59,11 @@ class IpCommand(object):
|
||||||
:param dev_type: String network device type (TYPE_VETH, TYPE_VLAN)
|
:param dev_type: String network device type (TYPE_VETH, TYPE_VLAN)
|
||||||
:return: status of the command execution
|
:return: status of the command execution
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def exists(self, device):
|
||||||
|
"""Method to dectect if a device exists.
|
||||||
|
|
||||||
|
:param device: A network device (string)
|
||||||
|
:return: True if device exists else False
|
||||||
|
"""
|
||||||
|
|
|
@ -17,12 +17,14 @@ import netifaces
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
|
|
||||||
from os_vif import exception
|
from os_vif import exception
|
||||||
|
from os_vif.internal.command.ip import ip_command
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def exists(device):
|
class Netifaces(ip_command.IpCommand):
|
||||||
|
|
||||||
|
def exists(self, device):
|
||||||
"""Return True if the device exists in the namespace."""
|
"""Return True if the device exists in the namespace."""
|
||||||
try:
|
try:
|
||||||
return bool(netifaces.ifaddresses(device))
|
return bool(netifaces.ifaddresses(device))
|
||||||
|
@ -32,14 +34,13 @@ def exists(device):
|
||||||
LOG.error("Failed to get interface addresses: %s", device)
|
LOG.error("Failed to get interface addresses: %s", device)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def set(self, device, check_exit_code=None, state=None, mtu=None,
|
||||||
def set(*args):
|
address=None, promisc=None):
|
||||||
exception.NotImplementedForOS(function='ip.set', os='Windows')
|
exception.NotImplementedForOS(function='ip.set', os='Windows')
|
||||||
|
|
||||||
|
def add(self, device, dev_type, check_exit_code=None, peer=None, link=None,
|
||||||
def add(*args):
|
vlan_id=None):
|
||||||
exception.NotImplementedForOS(function='ip.add', os='Windows')
|
exception.NotImplementedForOS(function='ip.add', os='Windows')
|
||||||
|
|
||||||
|
def delete(self, device, check_exit_code=None):
|
||||||
def delete(*args):
|
|
||||||
exception.NotImplementedForOS(function='ip.delete', os='Windows')
|
exception.NotImplementedForOS(function='ip.delete', os='Windows')
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
# 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 mock
|
||||||
|
|
||||||
|
from os_vif.tests.unit import base
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
@mock.patch("os.name", "posix")
|
||||||
|
def test_get_impl_linux(self):
|
||||||
|
ip_lib = api._get_impl()
|
||||||
|
self.assertIsInstance(ip_lib, linux_ip_lib.PyRoute2)
|
|
@ -23,22 +23,23 @@ class TestIPDevice(base.TestCase):
|
||||||
super(TestIPDevice, self).setUp()
|
super(TestIPDevice, self).setUp()
|
||||||
self.device_name = 'test_device'
|
self.device_name = 'test_device'
|
||||||
self.mock_log = mock.patch.object(ip_lib, "LOG").start()
|
self.mock_log = mock.patch.object(ip_lib, "LOG").start()
|
||||||
|
self.ip_lib = ip_lib.Netifaces()
|
||||||
|
|
||||||
@mock.patch.object(netifaces, 'ifaddresses', return_value=True)
|
@mock.patch.object(netifaces, 'ifaddresses', return_value=True)
|
||||||
def test_exists(self, mock_ifaddresses):
|
def test_exists(self, mock_ifaddresses):
|
||||||
self.assertTrue(ip_lib.exists(self.device_name))
|
self.assertTrue(self.ip_lib.exists(self.device_name))
|
||||||
mock_ifaddresses.assert_called_once_with(self.device_name)
|
mock_ifaddresses.assert_called_once_with(self.device_name)
|
||||||
|
|
||||||
@mock.patch.object(netifaces, 'ifaddresses', side_effect=ValueError())
|
@mock.patch.object(netifaces, 'ifaddresses', side_effect=ValueError())
|
||||||
def test_exists_not_found(self, mock_ifaddresses):
|
def test_exists_not_found(self, mock_ifaddresses):
|
||||||
self.assertFalse(ip_lib.exists(self.device_name))
|
self.assertFalse(self.ip_lib.exists(self.device_name))
|
||||||
mock_ifaddresses.assert_called_once_with(self.device_name)
|
mock_ifaddresses.assert_called_once_with(self.device_name)
|
||||||
self.mock_log.warning.assert_called_once_with(
|
self.mock_log.warning.assert_called_once_with(
|
||||||
"The device does not exist on the system: %s", self.device_name)
|
"The device does not exist on the system: %s", self.device_name)
|
||||||
|
|
||||||
@mock.patch.object(netifaces, 'ifaddresses', side_effect=OSError())
|
@mock.patch.object(netifaces, 'ifaddresses', side_effect=OSError())
|
||||||
def test_exists_os_error_exception(self, mock_ifaddresses):
|
def test_exists_os_error_exception(self, mock_ifaddresses):
|
||||||
self.assertFalse(ip_lib.exists(self.device_name))
|
self.assertFalse(self.ip_lib.exists(self.device_name))
|
||||||
mock_ifaddresses.assert_called_once_with(self.device_name)
|
mock_ifaddresses.assert_called_once_with(self.device_name)
|
||||||
self.mock_log.error.assert_called_once_with(
|
self.mock_log.error.assert_called_once_with(
|
||||||
"Failed to get interface addresses: %s", self.device_name)
|
"Failed to get interface addresses: %s", self.device_name)
|
||||||
|
|
Loading…
Reference in New Issue