-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
ci: untangle getting test images #2741
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"regexp" | ||
"runtime" | ||
"strings" | ||
"syscall" | ||
|
@@ -19,6 +20,36 @@ import ( | |
"github.com/opencontainers/runc/libcontainer/configs" | ||
) | ||
|
||
var busyboxTar string | ||
|
||
// init makes sure the container images are downloaded, | ||
// and initializes busyboxTar. If images can't be downloaded, | ||
// we are unable to run any tests, so panic. | ||
func init() { | ||
// Figure out path to get-images.sh. Note it won't work | ||
// in case the compiled test binary is moved elsewhere. | ||
_, ex, _, _ := runtime.Caller(0) | ||
getImages, err := filepath.Abs(filepath.Join(filepath.Dir(ex), "..", "..", "tests", "integration", "get-images.sh")) | ||
if err != nil { | ||
panic(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this cause t.Skip rather than panic ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't cause There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can, by defining a global variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You would want it to be |
||
} | ||
// Call it to make sure images are downloaded, and to get the paths. | ||
out, err := exec.Command(getImages).CombinedOutput() | ||
if err != nil { | ||
panic(fmt.Errorf("getImages error %s (output: %s)", err, out)) | ||
} | ||
// Extract the value of BUSYBOX_IMAGE. | ||
found := regexp.MustCompile(`(?m)^BUSYBOX_IMAGE=(.*)$`).FindSubmatchIndex(out) | ||
if len(found) < 4 { | ||
panic(fmt.Errorf("unable to find BUSYBOX_IMAGE=<value> in %q", out)) | ||
} | ||
busyboxTar = string(out[found[2]:found[3]]) | ||
// Finally, check the file is present | ||
if _, err := os.Stat(busyboxTar); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func ptrInt(v int) *int { | ||
return &v | ||
} | ||
|
@@ -114,9 +145,9 @@ func remove(dir string) { | |
// copyBusybox copies the rootfs for a busybox container created for the test image | ||
// into the new directory for the specific test | ||
func copyBusybox(dest string) error { | ||
out, err := exec.Command("sh", "-c", fmt.Sprintf("cp -a /busybox/* %s/", dest)).CombinedOutput() | ||
out, err := exec.Command("sh", "-c", fmt.Sprintf("tar --exclude './dev/*' -C %q -xf %q", dest, busyboxTar)).CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("copy error %q: %q", err, out) | ||
return fmt.Errorf("untar error %q: %q", err, out) | ||
} | ||
return nil | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/bin/bash | ||
|
||
# This script checks if container images needed for tests (currently | ||
# busybox and Debian 10 "Buster") are available locally, and downloads | ||
# them to testdata directory if not. | ||
# | ||
# The script is self-contained/standalone and is used from a few places | ||
# that need to ensure the images are downloaded. Its output is suitable | ||
# for consumption by shell via eval (see helpers.bash). | ||
# | ||
# XXX: Latest available images are fetched. Theoretically, | ||
# this can bring some instability in case of a broken image. | ||
# In this case, images will need to be pinned to a checksum | ||
# on a per-image and per-architecture basis. | ||
|
||
set -e -u -o pipefail | ||
|
||
# Root directory of integration tests. | ||
INTEGRATION_ROOT=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")") | ||
# Test data path. | ||
TESTDATA="${INTEGRATION_ROOT}/testdata" | ||
# Sanity check: $TESTDATA directory must exist. | ||
if [ ! -d "$TESTDATA" ]; then | ||
echo "Bad TESTDATA directory: $TESTDATA. Aborting" >&2 | ||
exit 1 | ||
fi | ||
|
||
function get() { | ||
local dest="$1" url="$2" | ||
|
||
[ -e "$dest" ] && return | ||
|
||
# Sanity check: $TESTDATA directory must be writable. | ||
if [ ! -w "$TESTDATA" ]; then | ||
echo "TESTDATA directory ($TESTDATA) not writable. Aborting" >&2 | ||
exit 1 | ||
fi | ||
|
||
if ! curl -o "$dest" -fsSL "$url"; then | ||
echo "Failed to get $url" 1>&2 | ||
exit 1 | ||
fi | ||
} | ||
|
||
arch=$(go env GOARCH) | ||
# Convert from GOARCH to whatever the URLs below are using. | ||
case $arch in | ||
arm64) | ||
arch=arm64v8 | ||
;; | ||
386) | ||
arch=i386 | ||
;; | ||
esac | ||
|
||
# busybox | ||
BUSYBOX_IMAGE="$TESTDATA/busybox-${arch}.tar.xz" | ||
get "$BUSYBOX_IMAGE" \ | ||
"https://github.com/docker-library/busybox/raw/dist-${arch}/stable/glibc/busybox.tar.xz" | ||
echo "BUSYBOX_IMAGE=$BUSYBOX_IMAGE" | ||
|
||
# debian | ||
DEBIAN_IMAGE="$TESTDATA/debian-${arch}.tar.xz" | ||
get "$DEBIAN_IMAGE" \ | ||
"https://github.com/debuerreotype/docker-debian-artifacts/raw/dist-${arch}/buster/slim/rootfs.tar.xz" | ||
echo "DEBIAN_IMAGE=$DEBIAN_IMAGE" | ||
|
||
# hello-world is local, no need to download. | ||
HELLO_IMAGE="$TESTDATA/hello-world-${arch}.tar" | ||
echo "HELLO_IMAGE=$HELLO_IMAGE" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function looks a tad complicated, but all it does is calls
get-images.sh
and parses its output to get the value of
BUSYBOX_IMAGE
. This way wedon't have to worry about doing some extra steps before running
go test
.