2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
2017-10-10 14:36:16 -04:00
|
|
|
"context"
|
2018-01-10 16:49:19 -05:00
|
|
|
"fmt"
|
2015-09-18 15:54:08 -04:00
|
|
|
"net"
|
2018-01-10 16:49:19 -05:00
|
|
|
"os"
|
2017-10-10 14:36:16 -04:00
|
|
|
"syscall"
|
2015-06-10 17:24:19 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-03-19 09:50:01 -04:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/discoverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
2021-05-27 20:15:56 -04:00
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
|
|
|
"github.com/docker/libkv/store/consul"
|
2017-10-10 14:36:16 -04:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
2015-06-10 17:24:19 -04:00
|
|
|
)
|
|
|
|
|
2016-03-30 17:42:58 -04:00
|
|
|
func init() {
|
|
|
|
consul.Register()
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
type driverTester struct {
|
|
|
|
t *testing.T
|
2015-09-18 17:00:36 -04:00
|
|
|
d *driver
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const testNetworkType = "overlay"
|
|
|
|
|
|
|
|
func setupDriver(t *testing.T) *driverTester {
|
2015-09-18 17:00:36 -04:00
|
|
|
dt := &driverTester{t: t}
|
2016-03-30 17:42:58 -04:00
|
|
|
config := make(map[string]interface{})
|
|
|
|
config[netlabel.GlobalKVClient] = discoverapi.DatastoreConfigData{
|
|
|
|
Scope: datastore.GlobalScope,
|
|
|
|
Provider: "consul",
|
|
|
|
Address: "127.0.0.01:8500",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Init(dt, config); err != nil {
|
2015-06-10 17:24:19 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
iface, err := net.InterfaceByName("eth0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil || len(addrs) == 0 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-28 14:54:03 -05:00
|
|
|
data := discoverapi.NodeDiscoveryData{
|
2015-09-18 15:54:08 -04:00
|
|
|
Address: addrs[0].String(),
|
|
|
|
Self: true,
|
|
|
|
}
|
2016-01-28 14:54:03 -05:00
|
|
|
dt.d.DiscoverNew(discoverapi.NodeDiscovery, data)
|
2015-06-10 17:24:19 -04:00
|
|
|
return dt
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupDriver(t *testing.T, dt *driverTester) {
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
Fini(dt.d)
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("test timed out because Fini() did not return on time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 14:58:10 -04:00
|
|
|
func (dt *driverTester) GetPluginGetter() plugingetter.PluginGetter {
|
2016-09-27 16:54:25 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
func (dt *driverTester) RegisterDriver(name string, drv driverapi.Driver,
|
|
|
|
cap driverapi.Capability) error {
|
|
|
|
if name != testNetworkType {
|
|
|
|
dt.t.Fatalf("Expected driver register name to be %q. Instead got %q",
|
|
|
|
testNetworkType, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := drv.(*driver); !ok {
|
|
|
|
dt.t.Fatalf("Expected driver type to be %T. Instead got %T",
|
|
|
|
&driver{}, drv)
|
|
|
|
}
|
|
|
|
|
|
|
|
dt.d = drv.(*driver)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverlayInit(t *testing.T) {
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := Init(&driverTester{t: t}, nil); err != nil {
|
2015-06-10 17:24:19 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverlayFiniWithoutConfig(t *testing.T) {
|
|
|
|
dt := &driverTester{t: t}
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := Init(dt, nil); err != nil {
|
2015-06-10 17:24:19 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupDriver(t, dt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverlayConfig(t *testing.T) {
|
|
|
|
dt := setupDriver(t)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
d := dt.d
|
2015-06-10 17:24:19 -04:00
|
|
|
if d.notifyCh == nil {
|
2017-05-21 22:25:52 -04:00
|
|
|
t.Fatal("Driver notify channel wasn't initialized after Config method")
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.exitCh == nil {
|
2017-05-21 22:25:52 -04:00
|
|
|
t.Fatal("Driver serfloop exit channel wasn't initialized after Config method")
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.serfInstance == nil {
|
|
|
|
t.Fatal("Driver serfinstance hasn't been initialized after Config method")
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupDriver(t, dt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverlayType(t *testing.T) {
|
|
|
|
dt := &driverTester{t: t}
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := Init(dt, nil); err != nil {
|
2015-06-10 17:24:19 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dt.d.Type() != testNetworkType {
|
|
|
|
t.Fatalf("Expected Type() to return %q. Instead got %q", testNetworkType,
|
|
|
|
dt.d.Type())
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 14:36:16 -04:00
|
|
|
|
|
|
|
// Test that the netlink socket close unblock the watchMiss to avoid deadlock
|
|
|
|
func TestNetlinkSocket(t *testing.T) {
|
|
|
|
// This is the same code used by the overlay driver to create the netlink interface
|
|
|
|
// for the watch miss
|
|
|
|
nlSock, err := nl.Subscribe(syscall.NETLINK_ROUTE, syscall.RTNLGRP_NEIGH)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
// set the receive timeout to not remain stuck on the RecvFrom if the fd gets closed
|
2018-03-19 09:50:01 -04:00
|
|
|
tv := unix.NsecToTimeval(soTimeout.Nanoseconds())
|
2017-10-10 14:36:16 -04:00
|
|
|
err = nlSock.SetReceiveTimeout(&tv)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
n := &network{id: "testnetid"}
|
|
|
|
ch := make(chan error)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
go func() {
|
2018-01-10 16:49:19 -05:00
|
|
|
n.watchMiss(nlSock, fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()))
|
2017-10-10 14:36:16 -04:00
|
|
|
ch <- nil
|
|
|
|
}()
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
nlSock.Close()
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
case <-ctx.Done():
|
|
|
|
{
|
|
|
|
t.Fatalf("Timeout expired")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|