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