test-lib.sh 4.9 KB
Newer Older
1 2 3 4 5 6 7 8
# Test framework for go-ipfs
#
# Copyright (c) 2014 Christian Couder
# MIT Licensed; see the LICENSE file in this repository.
#
# We are using sharness (https://github.com/mlafeldt/sharness)
# which was extracted from the Git test framework.

9 10 11
# use the ipfs tool to test against

# add current directory to path, for ipfs tool.
12
PATH=$(pwd)/bin:${PATH}
13

14 15 16 17 18
# set sharness verbosity. we set the env var directly as
# it's too late to pass in --verbose, and --verbose is harder
# to pass through in some cases.
test "$TEST_VERBOSE" = 1 && verbose=t

19
# assert the `ipfs` we're using is the right one.
20
if test `which ipfs` != $(pwd)/bin/ipfs; then
21 22 23 24 25
	echo >&2 "Cannot find the tests' local ipfs tool."
	echo >&2 "Please check test and ipfs tool installation."
	exit 1
fi

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26 27 28 29 30

# source the common hashes first.
. lib/test-lib-hashes.sh


31
SHARNESS_LIB="lib/sharness/sharness.sh"
32

33 34 35 36 37 38
. "$SHARNESS_LIB" || {
	echo >&2 "Cannot source: $SHARNESS_LIB"
	echo >&2 "Please check Sharness installation."
	exit 1
}

39 40 41 42 43 44 45
# overriding testcmp to make it use fsh (to see it better in output)
# have to do it twice so the first diff output doesnt show unless it's
# broken.
test_cmp() {
	diff -q "$@" >/dev/null || fsh diff -u "$@"
}

46 47
# Please put go-ipfs specific shell functions below

48
test "$TEST_NO_FUSE" != 1 && test_set_prereq FUSE
49
test "$TEST_EXPENSIVE" = 1 && test_set_prereq EXPENSIVE
50

51 52 53
test_cmp_repeat_10_sec() {
	for i in 1 2 3 4 5 6 7 8 9 10
	do
54
		test_cmp "$1" "$2" >/dev/null && return
55 56 57 58 59
		sleep 1
	done
	test_cmp "$1" "$2"
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60 61 62 63 64 65 66 67 68
test_run_repeat_10_sec() {
	for i in 1 2 3 4 5 6 7 8 9 10
	do
		(test_eval_ "$1") && return
		sleep 1
	done
	return 1 # failed
}

69
test_wait_output_n_lines_60_sec() {
70 71 72 73 74 75 76 77 78 79 80
	echo "$2" >expected_waitn
	for i in 1 2 3 4 5 6 7 8 9 10
	do
		cat "$1" | wc -l | tr -d " " >actual_waitn
		test_cmp "expected_waitn" "actual_waitn" && return
		sleep 2
	done
	cat "$1" | wc -l | tr -d " " >actual_waitn
	test_cmp "expected_waitn" "actual_waitn"
}

81 82
test_wait_open_tcp_port_10_sec() {
	for i in 1 2 3 4 5 6 7 8 9 10; do
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84 85 86 87
		# this is not a perfect check, but it's portable.
		# cant count on ss. not installed everywhere.
		# cant count on netstat using : or . as port delim. differ across platforms.
		echo $(netstat -aln | egrep "^tcp.*LISTEN" | egrep "[.:]$1" | wc -l) -gt 0
		if [ $(netstat -aln | egrep "^tcp.*LISTEN" | egrep "[.:]$1" | wc -l) -gt 0 ]; then
88 89 90 91 92 93 94
			return 0
		fi
		sleep 1
	done
	return 1
}

95
test_init_ipfs() {
96 97

	test_expect_success "ipfs init succeeds" '
98
		export IPFS_PATH="$(pwd)/.go-ipfs" &&
99
		ipfs init -b=1024 > /dev/null
100 101 102 103
	'

	test_expect_success "prepare config" '
		mkdir mountdir ipfs ipns &&
104
		ipfs config Mounts.IPFS "$(pwd)/ipfs" &&
105 106
		ipfs config Mounts.IPNS "$(pwd)/ipns" &&
		ipfs bootstrap rm --all
107 108
	'

109 110
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111 112 113 114 115 116 117 118 119 120 121 122 123
test_config_ipfs_gateway_readonly() {
	test_expect_success "prepare config -- gateway readonly" '
	  ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/5002
	'
}

test_config_ipfs_gateway_writable() {
	test_expect_success "prepare config -- gateway writable" '
	  ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/5002 &&
	  ipfs config -bool Gateway.Writable true
	'
}

124 125
test_launch_ipfs_daemon() {

126
	test_expect_success "'ipfs daemon' succeeds" '
127
		ipfs daemon >actual_daemon 2>daemon_err &
128 129
	'

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130 131 132
	# we say the daemon is ready when the API server is ready.
	# and we make sure there are no errors
	test_expect_success "'ipfs daemon' is ready" '
133
		IPFS_PID=$! &&
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
134 135 136 137 138 139 140 141 142 143
		test_run_repeat_10_sec "cat actual_daemon | grep \"API server listening on\"" &&
		printf "" >empty && test_cmp daemon_err empty ||
		fsh cat actual_daemon || fsh cat daemon_err
	'

	ADDR_API="/ip4/127.0.0.1/tcp/5001"
	test_expect_success "'ipfs daemon' output includes API address" '
		cat actual_daemon | grep "API server listening on $ADDR_API" ||
		fsh cat actual_daemon ||
		fsh "cat actual_daemon | grep \"API server listening on $ADDR_API\""
144
	'
145

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146 147 148 149 150 151 152 153
	ADDR_GWAY=`ipfs config Addresses.Gateway`
	if test "$ADDR_GWAY" != ""; then
		test_expect_success "'ipfs daemon' output includes Gateway address" '
			cat actual_daemon | grep "Gateway server listening on $ADDR_GWAY" ||
			fsh cat actual_daemon ||
			fsh "cat actual_daemon | grep \"Gateway server listening on $ADDR_GWAY\""
		'
	fi
154 155 156
}

test_mount_ipfs() {
157

158
	# make sure stuff is unmounted first.
159
	test_expect_success FUSE "'ipfs mount' succeeds" '
160 161
		umount $(pwd)/ipfs || true &&
		umount $(pwd)/ipns || true &&
162 163 164 165 166 167 168 169
		ipfs mount >actual
	'

	test_expect_success FUSE "'ipfs mount' output looks good" '
		echo "IPFS mounted at: $(pwd)/ipfs" >expected &&
		echo "IPNS mounted at: $(pwd)/ipns" >>expected &&
		test_cmp expected actual
	'
170 171 172 173 174 175 176 177 178

}

test_launch_ipfs_daemon_and_mount() {

	test_init_ipfs
	test_launch_ipfs_daemon
	test_mount_ipfs

179 180 181 182 183 184 185 186 187 188
}

test_kill_repeat_10_sec() {
	for i in 1 2 3 4 5 6 7 8 9 10
	do
		kill $1
		sleep 1
		! kill -0 $1 2>/dev/null && return
	done
	! kill -0 $1 2>/dev/null
189 190
}

191
test_kill_ipfs_daemon() {
192

193
	test_expect_success "'ipfs daemon' is still running" '
194 195 196
		kill -0 $IPFS_PID
	'

197
	test_expect_success "'ipfs daemon' can be killed" '
198
		test_kill_repeat_10_sec $IPFS_PID
199 200
	'
}