Ido Barkan has uploaded a new change for review.

Change subject: net: tests: convert dummy module into a class
......................................................................

net: tests: convert dummy module into a class

This class will be later moved into nettestlib and be unified with
other test device code.

Change-Id: Ie9c671cb0cf4b5e2e67090861e2018c90f0f95f8
Signed-off-by: Ido Barkan <ibar...@redhat.com>
---
M tests/functional/dummy.py
M tests/functional/networkTests.py
M tests/functional/veth.py
M tests/netlinkTests.py
4 files changed, 113 insertions(+), 112 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/95/44695/1

diff --git a/tests/functional/dummy.py b/tests/functional/dummy.py
index 17db02e..46c7dac 100644
--- a/tests/functional/dummy.py
+++ b/tests/functional/dummy.py
@@ -24,65 +24,68 @@
 from vdsm.utils import random_iface_name
 
 
-def create(prefix='dummy_', max_length=11):
-    """
-    Create a dummy interface with a pseudo-random suffix, e.g. dummy_ilXaYiSn7.
-    Limit the name to 11 characters to make room for VLAN IDs.
-    This assumes root privileges.
-    """
-    dummy_name = random_iface_name(prefix, max_length)
-    try:
-        linkAdd(dummy_name, linkType='dummy')
-    except IPRoute2Error as e:
-        raise SkipTest('Failed to create a dummy interface %s: %s' %
-                       (dummy_name, e))
-    else:
-        return dummy_name
+class Dummy(object):
 
+    def __init__(self, prefix='dummy_', max_length=11):
+        self.devName = random_iface_name(prefix, max_length)
 
-def remove(dummy_name):
-    """
-    Remove the dummy interface. This assumes root privileges.
-    """
+    def create(self):
+        """
+        Create a dummy interface with a pseudo-random suffix, e.g.
+        dummy_ilXaYiSn7.
+        Limit the name to 11 characters to make room for VLAN IDs.
+        This assumes root privileges.
+        """
+        try:
+            linkAdd(self.devName, linkType='dummy')
+        except IPRoute2Error as e:
+            raise SkipTest('Failed to create a dummy interface %s: %s' %
+                           (self.devName, e))
+        else:
+            return self.devName
 
-    try:
-        linkDel(dummy_name)
-    except IPRoute2Error as e:
-        raise SkipTest("Unable to delete the dummy interface %s: %s" %
-                       (dummy_name, e))
+    def remove(self):
+        """
+        Remove the dummy interface. This assumes root privileges.
+        """
+
+        try:
+            linkDel(self.devName)
+        except IPRoute2Error as e:
+            raise SkipTest("Unable to delete the dummy interface %s: %s" %
+                           (self.devName, e))
+
+    def setLinkUp(self):
+        self._setLinkState('up')
+
+    def setLinkDown(self):
+        self._setLinkState('down')
+
+    def setIP(self, ipaddr, netmask, family=4):
+        try:
+            addrAdd(self.devName, ipaddr, netmask, family)
+        except IPRoute2Error as e:
+            message = ('Failed to add the IPv%s address %s/%s to device %s: %s'
+                       % (family, ipaddr, netmask, self.devName, e))
+            if family == 6:
+                message += ("; NetworkManager may have set the sysctl "
+                            "disable_ipv6 flag on the device, please see e.g. "
+                            "RH BZ #1102064")
+            raise SkipTest(message)
+
+    def _setLinkState(self, state):
+        try:
+            linkSet(self.devName, [state])
+        except IPRoute2Error:
+            raise SkipTest('Failed to bring %s to state %s' % (
+                self.devName, state))
 
 
 @contextmanager
 def device(prefix='dummy_', max_length=11):
-    dummy_name = create(prefix, max_length)
+    dummy_interface = Dummy(prefix, max_length)
+    dummy_name = dummy_interface.create()
     try:
         yield dummy_name
     finally:
