summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/sdk/cases/go.py
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oeqa/sdk/cases/go.py')
-rw-r--r--meta/lib/oeqa/sdk/cases/go.py107
1 files changed, 107 insertions, 0 deletions
diff --git a/meta/lib/oeqa/sdk/cases/go.py b/meta/lib/oeqa/sdk/cases/go.py
new file mode 100644
index 0000000000..a050df7a9f
--- /dev/null
+++ b/meta/lib/oeqa/sdk/cases/go.py
@@ -0,0 +1,107 @@
1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7import os
8import shutil
9import unittest
10
11from oeqa.core.utils.path import remove_safe
12from oeqa.sdk.case import OESDKTestCase
13
14from oeqa.utils.subprocesstweak import errors_have_output
15from oe.go import map_arch
16errors_have_output()
17
18class GoCompileTest(OESDKTestCase):
19 td_vars = ['MACHINE', 'TARGET_ARCH']
20
21 @classmethod
22 def setUpClass(self):
23 # Copy test.go file to SDK directory (same as GCC test uses files_dir)
24 shutil.copyfile(os.path.join(self.tc.files_dir, 'test.go'),
25 os.path.join(self.tc.sdk_dir, 'test.go'))
26
27 def setUp(self):
28 target_arch = self.td.get("TARGET_ARCH")
29 # Check for go-cross-canadian package (uses target architecture)
30 if not self.tc.hasHostPackage("go-cross-canadian-%s" % target_arch):
31 raise unittest.SkipTest("GoCompileTest class: SDK doesn't contain a Go cross-canadian toolchain")
32
33 # Additional runtime check for go command availability
34 try:
35 self._run('which go')
36 except Exception as e:
37 raise unittest.SkipTest("GoCompileTest class: go command not available: %s" % str(e))
38
39 def test_go_build(self):
40 """Test Go build command (native compilation)"""
41 self._run('cd %s; go build -o test test.go' % self.tc.sdk_dir)
42
43 def test_go_module(self):
44 """Test Go module creation and building"""
45 # Create a simple Go module
46 self._run('cd %s; go mod init hello-go' % self.tc.sdk_dir)
47 self._run('cd %s; go build -o hello-go' % self.tc.sdk_dir)
48
49 @classmethod
50 def tearDownClass(self):
51 files = [os.path.join(self.tc.sdk_dir, f) \
52 for f in ['test.go', 'test', 'hello-go', 'go.mod', 'go.sum']]
53 for f in files:
54 remove_safe(f)
55
56class GoHostCompileTest(OESDKTestCase):
57 td_vars = ['MACHINE', 'SDK_SYS', 'TARGET_ARCH']
58
59 @classmethod
60 def setUpClass(self):
61 # Copy test.go file to SDK directory (same as GCC test uses files_dir)
62 shutil.copyfile(os.path.join(self.tc.files_dir, 'test.go'),
63 os.path.join(self.tc.sdk_dir, 'test.go'))
64
65 def setUp(self):
66 target_arch = self.td.get("TARGET_ARCH")
67 # Check for go-cross-canadian package (uses target architecture)
68 if not self.tc.hasHostPackage("go-cross-canadian-%s" % target_arch):
69 raise unittest.SkipTest("GoHostCompileTest class: SDK doesn't contain a Go cross-canadian toolchain")
70
71 # Additional runtime check for go command availability
72 try:
73 self._run('which go')
74 except Exception as e:
75 raise unittest.SkipTest("GoHostCompileTest class: go command not available: %s" % str(e))
76
77 def _get_go_arch(self):
78 """Get Go architecture from SDK_SYS"""
79 sdksys = self.td.get("SDK_SYS")
80 arch = sdksys.split('-')[0]
81
82 # Use mapping for other architectures
83 return map_arch(arch)
84
85 def test_go_cross_compile(self):
86 """Test Go cross-compilation for target"""
87 goarch = self._get_go_arch()
88 self._run('cd %s; GOOS=linux GOARCH=%s go build -o test-%s test.go' % (self.tc.sdk_dir, goarch, goarch))
89
90 def test_go_module_cross_compile(self):
91 """Test Go module cross-compilation"""
92 goarch = self._get_go_arch()
93 self._run('cd %s; go mod init hello-go' % self.tc.sdk_dir)
94 self._run('cd %s; GOOS=linux GOARCH=%s go build -o hello-go-%s' % (self.tc.sdk_dir, goarch, goarch))
95
96 @classmethod
97 def tearDownClass(self):
98 # Clean up files with dynamic architecture names
99 files = [os.path.join(self.tc.sdk_dir, f) \
100 for f in ['test.go', 'go.mod', 'go.sum']]
101 # Add common architecture-specific files that might be created
102 common_archs = ['arm64', 'arm', 'amd64', '386', 'mips', 'mipsle', 'ppc64', 'ppc64le', 'riscv64']
103 for arch in common_archs:
104 files.extend([os.path.join(self.tc.sdk_dir, f) \
105 for f in ['test-%s' % arch, 'hello-go-%s' % arch]])
106 for f in files:
107 remove_safe(f)