github.com/jjyr/docker@v1.5.0-rc2/daemon/networkdriver/bridge/driver_test.go (about)

     1  package bridge
     2  
     3  import (
     4  	"net"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/daemon/networkdriver/portmapper"
     9  	"github.com/docker/docker/engine"
    10  	"github.com/docker/docker/pkg/iptables"
    11  )
    12  
    13  func init() {
    14  	// reset the new proxy command for mocking out the userland proxy in tests
    15  	portmapper.NewProxy = portmapper.NewMockProxyCommand
    16  }
    17  
    18  func findFreePort(t *testing.T) int {
    19  	l, err := net.Listen("tcp", ":0")
    20  	if err != nil {
    21  		t.Fatal("Failed to find a free port")
    22  	}
    23  	defer l.Close()
    24  
    25  	result, err := net.ResolveTCPAddr("tcp", l.Addr().String())
    26  	if err != nil {
    27  		t.Fatal("Failed to resolve address to identify free port")
    28  	}
    29  	return result.Port
    30  }
    31  
    32  func newPortAllocationJob(eng *engine.Engine, port int) (job *engine.Job) {
    33  	strPort := strconv.Itoa(port)
    34  
    35  	job = eng.Job("allocate_port", "container_id")
    36  	job.Setenv("HostIP", "127.0.0.1")
    37  	job.Setenv("HostPort", strPort)
    38  	job.Setenv("Proto", "tcp")
    39  	job.Setenv("ContainerPort", strPort)
    40  	return
    41  }
    42  
    43  func newPortAllocationJobWithInvalidHostIP(eng *engine.Engine, port int) (job *engine.Job) {
    44  	strPort := strconv.Itoa(port)
    45  
    46  	job = eng.Job("allocate_port", "container_id")
    47  	job.Setenv("HostIP", "localhost")
    48  	job.Setenv("HostPort", strPort)
    49  	job.Setenv("Proto", "tcp")
    50  	job.Setenv("ContainerPort", strPort)
    51  	return
    52  }
    53  
    54  func TestAllocatePortDetection(t *testing.T) {
    55  	eng := engine.New()
    56  	eng.Logging = false
    57  
    58  	freePort := findFreePort(t)
    59  
    60  	// Init driver
    61  	job := eng.Job("initdriver")
    62  	if res := InitDriver(job); res != engine.StatusOK {
    63  		t.Fatal("Failed to initialize network driver")
    64  	}
    65  
    66  	// Allocate interface
    67  	job = eng.Job("allocate_interface", "container_id")
    68  	if res := Allocate(job); res != engine.StatusOK {
    69  		t.Fatal("Failed to allocate network interface")
    70  	}
    71  
    72  	// Allocate same port twice, expect failure on second call
    73  	job = newPortAllocationJob(eng, freePort)
    74  	if res := AllocatePort(job); res != engine.StatusOK {
    75  		t.Fatal("Failed to find a free port to allocate")
    76  	}
    77  	if res := AllocatePort(job); res == engine.StatusOK {
    78  		t.Fatal("Duplicate port allocation granted by AllocatePort")
    79  	}
    80  }
    81  
    82  func TestHostnameFormatChecking(t *testing.T) {
    83  	eng := engine.New()
    84  	eng.Logging = false
    85  
    86  	freePort := findFreePort(t)
    87  
    88  	// Init driver
    89  	job := eng.Job("initdriver")
    90  	if res := InitDriver(job); res != engine.StatusOK {
    91  		t.Fatal("Failed to initialize network driver")
    92  	}
    93  
    94  	// Allocate interface
    95  	job = eng.Job("allocate_interface", "container_id")
    96  	if res := Allocate(job); res != engine.StatusOK {
    97  		t.Fatal("Failed to allocate network interface")
    98  	}
    99  
   100  	// Allocate port with invalid HostIP, expect failure with Bad Request http status
   101  	job = newPortAllocationJobWithInvalidHostIP(eng, freePort)
   102  	if res := AllocatePort(job); res == engine.StatusOK {
   103  		t.Fatal("Failed to check invalid HostIP")
   104  	}
   105  }
   106  
   107  func TestMacAddrGeneration(t *testing.T) {
   108  	ip := net.ParseIP("192.168.0.1")
   109  	mac := generateMacAddr(ip).String()
   110  
   111  	// Should be consistent.
   112  	if generateMacAddr(ip).String() != mac {
   113  		t.Fatal("Inconsistent MAC address")
   114  	}
   115  
   116  	// Should be unique.
   117  	ip2 := net.ParseIP("192.168.0.2")
   118  	if generateMacAddr(ip2).String() == mac {
   119  		t.Fatal("Non-unique MAC address")
   120  	}
   121  }
   122  
   123  func TestLinkContainers(t *testing.T) {
   124  	eng := engine.New()
   125  	eng.Logging = false
   126  
   127  	// Init driver
   128  	job := eng.Job("initdriver")
   129  	if res := InitDriver(job); res != engine.StatusOK {
   130  		t.Fatal("Failed to initialize network driver")
   131  	}
   132  
   133  	// Allocate interface
   134  	job = eng.Job("allocate_interface", "container_id")
   135  	if res := Allocate(job); res != engine.StatusOK {
   136  		t.Fatal("Failed to allocate network interface")
   137  	}
   138  
   139  	job.Args[0] = "-I"
   140  
   141  	job.Setenv("ChildIP", "172.17.0.2")
   142  	job.Setenv("ParentIP", "172.17.0.1")
   143  	job.SetenvBool("IgnoreErrors", false)
   144  	job.SetenvList("Ports", []string{"1234"})
   145  
   146  	bridgeIface = "lo"
   147  	_, err := iptables.NewChain("DOCKER", bridgeIface, iptables.Filter)
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  
   152  	if res := LinkContainers(job); res != engine.StatusOK {
   153  		t.Fatalf("LinkContainers failed")
   154  	}
   155  
   156  	// flush rules
   157  	if _, err = iptables.Raw([]string{"-F", "DOCKER"}...); err != nil {
   158  		t.Fatal(err)
   159  	}
   160  
   161  }