2020-08-05 21:16:28 +02:00
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
2021-10-21 07:14:06 +02:00
ROOT_DIR = $( dirname " $0 " )
2020-08-05 21:16:28 +02:00
pushd " $ROOT_DIR "
ROOT_DIR = " $( pwd ) "
echo "Moving GOPATH into /tmp/ to test modules behavior."
2024-05-15 18:03:03 +02:00
export GOPATH = " ${ GOPATH :- $( go env GOPATH) } "
2020-08-05 21:16:28 +02:00
export ORIGINAL_GOPATH = " $GOPATH "
2021-10-21 07:14:06 +02:00
GOPATH = " $( mktemp -d) "
export GOPATH
2020-08-05 21:16:28 +02:00
2024-05-15 18:03:03 +02:00
export CGO_ENABLED = 0
GOARCH = " ${ GOARCH :- $( go env GOARCH) } "
2020-08-05 21:16:28 +02:00
pushd " $GOPATH " || exit 1
echo "Copying ko to temp gopath."
mkdir -p " $GOPATH /src/github.com/google/ko "
cp -r " $ROOT_DIR / " * " $GOPATH /src/github.com/google/ko/ "
2024-03-26 14:50:05 +02:00
pushd " $GOPATH /src/github.com/google/ko " || exit 1
2020-08-05 21:16:28 +02:00
echo "Building ko"
2024-05-15 18:03:03 +02:00
RESULT = " $( go build .) "
2020-08-05 21:16:28 +02:00
echo "Beginning scenarios."
FILTER = "[^ ]local[^ ]*"
2024-05-15 18:03:03 +02:00
echo "1. Test should create an image that outputs 'Hello World'."
RESULT = " $( ./ko build --local --platform= " linux/ $GOARCH " " $GOPATH /src/github.com/google/ko/test " | grep " $FILTER " | xargs -I% docker run %) "
2024-03-26 14:50:05 +02:00
if [ [ " $RESULT " != *"Hello there" * ] ] ; then
2020-08-05 21:16:28 +02:00
echo " Test FAILED. Saw $RESULT " && exit 1
else
echo "Test PASSED"
fi
2024-05-17 22:22:45 +02:00
echo "2. Test knative 'KO_FLAGS' variable is ignored."
2024-05-21 16:52:00 +02:00
# https://github.com/ko-build/ko/issues/1317
2024-05-17 22:22:45 +02:00
RESULT = " $( KO_FLAGS = "--platform=badvalue" ./ko build --local --platform= " linux/ $GOARCH " " $GOPATH /src/github.com/google/ko/test " | grep " $FILTER " | xargs -I% docker run %) "
if [ [ " $RESULT " != *"Hello there" * ] ] ; then
echo " Test FAILED. Saw $RESULT " && exit 1
else
echo "Test PASSED"
fi
echo "3. Linux capabilities."
2024-04-01 15:36:17 +02:00
pushd test/build-configs || exit 1
# run as non-root user with net_bind_service cap granted
docker_run_opts = "--user 1 --cap-add=net_bind_service"
2024-05-15 18:03:03 +02:00
RESULT = " $( ../../ko build --local --platform= " linux/ $GOARCH " ./caps/cmd | grep " $FILTER " | xargs -I% docker run $docker_run_opts %) "
2024-04-01 15:36:17 +02:00
if [ [ " $RESULT " != "No capabilities" ] ] ; then
echo " Test FAILED. Saw ' $RESULT ' but expected 'No capabilities'. Docker 'cap-add' must have no effect unless matching capabilities are granted to the file. " && exit 1
fi
# build with a different config requesting net_bind_service file capability
2024-05-15 18:03:03 +02:00
RESULT_WITH_FILE_CAPS = " $( KO_CONFIG_PATH = caps.ko.yaml ../../ko build --local --platform= " linux/ $GOARCH " ./caps/cmd | grep " $FILTER " | xargs -I% docker run $docker_run_opts %) "
2024-04-01 15:36:17 +02:00
if [ [ " $RESULT_WITH_FILE_CAPS " != "Has capabilities" * ] ] ; then
echo " Test FAILED. Saw ' $RESULT_WITH_FILE_CAPS ' but expected 'Has capabilities'. Docker 'cap-add' must work when matching capabilities are granted to the file. " && exit 1
else
echo "Test PASSED"
fi
popd || exit 1
2020-08-05 21:16:28 +02:00
popd || exit 1
popd || exit 1
export GOPATH = " $ORIGINAL_GOPATH "