test-lib.sh 5.88 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
# grab + output options
49
test "$TEST_NO_FUSE" != 1 && test_set_prereq FUSE
50
test "$TEST_EXPENSIVE" = 1 && test_set_prereq EXPENSIVE
51

52 53 54 55 56 57
if test "$TEST_VERBOSE" = 1; then
	echo '# TEST_VERBOSE='"$TEST_VERBOSE"
	echo '# TEST_NO_FUSE='"$TEST_NO_FUSE"
	echo '# TEST_EXPENSIVE='"$TEST_EXPENSIVE"
fi

58 59 60
test_cmp_repeat_10_sec() {
	for i in 1 2 3 4 5 6 7 8 9 10
	do
61
		test_cmp "$1" "$2" >/dev/null && return
62 63 64 65 66
		sleep 1
	done
	test_cmp "$1" "$2"
}

67 68
test_run_repeat_60_sec() {
	for i in 1 2 3 4 5 6
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69
	do
70 71 72 73 74
		for i in 1 2 3 4 5 6 7 8 9 10
		do
			(test_eval_ "$1") && return
			sleep 1
		done
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75 76 77 78
	done
	return 1 # failed
}

79
test_wait_output_n_lines_60_sec() {
80
	for i in 1 2 3 4 5 6
81
	do
82 83 84 85 86
		for i in 1 2 3 4 5 6 7 8 9 10
		do
			test $(cat "$1" | wc -l | tr -d " ") -ge $2 && return
			sleep 1
		done
87
	done
88 89
	actual=$(cat "$1" | wc -l | tr -d " ")
	fsh "expected $2 lines of output. got $actual"
90 91
}

92 93
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
94 95 96 97 98
		# 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
99 100 101 102 103 104 105
			return 0
		fi
		sleep 1
	done
	return 1
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

# test_config_set helps us make sure _we really did set_ a config value.
# it sets it and then tests it. This became elaborate because ipfs config
# was setting really weird things and am not sure why.
test_config_set() {

	# grab flags (like -bool in "ipfs config -bool")
	test_cfg_flags="" # unset in case.
	test "$#" = 3 && { test_cfg_flags=$1; shift; }

	test_cfg_key=$1
	test_cfg_val=$2

	# when verbose, tell the user what config values are being set
	test_cfg_cmd="ipfs config $test_cfg_flags \"$test_cfg_key\" \"$test_cfg_val\""
	test "$TEST_VERBOSE" = 1 && echo "$test_cfg_cmd"

	# ok try setting the config key/val pair.
	ipfs config $test_cfg_flags "$test_cfg_key" "$test_cfg_val"
	echo "$test_cfg_val" >cfg_set_expected
	ipfs config "$test_cfg_key" >cfg_set_actual
	test_cmp cfg_set_expected cfg_set_actual
}

130
test_init_ipfs() {
131 132

	test_expect_success "ipfs init succeeds" '
133
		export IPFS_PATH="$(pwd)/.go-ipfs" &&
134
		ipfs init -b=1024 > /dev/null
135 136
	'

137
	test_expect_success "prepare config -- mounting and bootstrap rm" '
138
		mkdir mountdir ipfs ipns &&
139 140 141 142
		test_config_set Mounts.IPFS "$(pwd)/ipfs" &&
		test_config_set Mounts.IPNS "$(pwd)/ipns" &&
		ipfs bootstrap rm --all ||
		fsh cat "\"$IPFS_PATH/config\""
143 144
	'

145 146
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
147
test_config_ipfs_gateway_readonly() {
148
	ADDR_GWAY=$1
149
	test_expect_success "prepare config -- gateway address" '
150 151
		test "$ADDR_GWAY" != "" &&
		test_config_set "Addresses.Gateway" "$ADDR_GWAY"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
152
	'
153 154 155 156 157 158 159

	# tell the user what's going on if they messed up the call.
	if test "$#" = 0; then
		echo "#			Error: must call with an address, for example:"
		echo '#			test_config_ipfs_gateway_readonly "/ip4/0.0.0.0/tcp/5002"'
		echo '#'
	fi
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
160 161 162
}

test_config_ipfs_gateway_writable() {
163 164 165

	test_config_ipfs_gateway_readonly $1

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
166
	test_expect_success "prepare config -- gateway writable" '
167 168
		test_config_set -bool Gateway.Writable true ||
		fsh cat "\"$IPFS_PATH/config\""
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
169 170 171
	'
}

172 173
test_launch_ipfs_daemon() {

174
	test_expect_success "'ipfs daemon' succeeds" '
175
		ipfs daemon >actual_daemon 2>daemon_err &
176 177
	'

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
178
	# we say the daemon is ready when the API server is ready.
179
	ADDR_API="/ip4/127.0.0.1/tcp/5001"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
180
	test_expect_success "'ipfs daemon' is ready" '
181
		IPFS_PID=$! &&
182
		test_wait_output_n_lines_60_sec actual_daemon 2 &&
183
		test_run_repeat_60_sec "grep \"API server listening on $ADDR_API\" actual_daemon" ||
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
184 185 186 187 188
		fsh cat actual_daemon || fsh cat daemon_err
	'

	if test "$ADDR_GWAY" != ""; then
		test_expect_success "'ipfs daemon' output includes Gateway address" '
189
			test_run_repeat_60_sec "grep \"Gateway server listening on $ADDR_GWAY\" actual_daemon" ||
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
190
			fsh cat daemon_err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
191 192
		'
	fi
193 194 195
}

test_mount_ipfs() {
196

197
	# make sure stuff is unmounted first.
198
	test_expect_success FUSE "'ipfs mount' succeeds" '
199 200
		umount $(pwd)/ipfs || true &&
		umount $(pwd)/ipns || true &&
201 202 203 204 205 206 207 208
		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
	'
209 210 211 212 213 214 215 216 217

}

test_launch_ipfs_daemon_and_mount() {

	test_init_ipfs
	test_launch_ipfs_daemon
	test_mount_ipfs

218 219 220 221 222 223 224 225 226 227
}

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
228 229
}

230
test_kill_ipfs_daemon() {
231

232
	test_expect_success "'ipfs daemon' is still running" '
233 234 235
		kill -0 $IPFS_PID
	'

236
	test_expect_success "'ipfs daemon' can be killed" '
237
		test_kill_repeat_10_sec $IPFS_PID
238 239
	'
}