kubo/test/sharness/t0002-docker-image.sh
Gus Eggert 3a15a0fc55
test: fix Docker tests in GH Actions (#9812)
GH Actions recently changed their Docker build implementation and it
has a different output than previously, causing the tests that parse
its output to fail.

This switches the test to not parse Docker build output. The parsing
was used to extract the image ID while still showing logs. A better
way to show logs and still know the image ID is to tag it, which is
what this now does.

This also renames the Docker tests so that they run earlier. This
takes better advantage of the fact that the sharness tests are run in
parallel. Since the Docker test are quite long, and are at the end of
the list, the test runner is not running other tests in parallel while
the Docker tests are running.
2023-04-12 09:03:11 +02:00

99 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Copyright (c) 2015 Christian Couder
# MIT Licensed; see the LICENSE file in this repository.
#
test_description="Test docker image"
. lib/test-lib.sh
# if in travis CI on OSX, docker is not available
if ! test_have_prereq DOCKER; then
skip_all='skipping docker tests, docker not available'
test_done
fi
test_expect_success "'docker --version' works" '
docker --version >actual
'
test_expect_success "'docker --version' output looks good" '
egrep "^Docker version" actual
'
TEST_TRASH_DIR=$(pwd)
TEST_SCRIPTS_DIR=$(dirname "$TEST_TRASH_DIR")
TEST_TESTS_DIR=$(dirname "$TEST_SCRIPTS_DIR")
APP_ROOT_DIR=$(dirname "$TEST_TESTS_DIR")
IMAGE_TAG=kubo_test
test_expect_success "docker image build succeeds" '
docker_build "$IMAGE_TAG" "$TEST_TESTS_DIR/../Dockerfile" "$APP_ROOT_DIR" ||
test_fsh echo "TEST_TESTS_DIR: $TEST_TESTS_DIR" ||
test_fsh echo "APP_ROOT_DIR : $APP_ROOT_DIR"
'
test_expect_success "write init scripts" '
echo "ipfs config Foo Bar" > 001.sh &&
echo "ipfs config Baz Qux" > 002.sh &&
chmod +x 002.sh
'
test_expect_success "docker image runs" '
DOC_ID=$(docker run -d \
-p 127.0.0.1:5001:5001 -p 127.0.0.1:8080:8080 \
-v "$PWD/001.sh":/container-init.d/001.sh \
-v "$PWD/002.sh":/container-init.d/002.sh \
"$IMAGE_TAG")
'
test_expect_success "docker container gateway is up" '
pollEndpoint -host=/ip4/127.0.0.1/tcp/8080 -http-url http://localhost:8080/api/v0/version -v -tries 30 -tout 1s
'
test_expect_success "docker container API is up" '
pollEndpoint -host=/ip4/127.0.0.1/tcp/5001 -http-url http://localhost:5001/version -v -tries 30 -tout 1s
'
test_expect_success "check that init scripts were run correctly and in the correct order" "
echo -e \"Sourcing '/container-init.d/001.sh'...\nExecuting '/container-init.d/002.sh'...\" > expected &&
docker logs $DOC_ID 2>/dev/null | grep -e 001.sh -e 002.sh > actual &&
test_cmp actual expected
"
test_expect_success "check that init script configs were applied" '
echo Bar > expected &&
docker exec "$DOC_ID" ipfs config Foo > actual &&
test_cmp actual expected &&
echo Qux > expected &&
docker exec "$DOC_ID" ipfs config Baz > actual &&
test_cmp actual expected
'
test_expect_success "simple ipfs add/cat can be run in docker container" '
echo "Hello Worlds" | tr -d "[:cntrl:]" > expected &&
HASH=$(docker_exec "$DOC_ID" "echo $(cat expected) | ipfs add -q" | tr -d "[:cntrl:]") &&
docker_exec "$DOC_ID" "ipfs cat $HASH" | tr -d "[:cntrl:]" > actual &&
test_cmp expected actual
'
read testcode <<EOF
pollEndpoint -host=/ip4/127.0.0.1/tcp/5001 -http-url http://localhost:5001/version -http-out | grep Commit | cut -d" " -f2 >actual ; \
test -s actual ; \
docker exec -i "$DOC_ID" ipfs version --enc json \
| sed 's/^.*"Commit":"\\\([^"]*\\\)".*$/\\\1/g' >expected ; \
test -s expected ; \
test_cmp expected actual
EOF
test_expect_success "version CurrentCommit is set" "$testcode"
test_expect_success "stop docker container" '
docker_stop "$DOC_ID"
'
docker_rm "$DOC_ID"
docker_rmi "$IMAGE_TAG"
test_done