2
1

test_compressor_base.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import os
  2. import infra.basetest
  3. class TestCompressorBase(infra.basetest.BRTest):
  4. """Common class to test a data compression/decompression package.
  5. Build an image containing the package enabled in config, start the
  6. emulator, login to it. It prepares a test data file with some
  7. redundancy to let compression program reduce the file size.
  8. Each test case that inherits from this class must have:
  9. __test__ = True - to let nose2 know that it is a test case
  10. config - defconfig fragment with the packages to run the test
  11. compress_cmd - the compression program command (ex: "gzip")
  12. it can also contain arguments (ex: "gzip -9")
  13. It also can have:
  14. decompress_cmd - the decompression program (ex: "gunzip")
  15. if unset, the default value is "un" appended with the
  16. value of "compress_cmd".
  17. check_integrity_cmd
  18. - the integrity check command (ex: "gzip -t")
  19. in unset, the default value is "compress_cmd" appended
  20. with " -t".
  21. compressed_file_ext
  22. - the file extension of compressed files created by the
  23. compress command. (ex: ".gz")
  24. if unset, the default value is a dot "." appended with
  25. the value of "compress_cmd".
  26. timeout - timeout to the compression command. Some compression
  27. program can take more time than the default value
  28. (set to 5 seconds).
  29. """
  30. __test__ = False
  31. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  32. """
  33. BR2_TARGET_ROOTFS_CPIO=y
  34. # BR2_TARGET_ROOTFS_TAR is not set
  35. """
  36. compress_cmd = "compressor-unknown"
  37. decompress_cmd = None
  38. check_integrity_cmd = None
  39. compressed_file_ext = None
  40. timeout = 5
  41. def __init__(self, names):
  42. """Setup common test variables."""
  43. super(TestCompressorBase, self).__init__(names)
  44. if self.decompress_cmd is None:
  45. self.decompress_cmd = "un" + self.compress_cmd
  46. if self.check_integrity_cmd is None:
  47. self.check_integrity_cmd = self.compress_cmd + " -t"
  48. if self.compressed_file_ext is None:
  49. self.compressed_file_ext = "." + self.compress_cmd
  50. self.ref_file = "reference.bin"
  51. self.test_file = "test.bin"
  52. self.comp_test_file = self.test_file + self.compressed_file_ext
  53. def login(self):
  54. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  55. self.emulator.boot(arch="armv5",
  56. kernel="builtin",
  57. options=["-initrd", cpio_file])
  58. self.emulator.login()
  59. def test_run(self):
  60. self.login()
  61. self.prepare_data()
  62. self.compress_test()
  63. self.check_integrity_test()
  64. self.uncompress_test()
  65. self.compare_test()
  66. def prepare_data(self):
  67. # Create a data file composed of: 128kB of random data, then
  68. # 128kB of zeroes, then a repetition of the 1st 128kB of
  69. # random.
  70. self.assertRunOk("dd if=/dev/urandom of=rand.bin bs=128k count=1")
  71. self.assertRunOk("dd if=/dev/zero of=zeroes.bin bs=128k count=1")
  72. self.assertRunOk(f"cat rand.bin zeroes.bin rand.bin > {self.ref_file}")
  73. # Keep a copy of the reference data since
  74. # compressor/decompressor programs usually unlink source data
  75. # file.
  76. self.assertRunOk(f"cp {self.ref_file} {self.test_file}")
  77. def compress_test(self):
  78. # Run the compression
  79. self.assertRunOk(f"{self.compress_cmd} {self.test_file}", timeout=self.timeout)
  80. # Check that the compressed file was created with the expected name
  81. self.assertRunOk(f"test -e {self.comp_test_file}")
  82. # Remove the input test file. Some compressors (like gzip) are
  83. # removing it automatically by default. Some others (like lz4)
  84. # are keeping those by default. We always remove it to make
  85. # sure a new file will be recreated by the decompression.
  86. self.assertRunOk(f"rm -f {self.test_file}")
  87. # Check the compressed file is smaller than the input.
  88. # The "ls -l" command is for simplifying debugging
  89. self.assertRunOk(f"ls -l {self.ref_file} {self.comp_test_file}")
  90. ref_sz_cmd = f"wc -c < {self.ref_file}"
  91. comp_sz_cmd = f"wc -c < {self.comp_test_file}"
  92. self.assertRunOk(f"test $({ref_sz_cmd}) -gt $({comp_sz_cmd})")
  93. def check_integrity_test(self):
  94. # Check the compressed file integrity
  95. self.assertRunOk(f"{self.check_integrity_cmd} {self.comp_test_file}")
  96. def uncompress_test(self):
  97. # Run the decompression
  98. self.assertRunOk(f"{self.decompress_cmd} {self.comp_test_file}")
  99. # Check the decompressed file was created with the correct name
  100. self.assertRunOk(f"test -e {self.test_file}")
  101. def compare_test(self):
  102. # Check the decompressed file is exactly the same as the
  103. # reference created at the beginning
  104. self.assertRunOk(f"cmp {self.test_file} {self.ref_file}")