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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2015-04-23 20:37:19 -04:00
|
|
|
func newInfo(t *testing.T) (*Info, error) {
|
|
|
|
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-05-21 14:04:49 -04:00
|
|
|
intf1 := &Interface{}
|
|
|
|
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-05-21 14:04:49 -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-05-21 14:04:49 -04:00
|
|
|
intf1.AddressIPv6 = addrv6
|
|
|
|
intf1.AddressIPv6.IP = ip6
|
|
|
|
|
|
|
|
veth = &netlink.Veth{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: vethName3, TxQLen: 0},
|
|
|
|
PeerName: vethName4}
|
|
|
|
|
|
|
|
if err := netlink.LinkAdd(veth); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
intf2 := &Interface{}
|
|
|
|
intf2.SrcName = vethName4
|
|
|
|
intf2.DstName = sboxIfaceName
|
|
|
|
|
|
|
|
ip4, addr, err = net.ParseCIDR("192.168.2.100/24")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
intf2.Address = addr
|
|
|
|
intf2.Address.IP = ip4
|
2015-04-23 20:37:19 -04:00
|
|
|
|
2015-05-21 14:04:49 -04:00
|
|
|
// ip6, addrv6, err := net.ParseCIDR("2001:DB8::ABCD/48")
|
|
|
|
ip6, addrv6, err = net.ParseCIDR("fe80::3/64")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
intf2.AddressIPv6 = addrv6
|
|
|
|
intf2.AddressIPv6.IP = ip6
|
|
|
|
|
|
|
|
sinfo := &Info{Interfaces: []*Interface{intf1, intf2}}
|
2015-04-23 20:37:19 -04:00
|
|
|
sinfo.Gateway = net.ParseIP("192.168.1.1")
|
2015-04-28 01:57:36 -04:00
|
|
|
// sinfo.GatewayIPv6 = net.ParseIP("2001:DB8::1")
|
|
|
|
sinfo.GatewayIPv6 = net.ParseIP("fe80::1")
|
2015-04-23 20:37:19 -04:00
|
|
|
|
|
|
|
return sinfo, nil
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
func verifySandbox(t *testing.T, s Sandbox) {
|
|
|
|
_, 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-05-21 14:04:49 -04:00
|
|
|
_, err = netlink.LinkByName(sboxIfaceName + "0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not find the interface %s inside the sandbox: %v", sboxIfaceName,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = netlink.LinkByName(sboxIfaceName + "1")
|
2015-04-23 20:37:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not find the interface %s inside the sandbox: %v", sboxIfaceName,
|
|
|
|
err)
|
|
|
|
}
|
2015-04-13 14:40:42 -04:00
|
|
|
}
|