Change in vdsm[master]: tests: Use testrunner.namedTemporaryDir

nsoffer at redhat.com nsoffer at redhat.com
Thu May 8 09:06:35 UTC 2014


Nir Soffer has uploaded a new change for review.

Change subject: tests: Use testrunner.namedTemporaryDir
......................................................................

tests: Use testrunner.namedTemporaryDir

Some tests were working hard ensuring proper cleanup when a test fails,
instead of using the existing infrastructure.

Change-Id: Idd7a91611a6abd5b88d51b748ce7e0172285875c
Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
M tests/miscTests.py
M tests/netinfoTests.py
2 files changed, 50 insertions(+), 69 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/06/27506/1

diff --git a/tests/miscTests.py b/tests/miscTests.py
index f1317fa..165365f 100644
--- a/tests/miscTests.py
+++ b/tests/miscTests.py
@@ -215,11 +215,8 @@
         Test that when given an empty dir the rotator works correctly.
         """
         prefix = "prefix"
-        dir = tempfile.mkdtemp()
-
-        misc.rotateFiles(dir, prefix, 0, persist=persist)
-
-        os.rmdir(dir)
+        with namedTemporaryDir() as dir:
+            misc.rotateFiles(dir, prefix, 0, persist=persist)
 
     def testFullDir(self, persist=False):
         """
@@ -231,32 +228,26 @@
                        'he went on, shaking his head, '
                        '"are a sure sign of a diseased mind."')
         # (C) Terry Pratchet - Small Gods
-        dir = tempfile.mkdtemp()
-        gen = 10
+        with namedTemporaryDir() as dir:
+            gen = 10
 
-        expectedDirContent = []
-        for i in range(gen):
-            fname = "%s.txt.%d" % (prefix, i + 1)
-            expectedDirContent.append("%s.txt.%d" % (prefix, i + 1))
-            f = open(os.path.join(dir, fname), "wb")
-            f.write(stubContent)
-            f.flush()
-            f.close()
+            expectedDirContent = []
+            for i in range(gen):
+                fname = "%s.txt.%d" % (prefix, i + 1)
+                expectedDirContent.append("%s.txt.%d" % (prefix, i + 1))
+                f = open(os.path.join(dir, fname), "wb")
+                f.write(stubContent)
+                f.flush()
+                f.close()
 
-        # Rotate
-        misc.rotateFiles(dir, prefix, gen, persist=persist)
+            # Rotate
+            misc.rotateFiles(dir, prefix, gen, persist=persist)
 
-        # Test result
-        currentDirContent = os.listdir(dir)
-        expectedDirContent.sort()
-        currentDirContent.sort()
-        try:
+            # Test result
+            currentDirContent = os.listdir(dir)
+            expectedDirContent.sort()
+            currentDirContent.sort()
             self.assertEquals(currentDirContent, expectedDirContent)
-        finally:
-            # Clean
-            for f in os.listdir(dir):
-                os.unlink(os.path.join(dir, f))
-            os.rmdir(dir)
 
 
 class ParseHumanReadableSize(TestCaseBase):
diff --git a/tests/netinfoTests.py b/tests/netinfoTests.py
index a8d0123..9f3a519 100644
--- a/tests/netinfoTests.py
+++ b/tests/netinfoTests.py
@@ -21,8 +21,6 @@
 import os
 from datetime import datetime
 from functools import partial
-from shutil import rmtree
-import tempfile
 import time
 from xml.dom import minidom
 
@@ -212,12 +210,11 @@
         deviceName = "___This_could_never_be_a_device_name___"
         ifcfg = ('DEVICE=%s' % deviceName + '\n' + 'ONBOOT=yes' + '\n' +
                  'MTU=1500' + '\n' + 'HWADDR=5e:64:6d:12:16:84' + '\n')
-        tempDir = tempfile.mkdtemp()
-        ifcfgPrefix = os.path.join(tempDir, 'ifcfg-')
-        filePath = ifcfgPrefix + deviceName
+        with namedTemporaryDir() as tempDir:
+            ifcfgPrefix = os.path.join(tempDir, 'ifcfg-')
+            filePath = ifcfgPrefix + deviceName
 
-        with MonkeyPatchScope([(netinfo, 'NET_CONF_PREF', ifcfgPrefix)]):
-            try:
+            with MonkeyPatchScope([(netinfo, 'NET_CONF_PREF', ifcfgPrefix)]):
                 with open(filePath, 'w') as ifcfgFile:
                     ifcfgFile.write(ifcfg + 'BOOTPROTO=dhcp\n')
                 self.assertEqual(getBootProtocol(deviceName, 'ifcfg'), 'dhcp')
