test-lib.sh 3.21 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
test_init_ipfs() {
68 69

	test_expect_success "ipfs init succeeds" '
70
		export IPFS_PATH="$(pwd)/.go-ipfs" &&
71
		ipfs init -b=1024 > /dev/null
72 73 74 75
	'

	test_expect_success "prepare config" '
		mkdir mountdir ipfs ipns &&
76
		ipfs config Mounts.IPFS "$(pwd)/ipfs" &&
77 78
		ipfs config Mounts.IPNS "$(pwd)/ipns" &&
		ipfs bootstrap rm --all
79 80
	'

81 82 83 84
}

test_launch_ipfs_daemon() {

85
	test_expect_success "'ipfs daemon' succeeds" '
86
		ipfs daemon >actual_daemon 2>daemon_err &
87 88
	'

89
	test_expect_success "'ipfs daemon' output looks good" '
90
		IPFS_PID=$! &&
91 92
		echo "API server listening on /ip4/127.0.0.1/tcp/5001" >expected_daemon &&
		test_cmp_repeat_10_sec expected_daemon actual_daemon ||
93 94
		fsh cat daemon_err
	'
95

96 97 98
}

test_mount_ipfs() {
99

100
	# make sure stuff is unmounted first.
101
	test_expect_success FUSE "'ipfs mount' succeeds" '
102 103
		umount $(pwd)/ipfs || true &&
		umount $(pwd)/ipns || true &&
104 105 106 107 108 109 110 111
		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
	'
112 113 114 115 116 117 118 119 120

}

test_launch_ipfs_daemon_and_mount() {

	test_init_ipfs
	test_launch_ipfs_daemon
	test_mount_ipfs

121 122 123 124 125 126 127 128 129 130
}

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
131 132
}

133
test_kill_ipfs_daemon() {
134

135
	test_expect_success "'ipfs daemon' is still running" '
136 137 138
		kill -0 $IPFS_PID
	'

139
	test_expect_success "'ipfs daemon' can be killed" '
140
		test_kill_repeat_10_sec $IPFS_PID
141 142
	'
}