Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <github@hollensbe.org> (github: erikh)
| 75 | 79 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,189 @@ |
| 0 |
+package nat |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "testing" |
|
| 4 |
+) |
|
| 5 |
+ |
|
| 6 |
+func TestParsePort(t *testing.T) {
|
|
| 7 |
+ var ( |
|
| 8 |
+ p int |
|
| 9 |
+ err error |
|
| 10 |
+ ) |
|
| 11 |
+ |
|
| 12 |
+ p, err = ParsePort("1234")
|
|
| 13 |
+ |
|
| 14 |
+ if err != nil || p != 1234 {
|
|
| 15 |
+ t.Fatal("Parsing '1234' did not succeed")
|
|
| 16 |
+ } |
|
| 17 |
+ |
|
| 18 |
+ // FIXME currently this is a valid port. I don't think it should be. |
|
| 19 |
+ // I'm leaving this test commented out until we make a decision. |
|
| 20 |
+ // - erikh |
|
| 21 |
+ |
|
| 22 |
+ /* |
|
| 23 |
+ p, err = ParsePort("0123")
|
|
| 24 |
+ |
|
| 25 |
+ if err != nil {
|
|
| 26 |
+ t.Fatal("Successfully parsed port '0123' to '123'")
|
|
| 27 |
+ } |
|
| 28 |
+ */ |
|
| 29 |
+ |
|
| 30 |
+ p, err = ParsePort("asdf")
|
|
| 31 |
+ |
|
| 32 |
+ if err == nil || p != 0 {
|
|
| 33 |
+ t.Fatal("Parsing port 'asdf' succeeded")
|
|
| 34 |
+ } |
|
| 35 |
+ |
|
| 36 |
+ p, err = ParsePort("1asdf")
|
|
| 37 |
+ |
|
| 38 |
+ if err == nil || p != 0 {
|
|
| 39 |
+ t.Fatal("Parsing port '1asdf' succeeded")
|
|
| 40 |
+ } |
|
| 41 |
+} |
|
| 42 |
+ |
|
| 43 |
+func TestPort(t *testing.T) {
|
|
| 44 |
+ p := NewPort("tcp", "1234")
|
|
| 45 |
+ |
|
| 46 |
+ if string(p) != "1234/tcp" {
|
|
| 47 |
+ t.Fatal("tcp, 1234 did not result in the string 1234/tcp")
|
|
| 48 |
+ } |
|
| 49 |
+ |
|
| 50 |
+ if p.Proto() != "tcp" {
|
|
| 51 |
+ t.Fatal("protocol was not tcp")
|
|
| 52 |
+ } |
|
| 53 |
+ |
|
| 54 |
+ if p.Port() != "1234" {
|
|
| 55 |
+ t.Fatal("port string value was not 1234")
|
|
| 56 |
+ } |
|
| 57 |
+ |
|
| 58 |
+ if p.Int() != 1234 {
|
|
| 59 |
+ t.Fatal("port int value was not 1234")
|
|
| 60 |
+ } |
|
| 61 |
+} |
|
| 62 |
+ |
|
| 63 |
+func TestSplitProtoPort(t *testing.T) {
|
|
| 64 |
+ var ( |
|
| 65 |
+ proto string |
|
| 66 |
+ port string |
|
| 67 |
+ ) |
|
| 68 |
+ |
|
| 69 |
+ proto, port = SplitProtoPort("1234/tcp")
|
|
| 70 |
+ |
|
| 71 |
+ if proto != "tcp" || port != "1234" {
|
|
| 72 |
+ t.Fatal("Could not split 1234/tcp properly")
|
|
| 73 |
+ } |
|
| 74 |
+ |
|
| 75 |
+ proto, port = SplitProtoPort("")
|
|
| 76 |
+ |
|
| 77 |
+ if proto != "" || port != "" {
|
|
| 78 |
+ t.Fatal("parsing an empty string yielded surprising results")
|
|
| 79 |
+ } |
|
| 80 |
+ |
|
| 81 |
+ proto, port = SplitProtoPort("1234")
|
|
| 82 |
+ |
|
| 83 |
+ if proto != "tcp" || port != "1234" {
|
|
| 84 |
+ t.Fatal("tcp is not the default protocol for portspec '1234'")
|
|
| 85 |
+ } |
|
| 86 |
+} |
|
| 87 |
+ |
|
| 88 |
+func TestParsePortSpecs(t *testing.T) {
|
|
| 89 |
+ var ( |
|
| 90 |
+ portMap map[Port]struct{}
|
|
| 91 |
+ bindingMap map[Port][]PortBinding |
|
| 92 |
+ err error |
|
| 93 |
+ ) |
|
| 94 |
+ |
|
| 95 |
+ portMap, bindingMap, err = ParsePortSpecs([]string{"1234/tcp", "2345/udp"})
|
|
| 96 |
+ |
|
| 97 |
+ if err != nil {
|
|
| 98 |
+ t.Fatalf("Error while processing ParsePortSpecs: %s", err.Error())
|
|
| 99 |
+ } |
|
| 100 |
+ |
|
| 101 |
+ if _, ok := portMap[Port("1234/tcp")]; !ok {
|
|
| 102 |
+ t.Fatal("1234/tcp was not parsed properly")
|
|
| 103 |
+ } |
|
| 104 |
+ |
|
| 105 |
+ if _, ok := portMap[Port("2345/udp")]; !ok {
|
|
| 106 |
+ t.Fatal("2345/udp was not parsed properly")
|
|
| 107 |
+ } |
|
| 108 |
+ |
|
| 109 |
+ for portspec, bindings := range bindingMap {
|
|
| 110 |
+ if len(bindings) != 1 {
|
|
| 111 |
+ t.Fatalf("%s should have exactly one binding", portspec)
|
|
| 112 |
+ } |
|
| 113 |
+ |
|
| 114 |
+ if bindings[0].HostIp != "" {
|
|
| 115 |
+ t.Fatalf("HostIp should not be set for %s", portspec)
|
|
| 116 |
+ } |
|
| 117 |
+ |
|
| 118 |
+ if bindings[0].HostPort != "" {
|
|
| 119 |
+ t.Fatalf("HostPort should not be set for %s", portspec)
|
|
| 120 |
+ } |
|
| 121 |
+ } |
|
| 122 |
+ |
|
| 123 |
+ portMap, bindingMap, err = ParsePortSpecs([]string{"1234:1234/tcp", "2345:2345/udp"})
|
|
| 124 |
+ |
|
| 125 |
+ if err != nil {
|
|
| 126 |
+ t.Fatalf("Error while processing ParsePortSpecs: %s", err.Error())
|
|
| 127 |
+ } |
|
| 128 |
+ |
|
| 129 |
+ if _, ok := portMap[Port("1234/tcp")]; !ok {
|
|
| 130 |
+ t.Fatal("1234/tcp was not parsed properly")
|
|
| 131 |
+ } |
|
| 132 |
+ |
|
| 133 |
+ if _, ok := portMap[Port("2345/udp")]; !ok {
|
|
| 134 |
+ t.Fatal("2345/udp was not parsed properly")
|
|
| 135 |
+ } |
|
| 136 |
+ |
|
| 137 |
+ for portspec, bindings := range bindingMap {
|
|
| 138 |
+ _, port := SplitProtoPort(string(portspec)) |
|
| 139 |
+ |
|
| 140 |
+ if len(bindings) != 1 {
|
|
| 141 |
+ t.Fatalf("%s should have exactly one binding", portspec)
|
|
| 142 |
+ } |
|
| 143 |
+ |
|
| 144 |
+ if bindings[0].HostIp != "" {
|
|
| 145 |
+ t.Fatalf("HostIp should not be set for %s", portspec)
|
|
| 146 |
+ } |
|
| 147 |
+ |
|
| 148 |
+ if bindings[0].HostPort != port {
|
|
| 149 |
+ t.Fatalf("HostPort should be %s for %s", port, portspec)
|
|
| 150 |
+ } |
|
| 151 |
+ } |
|
| 152 |
+ |
|
| 153 |
+ portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234:1234/tcp", "0.0.0.0:2345:2345/udp"})
|
|
| 154 |
+ |
|
| 155 |
+ if err != nil {
|
|
| 156 |
+ t.Fatalf("Error while processing ParsePortSpecs: %s", err.Error())
|
|
| 157 |
+ } |
|
| 158 |
+ |
|
| 159 |
+ if _, ok := portMap[Port("1234/tcp")]; !ok {
|
|
| 160 |
+ t.Fatal("1234/tcp was not parsed properly")
|
|
| 161 |
+ } |
|
| 162 |
+ |
|
| 163 |
+ if _, ok := portMap[Port("2345/udp")]; !ok {
|
|
| 164 |
+ t.Fatal("2345/udp was not parsed properly")
|
|
| 165 |
+ } |
|
| 166 |
+ |
|
| 167 |
+ for portspec, bindings := range bindingMap {
|
|
| 168 |
+ _, port := SplitProtoPort(string(portspec)) |
|
| 169 |
+ |
|
| 170 |
+ if len(bindings) != 1 {
|
|
| 171 |
+ t.Fatalf("%s should have exactly one binding", portspec)
|
|
| 172 |
+ } |
|
| 173 |
+ |
|
| 174 |
+ if bindings[0].HostIp != "0.0.0.0" {
|
|
| 175 |
+ t.Fatalf("HostIp is not 0.0.0.0 for %s", portspec)
|
|
| 176 |
+ } |
|
| 177 |
+ |
|
| 178 |
+ if bindings[0].HostPort != port {
|
|
| 179 |
+ t.Fatalf("HostPort should be %s for %s", port, portspec)
|
|
| 180 |
+ } |
|
| 181 |
+ } |
|
| 182 |
+ |
|
| 183 |
+ _, _, err = ParsePortSpecs([]string{"localhost:1234:1234/tcp"})
|
|
| 184 |
+ |
|
| 185 |
+ if err == nil {
|
|
| 186 |
+ t.Fatal("Received no error while trying to parse a hostname instead of ip")
|
|
| 187 |
+ } |
|
| 188 |
+} |