| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2015 Christian Couder |
| 4 | # MIT Licensed; see the LICENSE file in this repository. |
| 5 | # |
| 6 | |
| 7 | test_description="Test docker image" |
| 8 | |
| 9 | . lib/test-lib.sh |
| 10 | |
| 11 | # if in travis CI on OSX, docker is not available |
| 12 | if ! test_have_prereq DOCKER; then |
| 13 | skip_all='skipping docker tests, docker not available' |
| 14 | |
| 15 | test_done |
| 16 | fi |
| 17 | |
| 18 | test_expect_success "'docker --version' works" ' |
| 19 | docker --version >actual |
| 20 | ' |
| 21 | |
| 22 | test_expect_success "'docker --version' output looks good" ' |
| 23 | egrep "^Docker version" actual |
| 24 | ' |
| 25 | |
| 26 | TEST_TRASH_DIR=$(pwd) |
| 27 | TEST_SCRIPTS_DIR=$(dirname "$TEST_TRASH_DIR") |
| 28 | TEST_TESTS_DIR=$(dirname "$TEST_SCRIPTS_DIR") |
| 29 | APP_ROOT_DIR=$(dirname "$TEST_TESTS_DIR") |
| 30 | IMAGE_TAG=kubo_test |
| 31 | |
| 32 | test_expect_success "docker image build succeeds" ' |
| 33 | docker_build "$IMAGE_TAG" "$TEST_TESTS_DIR/../Dockerfile" "$APP_ROOT_DIR" || |
| 34 | test_fsh echo "TEST_TESTS_DIR: $TEST_TESTS_DIR" || |
| 35 | test_fsh echo "APP_ROOT_DIR : $APP_ROOT_DIR" |
| 36 | ' |
| 37 | |
| 38 | test_expect_success "write init scripts" ' |
| 39 | echo "ipfs config Mounts.IPFS Bar" > 001.sh && |
| 40 | echo "ipfs config Pubsub.Router Qux" > 002.sh && |
| 41 | chmod +x 002.sh |
| 42 | ' |
| 43 | |
| 44 | test_expect_success "docker image runs" ' |
| 45 | DOC_ID=$(docker run -d \ |
| 46 | -p 127.0.0.1:5001:5001 -p 127.0.0.1:8080:8080 \ |
| 47 | -v "$PWD/001.sh":/container-init.d/001.sh \ |
| 48 | -v "$PWD/002.sh":/container-init.d/002.sh \ |
| 49 | "$IMAGE_TAG") |
| 50 | ' |
| 51 | |
| 52 | test_expect_success "docker container gateway is up" ' |
| 53 | pollEndpoint -host=/ip4/127.0.0.1/tcp/8080 -http-url http://localhost:8080/ipfs/bafkqaddimvwgy3zao5xxe3debi -v -tries 30 -tout 1s |
| 54 | ' |
| 55 | |
| 56 | test_expect_success "docker container API is up" ' |
| 57 | pollEndpoint -host=/ip4/127.0.0.1/tcp/5001 -http-url http://localhost:5001/version -v -tries 30 -tout 1s |
| 58 | ' |
| 59 | |
| 60 | test_expect_success "check that init scripts were run correctly and in the correct order" " |
| 61 | echo -e \"Sourcing '/container-init.d/001.sh'...\nExecuting '/container-init.d/002.sh'...\" > expected && |
| 62 | docker logs $DOC_ID 2>/dev/null | grep -e 001.sh -e 002.sh > actual && |
| 63 | test_cmp actual expected |
| 64 | " |
| 65 | |
| 66 | test_expect_success "check that init script configs were applied" ' |
| 67 | echo Bar > expected && |
| 68 | docker exec "$DOC_ID" ipfs config Mounts.IPFS > actual && |
| 69 | test_cmp actual expected && |
| 70 | echo Qux > expected && |
| 71 | docker exec "$DOC_ID" ipfs config Pubsub.Router > actual && |
| 72 | test_cmp actual expected |
| 73 | ' |
| 74 | |
| 75 | test_expect_success "simple ipfs add/cat can be run in docker container" ' |
| 76 | echo "Hello Worlds" | tr -d "[:cntrl:]" > expected && |
| 77 | HASH=$(docker_exec "$DOC_ID" "echo $(cat expected) | ipfs add -q" | tr -d "[:cntrl:]") && |
| 78 | docker_exec "$DOC_ID" "ipfs cat $HASH" | tr -d "[:cntrl:]" > actual && |
| 79 | test_cmp expected actual |
| 80 | ' |
| 81 | |
| 82 | read testcode <<EOF |
| 83 | pollEndpoint -host=/ip4/127.0.0.1/tcp/5001 -http-url http://localhost:5001/version -http-out | grep Commit | cut -d" " -f2 >actual ; \ |
| 84 | test -s actual ; \ |
| 85 | docker exec -i "$DOC_ID" ipfs version --enc json \ |
| 86 | | sed 's/^.*"Commit":"\\\([^"]*\\\)".*$/\\\1/g' >expected ; \ |
| 87 | test -s expected ; \ |
| 88 | test_cmp expected actual |
| 89 | EOF |
| 90 | test_expect_success "version CurrentCommit is set" "$testcode" |
| 91 | |
| 92 | test_expect_success "stop docker container" ' |
| 93 | docker_stop "$DOC_ID" |
| 94 | ' |
| 95 | |
| 96 | docker_rm "$DOC_ID" |
| 97 | docker_rmi "$IMAGE_TAG" |
| 98 | test_done |