parsers_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package parsers
  2. import (
  3. "runtime"
  4. "strings"
  5. "testing"
  6. )
  7. func TestParseDockerDaemonHost(t *testing.T) {
  8. var (
  9. defaultHTTPHost = "tcp://127.0.0.1:2376"
  10. defaultUnix = "/var/run/docker.sock"
  11. defaultHOST = "unix:///var/run/docker.sock"
  12. )
  13. if runtime.GOOS == "windows" {
  14. defaultHOST = defaultHTTPHost
  15. }
  16. invalids := map[string]string{
  17. "0.0.0.0": "Invalid bind address format: 0.0.0.0",
  18. "tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d",
  19. "tcp:a.b.c.d/path": "Invalid bind address format: tcp:a.b.c.d/path",
  20. "udp://127.0.0.1": "Invalid bind address format: udp://127.0.0.1",
  21. "udp://127.0.0.1:2375": "Invalid bind address format: udp://127.0.0.1:2375",
  22. "tcp://unix:///run/docker.sock": "Invalid bind address format: unix",
  23. "tcp": "Invalid bind address format: tcp",
  24. "unix": "Invalid bind address format: unix",
  25. "fd": "Invalid bind address format: fd",
  26. }
  27. valids := map[string]string{
  28. "0.0.0.1:": "tcp://0.0.0.1:2376",
  29. "0.0.0.1:5555": "tcp://0.0.0.1:5555",
  30. "0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
  31. ":6666": "tcp://127.0.0.1:6666",
  32. ":6666/path": "tcp://127.0.0.1:6666/path",
  33. "": defaultHOST,
  34. " ": defaultHOST,
  35. " ": defaultHOST,
  36. "tcp://": defaultHTTPHost,
  37. "tcp://:7777": "tcp://127.0.0.1:7777",
  38. "tcp://:7777/path": "tcp://127.0.0.1:7777/path",
  39. " tcp://:7777/path ": "tcp://127.0.0.1:7777/path",
  40. "unix:///run/docker.sock": "unix:///run/docker.sock",
  41. "unix://": "unix:///var/run/docker.sock",
  42. "fd://": "fd://",
  43. "fd://something": "fd://something",
  44. }
  45. for invalidAddr, expectedError := range invalids {
  46. if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError {
  47. t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
  48. }
  49. }
  50. for validAddr, expectedAddr := range valids {
  51. if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, validAddr); err != nil || addr != expectedAddr {
  52. t.Errorf("%v -> expected %v, got (%v) addr (%v)", validAddr, expectedAddr, err, addr)
  53. }
  54. }
  55. }
  56. func TestParseTCP(t *testing.T) {
  57. var (
  58. defaultHTTPHost = "tcp://127.0.0.1:2376"
  59. )
  60. invalids := map[string]string{
  61. "0.0.0.0": "Invalid bind address format: 0.0.0.0",
  62. "tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d",
  63. "tcp:a.b.c.d/path": "Invalid bind address format: tcp:a.b.c.d/path",
  64. "udp://127.0.0.1": "Invalid proto, expected tcp: udp://127.0.0.1",
  65. "udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375",
  66. }
  67. valids := map[string]string{
  68. "": defaultHTTPHost,
  69. "tcp://": defaultHTTPHost,
  70. "0.0.0.1:": "tcp://0.0.0.1:2376",
  71. "0.0.0.1:5555": "tcp://0.0.0.1:5555",
  72. "0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
  73. ":6666": "tcp://127.0.0.1:6666",
  74. ":6666/path": "tcp://127.0.0.1:6666/path",
  75. "tcp://:7777": "tcp://127.0.0.1:7777",
  76. "tcp://:7777/path": "tcp://127.0.0.1:7777/path",
  77. }
  78. for invalidAddr, expectedError := range invalids {
  79. if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError {
  80. t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
  81. }
  82. }
  83. for validAddr, expectedAddr := range valids {
  84. if addr, err := ParseTCPAddr(validAddr, defaultHTTPHost); err != nil || addr != expectedAddr {
  85. t.Errorf("%v -> expected %v, got %v and addr %v", validAddr, expectedAddr, err, addr)
  86. }
  87. }
  88. }
  89. func TestParseInvalidUnixAddrInvalid(t *testing.T) {
  90. if _, err := ParseUnixAddr("tcp://127.0.0.1", "unix:///var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
  91. t.Fatalf("Expected an error, got %v", err)
  92. }
  93. if _, err := ParseUnixAddr("unix://tcp://127.0.0.1", "/var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" {
  94. t.Fatalf("Expected an error, got %v", err)
  95. }
  96. if v, err := ParseUnixAddr("", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" {
  97. t.Fatalf("Expected an %v, got %v", v, "unix:///var/run/docker.sock")
  98. }
  99. }
  100. func TestParseRepositoryTag(t *testing.T) {
  101. if repo, tag := ParseRepositoryTag("root"); repo != "root" || tag != "" {
  102. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "root", "", repo, tag)
  103. }
  104. if repo, tag := ParseRepositoryTag("root:tag"); repo != "root" || tag != "tag" {
  105. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "root", "tag", repo, tag)
  106. }
  107. if repo, digest := ParseRepositoryTag("root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); repo != "root" || digest != "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
  108. t.Errorf("Expected repo: '%s' and digest: '%s', got '%s' and '%s'", "root", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", repo, digest)
  109. }
  110. if repo, tag := ParseRepositoryTag("user/repo"); repo != "user/repo" || tag != "" {
  111. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "user/repo", "", repo, tag)
  112. }
  113. if repo, tag := ParseRepositoryTag("user/repo:tag"); repo != "user/repo" || tag != "tag" {
  114. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "user/repo", "tag", repo, tag)
  115. }
  116. if repo, digest := ParseRepositoryTag("user/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); repo != "user/repo" || digest != "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
  117. t.Errorf("Expected repo: '%s' and digest: '%s', got '%s' and '%s'", "user/repo", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", repo, digest)
  118. }
  119. if repo, tag := ParseRepositoryTag("url:5000/repo"); repo != "url:5000/repo" || tag != "" {
  120. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "", repo, tag)
  121. }
  122. if repo, tag := ParseRepositoryTag("url:5000/repo:tag"); repo != "url:5000/repo" || tag != "tag" {
  123. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "tag", repo, tag)
  124. }
  125. if repo, digest := ParseRepositoryTag("url:5000/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); repo != "url:5000/repo" || digest != "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
  126. t.Errorf("Expected repo: '%s' and digest: '%s', got '%s' and '%s'", "url:5000/repo", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", repo, digest)
  127. }
  128. }
  129. func TestParsePortMapping(t *testing.T) {
  130. if _, err := PartParser("ip:public:private", "192.168.1.1:80"); err == nil {
  131. t.Fatalf("Expected an error, got %v", err)
  132. }
  133. data, err := PartParser("ip:public:private", "192.168.1.1:80:8080")
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. if len(data) != 3 {
  138. t.FailNow()
  139. }
  140. if data["ip"] != "192.168.1.1" {
  141. t.Fail()
  142. }
  143. if data["public"] != "80" {
  144. t.Fail()
  145. }
  146. if data["private"] != "8080" {
  147. t.Fail()
  148. }
  149. }
  150. func TestParseKeyValueOpt(t *testing.T) {
  151. invalids := map[string]string{
  152. "": "Unable to parse key/value option: ",
  153. "key": "Unable to parse key/value option: key",
  154. }
  155. for invalid, expectedError := range invalids {
  156. if _, _, err := ParseKeyValueOpt(invalid); err == nil || err.Error() != expectedError {
  157. t.Fatalf("Expected error %v for %v, got %v", expectedError, invalid, err)
  158. }
  159. }
  160. valids := map[string][]string{
  161. "key=value": {"key", "value"},
  162. " key = value ": {"key", "value"},
  163. "key=value1=value2": {"key", "value1=value2"},
  164. " key = value1 = value2 ": {"key", "value1 = value2"},
  165. }
  166. for valid, expectedKeyValue := range valids {
  167. key, value, err := ParseKeyValueOpt(valid)
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. if key != expectedKeyValue[0] || value != expectedKeyValue[1] {
  172. t.Fatalf("Expected {%v: %v} got {%v: %v}", expectedKeyValue[0], expectedKeyValue[1], key, value)
  173. }
  174. }
  175. }
  176. func TestParsePortRange(t *testing.T) {
  177. if start, end, err := ParsePortRange("8000-8080"); err != nil || start != 8000 || end != 8080 {
  178. t.Fatalf("Error: %s or Expecting {start,end} values {8000,8080} but found {%d,%d}.", err, start, end)
  179. }
  180. }
  181. func TestParsePortRangeEmpty(t *testing.T) {
  182. if _, _, err := ParsePortRange(""); err == nil || err.Error() != "Empty string specified for ports." {
  183. t.Fatalf("Expected error 'Empty string specified for ports.', got %v", err)
  184. }
  185. }
  186. func TestParsePortRangeWithNoRange(t *testing.T) {
  187. start, end, err := ParsePortRange("8080")
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. if start != 8080 || end != 8080 {
  192. t.Fatalf("Expected start and end to be the same and equal to 8080, but were %v and %v", start, end)
  193. }
  194. }
  195. func TestParsePortRangeIncorrectRange(t *testing.T) {
  196. if _, _, err := ParsePortRange("9000-8080"); err == nil || !strings.Contains(err.Error(), "Invalid range specified for the Port") {
  197. t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
  198. }
  199. }
  200. func TestParsePortRangeIncorrectEndRange(t *testing.T) {
  201. if _, _, err := ParsePortRange("8000-a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
  202. t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
  203. }
  204. if _, _, err := ParsePortRange("8000-30a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
  205. t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
  206. }
  207. }
  208. func TestParsePortRangeIncorrectStartRange(t *testing.T) {
  209. if _, _, err := ParsePortRange("a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
  210. t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
  211. }
  212. if _, _, err := ParsePortRange("30a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
  213. t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
  214. }
  215. }
  216. func TestParseLink(t *testing.T) {
  217. name, alias, err := ParseLink("name:alias")
  218. if err != nil {
  219. t.Fatalf("Expected not to error out on a valid name:alias format but got: %v", err)
  220. }
  221. if name != "name" {
  222. t.Fatalf("Link name should have been name, got %s instead", name)
  223. }
  224. if alias != "alias" {
  225. t.Fatalf("Link alias should have been alias, got %s instead", alias)
  226. }
  227. // short format definition
  228. name, alias, err = ParseLink("name")
  229. if err != nil {
  230. t.Fatalf("Expected not to error out on a valid name only format but got: %v", err)
  231. }
  232. if name != "name" {
  233. t.Fatalf("Link name should have been name, got %s instead", name)
  234. }
  235. if alias != "name" {
  236. t.Fatalf("Link alias should have been name, got %s instead", alias)
  237. }
  238. // empty string link definition is not allowed
  239. if _, _, err := ParseLink(""); err == nil || !strings.Contains(err.Error(), "empty string specified for links") {
  240. t.Fatalf("Expected error 'empty string specified for links' but got: %v", err)
  241. }
  242. // more than two colons are not allowed
  243. if _, _, err := ParseLink("link:alias:wrong"); err == nil || !strings.Contains(err.Error(), "bad format for links: link:alias:wrong") {
  244. t.Fatalf("Expected error 'bad format for links: link:alias:wrong' but got: %v", err)
  245. }
  246. }