2014-04-18 01:43:01 +04:00
|
|
|
package daemon
|
2013-11-14 10:08:08 +04:00
|
|
|
|
|
|
|
import (
|
2014-07-25 02:19:50 +04:00
|
|
|
"github.com/docker/docker/nat"
|
2013-11-14 10:08:08 +04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseNetworkOptsPrivateOnly(t *testing.T) {
|
2014-02-12 04:48:44 +04:00
|
|
|
ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100::80"})
|
2013-11-14 10:08:08 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(ports) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(ports))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(bindings) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(bindings))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
for k := range ports {
|
|
|
|
if k.Proto() != "tcp" {
|
|
|
|
t.Logf("Expected tcp got %s", k.Proto())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if k.Port() != "80" {
|
|
|
|
t.Logf("Expected 80 got %s", k.Port())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
b, exists := bindings[k]
|
|
|
|
if !exists {
|
|
|
|
t.Log("Binding does not exist")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(b) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(b))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
s := b[0]
|
|
|
|
if s.HostPort != "" {
|
|
|
|
t.Logf("Expected \"\" got %s", s.HostPort)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if s.HostIp != "192.168.1.100" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseNetworkOptsPublic(t *testing.T) {
|
2014-02-12 04:48:44 +04:00
|
|
|
ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100:8080:80"})
|
2013-11-14 10:08:08 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(ports) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(ports))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(bindings) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(bindings))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
for k := range ports {
|
|
|
|
if k.Proto() != "tcp" {
|
|
|
|
t.Logf("Expected tcp got %s", k.Proto())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if k.Port() != "80" {
|
|
|
|
t.Logf("Expected 80 got %s", k.Port())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
b, exists := bindings[k]
|
|
|
|
if !exists {
|
|
|
|
t.Log("Binding does not exist")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(b) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(b))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
s := b[0]
|
|
|
|
if s.HostPort != "8080" {
|
|
|
|
t.Logf("Expected 8080 got %s", s.HostPort)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if s.HostIp != "192.168.1.100" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-01 22:11:34 +04:00
|
|
|
func TestParseNetworkOptsPublicNoPort(t *testing.T) {
|
|
|
|
ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100"})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Logf("Expected error Invalid containerPort")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if ports != nil {
|
|
|
|
t.Logf("Expected nil got %s", ports)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if bindings != nil {
|
|
|
|
t.Logf("Expected nil got %s", bindings)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseNetworkOptsNegativePorts(t *testing.T) {
|
|
|
|
ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100:-1:-1"})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
t.Logf("%v", len(ports))
|
|
|
|
t.Logf("%v", bindings)
|
|
|
|
if len(ports) != 0 {
|
|
|
|
t.Logf("Expected nil got %s", len(ports))
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if len(bindings) != 0 {
|
|
|
|
t.Logf("Expected 0 got %s", len(bindings))
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 10:08:08 +04:00
|
|
|
func TestParseNetworkOptsUdp(t *testing.T) {
|
2014-02-12 04:48:44 +04:00
|
|
|
ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100::6000/udp"})
|
2013-11-14 10:08:08 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(ports) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(ports))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(bindings) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(bindings))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
for k := range ports {
|
|
|
|
if k.Proto() != "udp" {
|
|
|
|
t.Logf("Expected udp got %s", k.Proto())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if k.Port() != "6000" {
|
|
|
|
t.Logf("Expected 6000 got %s", k.Port())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
b, exists := bindings[k]
|
|
|
|
if !exists {
|
|
|
|
t.Log("Binding does not exist")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(b) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(b))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
s := b[0]
|
|
|
|
if s.HostPort != "" {
|
|
|
|
t.Logf("Expected \"\" got %s", s.HostPort)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if s.HostIp != "192.168.1.100" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFullName(t *testing.T) {
|
2014-03-08 06:42:29 +04:00
|
|
|
name, err := GetFullContainerName("testing")
|
2013-11-14 10:08:08 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if name != "/testing" {
|
|
|
|
t.Fatalf("Expected /testing got %s", name)
|
|
|
|
}
|
2014-03-08 06:42:29 +04:00
|
|
|
if _, err := GetFullContainerName(""); err == nil {
|
2013-11-14 10:08:08 +04:00
|
|
|
t.Fatal("Error should not be nil")
|
|
|
|
}
|
|
|
|
}
|
2014-09-12 21:45:07 +04:00
|
|
|
|
|
|
|
func TestValidContainerNames(t *testing.T) {
|
|
|
|
invalidNames := []string{"-rm", "&sdfsfd", "safd%sd"}
|
|
|
|
validNames := []string{"word-word", "word_word", "1weoid"}
|
|
|
|
|
|
|
|
for _, name := range invalidNames {
|
|
|
|
if validContainerNamePattern.MatchString(name) {
|
|
|
|
t.Fatalf("%q is not a valid container name and was returned as valid.", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range validNames {
|
|
|
|
if !validContainerNamePattern.MatchString(name) {
|
|
|
|
t.Fatalf("%q is a valid container name and was returned as invalid.", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|