@@ -18,63 +18,62 @@ package container
1818
1919import (
2020 "fmt"
21+ "strconv"
2122 "strings"
2223 "testing"
2324
2425 "github.com/coreos/go-iptables/iptables"
2526 "gotest.tools/v3/assert"
2627
2728 "github.com/containerd/nerdctl/mod/tigron/expect"
29+ "github.com/containerd/nerdctl/mod/tigron/require"
2830 "github.com/containerd/nerdctl/mod/tigron/test"
2931
30- "github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3132 "github.com/containerd/nerdctl/v2/pkg/testutil"
3233 iptablesutil "github.com/containerd/nerdctl/v2/pkg/testutil/iptables"
3334 "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
35+ "github.com/containerd/nerdctl/v2/pkg/testutil/portlock"
3436)
3537
3638// TestKillCleanupForwards runs a container that exposes a port and then kill it.
3739// The test checks that the kill command effectively clean up
38- // the iptables forwards creted from the run.
40+ // the iptables forwards created from the run.
3941func TestKillCleanupForwards (t * testing.T ) {
40- // skip if rootless
41- if rootlessutil .IsRootless () {
42- t .Skip ("pkg/testutil/iptables does not support rootless" )
43- }
4442
45- const hostPort = 9999
43+ ipt , err := iptables .New ()
44+ assert .NilError (t , err )
4645
4746 testCase := nerdtest .Setup ()
4847
49- ipt , err := iptables .New ()
50- assert .NilError (t , err )
48+ testCase .Require = require .Not (nerdtest .Rootless )
5149
5250 testCase .Setup = func (data test.Data , helpers test.Helpers ) {
51+ hostPort , err := portlock .Acquire (0 )
52+ if err != nil {
53+ t .Logf ("Failed to acquire port: %v" , err )
54+ t .FailNow ()
55+ }
56+
57+ containerName := data .Identifier ()
58+
5359 helpers .Ensure (
5460 "run" , "-d" ,
5561 "--restart=no" ,
56- "--name" , data . Identifier () ,
62+ "--name" , containerName ,
5763 "-p" , fmt .Sprintf ("127.0.0.1:%d:80" , hostPort ),
5864 testutil .NginxAlpineImage ,
5965 )
60- }
61-
62- testCase .Cleanup = func (data test.Data , helpers test.Helpers ) {
63- helpers .Anyhow ("rm" , "-f" , data .Identifier ())
64- }
65-
66- testCase .Command = func (data test.Data , helpers test.Helpers ) test.TestableCommand {
67- name := data .Identifier ()
66+ nerdtest .EnsureContainerStarted (helpers , containerName )
6867
6968 containerID := strings .TrimSpace (
70- helpers .Capture ("inspect" , "-f" , "{{.Id}}" , name ),
69+ helpers .Capture ("inspect" , "-f" , "{{.Id}}" , containerName ),
7170 )
7271
7372 containerIP := strings .TrimSpace (
7473 helpers .Capture (
7574 "inspect" ,
7675 "-f" , "{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}" ,
77- name ,
76+ containerName ,
7877 ),
7978 )
8079
@@ -92,16 +91,48 @@ func TestKillCleanupForwards(t *testing.T) {
9291 )
9392 }
9493
95- assert . Assert (
96- t ,
97- iptablesutil . ForwardExists ( t , ipt , chain , containerIP , hostPort ),
98- "expected iptables forward to exist before kill" ,
99- )
94+ data . Labels (). Set ( "containerName" , containerName )
95+ data . Labels (). Set ( "containerIP" , containerIP )
96+ data . Labels (). Set ( "hostPort" , strconv . Itoa ( hostPort ))
97+ data . Labels (). Set ( "chain" , chain )
98+ }
10099
101- return helpers .Command ("kill" , name )
100+ testCase .SubTests = []* test.Case {
101+ {
102+ Description : "iptables forwarding rule should exist before container is killed" ,
103+ NoParallel : true ,
104+ Setup : func (data test.Data , helpers test.Helpers ) {
105+ containerIP := data .Labels ().Get ("containerIP" )
106+ hostPort , _ := strconv .Atoi (data .Labels ().Get ("hostPort" ))
107+ chain := data .Labels ().Get ("chain" )
108+
109+ assert .Equal (t , iptablesutil .ForwardExists (t , ipt , chain , containerIP , hostPort ), true )
110+ },
111+ },
112+ {
113+ Description : "kill container" ,
114+ NoParallel : true ,
115+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
116+ return helpers .Command ("kill" , data .Labels ().Get ("containerName" ))
117+ },
118+ Expected : test .Expects (expect .ExitCodeSuccess , nil , nil ),
119+ },
120+ {
121+ Description : "iptables forwarding rule should be removed after container is killed" ,
122+ NoParallel : true ,
123+ Setup : func (data test.Data , helpers test.Helpers ) {
124+ containerIP := data .Labels ().Get ("containerIP" )
125+ hostPort , _ := strconv .Atoi (data .Labels ().Get ("hostPort" ))
126+ chain := data .Labels ().Get ("chain" )
127+
128+ assert .Equal (t , iptablesutil .ForwardExists (t , ipt , chain , containerIP , hostPort ), false )
129+ },
130+ },
102131 }
103132
104- testCase .Expected = test .Expects (expect .ExitCodeSuccess , nil , nil )
133+ testCase .Cleanup = func (data test.Data , helpers test.Helpers ) {
134+ helpers .Anyhow ("rm" , "-f" , data .Identifier ())
135+ }
105136
106137 testCase .Run (t )
107138}
0 commit comments