blob: 74c1c4811732a1db0cc7fe3984c0a43285524197 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
6
Brad Bishop316dfdd2018-06-25 12:45:53 -04007import 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
Andrew Geissler90fd73c2021-03-05 15:25:55 -060051 def run_sdk_go_command(self, gocmd, proj, name):
52 cmd = "cd %s/src/%s/%s; " % (self.go_path, proj, name)
Brad Bishop316dfdd2018-06-25 12:45:53 -040053 cmd = cmd + ". %s; " % self.env_SDK
54 cmd = cmd + "export GOPATH=%s; " % self.go_path
Patrick Williamsdb4c27e2022-08-05 08:10:29 -050055 cmd = cmd + "export GOFLAGS=-modcacherw; "
56 cmd = cmd + "export CGO_ENABLED=1; "
Brad Bishop316dfdd2018-06-25 12:45:53 -040057 cmd = cmd + "${CROSS_COMPILE}go %s" % gocmd
58 return runCmd(cmd).status
59
60 def test_go_dep_build(self):
Andrew Geissler90fd73c2021-03-05 15:25:55 -060061 proj = "github.com/direnv"
62 name = "direnv"
63 ver = "v2.27.0"
Brad Bishop316dfdd2018-06-25 12:45:53 -040064 archive = ".tar.gz"
65 url = "https://%s/%s/archive/%s%s" % (proj, name, ver, archive)
66
67 runCmd("cd %s; wget %s" % (self.tmpdir_SDKQA, url))
68 runCmd("cd %s; tar -xf %s" % (self.tmpdir_SDKQA, ver+archive))
69 runCmd("mkdir -p %s/src/%s" % (self.go_path, proj))
Andrew Geissler90fd73c2021-03-05 15:25:55 -060070 runCmd("mv %s/direnv-2.27.0 %s/src/%s/%s"
Brad Bishop316dfdd2018-06-25 12:45:53 -040071 % (self.tmpdir_SDKQA, self.go_path, proj, name))
Andrew Geissler90fd73c2021-03-05 15:25:55 -060072 retv = self.run_sdk_go_command('build', proj, name)
Brad Bishop316dfdd2018-06-25 12:45:53 -040073 self.assertEqual(retv, 0,
74 msg="Running go build failed for %s" % name)