Skip to content

Commit 27dfb56

Browse files
committed
tests: Improve test-elf-detection.sh robustness and diagnostics
1 parent 7caaff7 commit 27dfb56

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

scripts/test-elf-detection.sh

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,70 @@
11
#!/usr/bin/env bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the SwiftContainerPlugin open source project
5+
##
6+
## Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors
7+
## Licensed under Apache License v2.0
8+
##
9+
## See LICENSE.txt for license information
10+
## See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors
11+
##
12+
## SPDX-License-Identifier: Apache-2.0
13+
##
14+
##===----------------------------------------------------------------------===##
215

3-
set -ex -o pipefail
16+
#
17+
# This script assumes that the Static Linux SDK has already been installed
18+
#
419

20+
log() { printf -- "** %s\n" "$*" >&2; }
21+
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
22+
fatal() { error "$@"; exit 1; }
23+
24+
set -euo pipefail
25+
26+
RUNTIME=${RUNTIME-"docker"}
27+
28+
#
29+
# Create a test package
30+
#
531
PKGPATH=$(mktemp -d)
632
swift package --package-path "$PKGPATH" init --type executable --name hello
733

34+
cleanup() {
35+
log "Deleting temporary package $PKGPATH"
36+
rm -rf "$PKGPATH"
37+
}
38+
trap cleanup EXIT
39+
40+
#
841
# Build and package an x86_64 binary
42+
#
943
swift build --package-path "$PKGPATH" --swift-sdk x86_64-swift-linux-musl
10-
file "$PKGPATH/.build/x86_64-swift-linux-musl/debug/hello"
44+
FILETYPE=$(file "$PKGPATH/.build/x86_64-swift-linux-musl/debug/hello")
45+
log "Executable type: $FILETYPE"
46+
1147
IMGREF=$(swift run containertool --repository localhost:5000/elf_test "$PKGPATH/.build/x86_64-swift-linux-musl/debug/hello" --from scratch)
12-
docker pull "$IMGREF"
13-
docker inspect "$IMGREF" --format "{{.Architecture}}" | grep amd64
14-
echo x86_64 detection: PASSED
48+
$RUNTIME pull "$IMGREF"
49+
IMGARCH=$($RUNTIME inspect "$IMGREF" --format "{{.Architecture}}")
50+
if [ "$IMGARCH" = "amd64" ] ; then
51+
log "x86_64 detection: PASSED"
52+
else
53+
fatal "x86_64 detection: FAILED - image architecture was $IMGARCH; expected amd64"
54+
fi
1555

56+
#
1657
# Build and package an aarch64 binary
58+
#
1759
swift build --package-path "$PKGPATH" --swift-sdk aarch64-swift-linux-musl
18-
file "$PKGPATH/.build/aarch64-swift-linux-musl/debug/hello"
19-
IMGREF=$(swift run containertool --repository localhost:5000/elf_test "$PKGPATH/.build/aarch64-swift-linux-musl/debug/hello" --from scratch)
20-
docker pull "$IMGREF"
21-
docker inspect "$IMGREF" --format "{{.Architecture}}" | grep arm64
60+
FILETYPE=$(file "$PKGPATH/.build/x86_64-swift-linux-musl/debug/hello")
61+
log "Executable type: $FILETYPE"
2262

23-
echo aarch64 detection: PASSED
63+
IMGREF=$(swift run containertool --repository localhost:5000/elf_test "$PKGPATH/.build/aarch64-swift-linux-musl/debug/hello" --from scratch)
64+
$RUNTIME pull "$IMGREF"
65+
IMGARCH=$($RUNTIME inspect "$IMGREF" --format "{{.Architecture}}")
66+
if [ "$IMGARCH" = "arm64" ] ; then
67+
log "aarch64 detection: PASSED"
68+
else
69+
fatal "aarch64 detection: FAILED - image architecture was $IMGARCH; expected arm64"
70+
fi

0 commit comments

Comments
 (0)