1#
2# SPDX-License-Identifier: MIT
3#
4
5import glob
6import os
7import shutil
8import tempfile
9from oeqa.selftest.case import OESelftestTestCase
10from oeqa.utils.commands import runCmd, bitbake, get_bb_vars
11
12
13class oeGoToolchainSelfTest(OESelftestTestCase):
14    """
15    Test cases for OE's Go toolchain
16    """
17
18    @staticmethod
19    def get_sdk_environment(tmpdir_SDKQA):
20        pattern = os.path.join(tmpdir_SDKQA, "environment-setup-*")
21        # FIXME: this is a very naive implementation
22        return glob.glob(pattern)[0]
23
24    @staticmethod
25    def get_sdk_toolchain():
26        bb_vars = get_bb_vars(['SDK_DEPLOY', 'TOOLCHAIN_OUTPUTNAME'],
27                              "meta-go-toolchain")
28        sdk_deploy = bb_vars['SDK_DEPLOY']
29        toolchain_name = bb_vars['TOOLCHAIN_OUTPUTNAME']
30        return os.path.join(sdk_deploy, toolchain_name + ".sh")
31
32    @classmethod
33    def setUpClass(cls):
34        super(oeGoToolchainSelfTest, cls).setUpClass()
35        cls.tmpdir_SDKQA = tempfile.mkdtemp(prefix='SDKQA')
36        cls.go_path = os.path.join(cls.tmpdir_SDKQA, "go")
37        # Build the SDK and locate it in DEPLOYDIR
38        bitbake("meta-go-toolchain")
39        cls.sdk_path = oeGoToolchainSelfTest.get_sdk_toolchain()
40        # Install the SDK into the tmpdir
41        runCmd("sh %s -y -d \"%s\"" % (cls.sdk_path, cls.tmpdir_SDKQA))
42        cls.env_SDK = oeGoToolchainSelfTest.get_sdk_environment(cls.tmpdir_SDKQA)
43
44    @classmethod
45    def tearDownClass(cls):
46        shutil.rmtree(cls.tmpdir_SDKQA, ignore_errors=True)
47        super(oeGoToolchainSelfTest, cls).tearDownClass()
48
49    def run_sdk_go_command(self, gocmd, proj, name):
50        cmd = "cd %s/src/%s/%s; " % (self.go_path, proj, name)
51        cmd = cmd + ". %s; " % self.env_SDK
52        cmd = cmd + "export GOPATH=%s; " % self.go_path
53        cmd = cmd + "${CROSS_COMPILE}go %s" % gocmd
54        return runCmd(cmd).status
55
56    def test_go_dep_build(self):
57        proj = "github.com/direnv"
58        name = "direnv"
59        ver = "v2.27.0"
60        archive = ".tar.gz"
61        url = "https://%s/%s/archive/%s%s" % (proj, name, ver, archive)
62
63        runCmd("cd %s; wget %s" % (self.tmpdir_SDKQA, url))
64        runCmd("cd %s; tar -xf %s" % (self.tmpdir_SDKQA, ver+archive))
65        runCmd("mkdir -p %s/src/%s" % (self.go_path, proj))
66        runCmd("mv %s/direnv-2.27.0 %s/src/%s/%s"
67               % (self.tmpdir_SDKQA, self.go_path, proj, name))
68        retv = self.run_sdk_go_command('build', proj, name)
69        self.assertEqual(retv, 0,
70                         msg="Running go build failed for %s" % name)
71