2015-04-13 14:40:42 -04:00
|
|
|
package sandbox
|
|
|
|
|
|
|
|
import (
|
2015-04-23 20:37:19 -04:00
|
|
|
"net"
|
2015-04-13 14:40:42 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
2015-05-26 17:14:01 -04:00
|
|
|
"time"
|
2015-04-13 14:40:42 -04:00
|
|
|
|
2015-04-18 00:37:54 -04:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-04-23 20:37:19 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
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"
|
|
|
|
)
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
func newKey(t *testing.T) (string, error) {
|
2015-04-18 00:37:54 -04:00
|
|
|
name, err := netutils.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
|
|
|
|
}
|
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
func newInfo(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}
|
2015-05-21 14:04:49 -04:00
|
|
|
if err := netlink.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}
|
|
|
|
|
|
|
|
if err := netlink.LinkAdd(veth); err != nil {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
origns, err := netns.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not get the current netns: %v", err)
|
|
|
|
}
|
|
|
|
defer origns.Close()
|
|
|
|
|
|
|
|
f, err := os.OpenFile(s.Key(), os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed top open network namespace path %q: %v", s.Key(), err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
nsFD := f.Fd()
|
|
|
|
if err = netns.Set(netns.NsHandle(nsFD)); err != nil {
|
|
|
|
t.Fatalf("Setting to the namespace pointed to by the sandbox %s failed: %v", s.Key(), err)
|
|
|
|
}
|
2015-04-23 20:37:19 -04:00
|
|
|
defer netns.Set(origns)
|
2015-04-13 14:40:42 -04:00
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
for _, suffix := range ifaceSuffixes {
|
|
|
|
_, err = netlink.LinkByName(sboxIfaceName + suffix)
|
|
|
|
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"
|
|
|
|
|
|
|
|
i := &InterfaceStatistics{}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|