@cryptotaxi247 / kubo / commits / f1d34a2a8

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

Juan Batiz-Benet committed Jan 31, 2015 at 19:15 UTC f1d34a2a8514c2273c87b062fe9d3ccaaaadaccc
9 files changed +170 -129
cmd/ipfs/daemon.go
+2
@@ -183,6 +183,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
183 if rootRedirect != nil {
184 opts = append(opts, rootRedirect)
185 }
186 + fmt.Printf("Gateway server listening on %s\n", gatewayMaddr)
187 err := corehttp.ListenAndServe(node, gatewayMaddr.String(), opts...)
188 if err != nil {
189 log.Error(err)
@@ -198,6 +199,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
199 if rootRedirect != nil {
200 opts = append(opts, rootRedirect)
201 }
202 + fmt.Printf("API server listening on %s\n", apiMaddr)
203 if err := corehttp.ListenAndServe(node, apiMaddr.String(), opts...); err != nil {
204 res.SetError(err, cmds.ErrNormal)
205 return
core/corehttp/corehttp.go
+2 -4
@@ -1,7 +1,6 @@
1 package corehttp
2
3 import (
4 - "fmt"
4 "net/http"
5
6 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
35 return err
36 }
37 }
39 - return listenAndServe("API", n, addr, mux)
38 + return listenAndServe(n, addr, mux)
39 }
40
42 -func listenAndServe(name string, node *core.IpfsNode, addr ma.Multiaddr, mux *http.ServeMux) error {
41 +func listenAndServe(node *core.IpfsNode, addr ma.Multiaddr, mux *http.ServeMux) error {
42 _, host, err := manet.DialArgs(addr)
43 if err != nil {
44 return err
@@ -52,7 +51,6 @@ func listenAndServe(name string, node *core.IpfsNode, addr ma.Multiaddr, mux *ht
51 serverExited := make(chan struct{})
52
53 go func() {
55 - fmt.Printf("%s server listening on %s\n", name, addr)
54 serverError = server.ListenAndServe(host, mux)
55 close(serverExited)
56 }()
test/sharness/lib/test-lib-hashes.sh new
+5
@@ -0,0 +1,5 @@
1 +# this file defines several useful hashes used across the test codebase.
2 +# thus they can be defined + changed in one place
3 +
4 +HASH_WELCOME_DOCS="QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT"
5 +HASH_EMPTY_DIR="QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
test/sharness/lib/test-lib.sh
+53 -5
@@ -23,6 +23,11 @@ if test `which ipfs` != $(pwd)/bin/ipfs; then
23 exit 1
24 fi
25
26 +
27 +# source the common hashes first.
28 +. lib/test-lib-hashes.sh
29 +
30 +
31 SHARNESS_LIB="lib/sharness/sharness.sh"
32
33 . "$SHARNESS_LIB" || {
@@ -52,6 +57,15 @@ test_cmp_repeat_10_sec() {
57 test_cmp "$1" "$2"
58 }
59
60 +test_run_repeat_10_sec() {
61 + for i in 1 2 3 4 5 6 7 8 9 10
62 + do
63 + (test_eval_ "$1") && return
64 + sleep 1
65 + done
66 + return 1 # failed
67 +}
68 +
69 test_wait_output_n_lines_60_sec() {
70 echo "$2" >expected_waitn
71 for i in 1 2 3 4 5 6 7 8 9 10
@@ -66,7 +80,11 @@ test_wait_output_n_lines_60_sec() {
80
81 test_wait_open_tcp_port_10_sec() {
82 for i in 1 2 3 4 5 6 7 8 9 10; do
69 - if [ $(ss -lt "sport == :$1" | wc -l) -gt 1 ]; then
83 + # this is not a perfect check, but it's portable.
84 + # cant count on ss. not installed everywhere.
85 + # cant count on netstat using : or . as port delim. differ across platforms.
86 + echo $(netstat -aln | egrep "^tcp.*LISTEN" | egrep "[.:]$1" | wc -l) -gt 0
87 + if [ $(netstat -aln | egrep "^tcp.*LISTEN" | egrep "[.:]$1" | wc -l) -gt 0 ]; then
88 return 0
89 fi
90 sleep 1
@@ -90,19 +108,49 @@ test_init_ipfs() {
108
109 }
110
111 +test_config_ipfs_gateway_readonly() {
112 + test_expect_success "prepare config -- gateway readonly" '
113 + ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/5002
114 + '
115 +}
116 +
117 +test_config_ipfs_gateway_writable() {
118 + test_expect_success "prepare config -- gateway writable" '
119 + ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/5002 &&
120 + ipfs config -bool Gateway.Writable true
121 + '
122 +}
123 +
124 test_launch_ipfs_daemon() {
125
126 test_expect_success "'ipfs daemon' succeeds" '
127 ipfs daemon >actual_daemon 2>daemon_err &
128 '
129
99 - test_expect_success "'ipfs daemon' output looks good" '
130 + # we say the daemon is ready when the API server is ready.
131 + # and we make sure there are no errors
132 + test_expect_success "'ipfs daemon' is ready" '
133 IPFS_PID=$! &&
101 - echo "API server listening on /ip4/127.0.0.1/tcp/5001" >expected_daemon &&
102 - test_cmp_repeat_10_sec expected_daemon actual_daemon ||
103 - fsh cat daemon_err
134 + test_run_repeat_10_sec "cat actual_daemon | grep \"API server listening on\"" &&
135 + printf "" >empty && test_cmp daemon_err empty ||
136 + fsh cat actual_daemon || fsh cat daemon_err
137 + '
138 +
139 + ADDR_API="/ip4/127.0.0.1/tcp/5001"
140 + test_expect_success "'ipfs daemon' output includes API address" '
141 + cat actual_daemon | grep "API server listening on $ADDR_API" ||
142 + fsh cat actual_daemon ||
143 + fsh "cat actual_daemon | grep \"API server listening on $ADDR_API\""
144 '
145
146 + ADDR_GWAY=`ipfs config Addresses.Gateway`
147 + if test "$ADDR_GWAY" != ""; then
148 + test_expect_success "'ipfs daemon' output includes Gateway address" '
149 + cat actual_daemon | grep "Gateway server listening on $ADDR_GWAY" ||
150 + fsh cat actual_daemon ||
151 + fsh "cat actual_daemon | grep \"Gateway server listening on $ADDR_GWAY\""
152 + '
153 + fi
154 }
155
156 test_mount_ipfs() {
test/sharness/t0020-init.sh
+4 -4
@@ -10,7 +10,8 @@ test_description="Test init command"
10
11 test_expect_success "ipfs init succeeds" '
12 export IPFS_PATH="$(pwd)/.go-ipfs" &&
13 - ipfs init >actual_init
13 + BITS="2048" &&
14 + ipfs init --bits="$BITS" >actual_init
15 '
16
17 test_expect_success ".go-ipfs/ has been created" '
@@ -34,10 +35,9 @@ test_expect_success "ipfs peer id looks good" '
35 '
36
37 test_expect_success "ipfs init output looks good" '
37 - STARTHASH="QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT" &&
38 - STARTFILE="ipfs cat /ipfs/$STARTHASH/readme"
38 + STARTFILE="ipfs cat /ipfs/$HASH_WELCOME_DOCS/readme" &&
39 echo "initializing ipfs node at $IPFS_PATH" >expected &&
40 - echo "generating 4096-bit RSA keypair...done" >>expected &&
40 + echo "generating $BITS-bit RSA keypair...done" >>expected &&
41 echo "peer identity: $PEERID" >>expected &&
42 echo "to get started, enter:" >>expected &&
43 printf "\\n\\t$STARTFILE\\n\\n" >>expected &&
test/sharness/t0060-daemon.sh
+4 -3
@@ -34,11 +34,12 @@ test_expect_success "ipfs peer id looks good" '
34
35 # note this is almost the same as t0020-init.sh "ipfs init output looks good"
36 test_expect_success "ipfs daemon output looks good" '
37 - STARTHASH="QmTTFXiXoixwT53tcGPu419udsHEHYu6AHrQC8HAKdJYaZ" &&
37 + STARTFILE="ipfs cat /ipfs/$HASH_WELCOME_DOCS/readme" &&
38 echo "initializing ipfs node at $IPFS_PATH" >expected &&
39 - echo "generating key pair...done" >>expected &&
39 + echo "generating 4096-bit RSA keypair...done" >>expected &&
40 echo "peer identity: $PEERID" >>expected &&
41 - echo "\nto get started, enter: ipfs cat $STARTHASH" >>expected &&
41 + echo "to get started, enter:" >>expected &&
42 + printf "\\n\\t$STARTFILE\\n\\n" >>expected &&
43 echo "daemon listening on /ip4/127.0.0.1/tcp/5001" >>expected &&
44 test_cmp_repeat_10_sec expected actual_init
45 '
test/sharness/t0080-repo.sh
+7 -7
@@ -43,8 +43,8 @@ test_expect_success "'ipfs pin rm' succeeds" '
43
44 test_expect_success "file no longer pinned" '
45 # we expect the welcome files to show up here
46 - echo QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT >expected2 &&
47 - ipfs refs -r QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT >>expected2 &&
46 + echo "$HASH_WELCOME_DOCS" >expected2 &&
47 + ipfs refs -r "$HASH_WELCOME_DOCS" >>expected2 &&
48 cat expected2 | sort >expected_sorted2 &&
49 ipfs pin ls -type=recursive | sort >actual2 &&
50 test_cmp expected_sorted2 actual2
@@ -87,8 +87,8 @@ test_expect_success "'ipfs repo gc' removes file" '
87
88 test_expect_success "'ipfs refs local' no longer shows file" '
89 echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >expected8 &&
90 - echo QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT >>expected8 &&
91 - ipfs refs -r QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT >>expected8 &&
90 + echo "$HASH_WELCOME_DOCS" >>expected8 &&
91 + ipfs refs -r "$HASH_WELCOME_DOCS" >>expected8 &&
92 cat expected8 | sort >expected_sorted8 &&
93 ipfs refs local | sort >actual8 &&
94 test_cmp expected_sorted8 actual8
@@ -101,7 +101,7 @@ test_expect_success "adding multiblock random file succeeds" '
101
102 test_expect_success "'ipfs pin ls -type=indirect' is correct" '
103 ipfs refs "$MBLOCKHASH" >refsout &&
104 - ipfs refs -r "QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT" >>refsout &&
104 + ipfs refs -r "$HASH_WELCOME_DOCS" >>refsout &&
105 cat refsout | sort >refsout_sorted &&
106 ipfs pin ls -type=indirect | sort >indirectpins &&
107 test_cmp refsout_sorted indirectpins
@@ -129,8 +129,8 @@ test_expect_success "'ipfs pin ls -type=direct' is correct" '
129
130 test_expect_success "'ipfs pin ls -type=recursive' is correct" '
131 echo "$MBLOCKHASH" >rp_expected &&
132 - echo QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT >>rp_expected &&
133 - ipfs refs -r "QmPXME1oRtoT627YKaDPDQ3PwA8tdP9rWuAAweLzqSwAWT" >>rp_expected &&
132 + echo "$HASH_WELCOME_DOCS" >>rp_expected &&
133 + ipfs refs -r "$HASH_WELCOME_DOCS" >>rp_expected &&
134 cat rp_expected | sort >rp_exp_sorted &&
135 ipfs pin ls -type=recursive | sort >rp_actual &&
136 test_cmp rp_exp_sorted rp_actual
test/sharness/t0100-http-gateway.sh deleted
-106
@@ -1,106 +0,0 @@
1 -#!/bin/sh
2 -#
3 -# Copyright (c) 2014 Christian Couder
4 -# MIT Licensed; see the LICENSE file in this repository.
5 -#
6 -
7 -test_description="Test HTTP Gateway"
8 -
9 -exec 3>&1 4>&2
10 -. lib/test-lib.sh
11 -
12 -test_expect_success "Configure http gateway" '
13 - export IPFS_PATH="$PWD/.go-ipfs";
14 - if ! [ -e ../ipfs-path ]; then
15 - IPFS_PATH="$PWD/../ipfs-path" ipfs init;
16 - fi;
17 - cp -R ../ipfs-path "$IPFS_PATH" &&
18 - ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/5002 &&
19 - ipfs config -bool Gateway.Writable true
20 -'
21 -
22 -test_expect_success "ipfs daemon --init launches and listen to TCP port 5002" '
23 - export IPFS_PATH="$PWD/.go-ipfs" &&
24 - ipfs daemon 2>&1 >actual_init &
25 - IPFS_PID=$(ps | grep ipfs | awk "{print \$1}");
26 - test_wait_open_tcp_port_10_sec 5002
27 -'
28 -
29 -test_expect_success "HTTP gateway gives access to sample file" '
30 - curl -s -o welcome http://localhost:5002/ipfs/QmTTFXiXoixwT53tcGPu419udsHEHYu6AHrQC8HAKdJYaZ &&
31 - grep "Hello and Welcome to IPFS!" welcome
32 -'
33 -
34 -test_expect_success "HTTP POST file gives Hash" '
35 - echo "$RANDOM" >infile
36 - curl -svX POST --data-binary @infile http://localhost:5002/ipfs/ 2>curl.out &&
37 - grep "HTTP/1.1 201 Created" curl.out
38 -'
39 -
40 -test_expect_success "We can HTTP GET file just created" '
41 - path=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r")
42 - curl -so outfile http://localhost:5002$path &&
43 - diff -u infile outfile
44 -'
45 -
46 -test_expect_success "HTTP PUT empty directory" '
47 - echo "PUT http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/" &&
48 - curl -svX PUT http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/ 2>curl.out &&
49 - cat curl.out &&
50 - grep "Ipfs-Hash: QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" curl.out &&
51 - grep "Location: /ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/" curl.out &&
52 - grep "HTTP/1.1 201 Created" curl.out
53 -'
54 -
55 -test_expect_success "HTTP GET empty directory" '
56 - echo "GET http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/" &&
57 - curl -so outfile http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/ 2>curl.out &&
58 - grep "Index of /ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/" outfile
59 -'
60 -
61 -test_expect_success "HTTP PUT file to construct a hierarchy" '
62 - echo "$RANDOM" >infile
63 - echo "PUT http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/test.txt" &&
64 - curl -svX PUT --data-binary @infile http://localhost:5002/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/test.txt 2>curl.out &&
65 - grep "HTTP/1.1 201 Created" curl.out &&
66 - grep Location curl.out
67 -'
68 -
69 -test_expect_success "We can HTTP GET file just created" '
70 - path=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r");
71 - echo "$path" = "${path%/test.txt}/test.txt";
72 - [ "$path" = "${path%/test.txt}/test.txt" ] &&
73 - echo "GET http://localhost:5002$path" &&
74 - curl -so outfile http://localhost:5002$path &&
75 - diff -u infile outfile
76 -'
77 -
78 -test_expect_success "HTTP PUT file to append to existing hierarchy" '
79 - echo "$RANDOM" >infile2;
80 - echo "PUT http://localhost:5002${path%/test.txt}/test/test.txt" &&
81 - curl -svX PUT --data-binary @infile2 http://localhost:5002${path%/test.txt}/test/test.txt 2>curl.out &&
82 - grep "HTTP/1.1 201 Created" curl.out &&
83 - grep Location curl.out
84 -'
85 -
86 -test_expect_success "We can HTTP GET file just created" '
87 - path=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r");
88 - [ "$path" = "${path%/test/test.txt}/test/test.txt" ] &&
89 - echo "GET http://localhost:5002$path" &&
90 - curl -so outfile2 http://localhost:5002$path &&
91 - diff -u infile2 outfile2 &&
92 - echo "GET http://localhost:5002${path%/test/test.txt}/test.txt" &&
93 - curl -so outfile http://localhost:5002${path%/test/test.txt}/test.txt &&
94 - diff -u infile outfile
95 -'
96 -
97 -test_expect_success "daemon is still running" '
98 - echo IPFS_PID=$IPFS_PID;
99 - kill -15 $IPFS_PID
100 -'
101 -
102 -test_expect_success "'ipfs daemon' can be killed" '
103 - test_kill_repeat_10_sec $IPFS_PID
104 -'
105 -
106 -test_done
test/sharness/t0111-gateway-writable.sh new
+93
@@ -0,0 +1,93 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2014 Christian Couder
4 +# MIT Licensed; see the LICENSE file in this repository.
5 +#
6 +
7 +test_description="Test HTTP Gateway"
8 +
9 +exec 3>&1 4>&2
10 +. lib/test-lib.sh
11 +
12 +test_init_ipfs
13 +test_config_ipfs_gateway_writable
14 +test_launch_ipfs_daemon
15 +
16 +test_expect_success "ipfs daemon listening to TCP port 5002" '
17 + test_wait_open_tcp_port_10_sec 5002
18 +'
19 +
20 +test_expect_success "HTTP gateway gives access to sample file" '
21 + curl -s -o welcome "http://localhost:5002/ipfs/$HASH_WELCOME_DOCS/readme" &&
22 + grep "Hello and Welcome to IPFS!" welcome
23 +'
24 +
25 +test_expect_success "HTTP POST file gives Hash" '
26 + echo "$RANDOM" >infile &&
27 + curl -svX POST --data-binary @infile http://localhost:5002/ipfs/ 2>curl.out &&
28 + grep "HTTP/1.1 201 Created" curl.out
29 +'
30 +
31 +test_expect_success "We can HTTP GET file just created" '
32 + FILEPATH=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r")
33 + curl -so outfile http://localhost:5002$FILEPATH &&
34 + diff -u infile outfile
35 +'
36 +
37 +test_expect_success "HTTP PUT empty directory" '
38 + echo "PUT http://localhost:5002/ipfs/$HASH_EMPTY_DIR/" &&
39 + curl -svX PUT "http://localhost:5002/ipfs/$HASH_EMPTY_DIR/" 2>curl.out &&
40 + cat curl.out &&
41 + grep "Ipfs-Hash: $HASH_EMPTY_DIR" curl.out &&
42 + grep "Location: /ipfs/$HASH_EMPTY_DIR/" curl.out &&
43 + grep "HTTP/1.1 201 Created" curl.out
44 +'
45 +
46 +test_expect_success "HTTP GET empty directory" '
47 + echo "GET http://localhost:5002/ipfs/$HASH_EMPTY_DIR/" &&
48 + curl -so outfile "http://localhost:5002/ipfs/$HASH_EMPTY_DIR/" 2>curl.out &&
49 + grep "Index of /ipfs/$HASH_EMPTY_DIR/" outfile
50 +'
51 +
52 +test_expect_success "HTTP PUT file to construct a hierarchy" '
53 + echo "$RANDOM" >infile
54 + echo "PUT http://localhost:5002/ipfs/$HASH_EMPTY_DIR/test.txt" &&
55 + curl -svX PUT --data-binary @infile "http://localhost:5002/ipfs/$HASH_EMPTY_DIR/test.txt" 2>curl.out &&
56 + grep "HTTP/1.1 201 Created" curl.out &&
57 + grep Location curl.out
58 +'
59 +
60 +test_expect_success "We can HTTP GET file just created" '
61 + FILEPATH=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r") &&
62 + echo "$FILEPATH" = "${FILEPATH%/test.txt}/test.txt" &&
63 + [ "$FILEPATH" = "${FILEPATH%/test.txt}/test.txt" ] &&
64 + echo "GET http://localhost:5002$FILEPATH" &&
65 + curl -so outfile http://localhost:5002$FILEPATH &&
66 + diff -u infile outfile
67 +'
68 +
69 +test_expect_success "HTTP PUT file to append to existing hierarchy" '
70 + echo "$RANDOM" >infile2 &&
71 + echo "PUT http://localhost:5002${FILEPATH%/test.txt}/test/test.txt" &&
72 + curl -svX PUT --data-binary @infile2 "http://localhost:5002${FILEPATH%/test.txt}/test/test.txt 2>curl.out" &&
73 + grep "HTTP/1.1 201 Created" curl.out &&
74 + grep Location curl.out
75 +'
76 +
77 +# TODO: this seems not to be working.
78 +# $FILEPATH is set to: /ipfs/QmcpPkdv1K5Rk1bT9Y4rx4FamT7ujry2C61HMzZEAuAnms/test.txt
79 +# $FILEPATH should be set to /ipfs/<some hash>/test/test.txt
80 +test_expect_failure "We can HTTP GET file just created" '
81 + FILEPATH=$(grep Location curl.out | cut -d" " -f3- | tr -d "\r");
82 + [ "$FILEPATH" = "${FILEPATH%/test/test.txt}/test/test.txt" ] &&
83 + echo "GET http://localhost:5002$FILEPATH" &&
84 + curl -so outfile2 "http://localhost:5002$FILEPATH" &&
85 + diff -u infile2 outfile2 &&
86 + echo "GET http://localhost:5002${FILEPATH%/test/test.txt}/test.txt" &&
87 + curl -so outfile "http://localhost:5002${FILEPATH%/test/test.txt}/test.txt" &&
88 + diff -u infile outfile
89 +'
90 +
91 +test_kill_ipfs_daemon
92 +
93 +test_done