config_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package registry
  2. import (
  3. "reflect"
  4. "sort"
  5. "strings"
  6. "testing"
  7. )
  8. func TestLoadAllowNondistributableArtifacts(t *testing.T) {
  9. testCases := []struct {
  10. registries []string
  11. cidrStrs []string
  12. hostnames []string
  13. err string
  14. }{
  15. {
  16. registries: []string{"1.2.3.0/24"},
  17. cidrStrs: []string{"1.2.3.0/24"},
  18. },
  19. {
  20. registries: []string{"2001:db8::/120"},
  21. cidrStrs: []string{"2001:db8::/120"},
  22. },
  23. {
  24. registries: []string{"127.0.0.1"},
  25. hostnames: []string{"127.0.0.1"},
  26. },
  27. {
  28. registries: []string{"127.0.0.1:8080"},
  29. hostnames: []string{"127.0.0.1:8080"},
  30. },
  31. {
  32. registries: []string{"2001:db8::1"},
  33. hostnames: []string{"2001:db8::1"},
  34. },
  35. {
  36. registries: []string{"[2001:db8::1]:80"},
  37. hostnames: []string{"[2001:db8::1]:80"},
  38. },
  39. {
  40. registries: []string{"[2001:db8::1]:80"},
  41. hostnames: []string{"[2001:db8::1]:80"},
  42. },
  43. {
  44. registries: []string{"1.2.3.0/24", "2001:db8::/120", "127.0.0.1", "127.0.0.1:8080"},
  45. cidrStrs: []string{"1.2.3.0/24", "2001:db8::/120"},
  46. hostnames: []string{"127.0.0.1", "127.0.0.1:8080"},
  47. },
  48. {
  49. registries: []string{"http://mytest.com"},
  50. err: "allow-nondistributable-artifacts registry http://mytest.com should not contain '://'",
  51. },
  52. {
  53. registries: []string{"https://mytest.com"},
  54. err: "allow-nondistributable-artifacts registry https://mytest.com should not contain '://'",
  55. },
  56. {
  57. registries: []string{"HTTP://mytest.com"},
  58. err: "allow-nondistributable-artifacts registry HTTP://mytest.com should not contain '://'",
  59. },
  60. {
  61. registries: []string{"svn://mytest.com"},
  62. err: "allow-nondistributable-artifacts registry svn://mytest.com should not contain '://'",
  63. },
  64. {
  65. registries: []string{"-invalid-registry"},
  66. err: "Cannot begin or end with a hyphen",
  67. },
  68. {
  69. registries: []string{`mytest-.com`},
  70. err: `allow-nondistributable-artifacts registry mytest-.com is not valid: invalid host "mytest-.com"`,
  71. },
  72. {
  73. registries: []string{`1200:0000:AB00:1234:0000:2552:7777:1313:8080`},
  74. err: `allow-nondistributable-artifacts registry 1200:0000:AB00:1234:0000:2552:7777:1313:8080 is not valid: invalid host "1200:0000:AB00:1234:0000:2552:7777:1313:8080"`,
  75. },
  76. {
  77. registries: []string{`mytest.com:500000`},
  78. err: `allow-nondistributable-artifacts registry mytest.com:500000 is not valid: invalid port "500000"`,
  79. },
  80. {
  81. registries: []string{`"mytest.com"`},
  82. err: `allow-nondistributable-artifacts registry "mytest.com" is not valid: invalid host "\"mytest.com\""`,
  83. },
  84. {
  85. registries: []string{`"mytest.com:5000"`},
  86. err: `allow-nondistributable-artifacts registry "mytest.com:5000" is not valid: invalid host "\"mytest.com"`,
  87. },
  88. }
  89. for _, testCase := range testCases {
  90. config := newServiceConfig(ServiceOptions{})
  91. err := config.LoadAllowNondistributableArtifacts(testCase.registries)
  92. if testCase.err == "" {
  93. if err != nil {
  94. t.Fatalf("expect no error, got '%s'", err)
  95. }
  96. cidrStrs := []string{}
  97. for _, c := range config.AllowNondistributableArtifactsCIDRs {
  98. cidrStrs = append(cidrStrs, c.String())
  99. }
  100. sort.Strings(testCase.cidrStrs)
  101. sort.Strings(cidrStrs)
  102. if (len(testCase.cidrStrs) > 0 || len(cidrStrs) > 0) && !reflect.DeepEqual(testCase.cidrStrs, cidrStrs) {
  103. t.Fatalf("expect AllowNondistributableArtifactsCIDRs to be '%+v', got '%+v'", testCase.cidrStrs, cidrStrs)
  104. }
  105. sort.Strings(testCase.hostnames)
  106. sort.Strings(config.AllowNondistributableArtifactsHostnames)
  107. if (len(testCase.hostnames) > 0 || len(config.AllowNondistributableArtifactsHostnames) > 0) && !reflect.DeepEqual(testCase.hostnames, config.AllowNondistributableArtifactsHostnames) {
  108. t.Fatalf("expect AllowNondistributableArtifactsHostnames to be '%+v', got '%+v'", testCase.hostnames, config.AllowNondistributableArtifactsHostnames)
  109. }
  110. } else {
  111. if err == nil {
  112. t.Fatalf("expect error '%s', got no error", testCase.err)
  113. }
  114. if !strings.Contains(err.Error(), testCase.err) {
  115. t.Fatalf("expect error '%s', got '%s'", testCase.err, err)
  116. }
  117. }
  118. }
  119. }
  120. func TestValidateMirror(t *testing.T) {
  121. valid := []string{
  122. "http://mirror-1.com",
  123. "http://mirror-1.com/",
  124. "https://mirror-1.com",
  125. "https://mirror-1.com/",
  126. "http://localhost",
  127. "https://localhost",
  128. "http://localhost:5000",
  129. "https://localhost:5000",
  130. "http://127.0.0.1",
  131. "https://127.0.0.1",
  132. "http://127.0.0.1:5000",
  133. "https://127.0.0.1:5000",
  134. }
  135. invalid := []string{
  136. "!invalid!://%as%",
  137. "ftp://mirror-1.com",
  138. "http://mirror-1.com/?q=foo",
  139. "http://mirror-1.com/v1/",
  140. "http://mirror-1.com/v1/?q=foo",
  141. "http://mirror-1.com/v1/?q=foo#frag",
  142. "http://mirror-1.com?q=foo",
  143. "https://mirror-1.com#frag",
  144. "https://mirror-1.com/#frag",
  145. "http://foo:bar@mirror-1.com/",
  146. "https://mirror-1.com/v1/",
  147. "https://mirror-1.com/v1/#",
  148. "https://mirror-1.com?q",
  149. }
  150. for _, address := range valid {
  151. if ret, err := ValidateMirror(address); err != nil || ret == "" {
  152. t.Errorf("ValidateMirror(`"+address+"`) got %s %s", ret, err)
  153. }
  154. }
  155. for _, address := range invalid {
  156. if ret, err := ValidateMirror(address); err == nil || ret != "" {
  157. t.Errorf("ValidateMirror(`"+address+"`) got %s %s", ret, err)
  158. }
  159. }
  160. }
  161. func TestLoadInsecureRegistries(t *testing.T) {
  162. testCases := []struct {
  163. registries []string
  164. index string
  165. err string
  166. }{
  167. {
  168. registries: []string{"127.0.0.1"},
  169. index: "127.0.0.1",
  170. },
  171. {
  172. registries: []string{"127.0.0.1:8080"},
  173. index: "127.0.0.1:8080",
  174. },
  175. {
  176. registries: []string{"2001:db8::1"},
  177. index: "2001:db8::1",
  178. },
  179. {
  180. registries: []string{"[2001:db8::1]:80"},
  181. index: "[2001:db8::1]:80",
  182. },
  183. {
  184. registries: []string{"http://mytest.com"},
  185. index: "mytest.com",
  186. },
  187. {
  188. registries: []string{"https://mytest.com"},
  189. index: "mytest.com",
  190. },
  191. {
  192. registries: []string{"HTTP://mytest.com"},
  193. index: "mytest.com",
  194. },
  195. {
  196. registries: []string{"svn://mytest.com"},
  197. err: "insecure registry svn://mytest.com should not contain '://'",
  198. },
  199. {
  200. registries: []string{"-invalid-registry"},
  201. err: "Cannot begin or end with a hyphen",
  202. },
  203. {
  204. registries: []string{`mytest-.com`},
  205. err: `insecure registry mytest-.com is not valid: invalid host "mytest-.com"`,
  206. },
  207. {
  208. registries: []string{`1200:0000:AB00:1234:0000:2552:7777:1313:8080`},
  209. err: `insecure registry 1200:0000:AB00:1234:0000:2552:7777:1313:8080 is not valid: invalid host "1200:0000:AB00:1234:0000:2552:7777:1313:8080"`,
  210. },
  211. {
  212. registries: []string{`mytest.com:500000`},
  213. err: `insecure registry mytest.com:500000 is not valid: invalid port "500000"`,
  214. },
  215. {
  216. registries: []string{`"mytest.com"`},
  217. err: `insecure registry "mytest.com" is not valid: invalid host "\"mytest.com\""`,
  218. },
  219. {
  220. registries: []string{`"mytest.com:5000"`},
  221. err: `insecure registry "mytest.com:5000" is not valid: invalid host "\"mytest.com"`,
  222. },
  223. }
  224. for _, testCase := range testCases {
  225. config := newServiceConfig(ServiceOptions{})
  226. err := config.LoadInsecureRegistries(testCase.registries)
  227. if testCase.err == "" {
  228. if err != nil {
  229. t.Fatalf("expect no error, got '%s'", err)
  230. }
  231. match := false
  232. for index := range config.IndexConfigs {
  233. if index == testCase.index {
  234. match = true
  235. }
  236. }
  237. if !match {
  238. t.Fatalf("expect index configs to contain '%s', got %+v", testCase.index, config.IndexConfigs)
  239. }
  240. } else {
  241. if err == nil {
  242. t.Fatalf("expect error '%s', got no error", testCase.err)
  243. }
  244. if !strings.Contains(err.Error(), testCase.err) {
  245. t.Fatalf("expect error '%s', got '%s'", testCase.err, err)
  246. }
  247. }
  248. }
  249. }