container_unit_test.go 4.1 KB

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