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 7, 2015
1 parent 2c0fa4b commit 0e1e1bf
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 73 deletions.
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
167 changes: 101 additions & 66 deletions test/sharness/t0160-resolve.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,104 +14,139 @@ test_expect_success "resolve: prepare files" '
c_hash=$(ipfs add -q -r a/b/c | tail -n1)
'

test_resolve_setup_name() {
ref=$1
TEST_COMMAND=test_expect_success

test_expect_success "resolve: prepare name" '
test_name_publish() {
ref="$1"; shift
publish_args="$(shellquote "$@")"

"$TEST_COMMAND" "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_COMMAND" "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"

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

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

test_resolve_cmd() {

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"
# should work offline

test_resolve_setup_name "/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 "/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/$b_hash"
test_resolve "/ipns/$id_hash" "/ipfs/$b_hash"
test_resolve "/ipns/$id_hash/c" "/ipfs/$c_hash"
# Cache should not be in effect despite using the default TTL.
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/$c_hash"
test_resolve "/ipns/$id_hash" "/ipfs/$c_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"

#todo remove this once the online resolve is fixed
test_resolve_fail() {
src=$1
dst=$2
test_name_publish "/ipfs/$c_hash"
test_name_resolve "/ipfs/$c_hash"
test_resolve "/ipns/$id_hash" "/ipfs/$c_hash"

test_expect_failure "resolve succeeds: $src" '
ipfs resolve "$src" >actual
'
# should work online
test_launch_ipfs_daemon

test_expect_failure "resolved correctly: $src -> $dst" '
printf "$dst" >expected &&
test_cmp expected actual
'
}
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"

# https://github.com/ipfs/go-ipfs/issues/1941 sharness suite: ipfs name
# publish: “Error: failed to find any peer in table”
TEST_COMMAND=test_expect_failure

test_name_publish "/ipfs/$a_hash" --ttl=0s
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_name_publish "/ipfs/$b_hash" --ttl=0s
test_name_resolve "/ipfs/$b_hash"
test_resolve "/ipns/$id_hash" "/ipfs/$b_hash"
test_resolve "/ipns/$id_hash/c" "/ipfs/$c_hash"

test_name_publish "/ipfs/$c_hash" --ttl=0s
test_name_resolve "/ipfs/$c_hash"
test_resolve "/ipns/$id_hash" "/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 (wait "$EXPIRY_PID").

TTL=10

test_name_publish "/ipfs/$a_hash" --ttl="${TTL}s"
test_name_resolve "/ipfs/$a_hash"

# The cache entry has expired when this finishes.
go-sleep "${TTL}s" & EXPIRY_PID="$!"
# Publish a new version now, the previous version should still be cached.
test_name_publish "/ipfs/$b_hash" --ttl="${TTL}s"

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" '
kill -0 "$EXPIRY_PID"
'
wait "$EXPIRY_PID"
# The previous version is no longer cached.

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"
test_name_resolve "/ipfs/$b_hash"

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"
go-sleep "${TTL}s" & EXPIRY_PID="$!"
# 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_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_resolve "/ipns/$id_hash" "/ipfs/$b_hash"
test_resolve "/ipns/$id_hash/c" "/ipfs/$c_hash"

test_resolve_setup_name_fail "/ipfs/$c_hash"
test_resolve_fail "/ipns/$id_hash" "/ipfs/$c_hash"
}
test_expect_success "tests did not take too long" '
kill -0 "$EXPIRY_PID"
'
wait "$EXPIRY_PID"
# The previous version is no longer cached.

# should work offline
test_resolve_cmd
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 0e1e1bf

Please sign in to comment.