test-lib.sh 2.42 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
# assert the `ipfs` we're using is the right one.
15
if test `which ipfs` != $(pwd)/bin/ipfs; then
16 17 18 19 20
	echo >&2 "Cannot find the tests' local ipfs tool."
	echo >&2 "Please check test and ipfs tool installation."
	exit 1
fi

21
SHARNESS_LIB="lib/sharness/sharness.sh"
22

23 24 25 26 27 28 29 30
. "$SHARNESS_LIB" || {
	echo >&2 "Cannot source: $SHARNESS_LIB"
	echo >&2 "Please check Sharness installation."
	exit 1
}

# Please put go-ipfs specific shell functions below

31 32
test "$TEST_NO_FUSE" != 1 && test_set_prereq FUSE

33 34 35 36 37 38 39 40 41
test_cmp_repeat_10_sec() {
	for i in 1 2 3 4 5 6 7 8 9 10
	do
		test_cmp "$1" "$2" && return
		sleep 1
	done
	test_cmp "$1" "$2"
}

42
test_wait_output_n_lines_60_sec() {
43 44 45 46 47 48 49 50 51 52 53
	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"
}

54 55 56 57
test_launch_ipfs_mount() {

	test_expect_success "ipfs init succeeds" '
		export IPFS_DIR="$(pwd)/.go-ipfs" &&
58
		ipfs init -b=1024
59 60 61 62
	'

	test_expect_success "prepare config" '
		mkdir mountdir ipfs ipns &&
63 64
		ipfs config Mounts.IPFS "$(pwd)/ipfs" &&
		ipfs config Mounts.IPNS "$(pwd)/ipns"
65 66
	'

67 68
	test_expect_success FUSE "'ipfs daemon' succeeds" '
		ipfs daemon >actual &
69 70
	'

71
	test_expect_success FUSE "'ipfs daemon' output looks good" '
72
		IPFS_PID=$! &&
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
		echo "daemon listening on /ip4/127.0.0.1/tcp/5001" >expected &&
74
		test_cmp_repeat_10_sec expected actual
75
	'
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

	test_expect_success FUSE "'ipfs mount' succeeds" '
		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
	'
}

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
96 97 98 99
}

test_kill_ipfs_mount() {

100
	test_expect_success FUSE "'ipfs daemon' is still running" '
101 102 103
		kill -0 $IPFS_PID
	'

104 105
	test_expect_success FUSE "'ipfs daemon' can be killed" '
		test_kill_repeat_10_sec $IPFS_PID
106 107
	'
}