2016-05-24 16:12:16 -04:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package ipvs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/libnetwork/testutils"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
"github.com/vishvananda/netlink/nl"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
schedMethods = []string{
|
|
|
|
RoundRobin,
|
|
|
|
LeastConnection,
|
|
|
|
DestinationHashing,
|
|
|
|
SourceHashing,
|
|
|
|
}
|
|
|
|
|
|
|
|
protocols = []string{
|
|
|
|
"TCP",
|
|
|
|
"UDP",
|
|
|
|
"FWM",
|
|
|
|
}
|
|
|
|
|
|
|
|
fwdMethods = []uint32{
|
|
|
|
ConnectionFlagMasq,
|
|
|
|
ConnectionFlagTunnel,
|
|
|
|
ConnectionFlagDirectRoute,
|
|
|
|
}
|
|
|
|
|
|
|
|
fwdMethodStrings = []string{
|
|
|
|
"Masq",
|
|
|
|
"Tunnel",
|
|
|
|
"Route",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
func lookupFwMethod(fwMethod uint32) string {
|
2016-05-24 16:12:16 -04:00
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
switch fwMethod {
|
|
|
|
case ConnectionFlagMasq:
|
|
|
|
return fwdMethodStrings[0]
|
|
|
|
case ConnectionFlagTunnel:
|
|
|
|
return fwdMethodStrings[1]
|
|
|
|
case ConnectionFlagDirectRoute:
|
|
|
|
return fwdMethodStrings[2]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2016-05-24 16:12:16 -04:00
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
func checkDestination(t *testing.T, i *Handle, s *Service, d *Destination, checkPresent bool) {
|
2017-05-25 01:10:42 -04:00
|
|
|
var dstFound bool
|
2016-05-24 16:12:16 -04:00
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
dstArray, err := i.GetDestinations(s)
|
|
|
|
require.NoError(t, err)
|
2016-05-24 16:12:16 -04:00
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
for _, dst := range dstArray {
|
2017-05-25 01:10:42 -04:00
|
|
|
if dst.Address.Equal(d.Address) && dst.Port == d.Port && lookupFwMethod(dst.ConnectionFlags) == lookupFwMethod(d.ConnectionFlags) {
|
2017-05-20 13:39:30 -04:00
|
|
|
dstFound = true
|
|
|
|
break
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
switch checkPresent {
|
|
|
|
case true: //The test expects the service to be present
|
|
|
|
if !dstFound {
|
|
|
|
|
|
|
|
t.Fatalf("Did not find the service %s in ipvs output", d.Address.String())
|
|
|
|
}
|
|
|
|
case false: //The test expects that the service should not be present
|
|
|
|
if dstFound {
|
|
|
|
t.Fatalf("Did not find the destination %s fwdMethod %s in ipvs output", d.Address.String(), lookupFwMethod(d.ConnectionFlags))
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) {
|
2016-05-24 16:12:16 -04:00
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
svcArray, err := i.GetServices()
|
|
|
|
require.NoError(t, err)
|
2016-05-24 16:12:16 -04:00
|
|
|
|
2017-05-25 01:10:42 -04:00
|
|
|
var svcFound bool
|
2016-05-24 16:12:16 -04:00
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
for _, svc := range svcArray {
|
2016-05-24 16:12:16 -04:00
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
if svc.Protocol == s.Protocol && svc.Address.String() == s.Address.String() && svc.Port == s.Port {
|
|
|
|
svcFound = true
|
|
|
|
break
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
switch checkPresent {
|
|
|
|
case true: //The test expects the service to be present
|
|
|
|
if !svcFound {
|
|
|
|
|
|
|
|
t.Fatalf("Did not find the service %s in ipvs output", s.Address.String())
|
|
|
|
}
|
|
|
|
case false: //The test expects that the service should not be present
|
|
|
|
if svcFound {
|
|
|
|
t.Fatalf("Did not expect the service %s in ipvs output", s.Address.String())
|
|
|
|
}
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
2017-05-20 13:39:30 -04:00
|
|
|
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFamily(t *testing.T) {
|
|
|
|
if testutils.RunningOnCircleCI() {
|
2017-02-07 01:31:11 -05:00
|
|
|
t.Skip("Skipping as not supported on CIRCLE CI kernel")
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
id, err := getIPVSFamily()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotEqual(t, 0, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService(t *testing.T) {
|
|
|
|
if testutils.RunningOnCircleCI() {
|
2017-02-07 01:31:11 -05:00
|
|
|
t.Skip("Skipping as not supported on CIRCLE CI kernel")
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
|
|
|
|
i, err := New("")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, protocol := range protocols {
|
|
|
|
for _, schedMethod := range schedMethods {
|
|
|
|
|
|
|
|
s := Service{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
SchedName: schedMethod,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch protocol {
|
|
|
|
case "FWM":
|
|
|
|
s.FWMark = 1234
|
|
|
|
case "TCP":
|
|
|
|
s.Protocol = syscall.IPPROTO_TCP
|
|
|
|
s.Port = 80
|
|
|
|
s.Address = net.ParseIP("1.2.3.4")
|
|
|
|
s.Netmask = 0xFFFFFFFF
|
|
|
|
case "UDP":
|
|
|
|
s.Protocol = syscall.IPPROTO_UDP
|
|
|
|
s.Port = 53
|
|
|
|
s.Address = net.ParseIP("2.3.4.5")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := i.NewService(&s)
|
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkService(t, i, &s, true)
|
2016-05-24 16:12:16 -04:00
|
|
|
for _, updateSchedMethod := range schedMethods {
|
|
|
|
if updateSchedMethod == schedMethod {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.SchedName = updateSchedMethod
|
|
|
|
err = i.UpdateService(&s)
|
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkService(t, i, &s, true)
|
|
|
|
|
|
|
|
scopy, err := i.GetService(&s)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, (*scopy).Address.String(), s.Address.String())
|
|
|
|
assert.Equal(t, (*scopy).Port, s.Port)
|
|
|
|
assert.Equal(t, (*scopy).Protocol, s.Protocol)
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = i.DelService(&s)
|
2017-02-07 01:31:11 -05:00
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkService(t, i, &s, false)
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 07:06:55 -04:00
|
|
|
svcs := []Service{
|
|
|
|
{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
SchedName: RoundRobin,
|
|
|
|
Protocol: syscall.IPPROTO_TCP,
|
|
|
|
Port: 80,
|
|
|
|
Address: net.ParseIP("10.20.30.40"),
|
|
|
|
Netmask: 0xFFFFFFFF,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
SchedName: LeastConnection,
|
|
|
|
Protocol: syscall.IPPROTO_UDP,
|
|
|
|
Port: 8080,
|
|
|
|
Address: net.ParseIP("10.20.30.41"),
|
|
|
|
Netmask: 0xFFFFFFFF,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Create services for testing flush
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if !i.IsServicePresent(&svc) {
|
|
|
|
err = i.NewService(&svc)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
checkService(t, i, &svc, true)
|
|
|
|
} else {
|
|
|
|
t.Errorf("svc: %v exists", svc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = i.Flush()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
got, err := i.GetServices()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if len(got) != 0 {
|
|
|
|
t.Errorf("Unexpected services after flush")
|
|
|
|
}
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func createDummyInterface(t *testing.T) {
|
|
|
|
if testutils.RunningOnCircleCI() {
|
2017-02-07 01:31:11 -05:00
|
|
|
t.Skip("Skipping as not supported on CIRCLE CI kernel")
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dummy := &netlink.Dummy{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{
|
|
|
|
Name: "dummy",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := netlink.LinkAdd(dummy)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dummyLink, err := netlink.LinkByName("dummy")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ip, ipNet, err := net.ParseCIDR("10.1.1.1/24")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ipNet.IP = ip
|
|
|
|
|
|
|
|
ipAddr := &netlink.Addr{IPNet: ipNet, Label: ""}
|
|
|
|
err = netlink.AddrAdd(dummyLink, ipAddr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDestination(t *testing.T) {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
|
|
|
|
createDummyInterface(t)
|
|
|
|
i, err := New("")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
for _, protocol := range protocols {
|
2016-05-24 16:12:16 -04:00
|
|
|
|
|
|
|
s := Service{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
SchedName: RoundRobin,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch protocol {
|
|
|
|
case "FWM":
|
|
|
|
s.FWMark = 1234
|
|
|
|
case "TCP":
|
|
|
|
s.Protocol = syscall.IPPROTO_TCP
|
|
|
|
s.Port = 80
|
|
|
|
s.Address = net.ParseIP("1.2.3.4")
|
|
|
|
s.Netmask = 0xFFFFFFFF
|
|
|
|
case "UDP":
|
|
|
|
s.Protocol = syscall.IPPROTO_UDP
|
|
|
|
s.Port = 53
|
|
|
|
s.Address = net.ParseIP("2.3.4.5")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := i.NewService(&s)
|
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkService(t, i, &s, true)
|
2016-05-24 16:12:16 -04:00
|
|
|
|
|
|
|
s.SchedName = ""
|
2017-05-20 13:39:30 -04:00
|
|
|
for _, fwdMethod := range fwdMethods {
|
2016-05-24 16:12:16 -04:00
|
|
|
d1 := Destination{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
Address: net.ParseIP("10.1.1.2"),
|
|
|
|
Port: 5000,
|
|
|
|
Weight: 1,
|
|
|
|
ConnectionFlags: fwdMethod,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := i.NewDestination(&s, &d1)
|
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkDestination(t, i, &s, &d1, true)
|
2016-05-24 16:12:16 -04:00
|
|
|
d2 := Destination{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
Address: net.ParseIP("10.1.1.3"),
|
|
|
|
Port: 5000,
|
|
|
|
Weight: 1,
|
|
|
|
ConnectionFlags: fwdMethod,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = i.NewDestination(&s, &d2)
|
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkDestination(t, i, &s, &d2, true)
|
2016-05-24 16:12:16 -04:00
|
|
|
|
|
|
|
d3 := Destination{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
Address: net.ParseIP("10.1.1.4"),
|
|
|
|
Port: 5000,
|
|
|
|
Weight: 1,
|
|
|
|
ConnectionFlags: fwdMethod,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = i.NewDestination(&s, &d3)
|
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkDestination(t, i, &s, &d3, true)
|
2016-05-24 16:12:16 -04:00
|
|
|
|
2017-05-20 13:39:30 -04:00
|
|
|
for _, updateFwdMethod := range fwdMethods {
|
2016-05-24 16:12:16 -04:00
|
|
|
if updateFwdMethod == fwdMethod {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
d1.ConnectionFlags = updateFwdMethod
|
|
|
|
err = i.UpdateDestination(&s, &d1)
|
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkDestination(t, i, &s, &d1, true)
|
2016-05-24 16:12:16 -04:00
|
|
|
|
|
|
|
d2.ConnectionFlags = updateFwdMethod
|
|
|
|
err = i.UpdateDestination(&s, &d2)
|
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkDestination(t, i, &s, &d2, true)
|
2016-05-24 16:12:16 -04:00
|
|
|
|
|
|
|
d3.ConnectionFlags = updateFwdMethod
|
|
|
|
err = i.UpdateDestination(&s, &d3)
|
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkDestination(t, i, &s, &d3, true)
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = i.DelDestination(&s, &d1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = i.DelDestination(&s, &d2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = i.DelDestination(&s, &d3)
|
|
|
|
assert.NoError(t, err)
|
2017-05-20 13:39:30 -04:00
|
|
|
checkDestination(t, i, &s, &d3, false)
|
|
|
|
|
2016-05-24 16:12:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|