nat_test.go 9.8 KB

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