diff --git a/scripts/test-elf-detection.sh b/scripts/test-elf-detection.sh index 73f74e5..0bfea77 100755 --- a/scripts/test-elf-detection.sh +++ b/scripts/test-elf-detection.sh @@ -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 diff --git a/scripts/test-plugin-output-streaming.sh b/scripts/test-plugin-output-streaming.sh index c19d2ae..8909db3 100755 --- a/scripts/test-plugin-output-streaming.sh +++ b/scripts/test-plugin-output-streaming.sh @@ -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; } @@ -16,12 +29,15 @@ 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" <&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 '" \ -e "error: Help: --repository Repository path" \ -e "error: Usage: containertool [] --repository " \ -e "error: See 'containertool --help' for more information." "$PKGPATH/output" -echo Plugin error output: PASSED +log Plugin error output: PASSED