1#!/usr/bin/env bash
2# Run integration tests against the latest docker-ce dind
3set -eu -o pipefail
4
5function container_ip {
6    local cid=$1
7    local network=$2
8    docker inspect \
9        -f "{{.NetworkSettings.Networks.${network}.IPAddress}}" "$cid"
10}
11
12function fetch_images {
13    ./scripts/test/e2e/load-image fetch-only
14}
15
16function setup {
17    local project=$1
18    local file=$2
19
20    test "${DOCKERD_EXPERIMENTAL:-0}" -eq "1" && file="${file}:./e2e/compose-env.experimental.yaml"
21
22    if [[ "${TEST_CONNHELPER:-}" = "ssh" ]];then
23        test ! -f "${HOME}/.ssh/id_rsa" && ssh-keygen -t rsa -C docker-e2e-dummy -N "" -f "${HOME}/.ssh/id_rsa" -q
24        grep "^StrictHostKeyChecking no" "${HOME}/.ssh/config" > /dev/null 2>&1 || echo "StrictHostKeyChecking no" > "${HOME}/.ssh/config"
25        TEST_CONNHELPER_SSH_ID_RSA_PUB=$(cat "${HOME}/.ssh/id_rsa.pub")
26        export TEST_CONNHELPER_SSH_ID_RSA_PUB
27        file="${file}:./e2e/compose-env.connhelper-ssh.yaml"
28    fi
29    COMPOSE_PROJECT_NAME=$project COMPOSE_FILE=$file docker-compose up --build -d >&2
30
31    local network="${project}_default"
32    # TODO: only run if inside a container
33    docker network connect "$network" "$(hostname)"
34
35    engine_ip="$(container_ip "${project}_engine_1" "$network")"
36    engine_host="tcp://$engine_ip:2375"
37    if [[ "${TEST_CONNHELPER:-}" = "ssh" ]];then
38        engine_host="ssh://penguin@${engine_ip}"
39    fi
40    (
41        export DOCKER_HOST="$engine_host"
42        timeout 200 ./scripts/test/e2e/wait-on-daemon
43        ./scripts/test/e2e/load-image
44        is_swarm_enabled || docker swarm init
45    ) >&2
46    echo "$engine_host"
47}
48
49function is_swarm_enabled {
50    docker info 2> /dev/null | grep -q 'Swarm: active'
51}
52
53function cleanup {
54    local project=$1
55    local network="${project}_default"
56    docker network disconnect "$network" "$(hostname)"
57    COMPOSE_PROJECT_NAME=$1 COMPOSE_FILE=$2 docker-compose down -v --rmi local >&2
58}
59
60function runtests {
61    local engine_host=$1
62
63    # shellcheck disable=SC2086
64    env -i \
65        TEST_DOCKER_HOST="$engine_host" \
66        TEST_DOCKER_CERT_PATH="${DOCKER_CERT_PATH-}" \
67        TEST_KUBECONFIG="${KUBECONFIG-}" \
68        TEST_REMOTE_DAEMON="${REMOTE_DAEMON-}" \
69        TEST_SKIP_PLUGIN_TESTS="${SKIP_PLUGIN_TESTS-}" \
70        GOPATH="$GOPATH" \
71        PATH="$PWD/build/:/usr/bin" \
72        HOME="$HOME" \
73        "$(command -v go)" test -v ./e2e/... ${TESTFLAGS-}
74}
75
76export unique_id="${E2E_UNIQUE_ID:-cliendtoendsuite}"
77compose_env_file=./e2e/compose-env.yaml
78
79cmd=${1-}
80
81case "$cmd" in
82    setup)
83        setup "$unique_id" "$compose_env_file"
84        exit
85        ;;
86    cleanup)
87        cleanup "$unique_id" "$compose_env_file"
88        exit
89        ;;
90    fetch-images)
91        fetch_images
92        exit
93        ;;
94    test)
95        engine_host=${2-}
96        if [[ -z "${engine_host}" ]]; then
97            echo "missing parameter docker engine host"
98            echo "Usage: $0 test ENGINE_HOST"
99            exit 3
100        fi
101        runtests "$engine_host"
102        ;;
103    run|"")
104        engine_host="$(setup "$unique_id" "$compose_env_file")"
105        testexit=0
106        runtests "$engine_host" || testexit=$?
107        cleanup "$unique_id" "$compose_env_file"
108        exit $testexit
109        ;;
110    shell)
111        $SHELL
112        ;;
113    *)
114        echo "Unknown command: $cmd"
115        echo "Usage: "
116        echo "    $0 [setup | cleanup | test | run] [engine_host]"
117        exit 1
118        ;;
119esac
120