def umount(self): if self.fsType in ("swap", "none", "unallocated"): return if not self.devPath or not self.mounted: return try: logCall("umount -n %s" % self.mountPoint) except RuntimeError: log.warning("Unmount of %s from %s failed - trying again", self.devPath, self.mountPoint) clean = False for x in range(5): logCall("sync") time.sleep(1) try: logCall("umount -n %s" % self.mountPoint) except RuntimeError: pass else: clean = True break if not clean: log.error("Unmount failed because these files " "were still open:") for path in sorted(helperfuncs.getMountedFiles(self.mountPoint)): log.error(path) raise RuntimeError("Failed to unmount %s" % self.devPath) self.detach() self.mounted = False
def testGetMountedFiles(self): class mock_popen: def read(xself): return ' 1234 5678' class mock_os: def __init__(xself, listdir_hits, readlink_hits): xself.listdir_hits = listdir_hits xself.readlink_hits = readlink_hits xself.path = os.path def listdir(xself, path): xself.listdir_hits.add(path) if path == '/proc/1234/fd': return ['1', '2'] elif path == '/proc/5678/fd': return ['3'] def readlink(xself, path): xself.readlink_hits.add(path) return paths[path.split('/')[-1]] def popen(xself, path, mode): self.failUnlessEqual(path, 'fuser -m /mnt/null 2>/dev/null') self.failUnlessEqual(mode, 'r') return mock_popen() listdir_hits = set() listdir_want = set(['/proc/1234/fd', '/proc/5678/fd']) readlink_hits = set() readlink_want = set(['/proc/1234/fd/1', '/proc/1234/fd/2', '/proc/5678/fd/3']) paths = { '1': '/irrelevant', '2': '/mnt/null/foo', '3': '/mnt/null/bar'} _os = helperfuncs.os try: helperfuncs.os = mock_os(listdir_hits, readlink_hits) paths = helperfuncs.getMountedFiles('/mnt/null') self.failUnlessEqual(paths, set(['/mnt/null/foo', '/mnt/null/bar'])) self.failUnlessEqual(listdir_hits, listdir_want) self.failUnlessEqual(readlink_hits, readlink_want) finally: helperfuncs.os = _os