move UDP conntrack operations together to pkg/proxy/util/conntrack.go

This commit is contained in:
m1093782566
2017-07-24 15:02:07 +08:00
parent 577fdf91c2
commit 7b8372db99
5 changed files with 161 additions and 56 deletions

View File

@@ -18,6 +18,7 @@ package util
import (
"fmt"
"strconv"
"strings"
"testing"
@@ -74,7 +75,7 @@ func TestExecConntrackTool(t *testing.T) {
}
}
func TestDeleteServiceConnections(t *testing.T) {
func TestClearUDPConntrackForIP(t *testing.T) {
fcmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
@@ -105,8 +106,10 @@ func TestDeleteServiceConnections(t *testing.T) {
svcCount := 0
for i := range testCases {
DeleteServiceConnections(&fexec, testCases[i])
for _, ip := range testCases[i] {
if err := ClearUDPConntrackForIP(&fexec, ip); err != nil {
t.Errorf("Unexepected error: %v", err)
}
expectCommand := fmt.Sprintf("conntrack -D --orig-dst %s -p udp", ip)
execCommand := strings.Join(fcmd.CombinedOutputLog[svcCount], " ")
if expectCommand != execCommand {
@@ -119,3 +122,93 @@ func TestDeleteServiceConnections(t *testing.T) {
}
}
}
func TestClearUDPConntrackForPort(t *testing.T) {
fcmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
func() ([]byte, error) {
return []byte(""), fmt.Errorf("conntrack v1.4.2 (conntrack-tools): 0 flow entries have been deleted.")
},
},
}
fexec := fakeexec.FakeExec{
CommandScript: []fakeexec.FakeCommandAction{
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
LookPathFunc: func(cmd string) (string, error) { return cmd, nil },
}
testCases := []string{
"8080",
"9090",
}
svcCount := 0
for i := range testCases {
portNum, _ := strconv.Atoi(testCases[i])
err := ClearUDPConntrackForPort(&fexec, portNum)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expectCommand := fmt.Sprintf("conntrack -D -p udp --dport %s", testCases[i])
execCommand := strings.Join(fcmd.CombinedOutputLog[svcCount], " ")
if expectCommand != execCommand {
t.Errorf("Exepect comand: %s, but executed %s", expectCommand, execCommand)
}
svcCount += 1
if svcCount != fexec.CommandCalls {
t.Errorf("Exepect comand executed %d times, but got %d", svcCount, fexec.CommandCalls)
}
}
}
func TestDeleteUDPConnections(t *testing.T) {
fcmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
func() ([]byte, error) {
return []byte(""), fmt.Errorf("conntrack v1.4.2 (conntrack-tools): 0 flow entries have been deleted.")
},
},
}
fexec := fakeexec.FakeExec{
CommandScript: []fakeexec.FakeCommandAction{
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
LookPathFunc: func(cmd string) (string, error) { return cmd, nil },
}
testCases := []struct {
origin string
dest string
}{
{
origin: "1.2.3.4",
dest: "10.20.30.40",
},
{
origin: "2.3.4.5",
dest: "20.30.40.50",
},
}
svcCount := 0
for i := range testCases {
err := ClearUDPConntrackForPeers(&fexec, testCases[i].origin, testCases[i].dest)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
expectCommand := fmt.Sprintf("conntrack -D --orig-dst %s --dst-nat %s -p udp", testCases[i].origin, testCases[i].dest)
execCommand := strings.Join(fcmd.CombinedOutputLog[svcCount], " ")
if expectCommand != execCommand {
t.Errorf("Exepect comand: %s, but executed %s", expectCommand, execCommand)
}
svcCount += 1
if svcCount != fexec.CommandCalls {
t.Errorf("Exepect comand executed %d times, but got %d", svcCount, fexec.CommandCalls)
}
}
}