Skip to content

Commit

Permalink
Fix/enhance IPFS tests
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <[email protected]>
  • Loading branch information
apostasie committed Jul 5, 2024
1 parent bc24aa9 commit 3e44585
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
4 changes: 3 additions & 1 deletion cmd/nerdctl/container_create_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ func TestCreateWithMACAddress(t *testing.T) {
network := test.Network
wantErr := test.WantErr
t.Run(testName, func(tt *testing.T) {
tt.Parallel()
// FIXME IMPORTANT: it is painfully obvious that parallelization here is breaking this test very often
// Something seems to be racy wrt network creation, or container creation with custom network
// tt.Parallel()

macAddress, err := nettestutil.GenerateMACAddress()
if err != nil {
Expand Down
43 changes: 36 additions & 7 deletions cmd/nerdctl/ipfs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import (
"fmt"
"regexp"
"testing"
"time"

"github.com/containerd/nerdctl/v2/pkg/infoutil"
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/portlock"

"gotest.tools/v3/assert"
)
Expand Down Expand Up @@ -71,25 +74,51 @@ func TestIPFSAddress(t *testing.T) {
}

func runIPFSDaemonContainer(t *testing.T, base *testutil.Base) (ipfsAddress string, done func()) {
name := "test-ipfs-address"
port, err := portlock.Acquire(0)
assert.NilError(base.T, err, fmt.Errorf("failed acquiring port: %w", err))

name := "test-ipfs-address-" + testutil.Identifier(t)
var ipfsaddr string
var addrTest string
if detachedNetNS, _ := rootlessutil.DetachedNetNS(); detachedNetNS != "" {
// detached-netns mode can't use .NetworkSettings.IPAddress, because the daemon and CNI has different network namespaces
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()
ipfsaddr = "/ip4/127.0.0.1/tcp/5999"
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()
ipfsaddr = fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)
addrTest = fmt.Sprintf("127.0.0.1:%d", port)
} else {
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()
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()
iplines := base.Cmd("inspect", name, "-f", "'{{json .NetworkSettings.IPAddress}}'").OutLines()
t.Logf("IPAddress=%v", iplines)
assert.Equal(t, len(iplines), 2)
matches := iplineRegexp.FindStringSubmatch(iplines[0])
t.Logf("ip address matches=%v", matches)
assert.Equal(t, len(matches), 2)
ipfsaddr = fmt.Sprintf("/ip4/%s/tcp/5001", matches[1])
ipfsaddr = fmt.Sprintf("/ip4/%s/tcp/%d", matches[1], port)
addrTest = matches[1] + fmt.Sprintf(":%d", port)
}

_, err = nettestutil.HTTPGet(fmt.Sprintf("http://%s/api/v0", addrTest), 30, true)
// Not there... give it more time...
if err != nil {
time.Sleep(1 * time.Second)
_, err = nettestutil.HTTPGet(fmt.Sprintf("http://%s/api/v0", addrTest), 30, true)
}
if err != nil {
fmt.Println("Something is wrong with ipfs communication")
res := base.Cmd("inspect", name).Run()
fmt.Println(res)
res = base.Cmd("logs", name).Run()
fmt.Println(res)
}
assert.NilError(t, err)

return ipfsaddr, func() {
base.Cmd("kill", "test-ipfs-address").AssertOK()
base.Cmd("rm", "test-ipfs-address").AssertOK()
base.Cmd("kill", name).AssertOK()
base.Cmd("rm", name).AssertOK()
err = portlock.Release(port)
if err != nil {
t.Errorf("failed to release ipfs port %d: %v", port, err)
}
}
}

Expand Down

0 comments on commit 3e44585

Please sign in to comment.