nat_test.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. package nat
  2. import (
  3. "testing"
  4. )
  5. func TestParsePort(t *testing.T) {
  6. var (
  7. p int
  8. err error
  9. )
  10. p, err = ParsePort("1234")
  11. if err != nil || p != 1234 {
  12. t.Fatal("Parsing '1234' did not succeed")
  13. }
  14. // FIXME currently this is a valid port. I don't think it should be.
  15. // I'm leaving this test commented out until we make a decision.
  16. // - erikh
  17. /*
  18. p, err = ParsePort("0123")
  19. if err != nil {
  20. t.Fatal("Successfully parsed port '0123' to '123'")
  21. }
  22. */
  23. p, err = ParsePort("asdf")
  24. if err == nil || p != 0 {
  25. t.Fatal("Parsing port 'asdf' succeeded")
  26. }
  27. p, err = ParsePort("1asdf")
  28. if err == nil || p != 0 {
  29. t.Fatal("Parsing port '1asdf' succeeded")
  30. }
  31. }
  32. func TestPort(t *testing.T) {
  33. p, err := NewPort("tcp", "1234")
  34. if err != nil {
  35. t.Fatalf("tcp, 1234 had a parsing issue: %v", err)
  36. }
  37. if string(p) != "1234/tcp" {
  38. t.Fatal("tcp, 1234 did not result in the string 1234/tcp")
  39. }
  40. if p.Proto() != "tcp" {
  41. t.Fatal("protocol was not tcp")
  42. }
  43. if p.Port() != "1234" {
  44. t.Fatal("port string value was not 1234")
  45. }
  46. if p.Int() != 1234 {
  47. t.Fatal("port int value was not 1234")
  48. }
  49. p, err = NewPort("tcp", "asd1234")
  50. if err == nil {
  51. t.Fatal("tcp, asd1234 was supposed to fail")
  52. }
  53. }
  54. func TestSplitProtoPort(t *testing.T) {
  55. var (
  56. proto string
  57. port string
  58. )
  59. proto, port = SplitProtoPort("1234/tcp")
  60. if proto != "tcp" || port != "1234" {
  61. t.Fatal("Could not split 1234/tcp properly")
  62. }
  63. proto, port = SplitProtoPort("")
  64. if proto != "" || port != "" {
  65. t.Fatal("parsing an empty string yielded surprising results", proto, port)
  66. }
  67. proto, port = SplitProtoPort("1234")
  68. if proto != "tcp" || port != "1234" {
  69. t.Fatal("tcp is not the default protocol for portspec '1234'", proto, port)
  70. }
  71. proto, port = SplitProtoPort("1234/")
  72. if proto != "tcp" || port != "1234" {
  73. t.Fatal("parsing '1234/' yielded:" + port + "/" + proto)
  74. }
  75. proto, port = SplitProtoPort("/tcp")
  76. if proto != "" || port != "" {
  77. t.Fatal("parsing '/tcp' yielded:" + port + "/" + proto)
  78. }
  79. }
  80. func TestParsePortSpecs(t *testing.T) {
  81. var (
  82. portMap map[Port]struct{}
  83. bindingMap map[Port][]PortBinding
  84. err error
  85. )
  86. portMap, bindingMap, err = ParsePortSpecs([]string{"1234/tcp", "2345/udp"})
  87. if err != nil {
  88. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  89. }
  90. if _, ok := portMap[Port("1234/tcp")]; !ok {
  91. t.Fatal("1234/tcp was not parsed properly")
  92. }
  93. if _, ok := portMap[Port("2345/udp")]; !ok {
  94. t.Fatal("2345/udp was not parsed properly")
  95. }
  96. for portspec, bindings := range bindingMap {
  97. if len(bindings) != 1 {
  98. t.Fatalf("%s should have exactly one binding", portspec)
  99. }
  100. if bindings[0].HostIP != "" {
  101. t.Fatalf("HostIP should not be set for %s", portspec)
  102. }
  103. if bindings[0].HostPort != "" {
  104. t.Fatalf("HostPort should not be set for %s", portspec)
  105. }
  106. }
  107. portMap, bindingMap, err = ParsePortSpecs([]string{"1234:1234/tcp", "2345:2345/udp"})
  108. if err != nil {
  109. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  110. }
  111. if _, ok := portMap[Port("1234/tcp")]; !ok {
  112. t.Fatal("1234/tcp was not parsed properly")
  113. }
  114. if _, ok := portMap[Port("2345/udp")]; !ok {
  115. t.Fatal("2345/udp was not parsed properly")
  116. }
  117. for portspec, bindings := range bindingMap {
  118. _, port := SplitProtoPort(string(portspec))
  119. if len(bindings) != 1 {
  120. t.Fatalf("%s should have exactly one binding", portspec)
  121. }
  122. if bindings[0].HostIP != "" {
  123. t.Fatalf("HostIP should not be set for %s", portspec)
  124. }
  125. if bindings[0].HostPort != port {
  126. t.Fatalf("HostPort should be %s for %s", port, portspec)
  127. }
  128. }
  129. portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234:1234/tcp", "0.0.0.0:2345:2345/udp"})
  130. if err != nil {
  131. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  132. }
  133. if _, ok := portMap[Port("1234/tcp")]; !ok {
  134. t.Fatal("1234/tcp was not parsed properly")
  135. }
  136. if _, ok := portMap[Port("2345/udp")]; !ok {
  137. t.Fatal("2345/udp was not parsed properly")
  138. }
  139. for portspec, bindings := range bindingMap {
  140. _, port := SplitProtoPort(string(portspec))
  141. if len(bindings) != 1 {
  142. t.Fatalf("%s should have exactly one binding", portspec)
  143. }
  144. if bindings[0].HostIP != "0.0.0.0" {
  145. t.Fatalf("HostIP is not 0.0.0.0 for %s", portspec)
  146. }
  147. if bindings[0].HostPort != port {
  148. t.Fatalf("HostPort should be %s for %s", port, portspec)
  149. }
  150. }
  151. _, _, err = ParsePortSpecs([]string{"localhost:1234:1234/tcp"})
  152. if err == nil {
  153. t.Fatal("Received no error while trying to parse a hostname instead of ip")
  154. }
  155. }
  156. func TestParsePortSpecsWithRange(t *testing.T) {
  157. var (
  158. portMap map[Port]struct{}
  159. bindingMap map[Port][]PortBinding
  160. err error
  161. )
  162. portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236/tcp", "2345-2347/udp"})
  163. if err != nil {
  164. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  165. }
  166. if _, ok := portMap[Port("1235/tcp")]; !ok {
  167. t.Fatal("1234/tcp was not parsed properly")
  168. }
  169. if _, ok := portMap[Port("2346/udp")]; !ok {
  170. t.Fatal("2345/udp was not parsed properly")
  171. }
  172. for portspec, bindings := range bindingMap {
  173. if len(bindings) != 1 {
  174. t.Fatalf("%s should have exactly one binding", portspec)
  175. }
  176. if bindings[0].HostIP != "" {
  177. t.Fatalf("HostIP should not be set for %s", portspec)
  178. }
  179. if bindings[0].HostPort != "" {
  180. t.Fatalf("HostPort should not be set for %s", portspec)
  181. }
  182. }
  183. portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236:1234-1236/tcp", "2345-2347:2345-2347/udp"})
  184. if err != nil {
  185. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  186. }
  187. if _, ok := portMap[Port("1235/tcp")]; !ok {
  188. t.Fatal("1234/tcp was not parsed properly")
  189. }
  190. if _, ok := portMap[Port("2346/udp")]; !ok {
  191. t.Fatal("2345/udp was not parsed properly")
  192. }
  193. for portspec, bindings := range bindingMap {
  194. _, port := SplitProtoPort(string(portspec))
  195. if len(bindings) != 1 {
  196. t.Fatalf("%s should have exactly one binding", portspec)
  197. }
  198. if bindings[0].HostIP != "" {
  199. t.Fatalf("HostIP should not be set for %s", portspec)
  200. }
  201. if bindings[0].HostPort != port {
  202. t.Fatalf("HostPort should be %s for %s", port, portspec)
  203. }
  204. }
  205. portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234-1236:1234-1236/tcp", "0.0.0.0:2345-2347:2345-2347/udp"})
  206. if err != nil {
  207. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  208. }
  209. if _, ok := portMap[Port("1235/tcp")]; !ok {
  210. t.Fatal("1234/tcp was not parsed properly")
  211. }
  212. if _, ok := portMap[Port("2346/udp")]; !ok {
  213. t.Fatal("2345/udp was not parsed properly")
  214. }
  215. for portspec, bindings := range bindingMap {
  216. _, port := SplitProtoPort(string(portspec))
  217. if len(bindings) != 1 || bindings[0].HostIP != "0.0.0.0" || bindings[0].HostPort != port {
  218. t.Fatalf("Expect single binding to port %s but found %s", port, bindings)
  219. }
  220. }
  221. _, _, err = ParsePortSpecs([]string{"localhost:1234-1236:1234-1236/tcp"})
  222. if err == nil {
  223. t.Fatal("Received no error while trying to parse a hostname instead of ip")
  224. }
  225. }
  226. func TestParseNetworkOptsPrivateOnly(t *testing.T) {
  227. ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100::80"})
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. if len(ports) != 1 {
  232. t.Logf("Expected 1 got %d", len(ports))
  233. t.FailNow()
  234. }
  235. if len(bindings) != 1 {
  236. t.Logf("Expected 1 got %d", len(bindings))
  237. t.FailNow()
  238. }
  239. for k := range ports {
  240. if k.Proto() != "tcp" {
  241. t.Logf("Expected tcp got %s", k.Proto())
  242. t.Fail()
  243. }
  244. if k.Port() != "80" {
  245. t.Logf("Expected 80 got %s", k.Port())
  246. t.Fail()
  247. }
  248. b, exists := bindings[k]
  249. if !exists {
  250. t.Log("Binding does not exist")
  251. t.FailNow()
  252. }
  253. if len(b) != 1 {
  254. t.Logf("Expected 1 got %d", len(b))
  255. t.FailNow()
  256. }
  257. s := b[0]
  258. if s.HostPort != "" {
  259. t.Logf("Expected \"\" got %s", s.HostPort)
  260. t.Fail()
  261. }
  262. if s.HostIP != "192.168.1.100" {
  263. t.Fail()
  264. }
  265. }
  266. }
  267. func TestParseNetworkOptsPublic(t *testing.T) {
  268. ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100:8080:80"})
  269. if err != nil {
  270. t.Fatal(err)
  271. }
  272. if len(ports) != 1 {
  273. t.Logf("Expected 1 got %d", len(ports))
  274. t.FailNow()
  275. }
  276. if len(bindings) != 1 {
  277. t.Logf("Expected 1 got %d", len(bindings))
  278. t.FailNow()
  279. }
  280. for k := range ports {
  281. if k.Proto() != "tcp" {
  282. t.Logf("Expected tcp got %s", k.Proto())
  283. t.Fail()
  284. }
  285. if k.Port() != "80" {
  286. t.Logf("Expected 80 got %s", k.Port())
  287. t.Fail()
  288. }
  289. b, exists := bindings[k]
  290. if !exists {
  291. t.Log("Binding does not exist")
  292. t.FailNow()
  293. }
  294. if len(b) != 1 {
  295. t.Logf("Expected 1 got %d", len(b))
  296. t.FailNow()
  297. }
  298. s := b[0]
  299. if s.HostPort != "8080" {
  300. t.Logf("Expected 8080 got %s", s.HostPort)
  301. t.Fail()
  302. }
  303. if s.HostIP != "192.168.1.100" {
  304. t.Fail()
  305. }
  306. }
  307. }
  308. func TestParseNetworkOptsPublicNoPort(t *testing.T) {
  309. ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100"})
  310. if err == nil {
  311. t.Logf("Expected error Invalid containerPort")
  312. t.Fail()
  313. }
  314. if ports != nil {
  315. t.Logf("Expected nil got %s", ports)
  316. t.Fail()
  317. }
  318. if bindings != nil {
  319. t.Logf("Expected nil got %s", bindings)
  320. t.Fail()
  321. }
  322. }
  323. func TestParseNetworkOptsNegativePorts(t *testing.T) {
  324. ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100:-1:-1"})
  325. if err == nil {
  326. t.Fail()
  327. }
  328. if len(ports) != 0 {
  329. t.Logf("Expected nil got %s", len(ports))
  330. t.Fail()
  331. }
  332. if len(bindings) != 0 {
  333. t.Logf("Expected 0 got %s", len(bindings))
  334. t.Fail()
  335. }
  336. }
  337. func TestParseNetworkOptsUdp(t *testing.T) {
  338. ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100::6000/udp"})
  339. if err != nil {
  340. t.Fatal(err)
  341. }
  342. if len(ports) != 1 {
  343. t.Logf("Expected 1 got %d", len(ports))
  344. t.FailNow()
  345. }
  346. if len(bindings) != 1 {
  347. t.Logf("Expected 1 got %d", len(bindings))
  348. t.FailNow()
  349. }
  350. for k := range ports {
  351. if k.Proto() != "udp" {
  352. t.Logf("Expected udp got %s", k.Proto())
  353. t.Fail()
  354. }
  355. if k.Port() != "6000" {
  356. t.Logf("Expected 6000 got %s", k.Port())
  357. t.Fail()
  358. }
  359. b, exists := bindings[k]
  360. if !exists {
  361. t.Log("Binding does not exist")
  362. t.FailNow()
  363. }
  364. if len(b) != 1 {
  365. t.Logf("Expected 1 got %d", len(b))
  366. t.FailNow()
  367. }
  368. s := b[0]
  369. if s.HostPort != "" {
  370. t.Logf("Expected \"\" got %s", s.HostPort)
  371. t.Fail()
  372. }
  373. if s.HostIP != "192.168.1.100" {
  374. t.Fail()
  375. }
  376. }
  377. }