-        remove(dummy_name)
-
-
-def setIP(dummy_name, ipaddr, netmask, family=4):
-    try:
-        addrAdd(dummy_name, ipaddr, netmask, family)
-    except IPRoute2Error as e:
-        message = ('Failed to add the IPv%s address %s/%s to device %s: %s'
-                   % (family, ipaddr, netmask, dummy_name, e))
-        if family == 6:
-            message += ('; NetworkManager may have set the sysctl disable_ipv6'
-                        ' flag on the device, please see e.g. RH BZ #1102064')
-        raise SkipTest(message)
-
-
-def setLinkUp(dummy_name):
-    _setLinkState(dummy_name, 'up')
-
-
-def setLinkDown(dummy_name):
-    _setLinkState(dummy_name, 'down')
-
-
-def _setLinkState(dummy_name, state):
-    try:
-        linkSet(dummy_name, [state])
-    except IPRoute2Error:
-        raise SkipTest('Failed to bring %s to state %s' % (dummy_name, state))
+        dummy_interface.remove()
diff --git a/tests/functional/networkTests.py b/tests/functional/networkTests.py
index e811955..10b4426 100644
--- a/tests/functional/networkTests.py
+++ b/tests/functional/networkTests.py
@@ -30,7 +30,8 @@
 from vdsm.constants import EXT_BRCTL, EXT_IFUP, EXT_IFDOWN
 from vdsm import ipwrapper
 from vdsm.ipwrapper import (routeExists, ruleExists, addrFlush, LinkType,
-                            getLinks, routeShowTable, linkDel, linkSet)
+                            getLinks, routeShowTable, linkDel, linkSet,
+                            addrAdd)
 from vdsm.netconfpersistence import KernelConfig, RunningConfig
 from vdsm.netinfo import (bridges, operstate, getRouteDeviceTo,
                           _get_dhclient_ifaces, BONDING_SLAVES,
@@ -96,14 +97,16 @@
     """Persists network configuration."""
     VdsProxy().save_config()
     for _ in range(DUMMY_POOL_SIZE):
-        dummyPool.add(dummy.create())
+        iface = dummy.Dummy()
+        iface.create()
+        dummyPool.add(iface)
 
 
 def tearDownModule():
     """Restores the network configuration previous to running tests."""
     VdsProxy().restoreNetConfig()
     for nic in dummyPool:
-        dummy.remove(nic)
+        nic.remove()
 
 
 @contextmanager
@@ -129,7 +132,7 @@
     dummies = []
     try:
         for _ in range(num):
-            dummies.append(dummyPool.pop())
+            dummies.append(dummyPool.pop().devName)
         yield dummies
     finally:
         for nic in dummies:
@@ -421,8 +424,8 @@
             # disabling IPv6 on Interface for removal of Router Solicitation
             sysctl.disable_ipv6(left)
             sysctl.disable_ipv6(right)
-            veth.setLinkUp(left)
-            veth.setLinkUp(right)
+            linkSet(left, ['up'])
+            linkSet(right, ['up'])
 
             # Vdsm scans for new devices every 15 seconds
             self.retryAssert(
@@ -1478,8 +1481,8 @@
             return self.vdsm_net.config.networks[net_name].get('blockingdhcp')
 
         with veth.pair() as (server, client):
-            veth.setIP(server, IP_ADDRESS, IP_CIDR)
-            veth.setLinkUp(server)
+            addrAdd(server, IP_ADDRESS, IP_CIDR)
+            linkSet(server, ['up'])
             dhcp_async_net = {'nic': client, 'bridged': False,
                               'bootproto': 'dhcp', 'blockingdhcp': False}
             status, msg = self.setupNetworks(
@@ -2039,9 +2042,9 @@
     @RequireVethMod
     def testSetupNetworksAddDelDhcp(self, bridged, families):
         with veth.pair() as (left, right):
-            veth.setIP(left, IP_ADDRESS, IP_CIDR)
-            veth.setIP(left, IPv6_ADDRESS, IPv6_CIDR, 6)
-            veth.setLinkUp(left)
+            addrAdd(left, IP_ADDRESS, IP_CIDR)
+            addrAdd(left, IPv6_ADDRESS, IPv6_CIDR, 6)
+            linkSet(left, ['up'])
             with dnsmasqDhcp(left):
                 dhcpv4 = 4 in families
                 dhcpv6 = 6 in families
@@ -2138,8 +2141,8 @@
             self.assertEqual(bridges[NETWORK_NAME]['dhcpv4'], dhcp)
 
         with veth.pair() as (left, right):
-            veth.setIP(left, IP_ADDRESS, IP_CIDR)
-            veth.setLinkUp(left)
+            addrAdd(left, IP_ADDRESS, IP_CIDR)
+            linkSet(left, ['up'])
             with dnsmasqDhcp(left):
                 try:
                     setup_test_network(dhcp=True)
@@ -2155,9 +2158,9 @@
         dhcpv4_ifaces = set()
         dhcpv6_ifaces = set()
         with veth.pair() as (server, client):
-            veth.setIP(server, IP_ADDRESS, IP_CIDR)
-            veth.setIP(server, IPv6_ADDRESS, IPv6_CIDR, 6)
-            veth.setLinkUp(server)
+            addrAdd(server, IP_ADDRESS, IP_CIDR)
+            addrAdd(server, IPv6_ADDRESS, IPv6_CIDR, 6)
+            linkSet(server, ['up'])
 
             with dnsmasqDhcp(server):
 
@@ -2182,16 +2185,16 @@
         with dummyIf(1) as nics:
             nic, = nics
 
-            dummy.setIP(nic, IP_ADDRESS, IP_CIDR)
+            addrAdd(nic, IP_ADDRESS, IP_CIDR)
             try:
-                dummy.setLinkUp(nic)
+                linkSet(nic, ['up'])
                 self.assertEqual(getRouteDeviceTo(IP_ADDRESS_IN_NETWORK), nic)
             finally:
                 addrFlush(nic)
 
-            dummy.setIP(nic, IPv6_ADDRESS, IPv6_CIDR, family=6)
+            addrAdd(nic, IPv6_ADDRESS, IPv6_CIDR, family=6)
             try:
-                dummy.setLinkUp(nic)
+                linkSet(nic, ['up'])
                 self.assertEqual(getRouteDeviceTo(IPv6_ADDRESS_IN_NETWORK),
                                  nic)
             finally:
@@ -2422,8 +2425,8 @@
                     .split('\0')[-1] for pid in pids]
 
         with veth.pair() as (server, client):
-            veth.setIP(server, IP_ADDRESS, IP_CIDR)
-            veth.setLinkUp(server)
+            addrAdd(server, IP_ADDRESS, IP_CIDR)
+            linkSet(server, ['up'])
             with dnsmasqDhcp(server):
                 with namedTemporaryDir(dir='/var/lib/dhclient') as dhdir:
                     # Start a non-vdsm owned dhclient for the 'client' iface
@@ -2536,8 +2539,10 @@
 
     @cleanupNet
     def testSetupNetworksRemoveBondWithKilledEnslavedNics(self):
-        nic = dummy.create()
-        nics = [nic]
+
+        nic = dummy.Dummy()
+        nic.create()
+        nics = [nic.devName]
         try:
             status, msg = self.setupNetworks(
                 {NETWORK_NAME:
@@ -2547,7 +2552,7 @@
             self.assertNetworkExists(NETWORK_NAME)
             self.assertBondExists(BONDING_NAME, nics)
         finally:
-            dummy.remove(nic)
+            nic.remove()
 
         status, msg = self.setupNetworks(
             {NETWORK_NAME: {'remove': True}},
diff --git a/tests/functional/veth.py b/tests/functional/veth.py
index 3f0b71d..8623cb6 100644
--- a/tests/functional/veth.py
+++ b/tests/functional/veth.py
@@ -20,10 +20,8 @@
 
 from nose.plugins.skip import SkipTest
 
-from vdsm.ipwrapper import linkAdd, IPRoute2Error
+from vdsm.ipwrapper import linkAdd, IPRoute2Error, linkDel
 from vdsm.utils import random_iface_name
-
-import dummy
 
 
 @contextmanager
@@ -44,16 +42,4 @@
         raise SkipTest('Failed to create a veth pair.')
     finally:
         # the peer device is removed by the kernel
-        dummy.remove(left_side)
-
-
-setIP = dummy.setIP
-setIP
-
-
-setLinkUp = dummy.setLinkUp
-setLinkUp
-
-
-setLinkDown = dummy.setLinkDown
-setLinkDown
+        linkDel(left_side)
diff --git a/tests/netlinkTests.py b/tests/netlinkTests.py
index 9751c53..0264d72 100644
--- a/tests/netlinkTests.py
+++ b/tests/netlinkTests.py
@@ -19,8 +19,9 @@
     @ValidateRunningAsRoot
     def test_iterate_after_events(self):
         with monitor.Monitor(timeout=self.TIMEOUT) as mon:
-            dummy_name = dummy.create()
-            dummy.remove(dummy_name)
+            iface = dummy.Dummy()
+            dummy_name = iface.create()
+            iface.remove()
             for event in mon:
                 if event.get('name') == dummy_name:
                     break
@@ -31,12 +32,13 @@
         iteration we start _set_and_remove_device, which is delayed for .2
         seconds. Then iteration starts and wait for new dummy.
         """
-        dummy_name = dummy.create()
+        iface = dummy.Dummy()
+        dummy_name = iface.create()
 
         def _set_and_remove_device():
             time.sleep(.2)
-            dummy.setLinkUp(dummy_name)
-            dummy.remove(dummy_name)
+            iface.setLinkUp()
+            iface.remove()
         add_device_thread = threading.Thread(target=_set_and_remove_device)
 
         with monitor.Monitor(timeout=self.TIMEOUT) as mon:
@@ -49,8 +51,9 @@
     @ValidateRunningAsRoot
     def test_stopped(self):
         with monitor.Monitor(timeout=self.TIMEOUT) as mon:
-            dummy_name = dummy.create()
-            dummy.remove(dummy_name)
+            iface = dummy.Dummy()
+            dummy_name = iface.create()
+            iface.remove()
 
         found = any(event.get('name') == dummy_name for event in mon)
         self.assertTrue(found, 'Expected event was not caught.')
@@ -61,10 +64,11 @@
                              groups=('ipv4-ifaddr',)) as mon_a:
             with monitor.Monitor(timeout=self.TIMEOUT,
                                  groups=('link', 'ipv4-route')) as mon_l_r:
-                dummy_name = dummy.create()
-                dummy.setIP(dummy_name, IP_ADDRESS, IP_CIDR)
-                dummy.setLinkUp(dummy_name)
-                dummy.remove(dummy_name)
+                iface = dummy.Dummy()
+                iface.create()
+                iface.setIP(IP_ADDRESS, IP_CIDR)
+                iface.setLinkUp()
+                iface.remove()
 
         for event in mon_a:
             self.assertIn('_addr', event['event'], "Caught event '%s' is not "
@@ -82,10 +86,11 @@
             iterator = iter(mon)
 
             # Generate events to avoid blocking
-            dummy_name = dummy.create()
+            iface = dummy.Dummy()
+            iface.create()
             iterator.next()
 
-            dummy.remove(dummy_name)
+            iface.remove()
             iterator.next()
 
         with self.assertRaises(StopIteration):
@@ -114,10 +119,11 @@
 
         with monitor.Monitor(timeout=self.TIMEOUT,
                              silent_timeout=True) as mon:
-            dummy_name = dummy.create()
-            dummy.setIP(dummy_name, IP_ADDRESS, IP_CIDR)
-            dummy.setLinkUp(dummy_name)
-            dummy.remove(dummy_name)
+            iface = dummy.Dummy()
+            dummy_name = iface.create()
+            iface.setIP(IP_ADDRESS, IP_CIDR)
+            iface.setLinkUp()
+            iface.remove()
 
             expected_events = _expected_events(dummy_name, IP_ADDRESS, IP_CIDR)
 
@@ -155,8 +161,9 @@
     def test_timeout_not_triggered(self):
         time_start = monotonic_time()
         with monitor.Monitor(timeout=self.TIMEOUT) as mon:
-            dummy_name = dummy.create()
-            dummy.remove(dummy_name)
+            iface = dummy.Dummy()
+            iface.create()
+            iface.remove()
 
             for event in mon:
                 break


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie9c671cb0cf4b5e2e67090861e2018c90f0f95f8
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Ido Barkan <ibar...@redhat.com>
_______________________________________________
vdsm-patches mailing list
vdsm-patches@lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to