test_parted.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import os
  2. import subprocess
  3. import infra.basetest
  4. class TestParted(infra.basetest.BRTest):
  5. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  6. """
  7. BR2_PACKAGE_E2FSPROGS=y
  8. BR2_PACKAGE_PARTED=y
  9. BR2_TARGET_ROOTFS_CPIO=y
  10. # BR2_TARGET_ROOTFS_TAR is not set
  11. """
  12. def test_run(self):
  13. # Prepare the disk image.
  14. disk_file = os.path.join(self.builddir, "images", "disk.img")
  15. self.emulator.logfile.write(f"Creating disk image: {disk_file}\n")
  16. self.emulator.logfile.flush()
  17. subprocess.check_call(
  18. ["dd", "if=/dev/zero", f"of={disk_file}", "bs=1M", "count=256"],
  19. stdout=self.emulator.logfile,
  20. stderr=self.emulator.logfile)
  21. # Run the emulator with a drive.
  22. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  23. self.emulator.boot(arch="armv5",
  24. kernel="builtin",
  25. options=[
  26. "-initrd", cpio_file,
  27. "-drive", f"file={disk_file},format=raw"])
  28. self.emulator.login()
  29. # We check the program can run.
  30. self.assertRunOk("parted --version")
  31. dev = "/dev/sda"
  32. parted = f"parted {dev}"
  33. # We print the partition list of our drive. Since the drive is
  34. # supposed to be blank, it should not have a partition table.
  35. part_list_cmd = f"{parted} print list"
  36. out, ret = self.emulator.run(part_list_cmd, timeout=10)
  37. self.assertEqual(ret, 0)
  38. self.assertIn("Partition Table: unknown", out)
  39. # We create a GPT partition table.
  40. mklabel_cmd = f"{parted} mklabel gpt"
  41. self.assertRunOk(mklabel_cmd, timeout=10)
  42. # We print again the partition list. We should now see our
  43. # partition table.
  44. out, ret = self.emulator.run(part_list_cmd, timeout=10)
  45. self.assertEqual(ret, 0)
  46. self.assertIn("Partition Table: gpt", out)
  47. # We create 3 partitions on our drive.
  48. partitions = [
  49. "MyPart1 ext2 1MiB 25%",
  50. "MyPart2 ext4 25% 50%",
  51. "MyPart3 ext4 50% 100%"
  52. ]
  53. for part in partitions:
  54. mkpart_cmd = f"{parted} mkpart {part}"
  55. self.assertRunOk(mkpart_cmd, timeout=10)
  56. # We print again the list of partitions, this time in machine
  57. # parseable format. We check we have our 3 partitions.
  58. cmd = f"parted -m {dev} print list"
  59. out, ret = self.emulator.run(cmd, timeout=10)
  60. self.assertEqual(ret, 0)
  61. for part in range(1, 4):
  62. self.assertTrue(out[1+part].startswith(f"{part}:"))
  63. self.assertTrue(out[1+part].endswith(f":MyPart{part}:;"))
  64. # We format our partitions.
  65. self.assertRunOk(f"mkfs.ext2 {dev}1", timeout=10)
  66. self.assertRunOk(f"mkfs.ext4 {dev}2", timeout=10)
  67. self.assertRunOk(f"mkfs.ext4 {dev}3", timeout=10)
  68. # We create a random data file in the temporary directory. It
  69. # will be the reference source file that will be copied later
  70. # on each of our filesystems.
  71. data_file = "data.bin"
  72. cmd = f"dd if=/dev/urandom of=/tmp/{data_file} bs=1M count=10"
  73. self.assertRunOk(cmd)
  74. # We compute the sha256 hash and save it for later.
  75. hash_cmd = f"sha256sum {data_file}"
  76. out, ret = self.emulator.run(f"( cd /tmp && {hash_cmd} )")
  77. self.assertEqual(ret, 0)
  78. data_sha256 = out[0]
  79. # For each partition, we create a mount point directory, mount
  80. # the filesystem, copy the reference data file in it, sync the
  81. # filesystem, and compute the sha256 hash of the file. This
  82. # sequence will exercise a bit the partitions and filesystems
  83. # in read/write operations.
  84. for part in range(1, 4):
  85. self.assertRunOk(f"mkdir -p /tmp/MyPart{part}")
  86. self.assertRunOk(f"mount {dev}{part} /tmp/MyPart{part}")
  87. self.assertRunOk(f"cp /tmp/{data_file} /tmp/MyPart{part}/")
  88. self.assertRunOk("sync")
  89. out, ret = self.emulator.run(f"( cd /tmp/MyPart{part} && {hash_cmd} )")
  90. self.assertEqual(ret, 0)
  91. self.assertEqual(out[0], data_sha256)