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,146 @@ 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+ tests := []struct {
74+ name string
75+ v4Enabled bool
76+ v6Enabled bool
77+ commandActions []testingexec.FakeCommandAction
78+ expectedContent string
79+ expectFile bool
80+ expectErr bool
81+ }{
82+ {
83+ name : "v4 enabled only" ,
84+ v4Enabled : true ,
85+ v6Enabled : false ,
86+ commandActions : []testingexec.FakeCommandAction {
87+ func (cmd string , args ... string ) exec.Cmd {
88+ return & testingexec.FakeCmd {
89+ CombinedOutputScript : []testingexec.FakeAction {v4SuccessAction },
90+ }
91+ },
92+ },
93+ expectedContent : nftV4Output + "\n " ,
94+ expectFile : true ,
95+ },
96+ {
97+ name : "v6 enabled only" ,
98+ v4Enabled : false ,
99+ v6Enabled : true ,
100+ commandActions : []testingexec.FakeCommandAction {
101+ func (cmd string , args ... string ) exec.Cmd {
102+ return & testingexec.FakeCmd {
103+ CombinedOutputScript : []testingexec.FakeAction {v6SuccessAction },
104+ }
105+ },
106+ },
107+ expectedContent : nftV6Output + "\n " ,
108+ expectFile : true ,
109+ },
110+ {
111+ name : "v4 and v6 enabled" ,
112+ v4Enabled : true ,
113+ v6Enabled : true ,
114+ commandActions : []testingexec.FakeCommandAction {
115+ func (cmd string , args ... string ) exec.Cmd {
116+ return & testingexec.FakeCmd {
117+ CombinedOutputScript : []testingexec.FakeAction {v4SuccessAction },
118+ }
119+ },
120+ func (cmd string , args ... string ) exec.Cmd {
121+ return & testingexec.FakeCmd {
122+ CombinedOutputScript : []testingexec.FakeAction {v6SuccessAction },
123+ }
124+ },
125+ },
126+ expectedContent : nftV4Output + "\n " + nftV6Output + "\n " ,
127+ expectFile : true ,
128+ },
129+ {
130+ name : "v4 command error" ,
131+ v4Enabled : true ,
132+ v6Enabled : true ,
133+ commandActions : []testingexec.FakeCommandAction {
134+ func (cmd string , args ... string ) exec.Cmd {
135+ return & testingexec.FakeCmd {
136+ CombinedOutputScript : []testingexec.FakeAction {v4ErrorAction },
137+ }
138+ },
139+ },
140+ expectFile : false ,
141+ expectErr : true ,
142+ },
143+ {
144+ name : "no rules found (empty output)" ,
145+ v4Enabled : true ,
146+ v6Enabled : true ,
147+ commandActions : []testingexec.FakeCommandAction {
148+ func (cmd string , args ... string ) exec.Cmd {
149+ return & testingexec.FakeCmd {CombinedOutputScript : []testingexec.FakeAction {emptySuccessAction }}
150+ },
151+ func (cmd string , args ... string ) exec.Cmd {
152+ return & testingexec.FakeCmd {CombinedOutputScript : []testingexec.FakeAction {emptySuccessAction }}
153+ },
154+ },
155+ expectFile : false ,
156+ expectErr : false ,
157+ },
158+ }
159+
160+ for _ , tc := range tests {
161+ t .Run (tc .name , func (t * testing.T ) {
162+ fs := afero .NewMemMapFs ()
163+ fs .MkdirAll (baseDir , os .ModePerm )
164+
165+ fakeExecutor := & testingexec.FakeExec {}
166+ fakeExecutor .CommandScript = tc .commandActions
167+
168+ dumper := & agentDumper {
169+ fs : fs ,
170+ executor : fakeExecutor ,
171+ v4Enabled : tc .v4Enabled ,
172+ v6Enabled : tc .v6Enabled ,
173+ }
174+
175+ err := dumper .dumpNFTables (baseDir )
176+
177+ if tc .expectErr {
178+ require .Error (t , err )
179+ return
180+ }
181+
182+ require .NoError (t , err )
183+
184+ filePath := filepath .Join (baseDir , "nftables" )
185+
186+ ok , err := afero .Exists (fs , filePath )
187+ require .NoError (t , err )
188+ assert .Equal (t , tc .expectFile , ok , "Expected nftables file existence to be %t" , tc .expectFile )
189+
190+ if tc .expectFile {
191+ content , err := afero .ReadFile (fs , filePath )
192+ require .NoError (t , err )
193+ assert .Equal (t , tc .expectedContent , string (content ), "File content does not match" )
194+ }
195+ })
196+ }
197+ }
0 commit comments