2015-07-02 01:00:48 -04:00
|
|
|
package osl
|
2015-04-13 14:40:42 -04:00
|
|
|
|
|
|
|
import (
|
2016-02-26 17:58:11 -05:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
2015-04-23 20:37:19 -04:00
|
|
|
"net"
|
2015-04-13 14:40:42 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-01-05 14:35:53 -05:00
|
|
|
"syscall"
|
2015-04-13 14:40:42 -04:00
|
|
|
"testing"
|
2015-05-26 17:14:01 -04:00
|
|
|
"time"
|
2015-04-13 14:40:42 -04:00
|
|
|
|
2016-01-05 14:35:53 -05:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2015-09-26 13:12:20 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-04-23 20:37:19 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
2016-01-05 14:35:53 -05:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
2015-04-13 14:40:42 -04:00
|
|
|
"github.com/vishvananda/netns"
|
|
|
|
)
|
|
|
|
|
2015-04-23 20:37:19 -04:00
|
|
|
const (
|
|
|
|
vethName1 = "wierdlongname1"
|
|
|
|
vethName2 = "wierdlongname2"
|
2015-05-21 14:04:49 -04:00
|
|
|
vethName3 = "wierdlongname3"
|
|
|
|
vethName4 = "wierdlongname4"
|
2015-04-23 20:37:19 -04:00
|
|
|
sboxIfaceName = "containername"
|
|
|
|
)
|
|
|
|
|
2016-02-26 17:58:11 -05:00
|
|
|
func generateRandomName(prefix string, size int) (string, error) {
|
|
|
|
id := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, id); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return prefix + hex.EncodeToString(id)[:size], nil
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
func newKey(t *testing.T) (string, error) {
|
2016-02-26 17:58:11 -05:00
|
|
|
name, err := generateRandomName("netns", 12)
|
2015-04-13 14:40:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
name = filepath.Join("/tmp", name)
|
|
|
|
if _, err := os.Create(name); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-05-26 17:14:01 -04:00
|
|
|
// Set the rpmCleanupPeriod to be low to make the test run quicker
|
2015-05-28 19:29:21 -04:00
|
|
|
gpmLock.Lock()
|
2015-05-27 16:20:24 -04:00
|
|
|
gpmCleanupPeriod = 2 * time.Second
|
2015-05-28 19:29:21 -04:00
|
|
|
gpmLock.Unlock()
|
2015-05-26 17:14:01 -04:00
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
func newInfo(hnd *netlink.Handle, t *testing.T) (Sandbox, error) {
|
2015-04-23 20:37:19 -04:00
|
|
|
veth := &netlink.Veth{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: vethName1, TxQLen: 0},
|
|
|
|
PeerName: vethName2}
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := hnd.LinkAdd(veth); err != nil {
|
2015-04-23 20:37:19 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the sandbox side pipe interface
|
|
|
|
// This is needed for cleanup on DeleteEndpoint()
|
2015-06-04 23:21:23 -04:00
|
|
|
intf1 := &nwIface{}
|
|
|
|
intf1.srcName = vethName2
|
|
|
|
intf1.dstName = sboxIfaceName
|
2015-04-23 20:37:19 -04:00
|
|
|
|
|
|
|
ip4, addr, err := net.ParseCIDR("192.168.1.100/24")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
intf1.address = addr
|
|
|
|
intf1.address.IP = ip4
|
2015-04-23 20:37:19 -04:00
|
|
|
|
2015-04-28 01:57:36 -04:00
|
|
|
// ip6, addrv6, err := net.ParseCIDR("2001:DB8::ABCD/48")
|
|
|
|
ip6, addrv6, err := net.ParseCIDR("fe80::2/64")
|
2015-04-23 20:37:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
intf1.addressIPv6 = addrv6
|
|
|
|
intf1.addressIPv6.IP = ip6
|
2015-05-21 14:04:49 -04:00
|
|
|
|
2015-05-19 20:08:56 -04:00
|
|
|
_, route, err := net.ParseCIDR("192.168.2.1/32")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
intf1.routes = []*net.IPNet{route}
|
2015-05-19 20:08:56 -04:00
|
|
|
|
2015-06-05 02:45:04 -04:00
|
|
|
intf2 := &nwIface{}
|
|
|
|
intf2.srcName = "testbridge"
|
|
|
|
intf2.dstName = sboxIfaceName
|
|
|
|
intf2.bridge = true
|
|
|
|
|
2015-05-21 14:04:49 -04:00
|
|
|
veth = &netlink.Veth{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: vethName3, TxQLen: 0},
|
|
|
|
PeerName: vethName4}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := hnd.LinkAdd(veth); err != nil {
|
2015-05-21 14:04:49 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-05 02:45:04 -04:00
|
|
|
intf3 := &nwIface{}
|
|
|
|
intf3.srcName = vethName4
|
|
|
|
intf3.dstName = sboxIfaceName
|
|
|
|
intf3.master = "testbridge"
|
2015-05-21 14:04:49 -04:00
|
|
|
|
2015-06-05 02:45:04 -04:00
|
|
|
info := &networkNamespace{iFaces: []*nwIface{intf1, intf2, intf3}}
|
2015-05-19 20:08:56 -04:00
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
info.gw = net.ParseIP("192.168.1.1")
|
2015-04-28 01:57:36 -04:00
|
|
|
// sinfo.GatewayIPv6 = net.ParseIP("2001:DB8::1")
|
2015-06-04 23:21:23 -04:00
|
|
|
info.gwv6 = net.ParseIP("fe80::1")
|
2015-04-23 20:37:19 -04:00
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
return info, nil
|
2015-04-23 20:37:19 -04:00
|
|
|
}
|
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
func verifySandbox(t *testing.T, s Sandbox, ifaceSuffixes []string) {
|
2015-04-13 14:40:42 -04:00
|
|
|
_, ok := s.(*networkNamespace)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("The sandox interface returned is not of type networkNamespace")
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
sbNs, err := netns.GetFromPath(s.Key())
|
2015-04-13 14:40:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed top open network namespace path %q: %v", s.Key(), err)
|
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
defer sbNs.Close()
|
2015-04-13 14:40:42 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
nh, err := netlink.NewHandleAt(sbNs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-04-13 14:40:42 -04:00
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
defer nh.Delete()
|
2015-04-13 14:40:42 -04:00
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
for _, suffix := range ifaceSuffixes {
|
2016-05-16 14:51:40 -04:00
|
|
|
_, err = nh.LinkByName(sboxIfaceName + suffix)
|
2015-06-04 23:21:23 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not find the interface %s inside the sandbox: %v",
|
|
|
|
sboxIfaceName+suffix, err)
|
|
|
|
}
|
2015-04-23 20:37:19 -04:00
|
|
|
}
|
2015-04-13 14:40:42 -04:00
|
|
|
}
|
2015-05-26 17:14:01 -04:00
|
|
|
|
2015-06-05 14:46:33 -04:00
|
|
|
func verifyCleanup(t *testing.T, s Sandbox, wait bool) {
|
|
|
|
if wait {
|
|
|
|
time.Sleep(time.Duration(gpmCleanupPeriod * 2))
|
|
|
|
}
|
|
|
|
|
2015-05-26 17:14:01 -04:00
|
|
|
if _, err := os.Stat(s.Key()); err == nil {
|
2015-06-05 14:46:33 -04:00
|
|
|
if wait {
|
|
|
|
t.Fatalf("The sandbox path %s is not getting cleaned up even after twice the cleanup period", s.Key())
|
|
|
|
} else {
|
|
|
|
t.Fatalf("The sandbox path %s is not cleaned up after running gc", s.Key())
|
|
|
|
}
|
2015-05-26 17:14:01 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-29 16:32:07 -04:00
|
|
|
|
|
|
|
func TestScanStatistics(t *testing.T) {
|
|
|
|
data :=
|
|
|
|
"Inter-| Receive | Transmit\n" +
|
|
|
|
" face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n" +
|
|
|
|
" eth0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
|
|
|
|
" wlan0: 7787685 11141 0 0 0 0 0 0 1681390 7220 0 0 0 0 0 0\n" +
|
|
|
|
" lo: 783782 1853 0 0 0 0 0 0 783782 1853 0 0 0 0 0 0\n" +
|
|
|
|
"lxcbr0: 0 0 0 0 0 0 0 0 9006 61 0 0 0 0 0 0\n"
|
|
|
|
|
2015-09-26 13:12:20 -04:00
|
|
|
i := &types.InterfaceStatistics{}
|
2015-06-29 16:32:07 -04:00
|
|
|
|
|
|
|
if err := scanInterfaceStats(data, "wlan0", i); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if i.TxBytes != 1681390 || i.TxPackets != 7220 || i.RxBytes != 7787685 || i.RxPackets != 11141 {
|
|
|
|
t.Fatalf("Error scanning the statistics")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanInterfaceStats(data, "lxcbr0", i); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if i.TxBytes != 9006 || i.TxPackets != 61 || i.RxBytes != 0 || i.RxPackets != 0 {
|
|
|
|
t.Fatalf("Error scanning the statistics")
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 14:35:53 -05:00
|
|
|
|
|
|
|
func TestDisableIPv6DAD(t *testing.T) {
|
|
|
|
if testutils.RunningOnCircleCI() {
|
|
|
|
t.Skipf("Skipping as not supported on CIRCLE CI kernel")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
|
|
|
|
ipv6, _ := types.ParseCIDR("2001:db8::44/64")
|
|
|
|
iface := &nwIface{addressIPv6: ipv6}
|
|
|
|
|
|
|
|
veth := &netlink.Veth{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: "sideA"},
|
|
|
|
PeerName: "sideB",
|
|
|
|
}
|
2016-06-15 16:07:52 -04:00
|
|
|
nlh, err := netlink.NewHandle(syscall.NETLINK_ROUTE)
|
2016-05-16 14:51:40 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = nlh.LinkAdd(veth)
|
2016-01-05 14:35:53 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
link, err := nlh.LinkByName("sideA")
|
2016-01-05 14:35:53 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
err = setInterfaceIPv6(nlh, link, iface)
|
2016-01-05 14:35:53 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
addrList, err := nlh.AddrList(link, nl.FAMILY_V6)
|
2016-01-05 14:35:53 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if addrList[0].Flags&syscall.IFA_F_NODAD == 0 {
|
|
|
|
t.Fatalf("Unexpected interface flags: 0x%x. Expected to contain 0x%x", addrList[0].Flags, syscall.IFA_F_NODAD)
|
|
|
|
}
|
|
|
|
}
|