class VirtualTapTestCase(unittest.TestCase): def _tap_exists_ifconfig(self): (ret, stdout, stderr) = _run_command(["/sbin/ifconfig", self.tap.name]) return ret == 0 def _get_mtu_ifconfig(self): (ret, stdout, stderr) = _run_command(["/sbin/ifconfig", self.tap.name]) self.assertEqual(stderr, "") self.assertEqual(ret, 0) remtu = re.compile("MTU:(\d+)+\ ") mtu = int(re.search(remtu, stdout).group(1)) return mtu def setUp(self): self.tap = VirtualTap("testtap%d") def test_open_close_tap(self): self.tap.open() self.assertIsNotNone(self.tap.fd) self.assertEqual(self.tap.fd, self.tap.fileno()) self.assertTrue(self.tap.name.startswith("testtap")) self.assertTrue(iface_exists(self.tap.name)) self.tap.close() self.assertFalse(iface_exists(self.tap.name)) def test_set_mtu(self): self.tap.open() self.assertGreater(iface_mtu(self.tap.name), 0) self.tap.set_mtu(1491) self.assertEqual(iface_mtu(self.tap.name), 1491) self.tap.close()
def _attach_to_network(self, tapname, vni, macttl, targetip, targetport): if tapname in self.vnet_tapname_map: vnet = self.vnet_tapname_map[tapname] msg = ("Ignoring network addition request for tapname %s," " already in use for vnet %r" % (tapname, vnet)) raise ValueError(msg) tap = VirtualTap(name=tapname) tap.open() self.taps.append(tap) # Set tap in non-blocking mode fcntl.fcntl(tap, fcntl.F_SETFL, os.O_NONBLOCK) vn = self._create_vnet(vni=vni, macttl=macttl) vn.targets = [(targetip, int(targetport))] vn.socket = self.socket vn.tap = tap tap.vnet = vn self.vnet_tapname_map[tapname] = vn for t in vn.targets: if _ip_is_multicast(t[0]): _join_mcast_group(vn.socket, t[0], self.mcastif) logging.info("Joined new network, vnet %r over tap %r", tap.vnet, tap)