Change in vdsm[master]: veth: introduce a context manager to create and destroy a ve...

osvoboda at redhat.com osvoboda at redhat.com
Thu Mar 19 14:18:33 UTC 2015


Ondřej Svoboda has uploaded a new change for review.

Change subject: veth: introduce a context manager to create and destroy a veth pair
......................................................................

veth: introduce a context manager to create and destroy a veth pair

Change-Id: I58688d23ea059ced35c77fd1c4262543582ae53b
Signed-off-by: Ondřej Svoboda <osvoboda at redhat.com>
---
M tests/functional/networkTests.py
M tests/functional/veth.py
M tests/netinfoTests.py
3 files changed, 39 insertions(+), 48 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/45/38945/1

diff --git a/tests/functional/networkTests.py b/tests/functional/networkTests.py
index 07f5e86..aa2a373 100644
--- a/tests/functional/networkTests.py
+++ b/tests/functional/networkTests.py
@@ -150,16 +150,6 @@
             dummyPool.add(nic)
 
 
- at contextmanager
-def vethIf():
-    """ Yields a tuple containing pair of veth devices."""
-    (left, right) = veth.create()
-    try:
-        yield (left, right)
-    finally:
-        veth.remove(left)
-
-
 def _waitForKnownOperstate(device, timeout=1):
     with monitor.Monitor(groups=('link',), timeout=timeout) as mon:
         state = operstate(device).lower()
@@ -421,7 +411,7 @@
                             <= (ARP_REQUEST_SIZE + DHCP_PACKET_SIZE),
                             '%s is out of range' % diff)
 
-        with vethIf() as (left, right):
+        with veth.pair() as (left, right):
             # disabling IPv6 on Interface for removal of Router Solicitation
             sysctl.disable_ipv6(left)
             sysctl.disable_ipv6(right)
@@ -1755,7 +1745,7 @@
         if el6 and 6 in families:
             raise SkipTest("el6's dnsmasq does not support DHCPv6")
 
-        with vethIf() as (left, right):
+        with veth.pair() as (left, right):
             veth.setIP(left, IP_ADDRESS, IP_CIDR)
             veth.setIP(left, IPv6_ADDRESS, IPv6_CIDR, 6)
             veth.setLinkUp(left)
@@ -1861,7 +1851,7 @@
                 ifcfg_bootproto)
             self.assertEqual(bridges[NETWORK_NAME]['dhcpv4'], dhcp)
 
-        with vethIf() as (left, right):
+        with veth.pair() as (left, right):
             veth.setIP(left, IP_ADDRESS, IP_CIDR)
             veth.setLinkUp(left)
             el6 = _system_is_el6()
@@ -1885,7 +1875,7 @@
 
         dhcpv4_ifaces = set()
         dhcpv6_ifaces = set()
-        with vethIf() as (server, client):
+        with veth.pair() as (server, client):
             veth.setIP(server, IP_ADDRESS, IP_CIDR)
             veth.setIP(server, IPv6_ADDRESS, IPv6_CIDR, 6)
             veth.setLinkUp(server)
@@ -2170,7 +2160,7 @@
             return [open('/proc/%s/cmdline' % pid).read().strip('\0')
                     .split('\0')[-1] for pid in pids]
 
-        with vethIf() as (server, client):
+        with veth.pair() as (server, client):
             veth.setIP(server, IP_ADDRESS, IP_CIDR)
             veth.setLinkUp(server)
             with dnsmasqDhcp(server, _system_is_el6()):
diff --git a/tests/functional/veth.py b/tests/functional/veth.py
index d28fd6a..3f0b71d 100644
--- a/tests/functional/veth.py
+++ b/tests/functional/veth.py
@@ -16,32 +16,35 @@
 #
 # Refer to the README and COPYING files for full details of the license
 #
+from contextlib import contextmanager
+
 from nose.plugins.skip import SkipTest
 
