#!/usr/bin/env bash
# Setup, run and teardown e2e test suite in containers.
set -eu -o pipefail

unique_id="${E2E_UNIQUE_ID:-cliendtoendsuite}"
e2e_env_image=docker-cli-e2e-env:$unique_id
dev_image=docker-cli-dev:$unique_id

function run_in_env {
    local cmd=$1
    docker run -i --rm \
        -v /var/run/docker.sock:/var/run/docker.sock \
        -e E2E_UNIQUE_ID \
        "$e2e_env_image" "$cmd"
}

docker build \
    -t "$e2e_env_image" \
    -f dockerfiles/Dockerfile.test-e2e-env .

docker build \
    -t "$dev_image" \
    -f dockerfiles/Dockerfile.dev .

notary_volume="${unique_id}_notary-fixtures"
docker volume create --name "$notary_volume"
docker run --rm \
    -v "$PWD:/go/src/github.com/docker/cli" \
    -v "$notary_volume:/data" \
    "$dev_image" \
    cp -r ./e2e/testdata/notary/* /data/

engine_host=$(run_in_env setup)
testexit=0


test_cmd="test"
if [[ -n "${TEST_DEBUG-}" ]]; then
    test_cmd="shell"
fi

docker run -i --rm \
    -v "$PWD:/go/src/github.com/docker/cli" \
    -v "$PWD/e2e/testdata/notary/root-ca.cert:/usr/local/share/ca-certificates/notary.cert" \
    --network "${unique_id}_default" \
    -e TESTFLAGS \
    -e ENGINE_HOST="$engine_host" \
    "$dev_image" \
    ./scripts/test/e2e/run "$test_cmd" "$engine_host" || testexit="$?"

run_in_env cleanup
exit "$testexit"
