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"
|
2019-08-15 16:39:14 -04:00
|
|
|
"runtime"
|
2016-10-13 14:14:39 -04:00
|
|
|
"strings"
|
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
|
|
|
|
2021-05-25 19:48:54 -04:00
|
|
|
"github.com/docker/docker/libnetwork/ns"
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/testutils"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2021-05-25 19:48:54 -04:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
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
|
|
|
|
2018-06-19 20:19:16 -04:00
|
|
|
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")
|
|
|
|
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 {
|
2017-05-21 22:25:52 -04:00
|
|
|
t.Fatalf("The sandbox interface returned is not of type networkNamespace")
|
2015-04-13 14:40:42 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2022-06-12 18:06:40 -04:00
|
|
|
defer nh.Close()
|
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 {
|
2021-05-27 20:15:56 -04:00
|
|
|
time.Sleep(gpmCleanupPeriod * 2)
|
2015-06-05 14:46:33 -04:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
|
2016-02-23 16:28:14 -05:00
|
|
|
key, err := newKey(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := NewSandbox(key, true, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
2019-08-15 16:39:14 -04:00
|
|
|
runtime.LockOSThread()
|
2021-05-27 20:15:56 -04:00
|
|
|
defer destroyTest(t, s)
|
2016-02-23 16:28:14 -05:00
|
|
|
|
|
|
|
n, ok := s.(*networkNamespace)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal(ok)
|
|
|
|
}
|
|
|
|
nlh := n.nlHandle
|
|
|
|
|
2016-01-05 14:35:53 -05:00
|
|
|
ipv6, _ := types.ParseCIDR("2001:db8::44/64")
|
2016-02-23 16:28:14 -05:00
|
|
|
iface := &nwIface{addressIPv6: ipv6, ns: n, dstName: "sideA"}
|
2016-01-05 14:35:53 -05:00
|
|
|
|
|
|
|
veth := &netlink.Veth{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: "sideA"},
|
|
|
|
PeerName: "sideB",
|
|
|
|
}
|
2016-02-23 16:28:14 -05:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-10-13 14:14:39 -04:00
|
|
|
|
2021-05-27 20:15:56 -04:00
|
|
|
func destroyTest(t *testing.T, s Sandbox) {
|
|
|
|
if err := s.Destroy(); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 14:14:39 -04:00
|
|
|
func TestSetInterfaceIP(t *testing.T) {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
|
2016-02-23 16:28:14 -05:00
|
|
|
key, err := newKey(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
|
|
}
|
2016-10-13 14:14:39 -04:00
|
|
|
|
2016-02-23 16:28:14 -05:00
|
|
|
s, err := NewSandbox(key, true, false)
|
2016-10-13 14:14:39 -04:00
|
|
|
if err != nil {
|
2016-02-23 16:28:14 -05:00
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
2016-10-13 14:14:39 -04:00
|
|
|
}
|
2019-08-15 16:39:14 -04:00
|
|
|
runtime.LockOSThread()
|
2021-05-27 20:15:56 -04:00
|
|
|
defer destroyTest(t, s)
|
2016-02-23 16:28:14 -05:00
|
|
|
|
|
|
|
n, ok := s.(*networkNamespace)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal(ok)
|
|
|
|
}
|
|
|
|
nlh := n.nlHandle
|
|
|
|
|
|
|
|
ipv4, _ := types.ParseCIDR("172.30.0.33/24")
|
|
|
|
ipv6, _ := types.ParseCIDR("2001:db8::44/64")
|
|
|
|
iface := &nwIface{address: ipv4, addressIPv6: ipv6, ns: n, dstName: "sideA"}
|
2016-10-13 14:14:39 -04:00
|
|
|
|
|
|
|
if err := nlh.LinkAdd(&netlink.Veth{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: "sideA"},
|
|
|
|
PeerName: "sideB",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
linkA, err := nlh.LinkByName("sideA")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
linkB, err := nlh.LinkByName("sideB")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nlh.LinkSetUp(linkA); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nlh.LinkSetUp(linkB); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setInterfaceIP(nlh, linkA, iface); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setInterfaceIPv6(nlh, linkA, iface); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = setInterfaceIP(nlh, linkB, iface)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected route conflict error, but succeeded")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "conflicts with existing route") {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = setInterfaceIPv6(nlh, linkB, iface)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected route conflict error, but succeeded")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "conflicts with existing route") {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2017-12-27 23:32:29 -05:00
|
|
|
|
|
|
|
func TestLiveRestore(t *testing.T) {
|
|
|
|
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
|
|
|
|
key, err := newKey(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := NewSandbox(key, true, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
2019-08-15 16:39:14 -04:00
|
|
|
runtime.LockOSThread()
|
2021-05-27 20:15:56 -04:00
|
|
|
defer destroyTest(t, s)
|
2017-12-27 23:32:29 -05:00
|
|
|
|
|
|
|
n, ok := s.(*networkNamespace)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal(ok)
|
|
|
|
}
|
|
|
|
nlh := n.nlHandle
|
|
|
|
|
|
|
|
ipv4, _ := types.ParseCIDR("172.30.0.33/24")
|
|
|
|
ipv6, _ := types.ParseCIDR("2001:db8::44/64")
|
|
|
|
iface := &nwIface{address: ipv4, addressIPv6: ipv6, ns: n, dstName: "sideA"}
|
|
|
|
|
|
|
|
if err := nlh.LinkAdd(&netlink.Veth{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: "sideA"},
|
|
|
|
PeerName: "sideB",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
linkA, err := nlh.LinkByName("sideA")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
linkB, err := nlh.LinkByName("sideB")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nlh.LinkSetUp(linkA); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nlh.LinkSetUp(linkB); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setInterfaceIP(nlh, linkA, iface); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setInterfaceIPv6(nlh, linkA, iface); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = setInterfaceIP(nlh, linkB, iface)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected route conflict error, but succeeded")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "conflicts with existing route") {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = setInterfaceIPv6(nlh, linkB, iface)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected route conflict error, but succeeded")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "conflicts with existing route") {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create newsandbox with Restore - TRUE
|
|
|
|
s, err = NewSandbox(key, true, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
2021-05-27 20:15:56 -04:00
|
|
|
defer destroyTest(t, s)
|
2017-12-27 23:32:29 -05:00
|
|
|
|
|
|
|
// Check if the IPV4 & IPV6 entry present
|
|
|
|
// If present , we should get error in below call
|
|
|
|
// It shows us , we don't delete any config in live-restore case
|
|
|
|
if err := setInterfaceIPv6(nlh, linkA, iface); err == nil {
|
|
|
|
t.Fatalf("Expected route conflict error, but succeeded for IPV6 ")
|
|
|
|
}
|
|
|
|
if err := setInterfaceIP(nlh, linkA, iface); err == nil {
|
|
|
|
t.Fatalf("Expected route conflict error, but succeeded for IPV4 ")
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 19:48:54 -04:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSandboxCreate(t *testing.T) {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
|
|
|
|
key, err := newKey(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := NewSandbox(key, true, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Key() != key {
|
|
|
|
t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
|
|
|
|
}
|
|
|
|
|
|
|
|
tbox, err := newInfo(ns.NlHandle(), t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to generate new sandbox info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range tbox.Info().Interfaces() {
|
|
|
|
err = s.AddInterface(i.SrcName(), i.DstName(),
|
|
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
|
|
|
tbox.InterfaceOptions().Address(i.Address()),
|
|
|
|
tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6()))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to add interfaces to sandbox: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.SetGateway(tbox.Info().Gateway())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to set gateway to sandbox: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.SetGatewayIPv6(tbox.Info().GatewayIPv6())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to set ipv6 gateway to sandbox: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
verifySandbox(t, s, []string{"0", "1", "2"})
|
|
|
|
|
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
verifyCleanup(t, s, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSandboxCreateTwice(t *testing.T) {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
|
|
|
|
key, err := newKey(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = NewSandbox(key, true, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
|
|
|
// Create another sandbox with the same key to see if we handle it
|
|
|
|
// gracefully.
|
|
|
|
s, err := NewSandbox(key, true, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
GC()
|
|
|
|
verifyCleanup(t, s, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSandboxGC(t *testing.T) {
|
|
|
|
key, err := newKey(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := NewSandbox(key, true, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
GC()
|
|
|
|
verifyCleanup(t, s, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddRemoveInterface(t *testing.T) {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
|
|
|
|
key, err := newKey(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := NewSandbox(key, true, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
|
|
|
if s.Key() != key {
|
|
|
|
t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
|
|
|
|
}
|
|
|
|
|
|
|
|
tbox, err := newInfo(ns.NlHandle(), t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to generate new sandbox info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range tbox.Info().Interfaces() {
|
|
|
|
err = s.AddInterface(i.SrcName(), i.DstName(),
|
|
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
|
|
|
tbox.InterfaceOptions().Address(i.Address()),
|
|
|
|
tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6()))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to add interfaces to sandbox: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verifySandbox(t, s, []string{"0", "1", "2"})
|
|
|
|
|
|
|
|
interfaces := s.Info().Interfaces()
|
|
|
|
if err := interfaces[0].Remove(); err != nil {
|
|
|
|
t.Fatalf("Failed to remove interfaces from sandbox: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
verifySandbox(t, s, []string{"1", "2"})
|
|
|
|
|
|
|
|
i := tbox.Info().Interfaces()[0]
|
|
|
|
if err := s.AddInterface(i.SrcName(), i.DstName(),
|
|
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
|
|
|
tbox.InterfaceOptions().Address(i.Address()),
|
|
|
|
tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6())); err != nil {
|
|
|
|
t.Fatalf("Failed to add interfaces to sandbox: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
verifySandbox(t, s, []string{"1", "2", "3"})
|
|
|
|
|
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
GC()
|
|
|
|
verifyCleanup(t, s, false)
|
|
|
|
}
|