Skip to content

Commit

Permalink
sharness tests for caching and --ttl
Browse files Browse the repository at this point in the history
Closes ipfs#1934.

License: MIT
Signed-off-by: Johan Kiviniemi <[email protected]>
  • Loading branch information
ion1 committed Nov 8, 2015
1 parent 2c0fa4b commit f38ff52
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 64 deletions.
41 changes: 39 additions & 2 deletions test/sharness/lib/test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ test_config_ipfs_gateway_writable() {
'
}

IPFS_PID=""
test_launch_ipfs_daemon() {

args="$@"
Expand Down Expand Up @@ -261,11 +262,12 @@ test_kill_repeat_10_sec() {
test_kill_ipfs_daemon() {

test_expect_success "'ipfs daemon' is still running" '
kill -0 $IPFS_PID
kill -0 "$IPFS_PID"
'

test_expect_success "'ipfs daemon' can be killed" '
test_kill_repeat_10_sec $IPFS_PID
test_kill_repeat_10_sec "$IPFS_PID" &&
IPFS_PID=""
'
}

Expand Down Expand Up @@ -353,3 +355,38 @@ test_check_peerid() {
}
}

# A workaround for https://github.com/ipfs/go-ipfs/issues/1941 ‘sharness suite:
# ipfs name publish: “Error: failed to find any peer in table”’. Certain
# commands fail whenever the daemon is running with no peers.
# test_expect_success_1941 runs test_expect_failure if that is the case and
# test_expect_success otherwise. As soon as #1941 is fixed, all invocations of
# test_expect_success_1941 can be replaced with test_expect_success and this
# function can be removed.
test_expect_success_1941() {
if [ -n "$IPFS_PID" ] && [ -z "$(ipfs swarm peers)" ]; then
# The daemon is running and has no peers.
test_expect_failure "(TODO: #1941) $@"
else
test_expect_success "$@"
fi
}

# Start a new timer.
# > test_timer_start TIMER_VARIABLE 10s
test_timer_start() {
go-sleep "$2" &
eval "$1='$!'"
}

# Check whether the given timer is still running. Only use before doing a
# test_timer_wait.
# > test_timer_is_running "$TIMER_VARIABLE"
test_timer_is_running() {
kill -0 "$1" 2>/dev/null
}

# Wait until the given timer finishes. Use against a timer exactly once.
# > test_timer_wait "$TIMER_VARIABLE"
test_timer_wait() {
wait "$1"
}
18 changes: 11 additions & 7 deletions test/sharness/t0100-name.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ test_description="Test ipfs repo operations"

test_init_ipfs

for TTL_PARAMS in "" "--ttl=0s" "--ttl=1h"; do

# test publishing a hash

test_expect_success "'ipfs name publish' succeeds" '
test_expect_success "'ipfs name publish${TTL_PARAMS:+ $TTL_PARAMS}' succeeds" '
PEERID=`ipfs id --format="<id>"` &&
test_check_peerid "${PEERID}" &&
ipfs name publish "/ipfs/$HASH_WELCOME_DOCS" >publish_out
ipfs name publish $TTL_PARAMS "/ipfs/$HASH_WELCOME_DOCS" >publish_out
'

test_expect_success "publish output looks good" '
Expand All @@ -34,10 +36,10 @@ test_expect_success "resolve output looks good" '

# now test with a path

test_expect_success "'ipfs name publish' succeeds" '
test_expect_success "'ipfs name publish${TTL_PARAMS:+ $TTL_PARAMS}' succeeds" '
PEERID=`ipfs id --format="<id>"` &&
test_check_peerid "${PEERID}" &&
ipfs name publish "/ipfs/$HASH_WELCOME_DOCS/help" >publish_out
ipfs name publish $TTL_PARAMS "/ipfs/$HASH_WELCOME_DOCS/help" >publish_out
'

test_expect_success "publish a path looks good" '
Expand All @@ -62,16 +64,18 @@ test_expect_success "ipfs cat on published content succeeds" '

# publish with an explicit node ID

test_expect_failure "'ipfs name publish <local-id> <hash>' succeeds" '
test_expect_failure "'ipfs name publish${TTL_PARAMS:+ $TTL_PARAMS} <local-id> <hash>' succeeds" '
PEERID=`ipfs id --format="<id>"` &&
test_check_peerid "${PEERID}" &&
echo ipfs name publish "${PEERID}" "/ipfs/$HASH_WELCOME_DOCS" &&
ipfs name publish "${PEERID}" "/ipfs/$HASH_WELCOME_DOCS" >actual_node_id_publish
echo ipfs name publish $TTL_PARAMS "${PEERID}" "/ipfs/$HASH_WELCOME_DOCS" &&
ipfs name publish $TTL_PARAMS "${PEERID}" "/ipfs/$HASH_WELCOME_DOCS" >actual_node_id_publish
'

test_expect_failure "publish with our explicit node ID looks good" '
echo "Published to ${PEERID}: /ipfs/$HASH_WELCOME_DOCS" >expected_node_id_publish &&
test_cmp expected_node_id_publish actual_node_id_publish
'

done

test_done
35 changes: 35 additions & 0 deletions test/sharness/t0130-multinode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ check_file_fetch() {
'
}

test_resolve() {
_nid="$1"
_name="$2"
_expected="$3"
_comment="${4:+ ($4)}" # Optional

test_expect_success "IPNS resolve on node $_nid$_comment" '
printf "%s" "$_expected" >expected &&
ipfsi "$_nid" resolve -r "$_name" >actual &&
test_cmp expected actual
'
}

run_basic_test() {
startup_cluster

Expand All @@ -67,6 +80,28 @@ run_basic_test() {
check_file_fetch 1 $FILEA_HASH filea
check_file_fetch 0 $FILEA_HASH filea

ref1="/ipfs/$HASH_WELCOME_DOCS"
test_expect_success "IPNS publish on node 1" '
node="$(ipfsi 1 id -f="<id>")" &&
ipfsi 1 name publish "$ref1"
'

for nid in 0 1 2; do
test_resolve "$nid" "/ipns/$node" "$ref1"
done

ref2="/ipfs/$FILEA_HASH"
test_expect_success "IPNS publish again on node 1" '
ipfsi 1 name publish "$ref2"
'

for nid in 0 1 2; do
test_resolve "$nid" "/ipns/$node" "$ref1" "cached result"
done
for nid in 3 4; do
test_resolve "$nid" "/ipns/$node" "$ref2" "new result"
done

test_expect_success "shut down nodes" '
iptb stop
'
Expand Down
135 changes: 80 additions & 55 deletions test/sharness/t0160-resolve.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,104 +14,129 @@ test_expect_success "resolve: prepare files" '
c_hash=$(ipfs add -q -r a/b/c | tail -n1)
'

test_resolve_setup_name() {
ref=$1
test_name_publish() {
ref="$1"; shift
publish_args="$(shellquote "$@")"

test_expect_success "resolve: prepare name" '
test_expect_success_1941 "resolve: name publish $ref${1:+ ($publish_args)}" '
id_hash=$(ipfs id -f="<id>") &&
ipfs name publish "$ref" &&
printf "$ref" >expected_nameval &&
ipfs name resolve >actual_nameval &&
test_cmp expected_nameval actual_nameval
ipfs name publish '"$publish_args"' "$ref"
'
}

test_resolve_setup_name_fail() {
ref=$1
test_name_resolve() {
ref="$1"

test_expect_failure "resolve: prepare name" '
id_hash=$(ipfs id -f="<id>") &&
ipfs name publish "$ref" &&
printf "$ref" >expected_nameval &&
test_expect_success_1941 "resolve: name resolve $ref" '
printf "%s" "$ref" >expected_nameval &&
ipfs name resolve >actual_nameval &&
test_cmp expected_nameval actual_nameval
'
}

test_resolve() {
src=$1
dst=$2
src="$1"
dst="$2"

# Just use test_expect_success once #1941 is fixed.
case "$src" in
/ipns/*) test_cmd=test_expect_success_1941 ;;
*) test_cmd=test_expect_success ;;
esac

test_expect_success "resolve succeeds: $src" '
"$test_cmd" "resolve succeeds: $src" '
ipfs resolve -r "$src" >actual
'

test_expect_success "resolved correctly: $src -> $dst" '
printf "$dst" >expected &&
"$test_cmd" "resolved correctly: $src -> $dst" '
printf "%s" "$dst" >expected &&
test_cmp expected actual
'
}

test_resolve_cmd() {

# Any parameters are passed to ipfs name publish.
test_common() {
test_resolve "/ipfs/$a_hash" "/ipfs/$a_hash"
test_resolve "/ipfs/$a_hash/b" "/ipfs/$b_hash"
test_resolve "/ipfs/$a_hash/b/c" "/ipfs/$c_hash"
test_resolve "/ipfs/$b_hash/c" "/ipfs/$c_hash"

test_resolve_setup_name "/ipfs/$a_hash"
test_name_publish "/ipfs/$a_hash" "$@"
test_name_resolve "/ipfs/$a_hash"
test_resolve "/ipns/$id_hash" "/ipfs/$a_hash"
test_resolve "/ipns/$id_hash/b" "/ipfs/$b_hash"
test_resolve "/ipns/$id_hash/b/c" "/ipfs/$c_hash"

test_resolve_setup_name "/ipfs/$b_hash"
test_name_publish "/ipfs/$b_hash" "$@"
test_name_resolve "/ipfs/$b_hash"
test_resolve "/ipns/$id_hash" "/ipfs/$b_hash"
test_resolve "/ipns/$id_hash/c" "/ipfs/$c_hash"

test_resolve_setup_name "/ipfs/$c_hash"
test_name_publish "/ipfs/$c_hash" "$@"
test_name_resolve "/ipfs/$c_hash"
test_resolve "/ipns/$id_hash" "/ipfs/$c_hash"
}

#todo remove this once the online resolve is fixed
test_resolve_fail() {
src=$1
dst=$2
# should work offline
test_common --ttl=0s

test_expect_failure "resolve succeeds: $src" '
ipfs resolve "$src" >actual
'
# should work offline with non-zero TTL; cache should not be in effect.
test_common # Default TTL
test_common --ttl=1h

test_expect_failure "resolved correctly: $src -> $dst" '
printf "$dst" >expected &&
test_cmp expected actual
'
}
# should work online
test_launch_ipfs_daemon
test_common --ttl=0s

test_resolve_cmd_fail() {
test_resolve "/ipfs/$a_hash" "/ipfs/$a_hash"
test_resolve "/ipfs/$a_hash/b" "/ipfs/$b_hash"
test_resolve "/ipfs/$a_hash/b/c" "/ipfs/$c_hash"
test_resolve "/ipfs/$b_hash/c" "/ipfs/$c_hash"
# The following tests test the caching by publishing a new name and expecting
# the previous resolve result to stay cached. The first test_(name_)resolve
# after a test_name_publish will generate the cache entry and any subsequent
# test_resolve invocations will use that cache entry until the expiry timer
# finishes (test_timer_wait).

test_resolve_setup_name_fail "/ipfs/$a_hash"
test_resolve_fail "/ipns/$id_hash" "/ipfs/$a_hash"
test_resolve_fail "/ipns/$id_hash/b" "/ipfs/$b_hash"
test_resolve_fail "/ipns/$id_hash/b/c" "/ipfs/$c_hash"
TTL=10

test_resolve_setup_name_fail "/ipfs/$b_hash"
test_resolve_fail "/ipns/$id_hash" "/ipfs/$b_hash"
test_resolve_fail "/ipns/$id_hash/c" "/ipfs/$c_hash"
test_name_publish "/ipfs/$a_hash" --ttl="${TTL}s"
test_name_resolve "/ipfs/$a_hash"

test_resolve_setup_name_fail "/ipfs/$c_hash"
test_resolve_fail "/ipns/$id_hash" "/ipfs/$c_hash"
}
# The cache entry has expired when this finishes.
test_timer_start EXPIRY_TIMER "${TTL}s"
# Publish a new version now, the previous version should still be cached.
test_name_publish "/ipfs/$b_hash" --ttl="${TTL}s"

# should work offline
test_resolve_cmd
test_resolve "/ipns/$id_hash" "/ipfs/$a_hash"
test_resolve "/ipns/$id_hash/b" "/ipfs/$b_hash"
test_resolve "/ipns/$id_hash/b/c" "/ipfs/$c_hash"

# Make sure the expiry timer is still running, otherwise the result might be
# wrong. If this fails, we will need to increase TTL above to give enough time
# for the tests.
test_expect_success "tests did not take too long" '
test_timer_is_running "$EXPIRY_TIMER"
'
test_expect_success "previous version is no longer cached" '
test_timer_wait "$EXPIRY_TIMER"
'

test_name_resolve "/ipfs/$b_hash"

test_timer_start EXPIRY_TIMER "${TTL}s"
# Publish a new version now, the previous version should still be cached.
test_name_publish "/ipfs/$c_hash" # Default TTL for the final one.

test_resolve "/ipns/$id_hash" "/ipfs/$b_hash"
test_resolve "/ipns/$id_hash/c" "/ipfs/$c_hash"

test_expect_success "tests did not take too long" '
test_timer_is_running "$EXPIRY_TIMER"
'
test_expect_success "previous version is no longer cached" '
test_timer_wait "$EXPIRY_TIMER"
'

test_name_resolve "/ipfs/$c_hash"
test_resolve "/ipns/$id_hash" "/ipfs/$c_hash"

# should work online
test_launch_ipfs_daemon
test_resolve_cmd_fail
test_kill_ipfs_daemon

test_done

0 comments on commit f38ff52

Please sign in to comment.