@@ -40,6 +40,7 @@ import (
4040 "github.com/containerd/nerdctl/v2/pkg/strutil"
4141 "github.com/containerd/nerdctl/v2/pkg/testutil"
4242 "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
43+ "github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
4344 "github.com/containerd/nerdctl/v2/pkg/testutil/test"
4445)
4546
@@ -568,3 +569,45 @@ func TestIssue3568(t *testing.T) {
568569
569570 testCase .Run (t )
570571}
572+
573+ // TestPortBindingWithCustomHost tests https://github.com/containerd/nerdctl/issues/3539
574+ func TestPortBindingWithCustomHost (t * testing.T ) {
575+ testCase := nerdtest .Setup ()
576+
577+ const (
578+ host = "127.0.0.2"
579+ hostPort = 8080
580+ )
581+ address := fmt .Sprintf ("%s:%d" , host , hostPort )
582+
583+ testCase .SubTests = []* test.Case {
584+ {
585+ Description : "Issue #3539 - Access to a container running when 127.0.0.2 is specified in -p in rootless mode." ,
586+ Setup : func (data test.Data , helpers test.Helpers ) {
587+ helpers .Ensure ("run" , "-d" , "--name" , data .Identifier (), "-p" , fmt .Sprintf ("%s:80" , address ), testutil .NginxAlpineImage )
588+ nerdtest .EnsureContainerStarted (helpers , data .Identifier ())
589+ },
590+ Cleanup : func (data test.Data , helpers test.Helpers ) {
591+ helpers .Anyhow ("rm" , "-f" , data .Identifier ())
592+ },
593+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
594+ return & test.Expected {
595+ ExitCode : 0 ,
596+ Errors : []error {},
597+ Output : test .All (
598+ func (stdout string , info string , t * testing.T ) {
599+ resp , err := nettestutil .HTTPGet (address , 30 , false )
600+ assert .NilError (t , err )
601+
602+ respBody , err := io .ReadAll (resp .Body )
603+ assert .NilError (t , err )
604+ assert .Assert (t , strings .Contains (string (respBody ), testutil .NginxAlpineIndexHTMLSnippet ))
605+ },
606+ ),
607+ }
608+ },
609+ },
610+ }
611+
612+ testCase .Run (t )
613+ }
0 commit comments