Ejemplo n.º 1
0
 def test_add_veth(self):
     ip_lib.IPWrapper().add_veth('tap0', 'tap1')
     self.execute.assert_called_once_with(
         '',
         'link', ('add', 'tap0', 'type', 'veth', 'peer', 'name', 'tap1'),
         None,
         as_root=True)
Ejemplo n.º 2
0
 def test_add_tuntap(self):
     ip_lib.IPWrapper().add_tuntap('tap0')
     self.execute.assert_called_once_with('',
                                          'tuntap',
                                          ('add', 'tap0', 'mode', 'tap'),
                                          None,
                                          as_root=True)
Ejemplo n.º 3
0
    def plug(self,
             device_name,
             port_id,
             mac_address,
             bridge=None,
             namespace=None,
             prefix=None):
        """Plug in the interface."""
        if not bridge:
            bridge = self.conf.ovs_integration_bridge

        self.check_bridge_exists(bridge)
        ip = ip_lib.IPWrapper()
        ns_dev = ip.device(device_name)

        if not ip_lib.device_exists(device_name, namespace=namespace):

            tap_name = self._get_tap_name(device_name)
            self._ovs_add_port(bridge, tap_name, port_id, mac_address)
            ns_dev.link.set_address(mac_address)

            # Add an interface created by ovs to the namespace.
            if namespace:
                namespace_obj = ip.ensure_namespace(namespace)
                namespace_obj.add_device_to_namespace(ns_dev)

        else:
            LOG.warn(_("Device %s already exists"), device_name)
        ns_dev.link.set_up()
Ejemplo n.º 4
0
    def test_namespace_is_empty(self):
        ip = ip_lib.IPWrapper('ns')
        with mock.patch.object(ip, 'get_devices') as get_devices:
            get_devices.return_value = [mock.Mock()]

            self.assertFalse(ip.namespace_is_empty())
            get_devices.assert_called_once_with(exclude_loopback=True)
Ejemplo n.º 5
0
    def plug(self,
             device_name,
             port_id,
             mac_address,
             bridge=None,
             namespace=None,
             prefix=None):
        """Plugin the interface."""
        ip = ip_lib.IPWrapper()
        if prefix:
            tap_name = device_name.replace(prefix, 'tap')
        else:
            tap_name = device_name.replace(self.DEV_NAME_PREFIX, 'tap')

        if not ip_lib.device_exists(device_name, namespace=namespace):
            # Create ns_veth in a namespace if one is configured.
            root_veth, ns_veth = ip.add_veth(tap_name,
                                             device_name,
                                             namespace2=namespace)
            ns_veth.link.set_address(mac_address)

        else:
            ns_veth = ip.device(device_name)
            root_veth = ip.device(tap_name)
            LOG.warn(_("Device %s already exists"), device_name)

        root_veth.link.set_up()
        ns_veth.link.set_up()
Ejemplo n.º 6
0
 def _remove_outdated_interfaces(self, device):
     """Finds and removes unused network device."""
     device_cidr_set = self._get_set_of_device_cidrs(device)
     for dev in ip_lib.IPWrapper().get_devices():
         if dev.name != device.name and dev.name[:3] == device.name[:3]:
             cidr_set = self._get_set_of_device_cidrs(dev)
             if device_cidr_set & cidr_set:
                 self.vif_driver.unplug(dev.name)
Ejemplo n.º 7
0
 def test_garbage_collect_namespace_does_not_exist(self):
     with mock.patch.object(ip_lib, 'IpNetnsCommand') as ip_ns_cmd_cls:
         ip_ns_cmd_cls.return_value.exists.return_value = False
         ip = ip_lib.IPWrapper('ns')
         with mock.patch.object(ip, 'namespace_is_empty') as mock_is_empty:
             self.assertFalse(ip.garbage_collect_namespace())
             ip_ns_cmd_cls.assert_has_calls([mock.call().exists('ns')])
             self.assertNotIn(mock.call().delete('ns'),
                              ip_ns_cmd_cls.return_value.mock_calls)
             self.assertEqual(mock_is_empty.mock_calls, [])
Ejemplo n.º 8
0
 def test_add_veth_with_namespaces(self):
     ns2 = 'ns2'
     with mock.patch.object(ip_lib.IPWrapper, 'ensure_namespace') as en:
         ip_lib.IPWrapper().add_veth('tap0', 'tap1', namespace2=ns2)
         en.assert_has_calls([mock.call(ns2)])
     self.execute.assert_called_once_with('', 'link',
                                          ('add', 'tap0', 'type', 'veth',
                                           'peer', 'name', 'tap1',
                                           'netns', ns2),
                                          None, as_root=True)
