def joinpaths_test(self): self.assertEqual(joinpaths("foo", "bar", "baz"), "foo/bar/baz") with tempfile.TemporaryDirectory() as tdname: open(os.path.join(tdname, "real-file"), "w").write("lorax test file") os.symlink(os.path.join(tdname, "real-file"), os.path.join(tdname, "link-file")) self.assertEqual(joinpaths(tdname, "link-file", follow_symlinks=True), os.path.join(tdname, "real-file"))
def mkfakebootdir(bootdir): """Populate a fake /boot directory with a kernel and initrd :param bootdir: An existing directory to create files/dirs under :type bootdir: str """ open(joinpaths(bootdir, "vmlinuz-4.18.13-200.fc28.x86_64"), "w").write("I AM A FAKE KERNEL") open(joinpaths(bootdir, "initramfs-4.18.13-200.fc28.x86_64.img"), "w").write("I AM A FAKE INITRD")
def mkfakerootdir(rootdir): """Populate a fake rootdir with a few directories and files :param rootdir: An existing directory to create files/dirs under :type rootdir: str Use this for testing the mk* functions that compress a directory tree """ dirs = ["/root", "/usr/sbin/", "/usr/local/", "/home/bart", "/etc/"] files = ["/etc/passwd", "/home/bart/.bashrc", "/root/.bashrc"] for d in dirs: os.makedirs(joinpaths(rootdir, d)) for f in files: if not os.path.isdir(joinpaths(rootdir, os.path.dirname(f))): os.makedirs(joinpaths(rootdir, os.path.dirname(f))) open(joinpaths(rootdir, f), "w").write("I AM FAKE FILE %s" % f.upper())
def partition_mount_test(self): """Test PartitionMount context manager (requires loop)""" with tempfile.NamedTemporaryFile( prefix="lorax.test.disk.") as disk_img: self.assertTrue(mkfakediskimg(disk_img.name)) # Make sure it can mount the / with /etc/passwd with PartitionMount(disk_img.name) as img_mount: self.assertTrue(img_mount is not None) self.assertTrue(os.path.isdir(img_mount.mount_dir)) self.assertTrue( os.path.exists( joinpaths(img_mount.mount_dir, "/etc/passwd"))) # Make sure submount works with PartitionMount(disk_img.name, submount="/a-sub-mount/") as img_mount: self.assertTrue(img_mount is not None) self.assertTrue(os.path.isdir(img_mount.mount_dir)) self.assertTrue( os.path.exists( joinpaths(img_mount.mount_dir, "/etc/passwd"))) # Make sure it can mount the /boot partition with a custom mount_ok function def mount_ok(mount_dir): kernels = glob.glob(joinpaths(mount_dir, "vmlinuz-*")) return len(kernels) > 0 with PartitionMount(disk_img.name, mount_ok=mount_ok) as img_mount: self.assertTrue(img_mount is not None) self.assertTrue(os.path.isdir(img_mount.mount_dir)) self.assertFalse( os.path.exists( joinpaths(img_mount.mount_dir, "/etc/passwd"))) self.assertTrue( os.path.exists( joinpaths(img_mount.mount_dir, "vmlinuz-4.18.13-200.fc28.x86_64"))) self.assertTrue( os.path.exists( joinpaths(img_mount.mount_dir, "initramfs-4.18.13-200.fc28.x86_64.img")))
def mount_ok(mount_dir): kernels = glob.glob(joinpaths(mount_dir, "vmlinuz-*")) return len(kernels) > 0