Skip to content

Commit 46c3689

Browse files
authored
feat: re-enable docker sharness tests (#8808)
The Docker sharness tests were disabled years ago when go-ipfs moved from Travis to CircleCI. This makes the tweaks necessary to re-enable them. The Docker image has since moved to be based on BusyBox which doesn't have the requisite wget version for the existing tests to work, so this adds some functionality to the pollEndpoint program to support polling HTTP endpoints as well.
1 parent d6de97b commit 46c3689

File tree

6 files changed

+89
-31
lines changed

6 files changed

+89
-31
lines changed

.circleci/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ jobs:
154154
working_directory: ~/ipfs/go-ipfs
155155
environment:
156156
<<: *default_environment
157-
TEST_NO_DOCKER: 1
157+
TEST_NO_DOCKER: 0
158158
TEST_NO_FUSE: 1
159159
TEST_VERBOSE: 1
160160
steps:

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Dockerfile.fast
55
!.git/refs/
66
!.git/packed-refs
77
test/sharness/lib/sharness/
8+
test/sharness/trash*
9+
rb-pinning-service-api/
810

911
# The Docker client might not be running on Linux
1012
# so delete any compiled binaries

test/dependencies/pollEndpoint/main.go

+68-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
package main
33

44
import (
5+
"context"
56
"flag"
7+
"io"
8+
"net"
9+
"net/http"
610
"os"
711
"time"
812

@@ -15,6 +19,8 @@ var (
1519
host = flag.String("host", "/ip4/127.0.0.1/tcp/5001", "the multiaddr host to dial on")
1620
tries = flag.Int("tries", 10, "how many tries to make before failing")
1721
timeout = flag.Duration("tout", time.Second, "how long to wait between attempts")
22+
httpURL = flag.String("http-url", "", "HTTP URL to fetch")
23+
httpOut = flag.Bool("http-out", false, "Print the HTTP response body to stdout")
1824
verbose = flag.Bool("v", false, "verbose logging")
1925
)
2026

@@ -37,18 +43,76 @@ func main() {
3743
start := time.Now()
3844
log.Debugf("starting at %s, tries: %d, timeout: %s, addr: %s", start, *tries, *timeout, addr)
3945

40-
for *tries > 0 {
46+
connTries := *tries
47+
for connTries > 0 {
4148
c, err := manet.Dial(addr)
4249
if err == nil {
4350
log.Debugf("ok - endpoint reachable with %d tries remaining, took %s", *tries, time.Since(start))
4451
c.Close()
45-
os.Exit(0)
52+
break
4653
}
4754
log.Debug("connect failed: ", err)
4855
time.Sleep(*timeout)
49-
*tries--
56+
connTries--
5057
}
5158

52-
log.Error("failed.")
59+
if err != nil {
60+
goto Fail
61+
}
62+
63+
if *httpURL != "" {
64+
dialer := &connDialer{addr: addr}
65+
httpClient := http.Client{Transport: &http.Transport{
66+
DialContext: dialer.DialContext,
67+
}}
68+
reqTries := *tries
69+
for reqTries > 0 {
70+
try := (*tries - reqTries) + 1
71+
log.Debugf("trying HTTP req %d: '%s'", try, *httpURL)
72+
if tryHTTPGet(&httpClient, *httpURL) {
73+
log.Debugf("HTTP req %d to '%s' succeeded", try, *httpURL)
74+
goto Success
75+
}
76+
log.Debugf("HTTP req %d to '%s' failed", try, *httpURL)
77+
time.Sleep(*timeout)
78+
reqTries--
79+
}
80+
goto Fail
81+
}
82+
83+
Success:
84+
os.Exit(0)
85+
86+
Fail:
87+
log.Error("failed")
5388
os.Exit(1)
5489
}
90+
91+
func tryHTTPGet(client *http.Client, url string) bool {
92+
resp, err := client.Get(*httpURL)
93+
if resp != nil && resp.Body != nil {
94+
defer resp.Body.Close()
95+
}
96+
if err != nil {
97+
return false
98+
}
99+
if resp.StatusCode != http.StatusOK {
100+
return false
101+
}
102+
if *httpOut {
103+
_, err := io.Copy(os.Stdout, resp.Body)
104+
if err != nil {
105+
panic(err)
106+
}
107+
}
108+
109+
return true
110+
}
111+
112+
type connDialer struct {
113+
addr ma.Multiaddr
114+
}
115+
116+
func (d connDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
117+
return (&manet.Dialer{}).DialContext(ctx, d.addr)
118+
}

test/ipfs-test-lib.sh

+1-6
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,7 @@ docker_run() {
6262

6363
# This takes a docker ID and a command as arguments
6464
docker_exec() {
65-
if test "$CIRCLE" = 1
66-
then
67-
sudo lxc-attach -n "$(docker inspect --format '{{.Id}}' $1)" -- /bin/bash -c "$2"
68-
else
69-
docker exec -t "$1" /bin/bash -c "$2"
70-
fi
65+
docker exec -t "$1" /bin/sh -c "$2"
7166
}
7267

7368
# This takes a docker ID as argument

test/sharness/t0300-docker-image.sh

+10-13
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,28 @@ TEST_TESTS_DIR=$(dirname "$TEST_SCRIPTS_DIR")
2929
APP_ROOT_DIR=$(dirname "$TEST_TESTS_DIR")
3030

3131
test_expect_success "docker image build succeeds" '
32-
docker_build "$TEST_TESTS_DIR/../Dockerfile" "$APP_ROOT_DIR" >actual ||
32+
docker_build "$TEST_TESTS_DIR/../Dockerfile" "$APP_ROOT_DIR" >build-actual ||
3333
test_fsh echo "TEST_TESTS_DIR: $TEST_TESTS_DIR" ||
3434
test_fsh echo "APP_ROOT_DIR : $APP_ROOT_DIR" ||
35-
test_fsh cat actual
35+
test_fsh cat build-actual
3636
'
3737

3838
test_expect_success "docker image build output looks good" '
39-
SUCCESS_LINE=$(egrep "^Successfully built" actual) &&
39+
SUCCESS_LINE=$(egrep "^Successfully built" build-actual) &&
4040
IMAGE_ID=$(expr "$SUCCESS_LINE" : "^Successfully built \(.*\)") ||
41-
test_fsh cat actual
41+
test_fsh cat build-actual
4242
'
4343

4444
test_expect_success "docker image runs" '
45-
DOC_ID=$(docker_run "$IMAGE_ID")
45+
DOC_ID=$(docker run -d -p 127.0.0.1:5001:5001 -p 127.0.0.1:8080:8080 "$IMAGE_ID")
4646
'
4747

48-
test_expect_success "docker image gateway is up" '
49-
docker_exec "$DOC_ID" "wget --retry-connrefused --waitretry=1 --timeout=30 -t 30 \
50-
-q -O - http://localhost:8080/version >/dev/null"
48+
test_expect_success "docker container gateway is up" '
49+
pollEndpoint -host=/ip4/127.0.0.1/tcp/8080 -http-url http://localhost:8080/api/v0/version -v -tries 30 -tout 1s
5150
'
5251

53-
test_expect_success "docker image API is up" '
54-
docker_exec "$DOC_ID" "wget --retry-connrefused --waitretry=1 --timeout=30 -t 30 \
55-
-q -O - http://localhost:5001/api/v0/version >/dev/null"
52+
test_expect_success "docker container API is up" '
53+
pollEndpoint -host=/ip4/127.0.0.1/tcp/5001 -http-url http://localhost:5001/version -v -tries 30 -tout 1s
5654
'
5755

5856
test_expect_success "simple ipfs add/cat can be run in docker container" '
@@ -63,8 +61,7 @@ test_expect_success "simple ipfs add/cat can be run in docker container" '
6361
'
6462

6563
read testcode <<EOF
66-
docker exec -i "$DOC_ID" wget --retry-connrefused --waitretry=1 --timeout=30 -t 30 \
67-
-q -O - http://localhost:8080/version | grep Commit | cut -d" " -f2 >actual ; \
64+
pollEndpoint -host=/ip4/127.0.0.1/tcp/5001 -http-url http://localhost:5001/version -http-out | grep Commit | cut -d" " -f2 >actual ; \
6865
test -s actual ; \
6966
docker exec -i "$DOC_ID" ipfs version --enc json \
7067
| sed 's/^.*"Commit":"\\\([^"]*\\\)".*$/\\\1/g' >expected ; \

test/sharness/t0301-docker-migrate.sh

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ test_expect_success "docker image build succeeds" '
3232

3333
test_init_ipfs
3434

35+
test_expect_success "configure migration sources" '
36+
ipfs config --json Migration.DownloadSources "[\"http://127.0.0.1:17233\"]"
37+
'
38+
3539
test_expect_success "make repo be version 4" '
3640
echo 4 > "$IPFS_PATH/version"
3741
'
@@ -43,17 +47,13 @@ test_expect_success "setup http response" '
4347
echo "v1.1.1" >> vers_resp
4448
'
4549

46-
pretend_server() {
47-
socat tcp-listen:17233,fork,bind=127.0.0.1,reuseaddr 'SYSTEM:cat vers_resp'!!STDERR &
48-
}
49-
5050
test_expect_success "startup fake dists server" '
51-
pretend_server > dist_serv_out &
51+
( socat tcp-listen:17233,fork,bind=127.0.0.1,reuseaddr "SYSTEM:cat vers_resp"!!STDERR 2> dist_serv_out ) &
5252
echo $! > netcat_pid
5353
'
5454

5555
test_expect_success "docker image runs" '
56-
DOC_ID=$(docker run -d -v "$IPFS_PATH":/data/ipfs --net=host -e IPFS_DIST_PATH="http://localhost:17233" "$IMAGE_ID" --migrate)
56+
DOC_ID=$(docker run -d -v "$IPFS_PATH":/data/ipfs --net=host "$IMAGE_ID")
5757
'
5858

5959
test_expect_success "docker container tries to pull migrations from netcat" '
@@ -74,7 +74,7 @@ test_expect_success "kill the net cat" '
7474
'
7575

7676
test_expect_success "correct version was requested" '
77-
grep "/fs-repo-migrations/v1.1.1/fs-repo-migrations_v1.1.1_linux-amd64.tar.gz" dist_serv_out > /dev/null
77+
grep "/fs-repo-6-to-7/v1.1.1/fs-repo-6-to-7_v1.1.1_linux-amd64.tar.gz" dist_serv_out > /dev/null
7878
'
7979

8080
test_done

0 commit comments

Comments
 (0)