# Path: os_vif/exception.py
# class ExceptionBase(Exception):
# class LibraryNotInitialized(ExceptionBase):
# class NoMatchingPlugin(ExceptionBase):
# class NoMatchingPortProfileClass(ExceptionBase):
# class NoSupportedPortProfileVersion(ExceptionBase):
# class NoMatchingVIFClass(ExceptionBase):
# class NoSupportedVIFVersion(ExceptionBase):
# class PlugException(ExceptionBase):
# class UnplugException(ExceptionBase):
# class NetworkMissingPhysicalNetwork(ExceptionBase):
# class NetworkInterfaceNotFound(ExceptionBase):
# class NetworkInterfaceTypeNotDefined(ExceptionBase):
# class ExternalImport(ExceptionBase):
# class NotImplementedForOS(ExceptionBase):
#     def __init__(self, message=None, **kwargs):
#     def format_message(self):
# 
# Path: os_vif/internal/ip/linux/impl_pyroute2.py
# LOG = logging.getLogger(__name__)
# class PyRoute2(ip_command.IpCommand):
#     def _ip_link(self, ip, command, check_exit_code, **kwargs):
#     def set(self, device, check_exit_code=None, state=None, mtu=None,
#             address=None, promisc=None, master=None):
#     def lookup_interface(self, ip, link):
#     def add(self, device, dev_type, check_exit_code=None, peer=None, link=None,
#             vlan_id=None, ageing=None):
#     def delete(self, device, check_exit_code=None):
#     def exists(self, device):
# 
# Path: os_vif/tests/unit/base.py
# class TestCase(testtools.TestCase):


from unittest import mock
from pyroute2 import iproute
from pyroute2.netlink import exceptions as ipexc
from pyroute2.netlink.rtnl import ifinfmsg
from os_vif import exception
from os_vif.internal.ip.linux import impl_pyroute2
from os_vif.tests.unit import base

    def test_add_other_type(self):
        self.assertRaises(exception.NetworkInterfaceTypeNotDefined,
                          self.ip.add, self.DEVICE, 'type_not_defined')

    def test_add_exit_code(self):
        self.ip_link.side_effect = ipexc.NetlinkError(self.ERROR_CODE,
                                                      msg="Error message")

        self.ip.add(self.DEVICE, self.TYPE_VETH, peer='peer',
                    check_exit_code=[self.ERROR_CODE])
        self.ip_link.assert_called_once_with(
            'add', ifname=self.DEVICE, kind=self.TYPE_VETH, peer='peer')

        self.assertRaises(
            exception.NetworkInterfaceNotFound,
            self.ip.add, self.DEVICE,
            self.TYPE_VLAN, peer='peer',
            check_exit_code=[self.OTHER_ERROR_CODE])

    def test_delete(self):
        with mock.patch.object(iproute.IPRoute, 'link_lookup',
                               return_value=[1]) as mock_link_lookup:
            self.ip.delete(self.DEVICE)
            mock_link_lookup.assert_called_once_with(ifname=self.DEVICE)
            self.ip_link.assert_called_once_with('del', index=1)

    def test_delete_no_interface_found(self):
        with mock.patch.object(iproute.IPRoute, 'link_lookup',
                               return_value=[]) as mock_link_lookup:

# Path: os_vif/exception.py
# class ExceptionBase(Exception):
# class LibraryNotInitialized(ExceptionBase):
# class NoMatchingPlugin(ExceptionBase):
# class NoMatchingPortProfileClass(ExceptionBase):
# class NoSupportedPortProfileVersion(ExceptionBase):
# class NoMatchingVIFClass(ExceptionBase):
# class NoSupportedVIFVersion(ExceptionBase):
# class PlugException(ExceptionBase):
# class UnplugException(ExceptionBase):
# class NetworkMissingPhysicalNetwork(ExceptionBase):
# class NetworkInterfaceNotFound(ExceptionBase):
# class NetworkInterfaceTypeNotDefined(ExceptionBase):
# class ExternalImport(ExceptionBase):
# class NotImplementedForOS(ExceptionBase):
#     def __init__(self, message=None, **kwargs):
#     def format_message(self):
# 
# Path: os_vif/internal/ip/linux/impl_pyroute2.py
# LOG = logging.getLogger(__name__)
# class PyRoute2(ip_command.IpCommand):
#     def _ip_link(self, ip, command, check_exit_code, **kwargs):
#     def set(self, device, check_exit_code=None, state=None, mtu=None,
#             address=None, promisc=None, master=None):
#     def lookup_interface(self, ip, link):
#     def add(self, device, dev_type, check_exit_code=None, peer=None, link=None,
#             vlan_id=None, ageing=None):
#     def delete(self, device, check_exit_code=None):
#     def exists(self, device):
# 
# Path: os_vif/tests/unit/base.py
# class TestCase(testtools.TestCase):


# Path: os_vif/tests/unit/internal/ip/linux/test_impl_pyroute2.py
from unittest import mock
from pyroute2 import iproute
from pyroute2.netlink import exceptions as ipexc
from pyroute2.netlink.rtnl import ifinfmsg
from os_vif import exception
from os_vif.internal.ip.linux import impl_pyroute2
from os_vif.tests.unit import base


    def test_add_other_type(self):
        self.assertRaises(exception.NetworkInterfaceTypeNotDefined,
                          self.ip.add, self.DEVICE, 'type_not_defined')

    def test_add_exit_code(self):
        self.ip_link.side_effect = ipexc.NetlinkError(self.ERROR_CODE,
                                                      msg="Error message")

        self.ip.add(self.DEVICE, self.TYPE_VETH, peer='peer',
                    check_exit_code=[self.ERROR_CODE])
        self.ip_link.assert_called_once_with(
            'add', ifname=self.DEVICE, kind=self.TYPE_VETH, peer='peer')

        self.assertRaises(
            exception.NetworkInterfaceNotFound,
            self.ip.add, self.DEVICE,
            self.TYPE_VLAN, peer='peer',
            check_exit_code=[self.OTHER_ERROR_CODE])

    def test_delete(self):
        with mock.patch.object(iproute.IPRoute, 'link_lookup',
                               return_value=[1]) as mock_link_lookup:
            self.ip.delete(self.DEVICE)
            mock_link_lookup.assert_called_once_with(ifname=self.DEVICE)
            self.ip_link.assert_called_once_with('del', index=1)

    def test_delete_no_interface_found(self):
        with mock.patch.object(iproute.IPRoute, 'link_lookup',
                               return_value=[]) as mock_link_lookup:

            self.assertRaises(exception.NetworkInterfaceNotFound,