Skip to content

tests: Improve diagnostics and robustness of test shell scripts #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 57 additions & 10 deletions scripts/test-elf-detection.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,70 @@
#!/usr/bin/env bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftContainerPlugin open source project
##
## Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

set -ex -o pipefail
#
# This script assumes that the Static Linux SDK has already been installed
#

log() { printf -- "** %s\n" "$*" >&2; }
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
fatal() { error "$@"; exit 1; }

set -euo pipefail

RUNTIME=${RUNTIME-"docker"}

#
# Create a test package
#
PKGPATH=$(mktemp -d)
swift package --package-path "$PKGPATH" init --type executable --name hello

cleanup() {
log "Deleting temporary package $PKGPATH"
rm -rf "$PKGPATH"
}
trap cleanup EXIT

#
# Build and package an x86_64 binary
#
swift build --package-path "$PKGPATH" --swift-sdk x86_64-swift-linux-musl
file "$PKGPATH/.build/x86_64-swift-linux-musl/debug/hello"
FILETYPE=$(file "$PKGPATH/.build/x86_64-swift-linux-musl/debug/hello")
log "Executable type: $FILETYPE"

IMGREF=$(swift run containertool --repository localhost:5000/elf_test "$PKGPATH/.build/x86_64-swift-linux-musl/debug/hello" --from scratch)
docker pull "$IMGREF"
docker inspect "$IMGREF" --format "{{.Architecture}}" | grep amd64
echo x86_64 detection: PASSED
$RUNTIME pull "$IMGREF"
IMGARCH=$($RUNTIME inspect "$IMGREF" --format "{{.Architecture}}")
if [ "$IMGARCH" = "amd64" ] ; then
log "x86_64 detection: PASSED"
else
fatal "x86_64 detection: FAILED - image architecture was $IMGARCH; expected amd64"
fi

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

echo aarch64 detection: PASSED
IMGREF=$(swift run containertool --repository localhost:5000/elf_test "$PKGPATH/.build/aarch64-swift-linux-musl/debug/hello" --from scratch)
$RUNTIME pull "$IMGREF"
IMGARCH=$($RUNTIME inspect "$IMGREF" --format "{{.Architecture}}")
if [ "$IMGARCH" = "arm64" ] ; then
log "aarch64 detection: PASSED"
else
fatal "aarch64 detection: FAILED - image architecture was $IMGARCH; expected arm64"
fi
23 changes: 20 additions & 3 deletions scripts/test-plugin-output-streaming.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#!/usr/bin/env bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftContainerPlugin open source project
##
## Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

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

set -exo pipefail
set -euo pipefail

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

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

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

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

echo Plugin error output: PASSED
log Plugin error output: PASSED