@@ -20,10 +20,13 @@ import (
2020 "fmt"
2121 "regexp"
2222 "testing"
23+ "time"
2324
2425 "github.com/containerd/nerdctl/v2/pkg/infoutil"
2526 "github.com/containerd/nerdctl/v2/pkg/rootlessutil"
2627 "github.com/containerd/nerdctl/v2/pkg/testutil"
28+ "github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
29+ "github.com/containerd/nerdctl/v2/pkg/testutil/portlock"
2730
2831 "gotest.tools/v3/assert"
2932)
@@ -71,25 +74,53 @@ func TestIPFSAddress(t *testing.T) {
7174}
7275
7376func runIPFSDaemonContainer (t * testing.T , base * testutil.Base ) (ipfsAddress string , done func ()) {
74- name := "test-ipfs-address"
77+ port , err := portlock .Acquire (0 )
78+ assert .NilError (base .T , err , fmt .Errorf ("failed acquiring port: %w" , err ))
79+
80+ name := "test-ipfs-address-" + testutil .Identifier (t )
7581 var ipfsaddr string
82+ var addrTest string
7683 if detachedNetNS , _ := rootlessutil .DetachedNetNS (); detachedNetNS != "" {
84+ fmt .Println ("with detached NS" )
7785 // detached-netns mode can't use .NetworkSettings.IPAddress, because the daemon and CNI has different network namespaces
78- 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 ()
79- ipfsaddr = "/ip4/127.0.0.1/tcp/5999"
86+ 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 ()
87+ ipfsaddr = fmt .Sprintf ("/ip4/127.0.0.1/tcp/%d" , port )
88+ addrTest = fmt .Sprintf ("127.0.0.1:%d" , port )
8089 } else {
81- 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 ()
90+ fmt .Println ("withOUT detached NS" )
91+ 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 ()
8292 iplines := base .Cmd ("inspect" , name , "-f" , "'{{json .NetworkSettings.IPAddress}}'" ).OutLines ()
8393 t .Logf ("IPAddress=%v" , iplines )
8494 assert .Equal (t , len (iplines ), 2 )
8595 matches := iplineRegexp .FindStringSubmatch (iplines [0 ])
8696 t .Logf ("ip address matches=%v" , matches )
8797 assert .Equal (t , len (matches ), 2 )
88- ipfsaddr = fmt .Sprintf ("/ip4/%s/tcp/5001" , matches [1 ])
98+ ipfsaddr = fmt .Sprintf ("/ip4/%s/tcp/%d" , matches [1 ], port )
99+ addrTest = matches [1 ] + fmt .Sprintf (":%d" , port )
100+ }
101+
102+ _ , err = nettestutil .HTTPGet (fmt .Sprintf ("http://%s/api/v0" , addrTest ), 30 , true )
103+ // Not there... give it more time...
104+ if err != nil {
105+ time .Sleep (1 * time .Second )
106+ _ , err = nettestutil .HTTPGet (fmt .Sprintf ("http://%s/api/v0" , addrTest ), 30 , true )
89107 }
108+ if err != nil {
109+ fmt .Println ("Something is wrong with ipfs communication" )
110+ res := base .Cmd ("inspect" , name ).Run ()
111+ fmt .Println (res )
112+ res = base .Cmd ("logs" , name ).Run ()
113+ fmt .Println (res )
114+ }
115+ assert .NilError (t , err )
116+
90117 return ipfsaddr , func () {
91- base .Cmd ("kill" , "test-ipfs-address" ).AssertOK ()
92- base .Cmd ("rm" , "test-ipfs-address" ).AssertOK ()
118+ base .Cmd ("kill" , name ).AssertOK ()
119+ base .Cmd ("rm" , name ).AssertOK ()
120+ err = portlock .Release (port )
121+ if err != nil {
122+ t .Errorf ("failed to release ipfs port %d: %v" , port , err )
123+ }
93124 }
94125}
95126
0 commit comments