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 2275c4e
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions cmd/nerdctl/ipfs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ 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"

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

func runIPFSDaemonContainer(t *testing.T, base *testutil.Base) (ipfsAddress string, done func()) {
name := "test-ipfs-address"
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"
addrTest = "127.0.0.1:5999"
} 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()
iplines := base.Cmd("inspect", name, "-f", "'{{json .NetworkSettings.IPAddress}}'").OutLines()
Expand All @@ -86,10 +90,20 @@ func runIPFSDaemonContainer(t *testing.T, base *testutil.Base) (ipfsAddress stri
t.Logf("ip address matches=%v", matches)
assert.Equal(t, len(matches), 2)
ipfsaddr = fmt.Sprintf("/ip4/%s/tcp/5001", matches[1])
addrTest = matches[1] + ":5001"
}

_, 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)
}
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()
}
}

Expand Down

0 comments on commit 2275c4e

Please sign in to comment.