test_pv.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. import infra.basetest
  3. class TestPv(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_PV=y
  7. BR2_TARGET_ROOTFS_CPIO=y
  8. # BR2_TARGET_ROOTFS_TAR is not set
  9. """
  10. def test_run(self):
  11. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  12. self.emulator.boot(arch="armv5",
  13. kernel="builtin",
  14. options=["-initrd", cpio_file])
  15. self.emulator.login()
  16. # We check the program can run.
  17. self.assertRunOk("pv --version")
  18. # We check that "pv" works like the "cat" command. We print a
  19. # message on its standard input and redirect its output to a
  20. # file. We also force the pv terminal width to 80, since
  21. # "infra.emulator" is setting a large width to prevent
  22. # line wrapping.
  23. msg = "Hello Buildroot!"
  24. out_file = "/tmp/out.txt"
  25. cmd = f"echo '{msg}' | pv -w80 > {out_file}"
  26. self.assertRunOk(cmd)
  27. # We check the pv output file contains exactly our message.
  28. cmd = f"cat {out_file}"
  29. out, ret = self.emulator.run(cmd)
  30. self.assertEqual(ret, 0)
  31. self.assertEqual(out[0], msg)
  32. # Finally, we check that "pv" correctly shows a progress
  33. # bar. We print few lines, one per second into "pv" setup in
  34. # line mode. We check the last pv status line contains the
  35. # correct count and a "100%" string that shows completion.
  36. lines = 5
  37. print_ln_cmd = f"( for X in $(seq {lines}) ; do echo $X ; sleep 1 ; done )"
  38. cmd = f"{print_ln_cmd} | pv -s{lines} -l -w80 > /dev/null"
  39. out, ret = self.emulator.run(cmd, timeout=10)
  40. self.assertEqual(ret, 0)
  41. # pv updates status may contain extra spaces, and is updated
  42. # with carriage return characters. We strip lines and filter
  43. # out empty remaining lines, to make sure we get the last
  44. # meaningful status line.
  45. pv_out = [ln.strip() for ln in out]
  46. pv_out = [ln for ln in pv_out if ln]
  47. last_line = pv_out[-1]
  48. self.assertTrue(last_line.startswith(str(lines)))
  49. self.assertTrue(last_line.endswith("100%"))