2011-01-18 09:24:57 +00:00
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
def makeLoopDev(device_name, file_name):
|
|
|
|
proc = subprocess.Popen(["dd", "if=/dev/zero", "of=%s" % file_name,
|
|
|
|
"bs=1024", "count=102400"],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
while True:
|
2013-01-23 17:28:19 +00:00
|
|
|
(out, err) = proc.communicate()
|
2011-01-18 09:24:57 +00:00
|
|
|
if proc.returncode is not None:
|
|
|
|
rc = proc.returncode
|
|
|
|
break
|
|
|
|
if rc:
|
|
|
|
raise OSError, "dd failed creating the file %s" % file_name
|
|
|
|
|
|
|
|
proc = subprocess.Popen(["losetup", device_name, file_name],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
while True:
|
2013-01-23 17:28:19 +00:00
|
|
|
(out, err) = proc.communicate()
|
2011-01-18 09:24:57 +00:00
|
|
|
if proc.returncode is not None:
|
|
|
|
rc = proc.returncode
|
|
|
|
break
|
|
|
|
if rc:
|
|
|
|
raise OSError, "losetup failed setting up the loop device %s" % device_name
|
|
|
|
|
|
|
|
def removeLoopDev(device_name, file_name):
|
|
|
|
proc = subprocess.Popen(["losetup", "-d", device_name],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
while True:
|
2013-01-23 17:28:19 +00:00
|
|
|
(out, err) = proc.communicate()
|
2011-01-18 09:24:57 +00:00
|
|
|
if proc.returncode is not None:
|
|
|
|
rc = proc.returncode
|
|
|
|
break
|
|
|
|
if rc:
|
|
|
|
raise OSError, "losetup failed removing the loop device %s" % device_name
|
|
|
|
|
|
|
|
os.unlink(file_name)
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
def getFreeLoopDev():
|
|
|
|
# There's a race condition here where another process could grab the loop
|
|
|
|
# device losetup gives us before we have time to set it up, but that's just
|
|
|
|
# a chance we'll have to take.
|
|
|
|
proc = subprocess.Popen(["losetup", "-f"],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
out = None
|
|
|
|
|
|
|
|
while True:
|
|
|
|
(out, err) = proc.communicate()
|
|
|
|
if proc.returncode is not None:
|
|
|
|
rc = proc.returncode
|
|
|
|
out = out.strip()
|
|
|
|
break
|
|
|
|
|
|
|
|
if rc:
|
|
|
|
raise OSError, "losetup failed to find a free device"
|
|
|
|
|
|
|
|
return out
|
2011-01-18 09:24:57 +00:00
|
|
|
|
|
|
|
class DevicelibsTestCase(unittest.TestCase):
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
_LOOP_DEVICES = ["/tmp/test-virtdev0", "/tmp/test-virtdev1"]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
import pyanaconda.anaconda_log
|
|
|
|
pyanaconda.anaconda_log.init()
|
2011-01-18 09:24:57 +00:00
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
unittest.TestCase.__init__(self, *args, **kwargs)
|
|
|
|
self._loopMap = {}
|
2011-01-18 09:24:57 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2013-01-23 17:28:19 +00:00
|
|
|
for file in self._LOOP_DEVICES:
|
|
|
|
dev = getFreeLoopDev()
|
2011-01-18 09:24:57 +00:00
|
|
|
makeLoopDev(dev, file)
|
2013-01-23 17:28:19 +00:00
|
|
|
self._loopMap[file] = dev
|
2011-01-18 09:24:57 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2013-01-23 17:28:19 +00:00
|
|
|
for (file, dev) in self._loopMap.iteritems():
|
2011-01-18 09:24:57 +00:00
|
|
|
removeLoopDev(dev, file)
|