github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/daemon/container_unit_test.go (about) 1 package daemon 2 3 import ( 4 "github.com/docker/docker/nat" 5 "testing" 6 ) 7 8 func TestParseNetworkOptsPrivateOnly(t *testing.T) { 9 ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100::80"}) 10 if err != nil { 11 t.Fatal(err) 12 } 13 if len(ports) != 1 { 14 t.Logf("Expected 1 got %d", len(ports)) 15 t.FailNow() 16 } 17 if len(bindings) != 1 { 18 t.Logf("Expected 1 got %d", len(bindings)) 19 t.FailNow() 20 } 21 for k := range ports { 22 if k.Proto() != "tcp" { 23 t.Logf("Expected tcp got %s", k.Proto()) 24 t.Fail() 25 } 26 if k.Port() != "80" { 27 t.Logf("Expected 80 got %s", k.Port()) 28 t.Fail() 29 } 30 b, exists := bindings[k] 31 if !exists { 32 t.Log("Binding does not exist") 33 t.FailNow() 34 } 35 if len(b) != 1 { 36 t.Logf("Expected 1 got %d", len(b)) 37 t.FailNow() 38 } 39 s := b[0] 40 if s.HostPort != "" { 41 t.Logf("Expected \"\" got %s", s.HostPort) 42 t.Fail() 43 } 44 if s.HostIp != "192.168.1.100" { 45 t.Fail() 46 } 47 } 48 } 49 50 func TestParseNetworkOptsPublic(t *testing.T) { 51 ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100:8080:80"}) 52 if err != nil { 53 t.Fatal(err) 54 } 55 if len(ports) != 1 { 56 t.Logf("Expected 1 got %d", len(ports)) 57 t.FailNow() 58 } 59 if len(bindings) != 1 { 60 t.Logf("Expected 1 got %d", len(bindings)) 61 t.FailNow() 62 } 63 for k := range ports { 64 if k.Proto() != "tcp" { 65 t.Logf("Expected tcp got %s", k.Proto()) 66 t.Fail() 67 } 68 if k.Port() != "80" { 69 t.Logf("Expected 80 got %s", k.Port()) 70 t.Fail() 71 } 72 b, exists := bindings[k] 73 if !exists { 74 t.Log("Binding does not exist") 75 t.FailNow() 76 } 77 if len(b) != 1 { 78 t.Logf("Expected 1 got %d", len(b)) 79 t.FailNow() 80 } 81 s := b[0] 82 if s.HostPort != "8080" { 83 t.Logf("Expected 8080 got %s", s.HostPort) 84 t.Fail() 85 } 86 if s.HostIp != "192.168.1.100" { 87 t.Fail() 88 } 89 } 90 } 91 92 func TestParseNetworkOptsPublicNoPort(t *testing.T) { 93 ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100"}) 94 95 if err == nil { 96 t.Logf("Expected error Invalid containerPort") 97 t.Fail() 98 } 99 if ports != nil { 100 t.Logf("Expected nil got %s", ports) 101 t.Fail() 102 } 103 if bindings != nil { 104 t.Logf("Expected nil got %s", bindings) 105 t.Fail() 106 } 107 } 108 109 func TestParseNetworkOptsNegativePorts(t *testing.T) { 110 ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100:-1:-1"}) 111 112 if err == nil { 113 t.Fail() 114 } 115 t.Logf("%v", len(ports)) 116 t.Logf("%v", bindings) 117 if len(ports) != 0 { 118 t.Logf("Expected nil got %s", len(ports)) 119 t.Fail() 120 } 121 if len(bindings) != 0 { 122 t.Logf("Expected 0 got %s", len(bindings)) 123 t.Fail() 124 } 125 } 126 127 func TestParseNetworkOptsUdp(t *testing.T) { 128 ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100::6000/udp"}) 129 if err != nil { 130 t.Fatal(err) 131 } 132 if len(ports) != 1 { 133 t.Logf("Expected 1 got %d", len(ports)) 134 t.FailNow() 135 } 136 if len(bindings) != 1 { 137 t.Logf("Expected 1 got %d", len(bindings)) 138 t.FailNow() 139 } 140 for k := range ports { 141 if k.Proto() != "udp" { 142 t.Logf("Expected udp got %s", k.Proto()) 143 t.Fail() 144 } 145 if k.Port() != "6000" { 146 t.Logf("Expected 6000 got %s", k.Port()) 147 t.Fail() 148 } 149 b, exists := bindings[k] 150 if !exists { 151 t.Log("Binding does not exist") 152 t.FailNow() 153 } 154 if len(b) != 1 { 155 t.Logf("Expected 1 got %d", len(b)) 156 t.FailNow() 157 } 158 s := b[0] 159 if s.HostPort != "" { 160 t.Logf("Expected \"\" got %s", s.HostPort) 161 t.Fail() 162 } 163 if s.HostIp != "192.168.1.100" { 164 t.Fail() 165 } 166 } 167 } 168 169 func TestGetFullName(t *testing.T) { 170 name, err := GetFullContainerName("testing") 171 if err != nil { 172 t.Fatal(err) 173 } 174 if name != "/testing" { 175 t.Fatalf("Expected /testing got %s", name) 176 } 177 if _, err := GetFullContainerName(""); err == nil { 178 t.Fatal("Error should not be nil") 179 } 180 } 181 182 func TestValidContainerNames(t *testing.T) { 183 invalidNames := []string{"-rm", "&sdfsfd", "safd%sd"} 184 validNames := []string{"word-word", "word_word", "1weoid"} 185 186 for _, name := range invalidNames { 187 if validContainerNamePattern.MatchString(name) { 188 t.Fatalf("%q is not a valid container name and was returned as valid.", name) 189 } 190 } 191 192 for _, name := range validNames { 193 if !validContainerNamePattern.MatchString(name) { 194 t.Fatalf("%q is a valid container name and was returned as invalid.", name) 195 } 196 } 197 }