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