test_rust.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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["USER"] = "br-user"
  19. env["PATH"] = "{}:".format(os.path.join(hostdir, 'bin')) + env["PATH"]
  20. env["CARGO_HOME"] = os.path.join(hostdir, 'usr', 'share', 'cargo')
  21. env["RUST_TARGET_PATH"] = os.path.join(hostdir, 'etc', 'rustc')
  22. cargo = os.path.join(hostdir, 'bin', 'cargo')
  23. workdir = os.path.join(tempfile.mkdtemp(suffix='-br2-testing-rust'),
  24. self.crate)
  25. manifest = os.path.join(workdir, 'Cargo.toml')
  26. prog = os.path.join(workdir, 'target', self.target, 'debug', self.crate)
  27. cmd = [cargo, 'init', '--bin', '--vcs', 'none', '-vv', workdir]
  28. ret = subprocess.call(cmd,
  29. stdout=self.b.logfile,
  30. stderr=self.b.logfile,
  31. env=env)
  32. if ret != 0:
  33. raise SystemError("Cargo init failed")
  34. cmd = [
  35. cargo, 'build', '-vv', '--target', self.target,
  36. '--manifest-path', manifest
  37. ]
  38. ret = subprocess.call(cmd,
  39. stdout=self.b.logfile,
  40. stderr=self.b.logfile,
  41. env=env)
  42. if ret != 0:
  43. raise SystemError("Cargo build failed")
  44. shutil.copy(prog, os.path.join(self.builddir, 'target', 'usr', 'bin'))
  45. self.b.build()
  46. shutil.rmtree(workdir)
  47. class TestRustBin(TestRustBase):
  48. config = \
  49. """
  50. BR2_arm=y
  51. BR2_cortex_a9=y
  52. BR2_ARM_ENABLE_NEON=y
  53. BR2_ARM_ENABLE_VFP=y
  54. BR2_TOOLCHAIN_EXTERNAL=y
  55. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  56. BR2_SYSTEM_DHCP="eth0"
  57. BR2_TARGET_ROOTFS_CPIO=y
  58. # BR2_TARGET_ROOTFS_TAR is not set
  59. BR2_PACKAGE_HOST_RUSTC=y
  60. """
  61. def test_run(self):
  62. self.build_test_prog()
  63. self.login()
  64. self.assertRunOk(self.crate)
  65. class TestRust(TestRustBase):
  66. config = \
  67. """
  68. BR2_arm=y
  69. BR2_cortex_a9=y
  70. BR2_ARM_ENABLE_NEON=y
  71. BR2_ARM_ENABLE_VFP=y
  72. BR2_TOOLCHAIN_EXTERNAL=y
  73. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  74. BR2_SYSTEM_DHCP="eth0"
  75. BR2_TARGET_ROOTFS_CPIO=y
  76. # BR2_TARGET_ROOTFS_TAR is not set
  77. BR2_PACKAGE_HOST_RUSTC=y
  78. BR2_PACKAGE_HOST_RUST=y
  79. """
  80. def test_run(self):
  81. self.build_test_prog()
  82. self.login()
  83. self.assertRunOk(self.crate)