1818package support
1919
2020import (
21+ "fmt"
2122 "os"
2223 "path/filepath"
2324 "testing"
@@ -27,6 +28,8 @@ import (
2728 "github.com/spf13/afero"
2829 "github.com/stretchr/testify/assert"
2930 "github.com/stretchr/testify/require"
31+ "k8s.io/utils/exec"
32+ testingexec "k8s.io/utils/exec/testing"
3033)
3134
3235func TestDumpLog (t * testing.T ) {
@@ -49,3 +52,156 @@ func TestDumpLog(t *testing.T) {
4952 require .NoError (t , err )
5053 assert .True (t , ok )
5154}
55+
56+ func TestDumpNFTables (t * testing.T ) {
57+ const nftV4Output = "table ip antrea { chain antrea-chain { type filter hook input priority 0; } }"
58+ const nftV6Output = "table ip6 antrea { chain antrea-chain6 { type filter hook input priority 0; } }"
59+
60+ v4ErrorAction := func () ([]byte , []byte , error ) {
61+ return nil , nil , fmt .Errorf ("v4 error" )
62+ }
63+ v4SuccessAction := func () ([]byte , []byte , error ) {
64+ return []byte (nftV4Output ), nil , nil
65+ }
66+ v6SuccessAction := func () ([]byte , []byte , error ) {
67+ return []byte (nftV6Output ), nil , nil
68+ }
69+ emptySuccessAction := func () ([]byte , []byte , error ) {
70+ return []byte ("" ), nil , nil
71+ }
72+
73+ originalV4Check := nftablesIPv4Supported
74+ originalV6Check := nftablesIPv6Supported
75+ defer func () {
76+ nftablesIPv4Supported = originalV4Check
77+ nftablesIPv6Supported = originalV6Check
78+ }()
79+
80+ nftablesIPv4Supported = func () bool { return true }
81+ nftablesIPv6Supported = func () bool { return true }
82+
83+ tests := []struct {
84+ name string
85+ v4Enabled bool
86+ v6Enabled bool
87+ commandActions []testingexec.FakeCommandAction
88+ expectedContent string
89+ expectFile bool
90+ expectErr bool
91+ }{
92+ {
93+ name : "v4 enabled only" ,
94+ v4Enabled : true ,
95+ v6Enabled : false ,
96+ commandActions : []testingexec.FakeCommandAction {
97+ func (cmd string , args ... string ) exec.Cmd {
98+ return & testingexec.FakeCmd {
99+ CombinedOutputScript : []testingexec.FakeAction {v4SuccessAction },
100+ }
101+ },
102+ },
103+ expectedContent : nftV4Output + "\n " ,
104+ expectFile : true ,
105+ },
106+ {
107+ name : "v6 enabled only" ,
108+ v4Enabled : false ,
109+ v6Enabled : true ,
110+ commandActions : []testingexec.FakeCommandAction {
111+ func (cmd string , args ... string ) exec.Cmd {
112+ return & testingexec.FakeCmd {
113+ CombinedOutputScript : []testingexec.FakeAction {v6SuccessAction },
114+ }
115+ },
116+ },
117+ expectedContent : nftV6Output + "\n " ,
118+ expectFile : true ,
119+ },
120+ {
121+ name : "v4 and v6 enabled" ,
122+ v4Enabled : true ,
123+ v6Enabled : true ,
124+ commandActions : []testingexec.FakeCommandAction {
125+ func (cmd string , args ... string ) exec.Cmd {
126+ return & testingexec.FakeCmd {
127+ CombinedOutputScript : []testingexec.FakeAction {v4SuccessAction },
128+ }
129+ },
130+ func (cmd string , args ... string ) exec.Cmd {
131+ return & testingexec.FakeCmd {
132+ CombinedOutputScript : []testingexec.FakeAction {v6SuccessAction },
133+ }
134+ },
135+ },
136+ expectedContent : nftV4Output + "\n " + nftV6Output + "\n " ,
137+ expectFile : true ,
138+ },
139+ {
140+ name : "v4 command error" ,
141+ v4Enabled : true ,
142+ v6Enabled : true ,
143+ commandActions : []testingexec.FakeCommandAction {
144+ func (cmd string , args ... string ) exec.Cmd {
145+ return & testingexec.FakeCmd {
146+ CombinedOutputScript : []testingexec.FakeAction {v4ErrorAction },
147+ }
148+ },
149+ },
150+ expectFile : false ,
151+ expectErr : true ,
152+ },
153+ {
154+ name : "no rules found (empty output)" ,
155+ v4Enabled : true ,
156+ v6Enabled : true ,
157+ commandActions : []testingexec.FakeCommandAction {
158+ func (cmd string , args ... string ) exec.Cmd {
159+ return & testingexec.FakeCmd {CombinedOutputScript : []testingexec.FakeAction {emptySuccessAction }}
160+ },
161+ func (cmd string , args ... string ) exec.Cmd {
162+ return & testingexec.FakeCmd {CombinedOutputScript : []testingexec.FakeAction {emptySuccessAction }}
163+ },
164+ },
165+ expectFile : false ,
166+ expectErr : false ,
167+ },
168+ }
169+
170+ for _ , tc := range tests {
171+ t .Run (tc .name , func (t * testing.T ) {
172+ fs := afero .NewMemMapFs ()
173+ fs .MkdirAll (baseDir , os .ModePerm )
174+
175+ fakeExecutor := & testingexec.FakeExec {}
176+ fakeExecutor .CommandScript = tc .commandActions
177+
178+ dumper := & agentDumper {
179+ fs : fs ,
180+ executor : fakeExecutor ,
181+ v4Enabled : tc .v4Enabled ,
182+ v6Enabled : tc .v6Enabled ,
183+ }
184+
185+ err := dumper .dumpNFTables (baseDir )
186+
187+ if tc .expectErr {
188+ require .Error (t , err )
189+ return
190+ }
191+
192+ require .NoError (t , err )
193+
194+ filePath := filepath .Join (baseDir , "nftables" )
195+
196+ ok , err := afero .Exists (fs , filePath )
197+ require .NoError (t , err )
198+ assert .Equal (t , tc .expectFile , ok , "Expected nftables file existence to be %t" , tc .expectFile )
199+
200+ if tc .expectFile {
201+ content , err := afero .ReadFile (fs , filePath )
202+ require .NoError (t , err )
203+ assert .Equal (t , tc .expectedContent , string (content ), "File content does not match" )
204+ }
205+ })
206+ }
207+ }
0 commit comments