summaryrefslogtreecommitdiff
path: root/tests/chutney/setup
blob: 20c18e4e9e2c5f13a7c922222c4f87ee0be3c387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env bash

set -xeuo pipefail

SCRIPT_NAME=$(basename "$0")

function usage()
{
    cat <<EOF
${SCRIPT_NAME}: Launch a chutney network to test arti

Usage:
  ${SCRIPT_NAME} [modules] : Launch chutney, and the provided modules.

Options:
  -h: Print this message.
  -n <network>: Name of the chutney network to launch (default: basic)

Modules:
  "proxy": Launch a arti-based SOCKS proxy.
EOF
}

PROXY=no
NETWORK="basic"
while getopts "hn:" opt ; do
    case "$opt" in
	h) usage
	   exit 0
	   ;;
	n) NETWORK="$OPTARG"
	   ;;
	*) echo "Unknown option. (Run $0 -h for usage)"
	   exit 1
	   ;;
    esac
done

# Remove the parsed flags
shift $((OPTIND-1))

for module in "$@"; do
    case "$module" in
	proxy) PROXY=yes
	       ;;
	*) echo "Unrecognized module. (Run $0 -h for usage)"
	   ;;
    esac
done

target="$NETWORK"
cd "$(git rev-parse --show-toplevel)"
echo "target=$target" > tests/chutney/arti.run

# Set and fully resolve chutney bin if not already set.
: "${CHUTNEY_BIN:=$(type -P chutney)}"
if [ -z "${CHUTNEY_BIN:-}" ]; then
    echo "Couldn't locate chutney bin. Ensure it's on PATH or set CHUTNEY_BIN."
    echo "You can install with:"
    echo "python3 -m pip install git+https://gitlab.torproject.org/tpo/core/chutney.git"
    exit 1
elif [ ! -x "$CHUTNEY_BIN" ]; then
    echo "CHUTNEY_BIN='$CHUTNEY_BIN' doesn't exist or isn't executable"
    exit 1
else
    # CHUTNEY_BIN is set; tell the user so.
    echo "Using chutney at '${CHUTNEY_BIN}'"
fi

# Use consistent CHUTNEY_BIN for all steps. We export it in case we end up
# calling other tools/scripts that directly look for chutney there.
echo "export CHUTNEY_BIN=\"$CHUTNEY_BIN\"" >> tests/chutney/arti.run

# Likewise use a consistent CHUTNEY_DATA_DIR
export CHUTNEY_DATA_DIR="${CHUTNEY_DATA_DIR:-$(pwd)}"
echo "export CHUTNEY_DATA_DIR=\"$CHUTNEY_DATA_DIR\"" >> tests/chutney/arti.run

"${CHUTNEY_BIN}" configure "$target"
"${CHUTNEY_BIN}" start "$target"
CHUTNEY_START_TIME=180 "${CHUTNEY_BIN}" wait_for_bootstrap "$target"
"${CHUTNEY_BIN}" verify "$target"

if [ "$PROXY" = "no" ] ; then
    exit 0
fi

ARTI_FLAGS=()
if [ "$(id -u)" = "0" ] ; then
    # If we are root, then we're probably running from CI.  Tell Arti
    # that's okay.
    ARTI_FLAGS+=("-o" "application.allow_running_as_root=true")
fi

if [ -x ./target/x86_64-unknown-linux-gnu/debug/arti ]; then
	cmd=./target/x86_64-unknown-linux-gnu/debug/arti
else
	cargo build
	cmd=./target/debug/arti
fi

(
	set +e
	"$cmd" proxy -c "${CHUTNEY_DATA_DIR}/nodes/arti.toml" -d 35353 \
	       "${ARTI_FLAGS[@]}" &
	arti_pid=$!
	echo "pid=$arti_pid" >> tests/chutney/arti.run
	wait "$arti_pid"
	echo "result=$?" >> tests/chutney/arti.run
) & disown

# Wait for arti to start listening (it does so "immediately", but we don't want to get there first)
# Really there should be a proper daemon startup protocol here, but arti doesn't have one yet.
for idx in $(seq 30); do
    if : >&/dev/null </dev/tcp/127.0.0.1/9150 ; then
	echo "Port 9150 seems open."
	break
    elif [ "$idx" == 30 ]; then
	echo "Waited 30 seconds without result; giving up on port 9150."
	exit 1
    else
	echo "waiting for port 9150..."
	sleep 1
    fi
    # verify Arti did not fail early
    # shellcheck disable=SC1091
    source tests/chutney/arti.run
    pid="${pid:?}"
    if ! kill -0 "$pid"; then
	result="${result:?}"
        echo "Arti failed to start with code $result"
	exit 1
    fi
done