parsers_test.go 13 KB

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