test.sh 5.85 KB
Newer Older
1
#!/usr/bin/env bash
2 3 4 5

set -e

if [ -n "$SKIP_TESTS" ]; then
6
	exit 0
7 8
fi

9 10 11 12 13
SOURCE_DIR=${SOURCE_DIR:-$( cd "$( dirname "${BASH_SOURCE[0]}" )" && dirname $( pwd ) )}
BUILD_DIR=$(pwd)
TMPDIR=${TMPDIR:-/tmp}
USER=${USER:-$(whoami)}

14 15
SUCCESS=1

16
VALGRIND="valgrind --leak-check=full --show-reachable=yes --error-exitcode=125 --num-callers=50 --suppressions=\"$SOURCE_DIR/libgit2_clar.supp\""
17
LEAKS="MallocStackLogging=1 MallocScribble=1 leaks -quiet -atExit -- nohup"
18

19 20 21 22
cleanup() {
	echo "Cleaning up..."

	if [ ! -z "$GITDAEMON_DIR" -a -f "${GITDAEMON_DIR}/pid" ]; then
23
		echo "Stopping git daemon..."
24 25 26 27
		kill $(cat "${GITDAEMON_DIR}/pid")
	fi

	if [ ! -z "$SSHD_DIR" -a -f "${SSHD_DIR}/pid" ]; then
28
		echo "Stopping SSH..."
29 30
		kill $(cat "${SSHD_DIR}/pid")
	fi
31 32

	echo "Done."
33 34
}

35
failure() {
36
	echo "Test exited with code: $1"
37
	SUCCESS=0
38 39
}

40 41 42 43 44
# Ask ctest what it would run if we were to invoke it directly.  This lets
# us manage the test configuration in a single place (tests/CMakeLists.txt)
# instead of running clar here as well.  But it allows us to wrap our test
# harness with a leak checker like valgrind.  Append the option to write
# JUnit-style XML files.
45
run_test() {
46
	TEST_CMD=$(ctest -N -V -R "^${1}$" | sed -n 's/^[0-9]*: Test command: //p')
47
	TEST_CMD="${TEST_CMD} -r${BUILD_DIR}/results_${1}.xml"
48 49 50

	if [ "$LEAK_CHECK" = "valgrind" ]; then
		RUNNER="$VALGRIND $TEST_CMD"
51 52
	elif [ "$LEAK_CHECK" = "leaks" ]; then
		RUNNER="$LEAKS $TEST_CMD"
53 54 55 56
	else
		RUNNER="$TEST_CMD"
	fi

57
	eval $RUNNER || failure
58
}
59

60 61 62
# Configure the test environment; run them early so that we're certain
# that they're started by the time we need them.

63
echo "##############################################################################"
64
echo "## Configuring test environment"
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
echo "##############################################################################"

if [ -z "$SKIP_GITDAEMON_TESTS" ]; then
	echo "Starting git daemon..."
	GITDAEMON_DIR=`mktemp -d ${TMPDIR}/gitdaemon.XXXXXXXX`
	git init --bare "${GITDAEMON_DIR}/test.git"
	git daemon --listen=localhost --export-all --enable=receive-pack --pid-file="${GITDAEMON_DIR}/pid" --base-path="${GITDAEMON_DIR}" "${GITDAEMON_DIR}" 2>/dev/null &
fi

if [ -z "$SKIP_PROXY_TESTS" ]; then
	echo "Starting HTTP proxy..."
	curl -L https://github.com/ethomson/poxyproxy/releases/download/v0.1.0/poxyproxy-0.1.0.jar >poxyproxy.jar
	java -jar poxyproxy.jar -d --port 8080 --credentials foo:bar >/dev/null 2>&1 &
fi

if [ -z "$SKIP_SSH_TESTS" ]; then
	echo "Starting ssh daemon..."
	HOME=`mktemp -d ${TMPDIR}/home.XXXXXXXX`
	SSHD_DIR=`mktemp -d ${TMPDIR}/sshd.XXXXXXXX`
	git init --bare "${SSHD_DIR}/test.git"
	cat >"${SSHD_DIR}/sshd_config" <<-EOF
86 87 88 89 90
	Port 2222
	ListenAddress 0.0.0.0
	Protocol 2
	HostKey ${SSHD_DIR}/id_rsa
	PidFile ${SSHD_DIR}/pid
91 92
	AuthorizedKeysFile ${HOME}/.ssh/authorized_keys
	LogLevel DEBUG
93 94 95 96
	RSAAuthentication yes
	PasswordAuthentication yes
	PubkeyAuthentication yes
	ChallengeResponseAuthentication no
