diff options
| author | Osama Abdelkader <osama.abdelkader@gmail.com> | 2025-09-14 20:25:50 +0200 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2025-09-18 11:16:34 +0100 |
| commit | 9479aac3157c3fae77843bcfc11202e301109f1b (patch) | |
| tree | 7a8869a162e99d36e59a9a4d40dc529495128b35 | |
| parent | af4a5882901610f9e09c3731469a7a065ace41a8 (diff) | |
| download | poky-9479aac3157c3fae77843bcfc11202e301109f1b.tar.gz | |
go: add sdk test
- Add meta/lib/oeqa/sdk/cases/go.py with GoCompileTest and GoHostCompileTest classes
- Test validates Go cross-compilation toolchain functionality
- Includes native compilation, cross-compilation, and Go module support
- Uses dynamic architecture detection for portability
(From OE-Core rev: 17015f692a6bf3697a89db51bbc4673a5efa1497)
Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
Signed-off-by: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
| -rw-r--r-- | meta/lib/oeqa/sdk/cases/go.py | 128 |
1 files changed, 128 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..9c15124f6a --- /dev/null +++ b/meta/lib/oeqa/sdk/cases/go.py | |||
| @@ -0,0 +1,128 @@ | |||
| 1 | # | ||
| 2 | # Copyright OpenEmbedded Contributors | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: MIT | ||
| 5 | # | ||
| 6 | |||
| 7 | import os | ||
| 8 | import shutil | ||
| 9 | import unittest | ||
| 10 | |||
| 11 | from oeqa.core.utils.path import remove_safe | ||
| 12 | from oeqa.sdk.case import OESDKTestCase | ||
| 13 | |||
| 14 | from oeqa.utils.subprocesstweak import errors_have_output | ||
| 15 | errors_have_output() | ||
| 16 | |||
| 17 | class GoCompileTest(OESDKTestCase): | ||
| 18 | td_vars = ['MACHINE', 'TARGET_ARCH'] | ||
| 19 | |||
| 20 | @classmethod | ||
| 21 | def setUpClass(self): | ||
| 22 | # Copy test.go file to SDK directory (same as GCC test uses files_dir) | ||
| 23 | shutil.copyfile(os.path.join(self.tc.files_dir, 'test.go'), | ||
| 24 | os.path.join(self.tc.sdk_dir, 'test.go')) | ||
| 25 | |||
| 26 | def setUp(self): | ||
| 27 | target_arch = self.td.get("TARGET_ARCH") | ||
| 28 | # Check for go-cross-canadian package (uses target architecture) | ||
| 29 | if not self.tc.hasHostPackage("go-cross-canadian-%s" % target_arch): | ||
| 30 | raise unittest.SkipTest("GoCompileTest class: SDK doesn't contain a Go cross-canadian toolchain") | ||
| 31 | |||
| 32 | # Additional runtime check for go command availability | ||
| 33 | try: | ||
| 34 | self._run('which go') | ||
| 35 | except Exception as e: | ||
| 36 | raise unittest.SkipTest("GoCompileTest class: go command not available: %s" % str(e)) | ||
| 37 | |||
| 38 | def test_go_build(self): | ||
| 39 | """Test Go build command (native compilation)""" | ||
| 40 | self._run('cd %s; go build -o test test.go' % self.tc.sdk_dir) | ||
| 41 | |||
| 42 | def test_go_module(self): | ||
| 43 | """Test Go module creation and building""" | ||
| 44 | # Create a simple Go module | ||
| 45 | self._run('cd %s; go mod init hello-go' % self.tc.sdk_dir) | ||
| 46 | self._run('cd %s; go build -o hello-go' % self.tc.sdk_dir) | ||
| 47 | |||
| 48 | @classmethod | ||
| 49 | def tearDownClass(self): | ||
| 50 | files = [os.path.join(self.tc.sdk_dir, f) \ | ||
| 51 | for f in ['test.go', 'test', 'hello-go', 'go.mod', 'go.sum']] | ||
| 52 | for f in files: | ||
| 53 | remove_safe(f) | ||
| 54 | |||
| 55 | class GoHostCompileTest(OESDKTestCase): | ||
| 56 | td_vars = ['MACHINE', 'SDK_SYS', 'TARGET_ARCH'] | ||
| 57 | |||
| 58 | # Architecture mapping from Yocto/Poky to Go | ||
| 59 | ARCH_MAP = { | ||
| 60 | 'aarch64': 'arm64', | ||
| 61 | 'cortexa57': 'arm64', # ARM Cortex-A57 is ARM64 | ||
| 62 | 'cortexa72': 'arm64', # ARM Cortex-A72 is ARM64 | ||
| 63 | 'cortexa53': 'arm64', # ARM Cortex-A53 is ARM64 | ||
| 64 | 'x86_64': 'amd64', | ||
| 65 | 'i586': '386', | ||
| 66 | 'i686': '386', | ||
| 67 | 'mips': 'mips', | ||
| 68 | 'mipsel': 'mipsle', | ||
| 69 | 'powerpc64': 'ppc64', | ||
| 70 | 'powerpc64le': 'ppc64le', | ||
| 71 | 'riscv64': 'riscv64', | ||
| 72 | } | ||
| 73 | |||
| 74 | @classmethod | ||
| 75 | def setUpClass(self): | ||
| 76 | # Copy test.go file to SDK directory (same as GCC test uses files_dir) | ||
| 77 | shutil.copyfile(os.path.join(self.tc.files_dir, 'test.go'), | ||
| 78 | os.path.join(self.tc.sdk_dir, 'test.go')) | ||
| 79 | |||
| 80 | def setUp(self): | ||
| 81 | target_arch = self.td.get("TARGET_ARCH") | ||
| 82 | # Check for go-cross-canadian package (uses target architecture) | ||
| 83 | if not self.tc.hasHostPackage("go-cross-canadian-%s" % target_arch): | ||
| 84 | raise unittest.SkipTest("GoHostCompileTest class: SDK doesn't contain a Go cross-canadian toolchain") | ||
| 85 | |||
| 86 | # Additional runtime check for go command availability | ||
| 87 | try: | ||
| 88 | self._run('which go') | ||
| 89 | except Exception as e: | ||
| 90 | raise unittest.SkipTest("GoHostCompileTest class: go command not available: %s" % str(e)) | ||
| 91 | |||
| 92 | def _get_go_arch(self): | ||
| 93 | """Get Go architecture from SDK_SYS""" | ||
| 94 | sdksys = self.td.get("SDK_SYS") | ||
| 95 | arch = sdksys.split('-')[0] | ||
| 96 | |||
| 97 | # Handle ARM variants | ||
| 98 | if arch.startswith('arm'): | ||
| 99 | return 'arm' | ||
| 100 | |||
| 101 | # Use mapping for other architectures | ||
| 102 | return self.ARCH_MAP.get(arch, arch) | ||
| 103 | |||
| 104 | def test_go_cross_compile(self): | ||
| 105 | """Test Go cross-compilation for target""" | ||
| 106 | goarch = self._get_go_arch() | ||
| 107 | self._run('cd %s; GOOS=linux GOARCH=%s go build -o test-%s test.go' % (self.tc.sdk_dir, goarch, goarch)) | ||
| 108 | |||
| 109 | def test_go_module_cross_compile(self): | ||
| 110 | """Test Go module cross-compilation""" | ||
| 111 | goarch = self._get_go_arch() | ||
| 112 | self._run('cd %s; go mod init hello-go' % self.tc.sdk_dir) | ||
| 113 | self._run('cd %s; GOOS=linux GOARCH=%s go build -o hello-go-%s' % (self.tc.sdk_dir, goarch, goarch)) | ||
| 114 | |||
| 115 | @classmethod | ||
| 116 | def tearDownClass(self): | ||
| 117 | # Clean up files with dynamic architecture names | ||
| 118 | files = [os.path.join(self.tc.sdk_dir, f) \ | ||
| 119 | for f in ['test.go', 'go.mod', 'go.sum']] | ||
| 120 | # Add architecture-specific files using the same mapping | ||
| 121 | for arch in self.ARCH_MAP.values(): | ||
| 122 | files.extend([os.path.join(self.tc.sdk_dir, f) \ | ||
| 123 | for f in ['test-%s' % arch, 'hello-go-%s' % arch]]) | ||
| 124 | # Add 'arm' for ARM variants | ||
| 125 | files.extend([os.path.join(self.tc.sdk_dir, f) \ | ||
| 126 | for f in ['test-arm', 'hello-go-arm']]) | ||
| 127 | for f in files: | ||
| 128 | remove_safe(f) | ||
