test_kmod.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import os
  2. import infra.basetest
  3. class TestKmod(infra.basetest.BRTest):
  4. # This test uses the "virtio_net" driver compiled as a module. We
  5. # need to recompile a Kernel for that.
  6. kernel_fragment = \
  7. infra.filepath("tests/package/test_kmod/linux-virtio-net.fragment")
  8. config = \
  9. f"""
  10. BR2_aarch64=y
  11. BR2_TOOLCHAIN_EXTERNAL=y
  12. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  13. BR2_LINUX_KERNEL=y
  14. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  15. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.6.35"
  16. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  17. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  18. BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{kernel_fragment}"
  19. BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
  20. BR2_PACKAGE_KMOD=y
  21. BR2_PACKAGE_KMOD_TOOLS=y
  22. BR2_TARGET_ROOTFS_EXT2=y
  23. BR2_TARGET_ROOTFS_EXT2_4=y
  24. # BR2_TARGET_ROOTFS_TAR is not set
  25. """
  26. def test_run(self):
  27. drive = os.path.join(self.builddir, "images", "rootfs.ext4")
  28. kern = os.path.join(self.builddir, "images", "Image")
  29. self.emulator.boot(arch="aarch64",
  30. kernel=kern,
  31. kernel_cmdline=["root=/dev/vda console=ttyAMA0"],
  32. options=["-M", "virt",
  33. "-cpu", "cortex-a57",
  34. "-m", "256M",
  35. "-drive", f"file={drive},if=virtio,format=raw"])
  36. self.emulator.login()
  37. # We check the kmod program can run. Busybox does not have a
  38. # kmod applet, so there is no possible confusion.
  39. self.assertRunOk("kmod --version")
  40. # We check the "modprobe" is the one from kmod, rather than
  41. # the Busybox applet version.
  42. out, ret = self.emulator.run("modprobe --version")
  43. self.assertEqual(ret, 0)
  44. self.assertTrue(out[0].startswith("kmod version"))
  45. # List modules with "kmod list", the virtio-net module should
  46. # NOT be loaded yet.
  47. out, ret = self.emulator.run("kmod list")
  48. self.assertEqual(ret, 0)
  49. self.assertNotIn("virtio_net", "\n".join(out))
  50. # Get module info with modinfo.
  51. out, ret = self.emulator.run("modinfo virtio-net")
  52. self.assertEqual(ret, 0)
  53. lsmod_out = "\n".join(out)
  54. self.assertRegex(lsmod_out, r'name: *virtio_net')
  55. self.assertRegex(lsmod_out, r'description: *Virtio network driver')
  56. # With this test configuration, we are not supposed to have an
  57. # eth0 Ethernet interface yet. Attempting to show info on this
  58. # interface is expected to fail .
  59. _, ret = self.emulator.run("ip link show dev eth0")
  60. self.assertNotEqual(ret, 0)
  61. # We try to load the module.
  62. self.assertRunOk("modprobe virtio-net")
  63. # We should now see the module in the list. This time, we use
  64. # the "lsmod" command.
  65. out, ret = self.emulator.run("lsmod")
  66. self.assertEqual(ret, 0)
  67. self.assertIn("virtio_net", "\n".join(out))
  68. # The eth0 interface is supposed to be available, after the
  69. # module loading. We configure the emulator user network to
  70. # test the driver networking functionality. See:
  71. # https://wiki.qemu.org/Documentation/Networking
  72. self.assertRunOk("ip addr add dev eth0 10.0.2.15/24")
  73. self.assertRunOk("ip link set dev eth0 up")
  74. # We check we can ping the emulator.
  75. ping_cmd = "ping -i 0.3 -c 2 10.0.2.2"
  76. self.assertRunOk(ping_cmd)
  77. # We check we can unload the driver.
  78. self.assertRunOk("modprobe -r virtio-net")
  79. # Now the driver is unloaded, we should no longer be able to
  80. # ping the emulator.
  81. _, ret = self.emulator.run(ping_cmd)
  82. self.assertNotEqual(ret, 0)