-import dummy
 from vdsm.ipwrapper import linkAdd, IPRoute2Error
 from vdsm.utils import random_iface_name
 
+import dummy
 
-def create(prefix='veth_', max_length=15):
+
+ at contextmanager
+def pair(prefix='veth_', max_length=15):
     """
-    Create a veth interface with a pseudo-random suffix (e.g. veth_m6Lz7uMK9c)
-    for both endpoints. Use the longest possible name length by default.
-    This assumes root privileges.
+    Yield a pair of veth devices. This assumes root privileges (currently
+    required by all tests anyway).
+
+    Both sides of the pair have a pseudo-random suffix (e.g. veth_m6Lz7uMK9c).
     """
-    leftPoint = random_iface_name(prefix, max_length)
-    rightPoint = random_iface_name(prefix, max_length)
+    left_side = random_iface_name(prefix, max_length)
+    right_side = random_iface_name(prefix, max_length)
     try:
-        linkAdd(leftPoint, linkType='veth', args=('peer', 'name', rightPoint))
+        linkAdd(left_side, linkType='veth',
+                args=('peer', 'name', right_side))
+        yield left_side, right_side
     except IPRoute2Error:
-        raise SkipTest('Failed to create a veth interface')
-    else:
-        return (leftPoint, rightPoint)
-
-
-# the peer device is removed by the kernel
-remove = dummy.remove
-remove
+        raise SkipTest('Failed to create a veth pair.')
+    finally:
+        # the peer device is removed by the kernel
+        dummy.remove(left_side)
 
 
 setIP = dummy.setIP
diff --git a/tests/netinfoTests.py b/tests/netinfoTests.py
index d70f92a..fb43089 100644
--- a/tests/netinfoTests.py
+++ b/tests/netinfoTests.py
@@ -180,24 +180,22 @@
     def testFakeNics(self):
         with MonkeyPatchScope([(ipwrapper.Link, '_fakeNics', ['veth_*',
                                                               'dummy_*'])]):
-            d1 = dummy.create()
-            d2 = dummy.create(prefix='mehd_')
-            v1a, v1b = veth.create()
-            v2a, v2b = veth.create(prefix='mehv_')
+            with veth.pair() as (v1a, v1b):
+                d1 = dummy.create()
+                fakes = set([d1, v1a, v1b])
+                nics = netinfo.nics()
+                dummy.remove(d1)
+                self.assertTrue(fakes.issubset(nics), 'Fake devices %s are not'
+                                ' listed in nics %s' % (fakes, nics))
 
-            fakes = set([d1, v1a, v1b])
-            hiddens = set([d2, v2a, v2b])
-            nics = netinfo.nics()
-            dummy.remove(d1)
-            dummy.remove(d2)
-            veth.remove(v1a)
-            veth.remove(v2a)
-
-            self.assertTrue(fakes.issubset(nics), 'Fake devices %s are not '
-                            'listed in nics %s' % (fakes, nics))
-            self.assertFalse(hiddens.intersection(nics), 'Some of hidden '
-                             'devices %s is shown in nics %s' % (hiddens,
-                                                                 nics))
+            with veth.pair(prefix='mehv_') as (v2a, v2b):
+                d2 = dummy.create(prefix='mehd_')
+                hiddens = set([d2, v2a, v2b])
+                dummy.remove(d2)
+                nics = netinfo.nics()
+                self.assertFalse(hiddens.intersection(nics), 'Some of hidden '
+                                 'devices %s is shown in nics %s' % (hiddens,
+                                                                     nics))
 
     def testGetIfaceCfg(self):
         deviceName = "___This_could_never_be_a_device_name___"


-- 
To view, visit https://gerrit.ovirt.org/38945
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I58688d23ea059ced35c77fd1c4262543582ae53b
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Ondřej Svoboda <osvoboda at redhat.com>


More information about the vdsm-patches mailing list