test_rust.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import os
  2. import tempfile
  3. import subprocess
  4. import shutil
  5. import infra.basetest
  6. class TestRustBase(infra.basetest.BRTest):
  7. target = 'armv7-unknown-linux-gnueabihf'
  8. crate = 'hello-world'
  9. def login(self):
  10. img = os.path.join(self.builddir, "images", "rootfs.cpio")
  11. self.emulator.boot(arch="armv7",
  12. kernel="builtin",
  13. options=["-initrd", img])
  14. self.emulator.login()
  15. def build_test_prog(self):
  16. hostdir = os.path.join(self.builddir, 'host')
  17. env = os.environ.copy()
  18. env["PATH"] = "{}:".format(os.path.join(hostdir, 'bin')) + env["PATH"]
  19. env["CARGO_HOME"] = os.path.join(hostdir, 'usr', 'share', 'cargo')
  20. env["RUST_TARGET_PATH"] = os.path.join(hostdir, 'etc', 'rustc')
  21. cargo = os.path.join(hostdir, 'bin', 'cargo')
  22. workdir = os.path.join(tempfile.mkdtemp(suffix='-br2-testing-rust'),
  23. self.crate)
  24. manifest = os.path.join(workdir, 'Cargo.toml')
  25. prog = os.path.join(workdir, 'target', self.target, 'debug', self.crate)
  26. cmd = [cargo, 'init', '--bin', '--vcs', 'none', '-vv', workdir]
  27. ret = subprocess.call(cmd,
  28. stdout=self.b.logfile,
  29. stderr=self.b.logfile,
  30. env=env)
  31. if ret != 0:
  32. raise SystemError("Cargo init failed")
  33. cmd = [
  34. cargo, 'build', '-vv', '--target', self.target,
  35. '--manifest-path', manifest
  36. ]
  37. ret = subprocess.call(cmd,
  38. stdout=self.b.logfile,
  39. stderr=self.b.logfile,
  40. env=env)
  41. if ret != 0:
  42. raise SystemError("Cargo build failed")
  43. shutil.copy(prog, os.path.join(self.builddir, 'target', 'usr', 'bin'))
  44. self.b.build()
  45. shutil.rmtree(workdir)
  46. def test_run(self):
  47. self.build_test_prog()
  48. self.login()
  49. _, exit_code = self.emulator.run(self.crate)
  50. self.assertEqual(exit_code, 0)
  51. class TestRustBin(TestRustBase):
  52. config = \
  53. """
  54. BR2_arm=y
  55. BR2_cortex_a9=y
  56. BR2_ARM_ENABLE_NEON=y
  57. BR2_ARM_ENABLE_VFP=y
  58. BR2_TOOLCHAIN_EXTERNAL=y
  59. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  60. BR2_SYSTEM_DHCP="eth0"
  61. BR2_LINUX_KERNEL=y
  62. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  63. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="4.11.3"
  64. BR2_LINUX_KERNEL_DEFCONFIG="vexpress"
  65. BR2_LINUX_KERNEL_DTS_SUPPORT=y
  66. BR2_LINUX_KERNEL_INTREE_DTS_NAME="vexpress-v2p-ca9"
  67. BR2_TARGET_ROOTFS_CPIO=y
  68. # BR2_TARGET_ROOTFS_TAR is not set
  69. BR2_PACKAGE_HOST_CARGO=y
  70. BR2_PACKAGE_HOST_RUSTC=y
  71. """
  72. class TestRust(TestRustBase):
  73. config = \
  74. """
  75. BR2_arm=y
  76. BR2_cortex_a9=y
  77. BR2_ARM_ENABLE_NEON=y
  78. BR2_ARM_ENABLE_VFP=y
  79. BR2_TOOLCHAIN_EXTERNAL=y
  80. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  81. BR2_SYSTEM_DHCP="eth0"
  82. BR2_LINUX_KERNEL=y
  83. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  84. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="4.11.3"
  85. BR2_LINUX_KERNEL_DEFCONFIG="vexpress"
  86. BR2_LINUX_KERNEL_DTS_SUPPORT=y
  87. BR2_LINUX_KERNEL_INTREE_DTS_NAME="vexpress-v2p-ca9"
  88. BR2_TARGET_ROOTFS_CPIO=y
  89. # BR2_TARGET_ROOTFS_TAR is not set
  90. BR2_PACKAGE_HOST_CARGO=y
  91. BR2_PACKAGE_HOST_RUSTC=y
  92. BR2_PACKAGE_HOST_RUST=y
  93. """