def test_eq_subclass(self): class Subclass(mount.Mount): pass m1 = mount.Mount("spec", "file") m2 = Subclass("spec", "file") self.assertFalse(m1 == m2, "%s should not equal %s" % (m1, m2))
def test_is_mounted(self, fs_spec, fs_file, equality): """ Verifies that both fs_spec and fs_file match the mounted target. """ with fake_mounts([b"server:/path /mnt/server:_path nfs defaults 0 0"]): mnt = mount.Mount(fs_spec, fs_file) self.assertEqual(mnt.isMounted(), equality)
def test_is_mounted_gluster_with_rdma(self): with fake_mounts([ "server:/volume.rdma /mnt/server:volume fuse.glusterfs " "defaults 0 0" ]): mnt = mount.Mount("server:/volume", "/mnt/server:volume") self.assertTrue(mnt.isMounted())
def testLoopMount(self): with namedTemporaryDir() as mpath: # two nested with blocks to be python 2.6 friendly with createFloppyImage(FLOPPY_SIZE) as path: m = mount.Mount(path, mpath) with loop_mount(m): self.assertTrue(m.isMounted())
def test_subclass_different_hash(self): class Subclass(mount.Mount): pass m1 = mount.Mount("spec", "file") m2 = Subclass("spec", "file") self.assertNotEqual(hash(m1), hash(m2))
def testLoopMount(self): with namedTemporaryDir() as mpath: # two nested with blocks to be python 2.6 friendly with createFloppyImage(FLOPPY_SIZE) as path: m = mount.Mount(path, mpath) m.mount(mntOpts="loop") try: self.assertTrue(m.isMounted()) finally: m.umount()
def test_is_mounted_with_symlink(self): with namedTemporaryDir() as dir: file = os.path.join(dir, "file") open(file, "w").close() link_to_file = os.path.join(dir, "link_to_file") os.symlink(file, link_to_file) mountpoint = "/mnt/mountpoint" with fake_mounts([b"%s %s nfs defaults 0 0" % (link_to_file, mountpoint)]): mnt = mount.Mount(link_to_file, mountpoint) self.assertTrue(mnt.isMounted())
def test_mkIsoFs(self, label): """ Tests mkimage.mkIsoFs creating an image and checking its content """ iso_img = mkimage.mkIsoFs("vmId_iso", self.files, label) self.assertTrue(os.path.exists(iso_img)) m = mount.Mount(iso_img, self.workdir) m.mount(mntOpts='loop') try: self._check_content() self._check_label(iso_img, label) finally: m.umount()
def testSymlinkMount(self): with namedTemporaryDir() as root_dir: backing_image = os.path.join(root_dir, 'backing.img') link_to_image = os.path.join(root_dir, 'link_to_image') mountpoint = os.path.join(root_dir, 'mountpoint') with open(backing_image, 'w') as f: os.ftruncate(f.fileno(), GiB) commands.run([MKFS_EXEC, "-F", backing_image]) os.symlink(backing_image, link_to_image) os.mkdir(mountpoint) m = mount.Mount(link_to_image, mountpoint) with loop_mount(m): self.assertTrue(m.isMounted())
def testLoopMount(self): with namedTemporaryDir() as mpath: # two nested with blocks to be python 2.6 friendly with createFloppyImage(FLOPPY_SIZE) as path: m = mount.Mount(path, mpath) m.mount(mntOpts="loop") try: self.assertTrue(m.isMounted()) finally: m.umount(force=True, freeloop=True) # TODO: Use libudev to wait for specific event with stopwatch("Wait for udev events"): udevadm.settle(5)
def test_mkFloppyFs(self, label): """ Tests mkimage.mkFloppyFs creating an image and checking its content. Requires root permissions for writing into the floppy image. """ floppy = mkimage.mkFloppyFs("vmId_floppy", self.files, label) self.assertTrue(os.path.exists(floppy)) m = mount.Mount(floppy, self.workdir) m.mount(mntOpts='loop') try: self._check_content(checkPerms=False) self._check_label(floppy, label) finally: m.umount()
def testSymlinkMount(self): with namedTemporaryDir() as root_dir: backing_image = os.path.join(root_dir, 'backing.img') link_to_image = os.path.join(root_dir, 'link_to_image') mountpoint = os.path.join(root_dir, 'mountpoint') with open(backing_image, 'w') as f: os.ftruncate(f.fileno(), 1024**3) rc, out, err = execCmd(['/sbin/mkfs.ext2', "-F", backing_image], raw=True) if rc != 0: raise RuntimeError("Error creating filesystem: %s" % err) os.symlink(backing_image, link_to_image) os.mkdir(mountpoint) m = mount.Mount(link_to_image, mountpoint) with loop_mount(m): self.assertTrue(m.isMounted())
def injectFilesToFs(floppy, files, fstype='auto'): if not os.path.abspath(floppy).startswith( os.path.join(_P_PAYLOAD_IMAGES, '')): raise ValueError('Image %s is not inside %s directory' % (floppy, _P_PAYLOAD_IMAGES)) dirname = None try: dirname = tempfile.mkdtemp() m = mount.Mount(floppy, dirname) m.mount(mntOpts='loop', vfstype=fstype) try: _decodeFilesIntoDir(files, dirname) finally: m.umount() finally: _commonCleanFs(dirname, floppy)
def test_mkFloppyFs(self, label): """ Tests mkimage.mkFloppyFs creating an image and checking its content. Requires root permissions for writing into the floppy image. """ floppy = mkimage.mkFloppyFs("vmId_floppy", self.files, label) self.assertTrue(os.path.exists(floppy)) m = mount.Mount(floppy, self.workdir) m.mount(mntOpts='loop') try: self._check_content(checkPerms=False) self._check_label(floppy, label) finally: m.umount(force=True, freeloop=True) # TODO: Use libudev to wait for specific event with stopwatch("Wait for udev events"): udevadm.settle(5)
def test_mkFloppyFs_overwrite(self, label): """ Test that mkimage.mkFloppyFs handle situation when the floppy image already exists. Requires root permissions for writing into the floppy image. """ floppy = mkimage.mkFloppyFs("vmId_floppy", self.files, label) self.assertTrue(os.path.exists(floppy)) # Now try again with the floppy image already in place floppy = mkimage.mkFloppyFs("vmId_floppy", self.files, label) self.assertTrue(os.path.exists(floppy)) m = mount.Mount(floppy, self.workdir) m.mount(mntOpts='loop') try: self._check_content(checkPerms=False) self._check_label(floppy, label) finally: m.umount()
def test_mkIsoFs(self, label): """ Tests mkimage.mkIsoFs creating an image and checking its content """ iso_img = mkimage.mkIsoFs("vmId_iso", self.files, label) self.assertTrue(os.path.exists(iso_img)) m = mount.Mount(iso_img, self.workdir) try: m.mount(mntOpts='loop') except mount.MountError as e: raise SkipTest("Error mounting iso image: %s" % e) try: self._check_content() self._check_label(iso_img, label) finally: m.umount(force=True, freeloop=True) # TODO: Use libudev to wait for specific event with stopwatch("Wait for udev events"): udevadm.settle(5)
def test_injectFilesToFs(self, fstype): """ Tests mkimage.injectFilesToFs creating an image and checking its content. Requires root permissions for writing into the floppy image. """ floppy = mkimage.getFileName("vmId_inject", self.files) command = [EXT_MKFS_MSDOS, '-C', floppy, '1440'] try: rc, out, err = execCmd(command, raw=True) mkimage.injectFilesToFs(floppy, self.files, fstype) self.assertTrue(os.path.exists(floppy)) m = mount.Mount(floppy, self.workdir) m.mount(mntOpts='loop') try: self._check_content(checkPerms=False) finally: m.umount() finally: rm_file(floppy)
def testSymlinkMount(self): with namedTemporaryDir() as root_dir: backing_image = os.path.join(root_dir, 'backing.img') link_to_image = os.path.join(root_dir, 'link_to_image') mountpoint = os.path.join(root_dir, 'mountpoint') with open(backing_image, 'w') as f: os.ftruncate(f.fileno(), 1024 ** 3) rc, out, err = execCmd(['/sbin/mkfs.ext2', "-F", backing_image], raw=True) if rc != 0: raise RuntimeError("Error creating filesystem: %s" % err) os.symlink(backing_image, link_to_image) os.mkdir(mountpoint) m = mount.Mount(link_to_image, mountpoint) m.mount(mntOpts="loop") try: self.assertTrue(m.isMounted()) finally: m.umount(force=True, freeloop=True) # TODO: Use libudev to wait for specific event with stopwatch("Wait for udev events"): udevadm.settle(5)
def mkFloppyFs(vmId, files, volumeName=None): floppy = dirname = None try: floppy = _getFileName(vmId, files) command = [EXT_MKFS_MSDOS, '-C', floppy, '1440'] if volumeName is not None: command.extend(['-n', volumeName]) rc, out, err = execCmd(command, raw=True) if rc: raise OSError(errno.EIO, "could not create floppy file: " "code %s, out %s\nerr %s" % (rc, out, err)) dirname = tempfile.mkdtemp() m = mount.Mount(floppy, dirname) m.mount(mntOpts='loop') try: _decodeFilesIntoDir(files, dirname) finally: m.umount(force=True, freeloop=True) finally: _commonCleanFs(dirname, floppy) return floppy
def test_ne_equal(self): m1 = mount.Mount("spec", "file") m2 = mount.Mount("spec", "file") self.assertFalse(m1 != m2, "%s should equal %s" % (m1, m2))
def test_eq_different(self, spec1, spec2, file1, file2): m1 = mount.Mount(spec1, file1) m2 = mount.Mount(spec2, file2) self.assertFalse(m1 == m2, "%s should not equal %s" % (m1, m2))
def test_eq_equal(self): m1 = mount.Mount("spec", "file") m2 = mount.Mount("spec", "file") self.assertTrue(m1 == m2, "%s should equal %s" % (m1, m2))
def test_not_equal_different_hash(self, spec1, spec2, file1, file2): m1 = mount.Mount(spec1, file1) m2 = mount.Mount(spec2, file2) self.assertNotEqual(hash(m1), hash(m2))
def test_equal_same_hash(self): m1 = mount.Mount("spec", "file") m2 = mount.Mount("spec", "file") self.assertEqual(hash(m1), hash(m2))
def test_is_mounted_normalize_kernel_mounts(self, mount_line): with fake_mounts([mount_line]): mnt = mount.Mount("server:/a/b", "/mnt/server:_a_b") self.assertTrue(mnt.isMounted())