97
	StrictModes no
98 99
	# Required here as sshd will simply close connection otherwise
	UsePAM no
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	EOF
	ssh-keygen -t rsa -f "${SSHD_DIR}/id_rsa" -N "" -q
	/usr/sbin/sshd -f "${SSHD_DIR}/sshd_config" -E "${SSHD_DIR}/log"

	# Set up keys
	mkdir "${HOME}/.ssh"
	ssh-keygen -t rsa -f "${HOME}/.ssh/id_rsa" -N "" -q
	cat "${HOME}/.ssh/id_rsa.pub" >>"${HOME}/.ssh/authorized_keys"
	while read algorithm key comment; do
		echo "[localhost]:2222 $algorithm $key" >>"${HOME}/.ssh/known_hosts"
	done <"${SSHD_DIR}/id_rsa.pub"

	# Get the fingerprint for localhost and remove the colons so we can
	# parse it as a hex number. Older versions have a different output
	# format.
	if [[ $(ssh -V 2>&1) == OpenSSH_6* ]]; then
		SSH_FINGERPRINT=$(ssh-keygen -F '[localhost]:2222' -f "${HOME}/.ssh/known_hosts" -l | tail -n 1 | cut -d ' ' -f 2 | tr -d ':')
	else
		SSH_FINGERPRINT=$(ssh-keygen -E md5 -F '[localhost]:2222' -f "${HOME}/.ssh/known_hosts" -l | tail -n 1 | cut -d ' ' -f 3 | cut -d : -f2- | tr -d :)
	fi
120 121 122 123
fi

# Run the tests that do not require network connectivity.

124 125 126 127 128
if [ -z "$SKIP_OFFLINE_TESTS" ]; then
	echo ""
	echo "##############################################################################"
	echo "## Running (offline) tests"
	echo "##############################################################################"
129

130
	run_test offline
131
fi
132

133 134 135 136
if [ -z "$SKIP_ONLINE_TESTS" ]; then
	# Run the various online tests.  The "online" test suite only includes the
	# default online tests that do not require additional configuration.  The
	# "proxy" and "ssh" test suites require further setup.
137

138 139 140 141
	echo ""
	echo "##############################################################################"
	echo "## Running (online) tests"
	echo "##############################################################################"
142

143
	run_test online
144
fi
145

146 147 148 149
if [ -z "$SKIP_GITDAEMON_TESTS" ]; then
	echo ""
	echo "Running gitdaemon tests"
	echo ""
150

151
	export GITTEST_REMOTE_URL="git://localhost/test.git"
152
	run_test gitdaemon
153 154
	unset GITTEST_REMOTE_URL
fi
155

156 157 158 159 160 161 162 163
if [ -z "$SKIP_PROXY_TESTS" ]; then
	echo ""
	echo "Running proxy tests"
	echo ""

	export GITTEST_REMOTE_PROXY_URL="localhost:8080"
	export GITTEST_REMOTE_PROXY_USER="foo"
	export GITTEST_REMOTE_PROXY_PASS="bar"
164
	run_test proxy
165 166 167 168
	unset GITTEST_REMOTE_PROXY_URL
	unset GITTEST_REMOTE_PROXY_USER
	unset GITTEST_REMOTE_PROXY_PASS
fi
169

170 171 172 173 174 175 176 177 178 179 180
if [ -z "$SKIP_SSH_TESTS" ]; then
	echo ""
	echo "Running ssh tests"
	echo ""

	export GITTEST_REMOTE_URL="ssh://localhost:2222/$SSHD_DIR/test.git"
	export GITTEST_REMOTE_USER=$USER
	export GITTEST_REMOTE_SSH_KEY="${HOME}/.ssh/id_rsa"
	export GITTEST_REMOTE_SSH_PUBKEY="${HOME}/.ssh/id_rsa.pub"
	export GITTEST_REMOTE_SSH_PASSPHRASE=""
	export GITTEST_REMOTE_SSH_FINGERPRINT="${SSH_FINGERPRINT}"
181
	run_test ssh
182 183 184 185 186 187 188
	unset GITTEST_REMOTE_URL
	unset GITTEST_REMOTE_USER
	unset GITTEST_REMOTE_SSH_KEY
	unset GITTEST_REMOTE_SSH_PUBKEY
	unset GITTEST_REMOTE_SSH_PASSPHRASE
	unset GITTEST_REMOTE_SSH_FINGERPRINT
fi
189

190
cleanup
191 192 193 194 195 196 197

if [ "$SUCCESS" -ne "1" ]; then
	echo "Some tests failed."
	exit 1
fi

echo "Success."
198
exit 0