Skip to content

Commit 73ea2cb

Browse files
committed
Fix/enhance IPFS tests
Signed-off-by: apostasie <[email protected]>
1 parent da0a680 commit 73ea2cb

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

cmd/nerdctl/ipfs_linux_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
2727
"github.com/containerd/nerdctl/v2/pkg/testutil"
2828
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
29+
"github.com/containerd/nerdctl/v2/pkg/testutil/portlock"
2930

3031
"gotest.tools/v3/assert"
3132
)
@@ -73,27 +74,30 @@ func TestIPFSAddress(t *testing.T) {
7374
}
7475

7576
func runIPFSDaemonContainer(t *testing.T, base *testutil.Base) (ipfsAddress string, done func()) {
77+
port, err := portlock.Acquire(0)
78+
assert.NilError(base.T, err, fmt.Errorf("failed acquiring port: %w", err))
79+
7680
name := "test-ipfs-address-" + testutil.Identifier(t)
7781
var ipfsaddr string
7882
var addrTest string
7983
if detachedNetNS, _ := rootlessutil.DetachedNetNS(); detachedNetNS != "" {
8084
// detached-netns mode can't use .NetworkSettings.IPAddress, because the daemon and CNI has different network namespaces
81-
base.Cmd("run", "-d", "-p", "127.0.0.1:5999:5999", "--name", name, "--entrypoint=/bin/sh", testutil.KuboImage, "-c", "ipfs init && ipfs config Addresses.API /ip4/0.0.0.0/tcp/5999 && ipfs daemon --offline").AssertOK()
82-
ipfsaddr = "/ip4/127.0.0.1/tcp/5999"
83-
addrTest = "127.0.0.1:5999"
85+
base.Cmd("run", "-d", "-p", fmt.Sprintf("127.0.0.1:%d:%d", port, port), "--name", name, "--entrypoint=/bin/sh", testutil.KuboImage, "-c", fmt.Sprintf("ipfs init && ipfs config Addresses.API /ip4/0.0.0.0/tcp/%d && ipfs daemon --offline", port)).AssertOK()
86+
ipfsaddr = fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)
87+
addrTest = fmt.Sprintf("127.0.0.1:%d", port)
8488
} else {
85-
base.Cmd("run", "-d", "--name", name, "--entrypoint=/bin/sh", testutil.KuboImage, "-c", "ipfs init && ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001 && ipfs daemon --offline").AssertOK()
89+
base.Cmd("run", "-d", "--name", name, "--entrypoint=/bin/sh", testutil.KuboImage, "-c", fmt.Sprintf("ipfs init && ipfs config Addresses.API /ip4/0.0.0.0/tcp/%d && ipfs daemon --offline", port)).AssertOK()
8690
iplines := base.Cmd("inspect", name, "-f", "'{{json .NetworkSettings.IPAddress}}'").OutLines()
8791
t.Logf("IPAddress=%v", iplines)
8892
assert.Equal(t, len(iplines), 2)
8993
matches := iplineRegexp.FindStringSubmatch(iplines[0])
9094
t.Logf("ip address matches=%v", matches)
9195
assert.Equal(t, len(matches), 2)
92-
ipfsaddr = fmt.Sprintf("/ip4/%s/tcp/5001", matches[1])
93-
addrTest = matches[1] + ":5001"
96+
ipfsaddr = fmt.Sprintf("/ip4/%s/tcp/%d", matches[1], port)
97+
addrTest = matches[1] + fmt.Sprintf(":%d", port)
9498
}
9599

96-
_, err := nettestutil.HTTPGet(fmt.Sprintf("http://%s/api/v0", addrTest), 30, true)
100+
_, err = nettestutil.HTTPGet(fmt.Sprintf("http://%s/api/v0", addrTest), 30, true)
97101
// Not there... give it more time...
98102
if err != nil {
99103
time.Sleep(1 * time.Second)
@@ -111,6 +115,10 @@ func runIPFSDaemonContainer(t *testing.T, base *testutil.Base) (ipfsAddress stri
111115
return ipfsaddr, func() {
112116
base.Cmd("kill", name).AssertOK()
113117
base.Cmd("rm", name).AssertOK()
118+
err = portlock.Release(port)
119+
if err != nil {
120+
t.Errorf("failed to release ipfs port %d: %v", port, err)
121+
}
114122
}
115123
}
116124

0 commit comments

Comments
 (0)