@@ -234,3 +234,50 @@ func TestDeleteUDPConnections(t *testing.T) {
234
234
t .Errorf ("Expect command executed %d times, but got %d" , svcCount , fexec .CommandCalls )
235
235
}
236
236
}
237
+
238
+ func TestClearUDPConntrackForPortNAT (t * testing.T ) {
239
+ fcmd := fakeexec.FakeCmd {
240
+ CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
241
+ func () ([]byte , error ) { return []byte ("1 flow entries have been deleted" ), nil },
242
+ func () ([]byte , error ) {
243
+ return []byte ("" ), fmt .Errorf ("conntrack v1.4.2 (conntrack-tools): 0 flow entries have been deleted" )
244
+ },
245
+ func () ([]byte , error ) { return []byte ("1 flow entries have been deleted" ), nil },
246
+ },
247
+ }
248
+ fexec := fakeexec.FakeExec {
249
+ CommandScript : []fakeexec.FakeCommandAction {
250
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
251
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
252
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
253
+ },
254
+ LookPathFunc : func (cmd string ) (string , error ) { return cmd , nil },
255
+ }
256
+ testCases := []struct {
257
+ name string
258
+ port int
259
+ dest string
260
+ }{
261
+ {
262
+ name : "IPv4 success" ,
263
+ port : 30211 ,
264
+ dest : "1.2.3.4" ,
265
+ },
266
+ }
267
+ svcCount := 0
268
+ for i , tc := range testCases {
269
+ err := ClearEntriesForPortNAT (& fexec , tc .dest , tc .port , v1 .ProtocolUDP )
270
+ if err != nil {
271
+ t .Errorf ("%s test case: unexpected error: %v" , tc .name , err )
272
+ }
273
+ expectCommand := fmt .Sprintf ("conntrack -D -p udp --dport %d --dst-nat %s" , tc .port , tc .dest ) + familyParamStr (utilnet .IsIPv6String (tc .dest ))
274
+ execCommand := strings .Join (fcmd .CombinedOutputLog [i ], " " )
275
+ if expectCommand != execCommand {
276
+ t .Errorf ("%s test case: Expect command: %s, but executed %s" , tc .name , expectCommand , execCommand )
277
+ }
278
+ svcCount ++
279
+ }
280
+ if svcCount != fexec .CommandCalls {
281
+ t .Errorf ("Expect command executed %d times, but got %d" , svcCount , fexec .CommandCalls )
282
+ }
283
+ }
0 commit comments