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