test_xvisor.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import os
  2. import re
  3. import infra.basetest
  4. class TestXvisor(infra.basetest.BRTest):
  5. # RISC-V 64bit is the "simplest" configuration to run
  6. # Xvisor into QEmu.
  7. config = \
  8. """
  9. BR2_riscv=y
  10. BR2_TOOLCHAIN_EXTERNAL=y
  11. BR2_PACKAGE_XVISOR=y
  12. BR2_TARGET_ROOTFS_CPIO=y
  13. # BR2_TARGET_ROOTFS_TAR is not set
  14. BR2_TARGET_OPENSBI=y
  15. BR2_TARGET_OPENSBI_CUSTOM_VERSION=y
  16. BR2_TARGET_OPENSBI_CUSTOM_VERSION_VALUE="1.5"
  17. BR2_TARGET_OPENSBI_PLAT="generic"
  18. BR2_PACKAGE_HOST_QEMU=y
  19. BR2_PACKAGE_HOST_QEMU_SYSTEM_MODE=y
  20. """
  21. xvisor_prompt = "XVisor# "
  22. def expect_xvisor_prompt(self, timeout=-1):
  23. self.emulator.qemu.expect(self.xvisor_prompt, timeout=timeout)
  24. def run_xvisor_cmd(self, cmd, timeout=-1):
  25. exit_code = 0
  26. if timeout != -1:
  27. timeout *= self.emulator.timeout_multiplier
  28. self.emulator.qemu.sendline(cmd)
  29. self.expect_xvisor_prompt(timeout)
  30. output = self.emulator.qemu.before.replace("\r\r", "\r").splitlines()[1:]
  31. # Some Xvisor commands (like "sleep") might not
  32. # produce any output
  33. if len(output) > 0:
  34. last_line = output[-1]
  35. else:
  36. last_line = ""
  37. if last_line.startswith("Error:"):
  38. match = re.search(last_line, r"code (-?\d)")
  39. if match is None:
  40. exit_code = -1
  41. else:
  42. exit_code = int(match.group(1))
  43. return output, exit_code
  44. def assertXvRunOk(self, cmd, timeout=-1):
  45. out, exit_code = self.run_xvisor_cmd(cmd, timeout)
  46. self.assertEqual(
  47. exit_code,
  48. 0,
  49. "\nFailed to run xvisor command: {}\noutput was:\n{}".format(
  50. cmd, ' '+'\n '.join(out))
  51. )
  52. def test_run(self):
  53. opensbi = os.path.join(self.builddir, "images", "fw_jump.bin")
  54. xvisor = os.path.join(self.builddir, "images", "vmm.bin")
  55. initrd = os.path.join(self.builddir, "images", "rootfs.cpio")
  56. self.emulator.boot(arch="riscv64",
  57. kernel=xvisor,
  58. options=["-M", "virt", "-cpu", "rv64", "-m", "256M",
  59. "-bios", opensbi, "-initrd", initrd])
  60. # There is no emulator.login(), since we start directly in
  61. # Xvisor prompt.
  62. self.expect_xvisor_prompt()
  63. # Check Xvisor version.
  64. output, exit_code = self.run_xvisor_cmd("version")
  65. self.assertEqual(exit_code, 0)
  66. self.assertTrue(output[0].startswith("Xvisor"))
  67. # Check a basic echo.
  68. test_str = "Hello Buildroot!"
  69. output, exit_code = self.run_xvisor_cmd("echo " + test_str)
  70. self.assertEqual(exit_code, 0)
  71. self.assertEqual(output[0].strip(), test_str)
  72. # Check a nonexisting command fails.
  73. _, exit_code = self.run_xvisor_cmd("bad_command")
  74. self.assertNotEqual(exit_code, 0)
  75. # Check an error of a valid command.
  76. _, exit_code = self.run_xvisor_cmd("vfs ls /nodir")
  77. self.assertNotEqual(exit_code, 0)
  78. # We mount the initrd...
  79. self.assertXvRunOk("vfs mount initrd /")
  80. # Check we see an existing file/symlink "os-release" in
  81. # "/etc", from our mounted initrd.
  82. output, exit_code = self.run_xvisor_cmd("vfs ls /etc")
  83. self.assertEqual(exit_code, 0)
  84. self.assertIn("os-release", "\n".join(output))
  85. # Check the word "Buildroot" is in the /etc/issue file.
  86. output, exit_code = self.run_xvisor_cmd("vfs cat /etc/issue")
  87. self.assertEqual(exit_code, 0)
  88. self.assertIn("Buildroot", "\n".join(output))
  89. # Check qemu is seen in host info.
  90. output, exit_code = self.run_xvisor_cmd("host info")
  91. self.assertEqual(exit_code, 0)
  92. self.assertIn("qemu", "\n".join(output))
  93. # Run a batch of status commands...
  94. cmds = [
  95. "blockdev list",
  96. "rbd list",
  97. "module info 0",
  98. "wallclock get_time",
  99. "heap info",
  100. "thread list",
  101. "vcpu list",
  102. "vcpu dumpreg 0",
  103. "devtree node show /",
  104. "host cpu info",
  105. "host ram info",
  106. "host resources",
  107. "host bus_list",
  108. "host bus_device_list platform"
  109. ]
  110. for cmd in cmds:
  111. self.assertXvRunOk(cmd)