Skip to content

Commit 4c6f38d

Browse files
authored
tests: Improve diagnostics and robustness of test shell scripts (#81)
Motivation ---------- Tests scripts are used by GitHub Actions and when running tests locally. They should have consistent structures, clean up after themselves and print useful messages so that failures can be diagnosed. Modifications ------------- * Update all test scripts to use the same structure and cleanup options as `run-integration-tests.sh` * Add better diagnostic messages Result ------ Test failures will be easier to diagnose Test Plan --------- Existing tests continue to pass.
1 parent 7caaff7 commit 4c6f38d

File tree

2 files changed

+77
-13
lines changed

2 files changed

+77
-13
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

scripts/test-plugin-output-streaming.sh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
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

316
# Test that error output streamed from containertool is printed correctly by the plugin.
417

5-
set -exo pipefail
18+
set -euo pipefail
619

720
log() { printf -- "** %s\n" "$*" >&2; }
821
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
@@ -16,23 +29,27 @@ cleanup() {
1629
}
1730
trap cleanup EXIT
1831

32+
# Create a test project which depends on this checkout of the plugin repository
33+
REPO_ROOT=$(git rev-parse --show-toplevel)
1934
swift package --package-path "$PKGPATH" init --type executable --name hello
2035
cat >> "$PKGPATH/Package.swift" <<EOF
2136
package.dependencies += [
22-
.package(path: "$PWD"),
37+
.package(path: "$REPO_ROOT"),
2338
]
2439
EOF
40+
cat "$PKGPATH/Package.swift"
2541

2642
# Run the plugin, forgetting a mandatory argument. Verify that the output is not corrupted.
2743
# The `swift package` command will return a nonzero exit code. This is expected, so disable pipefail.
2844
set +o pipefail
2945
swift package --package-path "$PKGPATH" --allow-network-connections all build-container-image 2>&1 | tee "$PKGPATH/output"
3046
set -o pipefail
3147

48+
# This checks that the output lines are not broken, but not that they appear in the correct order
3249
grep -F -x -e "error: Missing expected argument '--repository <repository>'" \
3350
-e "error: Help: --repository <repository> Repository path" \
3451
-e "error: Usage: containertool [<options>] --repository <repository> <executable>" \
3552
-e "error: See 'containertool --help' for more information." "$PKGPATH/output"
3653

37-
echo Plugin error output: PASSED
54+
log Plugin error output: PASSED
3855

0 commit comments

Comments
 (0)