test_bcc.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. import time
  3. import infra.basetest
  4. class TestBcc(infra.basetest.BRTest):
  5. # This test is using a Kernel >= 5.2, so it will use
  6. # CONFIG_IKHEADERS. Those Kernel headers are unpacked from
  7. # "/sys/kernel/kheaders.tar.xz" with a "tar" invocation. The
  8. # Busybox "tar" command invoked by bcc fails to unpack the Kernel
  9. # tar archive. We need the GNU Tar package. The Kernel also needs
  10. # few extra config options, for running execsnoop.
  11. kern_fragment = \
  12. infra.filepath("tests/package/test_bcc/linux-bcc.fragment")
  13. config = \
  14. f"""
  15. BR2_aarch64=y
  16. BR2_TOOLCHAIN_EXTERNAL=y
  17. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  18. BR2_LINUX_KERNEL=y
  19. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  20. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.6.32"
  21. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  22. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  23. BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{kern_fragment}"
  24. BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
  25. BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
  26. BR2_PACKAGE_BCC=y
  27. BR2_PACKAGE_TAR=y
  28. BR2_TARGET_ROOTFS_EXT2=y
  29. BR2_TARGET_ROOTFS_EXT2_4=y
  30. BR2_TARGET_ROOTFS_EXT2_SIZE="256M"
  31. # BR2_TARGET_ROOTFS_TAR is not set
  32. """
  33. def test_run(self):
  34. drive = os.path.join(self.builddir, "images", "rootfs.ext4")
  35. kern = os.path.join(self.builddir, "images", "Image")
  36. self.emulator.boot(arch="aarch64",
  37. kernel=kern,
  38. kernel_cmdline=["root=/dev/vda console=ttyAMA0"],
  39. options=["-M", "virt", "-cpu", "cortex-a57", "-m", "256M",
  40. "-drive", f"file={drive},if=virtio,format=raw"])
  41. self.emulator.login()
  42. log = "/root/execsnoop.log"
  43. test_cmd = "/bin/sleep 1"
  44. # bcc needs debugs to be mounted.
  45. self.assertRunOk("mount -t debugfs none /sys/kernel/debug/")
  46. # Generate some exec()s activity in background. We explicitly
  47. # call for "/bin/sleep" rather than just "sleep" to avoid
  48. # using any shell builtin and make sure we will exec() the
  49. # binary.
  50. cmd = f"while true ; do {test_cmd} ; done &"
  51. self.assertRunOk(cmd)
  52. # Run execsnoop, also in background...
  53. cmd = f"/usr/share/bcc/tools/execsnoop > {log} &"
  54. self.assertRunOk(cmd)
  55. for attempt in range(3):
  56. # Wait a bit, to let execsnoop to start and log some data.
  57. time.sleep(40 * self.timeout_multiplier)
  58. # We check that the log file contains some data.
  59. cmd = f"test -s {log}"
  60. _, ret = self.emulator.run(cmd)
  61. if ret == 0:
  62. break
  63. else:
  64. self.fail(f"Timeout while waiting for data in {log}.")
  65. # Kill our background execsnoop execution.
  66. self.assertRunOk("kill $!")
  67. # Check we have captured execution occurrences of out test
  68. # command.
  69. cmd = f"grep -Foc '{test_cmd}' {log}"
  70. out, ret = self.emulator.run(cmd)
  71. self.assertEqual(ret, 0)
  72. self.assertGreater(int(out[0]), 0)