test_mtools.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import os
  2. import infra.basetest
  3. class TestMtools(infra.basetest.BRTest):
  4. # We use a glibc toolchain to have iconv conversion working for
  5. # codepage 850.
  6. config = \
  7. """
  8. BR2_arm=y
  9. BR2_TOOLCHAIN_EXTERNAL=y
  10. BR2_TOOLCHAIN_EXTERNAL_BOOTLIN=y
  11. BR2_TOOLCHAIN_EXTERNAL_BOOTLIN_ARMV5_EABI_GLIBC_STABLE=y
  12. BR2_PACKAGE_MTOOLS=y
  13. BR2_TARGET_ROOTFS_CPIO=y
  14. # BR2_TARGET_ROOTFS_TAR is not set
  15. """
  16. def test_run(self):
  17. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  18. self.emulator.boot(arch="armv5",
  19. kernel="builtin",
  20. options=["-initrd", cpio_file])
  21. self.emulator.login()
  22. dos_img = "dos-fat.img"
  23. mtools_opts = f"-i {dos_img}"
  24. self.assertRunOk("mtools --version")
  25. # Create an empty image file to hold the FAT partition
  26. self.assertRunOk(f"dd if=/dev/zero of={dos_img} bs=1M count=1")
  27. # Any Mtools command is expected to fail on an unformated
  28. # partition.
  29. cmd = f"minfo {mtools_opts} ::"
  30. _, exit_code = self.emulator.run(cmd)
  31. self.assertNotEqual(exit_code, 0)
  32. # Now, let's format the partition file to FAT
  33. self.assertRunOk(f"mformat {mtools_opts} ::")
  34. # Run some Mtools commands on this empty partition
  35. self.assertRunOk(f"minfo {mtools_opts} ::")
  36. self.assertRunOk(f"mdir {mtools_opts} ::")
  37. self.assertRunOk(f"mlabel {mtools_opts} -N 12345678 ::BUILDROOT")
  38. # Create a reference file on our Linux filesystem
  39. self.assertRunOk("echo 'Hello Buildroot!' > file1.txt")
  40. # Copy the reference file into the DOS image, then perform
  41. # various file manipulations
  42. self.assertRunOk(f"mcopy {mtools_opts} file1.txt ::file2.txt")
  43. self.assertRunOk(f"mcopy {mtools_opts} ::file2.txt ::file3.txt")
  44. self.assertRunOk(f"mdel {mtools_opts} ::file2.txt")
  45. self.assertRunOk(f"mren {mtools_opts} ::file3.txt ::file4.txt")
  46. self.assertRunOk(f"mmd {mtools_opts} ::dir1")
  47. self.assertRunOk(f"mmove {mtools_opts} ::file4.txt ::dir1")
  48. self.assertRunOk(f"mdir {mtools_opts} ::dir1")
  49. self.assertRunOk(f"mdu {mtools_opts} -a ::")
  50. # Copy back the file from the DOS image to the Linux
  51. # filesystem
  52. self.assertRunOk(f"mcopy {mtools_opts} ::dir1/file4.txt file5.txt")
  53. # We expect this last copied file to have the same content as
  54. # the reference one created at the beginning
  55. self.assertRunOk("cmp file1.txt file5.txt")
  56. # Delete a directory tree containing a file
  57. self.assertRunOk(f"mdeltree {mtools_opts} ::dir1")