test-lib.sh 3.38 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

26
SHARNESS_LIB="lib/sharness/sharness.sh"
27

28 29 30 31 32 33
. "$SHARNESS_LIB" || {
	echo >&2 "Cannot source: $SHARNESS_LIB"
	echo >&2 "Please check Sharness installation."
	exit 1
}

34 35 36 37 38 39 40
# 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 "$@"
}

41 42
# Please put go-ipfs specific shell functions below

43
test "$TEST_NO_FUSE" != 1 && test_set_prereq FUSE
44
test "$TEST_EXPENSIVE" = 1 && test_set_prereq EXPENSIVE
45

46 47 48
test_cmp_repeat_10_sec() {
	for i in 1 2 3 4 5 6 7 8 9 10
	do
49
		test_cmp "$1" "$2" >/dev/null && return
50 51 52 53 54
		sleep 1
	done
	test_cmp "$1" "$2"
}

55
test_wait_output_n_lines_60_sec() {
56 57 58 59 60 61 62 63 64 65 66
	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"
}

67 68 69 70 71 72 73 74 75 76
test_wait_open_tcp_port_10_sec() {
	for i in 1 2 3 4 5 6 7 8 9 10; do
		if [ $(ss -lt "sport == :$1" | wc -l) -gt 1 ]; then
			return 0
		fi
		sleep 1
	done
	return 1
}

77
test_init_ipfs() {
78 79

	test_expect_success "ipfs init succeeds" '
80
		export IPFS_PATH="$(pwd)/.go-ipfs" &&
81
		ipfs init -b=1024 > /dev/null
82 83 84 85
	'

	test_expect_success "prepare config" '
		mkdir mountdir ipfs ipns &&
86
		ipfs config Mounts.IPFS "$(pwd)/ipfs" &&
87 88
		ipfs config Mounts.IPNS "$(pwd)/ipns" &&
		ipfs bootstrap rm --all
89 90
	'

91 92 93 94
}

test_launch_ipfs_daemon() {

95
	test_expect_success "'ipfs daemon' succeeds" '
96
		ipfs daemon >actual_daemon 2>daemon_err &
97 98
	'

99
	test_expect_success "'ipfs daemon' output looks good" '
100
		IPFS_PID=$! &&
101 102
		echo "API server listening on /ip4/127.0.0.1/tcp/5001" >expected_daemon &&
		test_cmp_repeat_10_sec expected_daemon actual_daemon ||
103 104
		fsh cat daemon_err
	'
105

106 107 108
}

test_mount_ipfs() {
109

110
	# make sure stuff is unmounted first.
111
	test_expect_success FUSE "'ipfs mount' succeeds" '
112 113
		umount $(pwd)/ipfs || true &&
		umount $(pwd)/ipns || true &&
114 115 116 117 118 119 120 121
		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
	'
122 123 124 125 126 127 128 129 130

}

test_launch_ipfs_daemon_and_mount() {

	test_init_ipfs
	test_launch_ipfs_daemon
	test_mount_ipfs

131 132 133 134 135 136 137 138 139 140
}

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
141 142
}

143
test_kill_ipfs_daemon() {
144

145
	test_expect_success "'ipfs daemon' is still running" '
146 147 148
		kill -0 $IPFS_PID
	'

149
	test_expect_success "'ipfs daemon' can be killed" '
150
		test_kill_repeat_10_sec $IPFS_PID
151 152
	'
}