parsers_test.go 8.8 KB

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