From f1d34a2a8514c2273c87b062fe9d3ccaaaadaccc Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sat, 31 Jan 2015 19:15:46 -0800 Subject: [PATCH] test/sharness: fix errors - core: daemon stdout print to cmd + daemon init checks - core: fixed bug where the gateway was printed as "API" - sharness/test-lib: daemon init checks - sharness/test-lib: portable TCP port check - sharness/init: fix test bits output - sharness: use common hashes in one place. - move t0100-http-gateway -> t0111-gateway-writable - sharness: test-lib funcs for gateway config - sharness/t0111-gateway-writable: use sh funcs - sharness/t0111-gateway-writable: fixes - escape all vars (always `cmd "$VAR"` never `cmd $VAR`) - use $FILEPATH, not $path - last test seems to fail --- cmd/ipfs/daemon.go | 2 + core/corehttp/corehttp.go | 6 +- test/sharness/lib/test-lib-hashes.sh | 5 ++ test/sharness/lib/test-lib.sh | 58 +++++++++++-- test/sharness/t0020-init.sh | 8 +- test/sharness/t0060-daemon.sh | 7 +- test/sharness/t0080-repo.sh | 14 ++-- test/sharness/t0100-http-gateway.sh | 106 ------------------------ test/sharness/t0111-gateway-writable.sh | 93 +++++++++++++++++++++ 9 files changed, 170 insertions(+), 129 deletions(-) create mode 100644 test/sharness/lib/test-lib-hashes.sh delete mode 100755 test/sharness/t0100-http-gateway.sh create mode 100755 test/sharness/t0111-gateway-writable.sh diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index d89c133ed..12a4dd27d 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -183,6 +183,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) { if rootRedirect != nil { opts = append(opts, rootRedirect) } + fmt.Printf("Gateway server listening on %s\n", gatewayMaddr) err := corehttp.ListenAndServe(node, gatewayMaddr.String(), opts...) if err != nil { log.Error(err) @@ -198,6 +199,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) { if rootRedirect != nil { opts = append(opts, rootRedirect) } + fmt.Printf("API server listening on %s\n", apiMaddr) if err := corehttp.ListenAndServe(node, apiMaddr.String(), opts...); err != nil { res.SetError(err, cmds.ErrNormal) return diff --git a/core/corehttp/corehttp.go b/core/corehttp/corehttp.go index 32b27b261..104b6566f 100644 --- a/core/corehttp/corehttp.go +++ b/core/corehttp/corehttp.go @@ -1,7 +1,6 @@ package corehttp import ( - "fmt" "net/http" manners "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/braintree/manners" @@ -36,10 +35,10 @@ func ListenAndServe(n *core.IpfsNode, listeningMultiAddr string, options ...Serv return err } } - return listenAndServe("API", n, addr, mux) + return listenAndServe(n, addr, mux) } -func listenAndServe(name string, node *core.IpfsNode, addr ma.Multiaddr, mux *http.ServeMux) error { +func listenAndServe(node *core.IpfsNode, addr ma.Multiaddr, mux *http.ServeMux) error { _, host, err := manet.DialArgs(addr) if err != nil { return err @@ -52,7 +51,6 @@ func listenAndServe(name string, node *core.IpfsNode, addr ma.Multiaddr, mux *ht serverExited := make(chan struct{}) go func() { - fmt.Printf("%s server listening on %s\n", name, addr) serverError = server.ListenAndServe(host, mux) close(serverExited) }() diff --git a/test/sharness/lib/test-lib-hashes.sh b/test/sharness/lib/test-lib-hashes.sh new file mode 100644 index 000000000..5dde98bf3 --- /dev/null +++ b/test/sharness/lib/test-lib-hashes.sh @@ -0,0 +1,5 @@ +# this file defines several useful hashes used across the test codebase. +# thus they can be defined + changed in one place + +HASH_WELCOME_DOCS="QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT" +HASH_EMPTY_DIR="QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" diff --git a/test/sharness/lib/test-lib.sh b/test/sharness/lib/test-lib.sh index 3b6992188..c7a65a27a 100644 --- a/test/sharness/lib/test-lib.sh +++ b/test/sharness/lib/test-lib.sh @@ -23,6 +23,11 @@ if test `which ipfs` != $(pwd)/bin/ipfs; then exit 1 fi + +# source the common hashes first. +. lib/test-lib-hashes.sh + + SHARNESS_LIB="lib/sharness/sharness.sh" . "$SHARNESS_LIB" || { @@ -52,6 +57,15 @@ test_cmp_repeat_10_sec() { test_cmp "$1" "$2" } +test_run_repeat_10_sec() { + for i in 1 2 3 4 5 6 7 8 9 10 + do + (test_eval_ "$1") && return + sleep 1 + done + return 1 # failed +} + test_wait_output_n_lines_60_sec() { echo "$2" >expected_waitn for i in 1 2 3 4 5 6 7 8 9 10 @@ -66,7 +80,11 @@ test_wait_output_n_lines_60_sec() { 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 + # this is not a perfect check, but it's portable. + # cant count on ss. not installed everywhere. + # cant count on netstat using : or . as port delim. differ across platforms. + echo $(netstat -aln | egrep "^tcp.*LISTEN" | egrep "[.:]$1" | wc -l) -gt 0 + if [ $(netstat -aln | egrep "^tcp.*LISTEN" | egrep "[.:]$1" | wc -l) -gt 0 ]; then return 0 fi sleep 1 @@ -90,19 +108,49 @@ test_init_ipfs() { } +test_config_ipfs_gateway_readonly() { + test_expect_success "prepare config -- gateway readonly" ' + ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/5002 + ' +} + +test_config_ipfs_gateway_writable() { + test_expect_success "prepare config -- gateway writable" ' + ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/5002 && + ipfs config -bool Gateway.Writable true + ' +} + test_launch_ipfs_daemon() { test_expect_success "'ipfs daemon' succeeds" ' ipfs daemon >actual_daemon 2>daemon_err & ' - test_expect_success "'ipfs daemon' output looks good" ' + # we say the daemon is ready when the API server is ready. + # and we make sure there are no errors + test_expect_success "'ipfs daemon' is ready" ' IPFS_PID=$! && - echo "API server listening on /ip4/127.0.0.1/tcp/5001" >expected_daemon && - test_cmp_repeat_10_sec expected_daemon actual_daemon || - fsh cat daemon_err + test_run_repeat_10_sec "cat actual_daemon | grep \"API server listening on\"" && + printf "" >empty && test_cmp daemon_err empty || + fsh cat actual_daemon || fsh cat daemon_err ' + ADDR_API="/ip4/127.0.0.1/tcp/5001" + test_expect_success "'ipfs daemon' output includes API address" ' + cat actual_daemon | grep "API server listening on $ADDR_API" || + fsh cat actual_daemon || + fsh "cat actual_daemon | grep \"API server listening on $ADDR_API\"" + ' + + ADDR_GWAY=`ipfs config Addresses.Gateway` + if test "$ADDR_GWAY" != ""; then + test_expect_success "'ipfs daemon' output includes Gateway address" ' + cat actual_daemon | grep "Gateway server listening on $ADDR_GWAY" || + fsh cat actual_daemon || + fsh "cat actual_daemon | grep \"Gateway server listening on $ADDR_GWAY\"" + ' + fi } test_mount_ipfs() { diff --git a/test/sharness/t0020-init.sh b/test/sharness/t0020-init.sh index a74fb6236..8344f6a45 100755 --- a/test/sharness/t0020-init.sh +++ b/test/sharness/t0020-init.sh @@ -10,7 +10,8 @@ test_description="Test init command" test_expect_success "ipfs init succeeds" ' export IPFS_PATH="$(pwd)/.go-ipfs" && - ipfs init >actual_init + BITS="2048" && + ipfs init --bits="$BITS" >actual_init ' test_expect_success ".go-ipfs/ has been created" ' @@ -34,10 +35,9 @@ test_expect_success "ipfs peer id looks good" ' ' test_expect_success "ipfs init output looks good" ' - STARTHASH="QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT" && - STARTFILE="ipfs cat /ipfs/$STARTHASH/readme" + STARTFILE="ipfs cat /ipfs/$HASH_WELCOME_DOCS/readme" && echo "initializing ipfs node at $IPFS_PATH" >expected && - echo "generating 4096-bit RSA keypair...done" >>expected && + echo "generating $BITS-bit RSA keypair...done" >>expected && echo "peer identity: $PEERID" >>expected && echo "to get started, enter:" >>expected && printf "\\n\\t$STARTFILE\\n\\n" >>expected && diff --git a/test/sharness/t0060-daemon.sh b/test/sharness/t0060-daemon.sh index cec75c6d6..5f6f6ce43 100755 --- a/test/sharness/t0060-daemon.sh +++ b/test/sharness/t0060-daemon.sh @@ -34,11 +34,12 @@ test_expect_success "ipfs peer id looks good" ' # note this is almost the same as t0020-init.sh "ipfs init output looks good" test_expect_success "ipfs daemon output looks good" ' - STARTHASH="QmTTFXiXoixwT53tcGPu419udsHEHYu6AHrQC8HAKdJYaZ" && + STARTFILE="ipfs cat /ipfs/$HASH_WELCOME_DOCS/readme" && echo "initializing ipfs node at $IPFS_PATH" >expected && - echo "generating key pair...done" >>expected && + echo "generating 4096-bit RSA keypair...done" >>expected && echo "peer identity: $PEERID" >>expected && - echo "\nto get started, enter: ipfs cat $STARTHASH" >>expected && + echo "to get started, enter:" >>expected && + printf "\\n\\t$STARTFILE\\n\\n" >>expected && echo "daemon listening on /ip4/127.0.0.1/tcp/5001" >>expected && test_cmp_repeat_10_sec expected actual_init ' diff --git a/test/sharness/t0080-repo.sh b/test/sharness/t0080-repo.sh index 852d76a24..fe816714c 100755 --- a/test/sharness/t0080-repo.sh +++ b/test/sharness/t0080-repo.sh @@ -43,8 +43,8 @@ test_expect_success "'ipfs pin rm' succeeds" ' test_expect_success "file no longer pinned" ' # we expect the welcome files to show up here - echo QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT >expected2 && - ipfs refs -r QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT >>expected2 && + echo "$HASH_WELCOME_DOCS" >expected2 && + ipfs refs -r "$HASH_WELCOME_DOCS" >>expected2 && cat expected2 | sort >expected_sorted2 && ipfs pin ls -type=recursive | sort >actual2 && test_cmp expected_sorted2 actual2 @@ -87,8 +87,8 @@ test_expect_success "'ipfs repo gc' removes file" ' test_expect_success "'ipfs refs local' no longer shows file" ' echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >expected8 && - echo QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT >>expected8 && - ipfs refs -r QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT >>expected8 && + echo "$HASH_WELCOME_DOCS" >>expected8 && + ipfs refs -r "$HASH_WELCOME_DOCS" >>expected8 && cat expected8 | sort >expected_sorted8 && ipfs refs local | sort >actual8 && test_cmp expected_sorted8 actual8 @@ -101,7 +101,7 @@ test_expect_success "adding multiblock random file succeeds" ' test_expect_success "'ipfs pin ls -type=indirect' is correct" ' ipfs refs "$MBLOCKHASH" >refsout && - ipfs refs -r "QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT" >>refsout && + ipfs refs -r "$HASH_WELCOME_DOCS" >>refsout && cat refsout | sort >refsout_sorted && ipfs pin ls -type=indirect | sort >indirectpins && test_cmp refsout_sorted indirectpins @@ -129,8 +129,8 @@ test_expect_success "'ipfs pin ls -type=direct' is correct" ' test_expect_success "'ipfs pin ls -type=recursive' is correct" ' echo "$MBLOCKHASH" >rp_expected && - echo QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT >>rp_expected && - ipfs refs -r "QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT" >>rp_expected && + echo "$HASH_WELCOME_DOCS" >>rp_expected && + ipfs refs -r "$HASH_WELCOME_DOCS" >>rp_expected && cat rp_expected | sort >rp_exp_sorted && ipfs pin ls -type=recursive | sort >rp_actual && test_cmp rp_exp_sorted rp_actual diff --git a/test/sharness/t0100-http-gateway.sh b/test/sharness/t0100-http-gateway.sh deleted file mode 100755 index b4d3810cf..000000000 --- a/test/sharness/t0100-http-gateway.sh +++ /dev/null @@ -1,106 +0,0 @@ -#!/bin/sh -# -# Copyright (c) 2014 Christian Couder -# MIT Licensed; see the LICENSE file in this repository. -# - -test_description="Test HTTP Gateway" - -exec 3>&1 4>&2 -. lib/test-lib.sh - -test_expect_success "Configure http gateway" ' - export IPFS_PATH="$PWD/.go-ipfs"; - if ! [ -e ../ipfs-path ]; then - IPFS_PATH="$PWD/../ipfs-path" ipfs init; - fi; - cp -R ../ipfs-path "$IPFS_PATH" && - ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/5002 && - ipfs config -bool Gateway.Writable true -' - -test_expect_success "ipfs daemon --init launches and listen to TCP port 5002" ' - export IPFS_PATH="$PWD/.go-ipfs" && - ipfs daemon 2>&1 >actual_init & - IPFS_PID=$(ps | grep ipfs | awk "{print \$1}"); - test_wait_open_tcp_port_10_sec 5002 -' - -test_expect_success "HTTP gateway gives access to sample file" ' - curl -s -o welcome http://localhost:5002/ipfs/QmTTFXiXoixwT53tcGPu419udsHEHYu6AHrQC8HAKdJYaZ && - grep "Hello and Welcome to IPFS!" welcome -' - -test_expect_success "HTTP POST file gives Hash" ' - echo "$RANDOM" >infile - curl -svX POST --data-binary @infile http://localhost:5002/ipfs/ 2>curl.out && - grep "HTTP/1.1 201 Created" curl.out -' - -test_expect_success "We can HTTP GET file just created" ' - path=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r") - curl -so outfile http://localhost:5002$path && - diff -u infile outfile -' - -test_expect_success "HTTP PUT empty directory" ' - echo "PUT http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/" && - curl -svX PUT http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/ 2>curl.out && - cat curl.out && - grep "Ipfs-Hash: QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" curl.out && - grep "Location: /ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/" curl.out && - grep "HTTP/1.1 201 Created" curl.out -' - -test_expect_success "HTTP GET empty directory" ' - echo "GET http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/" && - curl -so outfile http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/ 2>curl.out && - grep "Index of /ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/" outfile -' - -test_expect_success "HTTP PUT file to construct a hierarchy" ' - echo "$RANDOM" >infile - echo "PUT http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/test.txt" && - curl -svX PUT --data-binary @infile http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/test.txt 2>curl.out && - grep "HTTP/1.1 201 Created" curl.out && - grep Location curl.out -' - -test_expect_success "We can HTTP GET file just created" ' - path=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r"); - echo "$path" = "${path%/test.txt}/test.txt"; - [ "$path" = "${path%/test.txt}/test.txt" ] && - echo "GET http://localhost:5002$path" && - curl -so outfile http://localhost:5002$path && - diff -u infile outfile -' - -test_expect_success "HTTP PUT file to append to existing hierarchy" ' - echo "$RANDOM" >infile2; - echo "PUT http://localhost:5002${path%/test.txt}/test/test.txt" && - curl -svX PUT --data-binary @infile2 http://localhost:5002${path%/test.txt}/test/test.txt 2>curl.out && - grep "HTTP/1.1 201 Created" curl.out && - grep Location curl.out -' - -test_expect_success "We can HTTP GET file just created" ' - path=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r"); - [ "$path" = "${path%/test/test.txt}/test/test.txt" ] && - echo "GET http://localhost:5002$path" && - curl -so outfile2 http://localhost:5002$path && - diff -u infile2 outfile2 && - echo "GET http://localhost:5002${path%/test/test.txt}/test.txt" && - curl -so outfile http://localhost:5002${path%/test/test.txt}/test.txt && - diff -u infile outfile -' - -test_expect_success "daemon is still running" ' - echo IPFS_PID=$IPFS_PID; - kill -15 $IPFS_PID -' - -test_expect_success "'ipfs daemon' can be killed" ' - test_kill_repeat_10_sec $IPFS_PID -' - -test_done diff --git a/test/sharness/t0111-gateway-writable.sh b/test/sharness/t0111-gateway-writable.sh new file mode 100755 index 000000000..3e9559bd8 --- /dev/null +++ b/test/sharness/t0111-gateway-writable.sh @@ -0,0 +1,93 @@ +#!/bin/sh +# +# Copyright (c) 2014 Christian Couder +# MIT Licensed; see the LICENSE file in this repository. +# + +test_description="Test HTTP Gateway" + +exec 3>&1 4>&2 +. lib/test-lib.sh + +test_init_ipfs +test_config_ipfs_gateway_writable +test_launch_ipfs_daemon + +test_expect_success "ipfs daemon listening to TCP port 5002" ' + test_wait_open_tcp_port_10_sec 5002 +' + +test_expect_success "HTTP gateway gives access to sample file" ' + curl -s -o welcome "http://localhost:5002/ipfs/$HASH_WELCOME_DOCS/readme" && + grep "Hello and Welcome to IPFS!" welcome +' + +test_expect_success "HTTP POST file gives Hash" ' + echo "$RANDOM" >infile && + curl -svX POST --data-binary @infile http://localhost:5002/ipfs/ 2>curl.out && + grep "HTTP/1.1 201 Created" curl.out +' + +test_expect_success "We can HTTP GET file just created" ' + FILEPATH=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r") + curl -so outfile http://localhost:5002$FILEPATH && + diff -u infile outfile +' + +test_expect_success "HTTP PUT empty directory" ' + echo "PUT http://localhost:5002/ipfs/$HASH_EMPTY_DIR/" && + curl -svX PUT "http://localhost:5002/ipfs/$HASH_EMPTY_DIR/" 2>curl.out && + cat curl.out && + grep "Ipfs-Hash: $HASH_EMPTY_DIR" curl.out && + grep "Location: /ipfs/$HASH_EMPTY_DIR/" curl.out && + grep "HTTP/1.1 201 Created" curl.out +' + +test_expect_success "HTTP GET empty directory" ' + echo "GET http://localhost:5002/ipfs/$HASH_EMPTY_DIR/" && + curl -so outfile "http://localhost:5002/ipfs/$HASH_EMPTY_DIR/" 2>curl.out && + grep "Index of /ipfs/$HASH_EMPTY_DIR/" outfile +' + +test_expect_success "HTTP PUT file to construct a hierarchy" ' + echo "$RANDOM" >infile + echo "PUT http://localhost:5002/ipfs/$HASH_EMPTY_DIR/test.txt" && + curl -svX PUT --data-binary @infile "http://localhost:5002/ipfs/$HASH_EMPTY_DIR/test.txt" 2>curl.out && + grep "HTTP/1.1 201 Created" curl.out && + grep Location curl.out +' + +test_expect_success "We can HTTP GET file just created" ' + FILEPATH=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r") && + echo "$FILEPATH" = "${FILEPATH%/test.txt}/test.txt" && + [ "$FILEPATH" = "${FILEPATH%/test.txt}/test.txt" ] && + echo "GET http://localhost:5002$FILEPATH" && + curl -so outfile http://localhost:5002$FILEPATH && + diff -u infile outfile +' + +test_expect_success "HTTP PUT file to append to existing hierarchy" ' + echo "$RANDOM" >infile2 && + echo "PUT http://localhost:5002${FILEPATH%/test.txt}/test/test.txt" && + curl -svX PUT --data-binary @infile2 "http://localhost:5002${FILEPATH%/test.txt}/test/test.txt 2>curl.out" && + grep "HTTP/1.1 201 Created" curl.out && + grep Location curl.out +' + +# TODO: this seems not to be working. +# $FILEPATH is set to: /ipfs/QmcpPkdv1K5Rk1bT9Y4rx4FamT7ujry2C61HMzZEAuAnms/test.txt +# $FILEPATH should be set to /ipfs//test/test.txt +test_expect_failure "We can HTTP GET file just created" ' + FILEPATH=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r"); + [ "$FILEPATH" = "${FILEPATH%/test/test.txt}/test/test.txt" ] && + echo "GET http://localhost:5002$FILEPATH" && + curl -so outfile2 "http://localhost:5002$FILEPATH" && + diff -u infile2 outfile2 && + echo "GET http://localhost:5002${FILEPATH%/test/test.txt}/test.txt" && + curl -so outfile "http://localhost:5002${FILEPATH%/test/test.txt}/test.txt" && + diff -u infile outfile +' + +test_kill_ipfs_daemon + +test_done