@@ -229,53 +226,48 @@
                 with open(filePath, 'w') as ifcfgFile:
                     ifcfgFile.write(ifcfg)
                 self.assertEqual(getBootProtocol(deviceName, 'ifcfg'), None)
-            finally:
-                rmtree(tempDir)
 
     def testGetIfaceCfg(self):
         deviceName = "___This_could_never_be_a_device_name___"
         ifcfg = ('GATEWAY0=1.1.1.1\n' 'NETMASK=255.255.0.0\n')
-        tempDir = tempfile.mkdtemp()
-        ifcfgPrefix = os.path.join(tempDir, 'ifcfg-')
-        filePath = ifcfgPrefix + deviceName
+        with namedTemporaryDir() as tempDir:
+            ifcfgPrefix = os.path.join(tempDir, 'ifcfg-')
+            filePath = ifcfgPrefix + deviceName
 
-        with MonkeyPatchScope([(netinfo, 'NET_CONF_PREF', ifcfgPrefix)]):
-            try:
+            with MonkeyPatchScope([(netinfo, 'NET_CONF_PREF', ifcfgPrefix)]):
                 with open(filePath, 'w') as ifcfgFile:
                     ifcfgFile.write(ifcfg)
                 self.assertEqual(
                     netinfo.getIfaceCfg(deviceName)['GATEWAY'], '1.1.1.1')
                 self.assertEqual(
                     netinfo.getIfaceCfg(deviceName)['NETMASK'], '255.255.0.0')
-            finally:
-                rmtree(tempDir)
 
     def testGetBootProtocolUnified(self):
-        tempDir = tempfile.mkdtemp()
-        netsDir = os.path.join(tempDir, 'nets')
-        os.mkdir(netsDir)
-        networks = {
-            'nonVMOverNic':
-            {"nic": "eth0", "bridged": False, "bootproto": "dhcp"},
-            'bridgeOverNic':
-            {"nic": "eth1", "bridged": True},
-            'nonVMOverBond':
-            {"bonding": "bond0", "bridged": False, "bootproto": "dhcp"},
-            'bridgeOverBond':
-            {"bonding": "bond1", "bridged": True},
-            'vlanOverNic':
-            {"nic": "eth2", "bridged": False, "vlan": 1,
-             "bootproto": "dhcp"},
-            'bridgeOverVlan':
-            {"nic": "eth3", "bridged": True, "vlan": 1},
-            'vlanOverBond':
-            {"bonding": "bond2", "bridged": False, "bootproto": "dhcp",
-             "vlan": 1},
-            'bridgeOverVlanOverBond':
-            {"bonding": "bond3", "bridged": True, "vlan": 1}}
+        with namedTemporaryDir() as tempDir:
+            netsDir = os.path.join(tempDir, 'nets')
+            os.mkdir(netsDir)
+            networks = {
+                'nonVMOverNic':
+                {"nic": "eth0", "bridged": False, "bootproto": "dhcp"},
+                'bridgeOverNic':
+                {"nic": "eth1", "bridged": True},
+                'nonVMOverBond':
+                {"bonding": "bond0", "bridged": False, "bootproto": "dhcp"},
+                'bridgeOverBond':
+                {"bonding": "bond1", "bridged": True},
+                'vlanOverNic':
+                {"nic": "eth2", "bridged": False, "vlan": 1,
+                 "bootproto": "dhcp"},
+                'bridgeOverVlan':
+                {"nic": "eth3", "bridged": True, "vlan": 1},
+                'vlanOverBond':
+                {"bonding": "bond2", "bridged": False, "bootproto": "dhcp",
+                 "vlan": 1},
+                'bridgeOverVlanOverBond':
+                {"bonding": "bond3", "bridged": True, "vlan": 1}}
 
-        with MonkeyPatchScope([(netconfpersistence, 'CONF_RUN_DIR', tempDir)]):
-            try:
+            with MonkeyPatchScope([(netconfpersistence, 'CONF_RUN_DIR',
+                                   tempDir)]):
                 runningConfig = netconfpersistence.RunningConfig()
                 for network, attributes in networks.iteritems():
                     runningConfig.setNetwork(network, attributes)
@@ -292,8 +284,6 @@
                     self.assertEqual(
                         getBootProtocol(topLevelDevice, 'unified'),
                         attributes.get('bootproto'))
-            finally:
-                rmtree(tempDir)
 
     def testGetDhclientIfaces(self):
         LEASES = (


-- 
To view, visit http://gerrit.ovirt.org/27506
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idd7a91611a6abd5b88d51b748ce7e0172285875c
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer at redhat.com>


More information about the vdsm-patches mailing list