Ejemplo n.º 9
0
    def test_get_devices_malformed_line(self):
        self.execute.return_value = '\n'.join(LINK_SAMPLE + ['gibberish'])
        retval = ip_lib.IPWrapper().get_devices()
        self.assertEqual([ip_lib.IPDevice('lo'),
                          ip_lib.IPDevice('eth0'),
                          ip_lib.IPDevice('br-int'),
                          ip_lib.IPDevice('gw-ddc717df-49'),
                          ip_lib.IPDevice('eth0.50')],
                         retval)

        self.execute.assert_called_once_with('o', 'link', ('list',), None)
Ejemplo n.º 10
0
 def test_ensure_namespace(self):
     with mock.patch.object(ip_lib, 'IPDevice') as ip_dev:
         ip = ip_lib.IPWrapper()
         with mock.patch.object(ip.netns, 'exists') as ns_exists:
             ns_exists.return_value = False
             ip.ensure_namespace('ns')
             self.execute.assert_has_calls(
                 [mock.call([], 'netns', ('add', 'ns'), None,
                            as_root=True)])
             ip_dev.assert_has_calls([mock.call('lo', 'ns'),
                                      mock.call().link.set_up()])
Ejemplo n.º 11
0
    def test_garbage_collect_namespace_existing_empty_ns(self):
        with mock.patch.object(ip_lib, 'IpNetnsCommand') as ip_ns_cmd_cls:
            ip_ns_cmd_cls.return_value.exists.return_value = True

            ip = ip_lib.IPWrapper('ns')

            with mock.patch.object(ip, 'namespace_is_empty') as mock_is_empty:
                mock_is_empty.return_value = True
                self.assertTrue(ip.garbage_collect_namespace())

                mock_is_empty.assert_called_once_with()
                expected = [mock.call().exists('ns'), mock.call().delete('ns')]
                ip_ns_cmd_cls.assert_has_calls(expected)
Ejemplo n.º 12
0
    def _remove_outdated_interfaces(self, device):
        """Finds and removes unused network device."""
        list_dev = []
        for dev in ip_lib.IPWrapper().get_devices():
            if dev.name != device.name and dev.name[:3] == device.name[:3]:
                cidr_set = set()
                for a in dev.addr.list():
                    if a['ip_version'] == 4:
                        cidr_set.add(str(netaddr.IPNetwork(a['cidr']).cidr))
                list_dev.append((dev.name, cidr_set))
        device_cidr_set = set(str(netaddr.IPNetwork(a['cidr']).cidr)
                              for a in device.addr.list()
                              if a['ip_version'] == 4)

        for dev_name, cidr_set in list_dev:
            if device_cidr_set & cidr_set:
                self.vif_driver.unplug(dev_name)
Ejemplo n.º 13
0
    def test_garbage_collect_namespace_existing_not_empty(self):
        lo_device = mock.Mock()
        lo_device.name = 'lo'
        tap_device = mock.Mock()
        tap_device.name = 'tap1'

        with mock.patch.object(ip_lib, 'IpNetnsCommand') as ip_ns_cmd_cls:
            ip_ns_cmd_cls.return_value.exists.return_value = True

            ip = ip_lib.IPWrapper('ns')

            with mock.patch.object(ip, 'namespace_is_empty') as mock_is_empty:
                mock_is_empty.return_value = False

                self.assertFalse(ip.garbage_collect_namespace())

                mock_is_empty.assert_called_once_with()
                expected = [mock.call(ip), mock.call().exists('ns')]
                self.assertEqual(ip_ns_cmd_cls.mock_calls, expected)
                self.assertNotIn(mock.call().delete('ns'),
                                 ip_ns_cmd_cls.mock_calls)
Ejemplo n.º 14
0
 def test_get_device(self):
     dev = ip_lib.IPWrapper('ns').device('eth0')
     self.assertEqual(dev.namespace, 'ns')
     self.assertEqual(dev.name, 'eth0')
Ejemplo n.º 15
0
 def test_add_device_to_namespace_is_none(self):
     dev = mock.Mock()
     ip_lib.IPWrapper().add_device_to_namespace(dev)
     self.assertEqual(dev.mock_calls, [])
Ejemplo n.º 16
0
 def test_add_device_to_namespace(self):
     dev = mock.Mock()
     ip_lib.IPWrapper('ns').add_device_to_namespace(dev)
     dev.assert_has_calls([mock.call.link.set_netns('ns')])
Ejemplo n.º 17
0
 def test_ensure_namespace_existing(self):
     with mock.patch.object(ip_lib, 'IpNetnsCommand') as ip_ns_cmd:
         ip_ns_cmd.exists.return_value = True
         ns = ip_lib.IPWrapper().ensure_namespace('ns')
         self.assertFalse(self.execute.called)
         self.assertEqual(ns.namespace, 'ns')