config_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package registry
  2. import (
  3. "reflect"
  4. "sort"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestLoadAllowNondistributableArtifacts(t *testing.T) {
  10. testCases := []struct {
  11. registries []string
  12. cidrStrs []string
  13. hostnames []string
  14. err string
  15. }{
  16. {
  17. registries: []string{"1.2.3.0/24"},
  18. cidrStrs: []string{"1.2.3.0/24"},
  19. },
  20. {
  21. registries: []string{"2001:db8::/120"},
  22. cidrStrs: []string{"2001:db8::/120"},
  23. },
  24. {
  25. registries: []string{"127.0.0.1"},
  26. hostnames: []string{"127.0.0.1"},
  27. },
  28. {
  29. registries: []string{"127.0.0.1:8080"},
  30. hostnames: []string{"127.0.0.1:8080"},
  31. },
  32. {
  33. registries: []string{"2001:db8::1"},
  34. hostnames: []string{"2001:db8::1"},
  35. },
  36. {
  37. registries: []string{"[2001:db8::1]:80"},
  38. hostnames: []string{"[2001:db8::1]:80"},
  39. },
  40. {
  41. registries: []string{"[2001:db8::1]:80"},
  42. hostnames: []string{"[2001:db8::1]:80"},
  43. },
  44. {
  45. registries: []string{"1.2.3.0/24", "2001:db8::/120", "127.0.0.1", "127.0.0.1:8080"},
  46. cidrStrs: []string{"1.2.3.0/24", "2001:db8::/120"},
  47. hostnames: []string{"127.0.0.1", "127.0.0.1:8080"},
  48. },
  49. {
  50. registries: []string{"http://mytest.com"},
  51. err: "allow-nondistributable-artifacts registry http://mytest.com should not contain '://'",
  52. },
  53. {
  54. registries: []string{"https://mytest.com"},
  55. err: "allow-nondistributable-artifacts registry https://mytest.com should not contain '://'",
  56. },
  57. {
  58. registries: []string{"HTTP://mytest.com"},
  59. err: "allow-nondistributable-artifacts registry HTTP://mytest.com should not contain '://'",
  60. },
  61. {
  62. registries: []string{"svn://mytest.com"},
  63. err: "allow-nondistributable-artifacts registry svn://mytest.com should not contain '://'",
  64. },
  65. {
  66. registries: []string{"-invalid-registry"},
  67. err: "Cannot begin or end with a hyphen",
  68. },
  69. {
  70. registries: []string{`mytest-.com`},
  71. err: `allow-nondistributable-artifacts registry mytest-.com is not valid: invalid host "mytest-.com"`,
  72. },
  73. {
  74. registries: []string{`1200:0000:AB00:1234:0000:2552:7777:1313:8080`},
  75. 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"`,
  76. },
  77. {
  78. registries: []string{`mytest.com:500000`},
  79. err: `allow-nondistributable-artifacts registry mytest.com:500000 is not valid: invalid port "500000"`,
  80. },
  81. {
  82. registries: []string{`"mytest.com"`},
  83. err: `allow-nondistributable-artifacts registry "mytest.com" is not valid: invalid host "\"mytest.com\""`,
  84. },
  85. {
  86. registries: []string{`"mytest.com:5000"`},
  87. err: `allow-nondistributable-artifacts registry "mytest.com:5000" is not valid: invalid host "\"mytest.com"`,
  88. },
  89. }
  90. for _, testCase := range testCases {
  91. config := emptyServiceConfig
  92. err := config.LoadAllowNondistributableArtifacts(testCase.registries)
  93. if testCase.err == "" {
  94. if err != nil {
  95. t.Fatalf("expect no error, got '%s'", err)
  96. }
  97. cidrStrs := []string{}
  98. for _, c := range config.AllowNondistributableArtifactsCIDRs {
  99. cidrStrs = append(cidrStrs, c.String())
  100. }
  101. sort.Strings(testCase.cidrStrs)
  102. sort.Strings(cidrStrs)
  103. if (len(testCase.cidrStrs) > 0 || len(cidrStrs) > 0) && !reflect.DeepEqual(testCase.cidrStrs, cidrStrs) {
  104. t.Fatalf("expect AllowNondistributableArtifactsCIDRs to be '%+v', got '%+v'", testCase.cidrStrs, cidrStrs)
  105. }
  106. sort.Strings(testCase.hostnames)
  107. sort.Strings(config.AllowNondistributableArtifactsHostnames)
  108. if (len(testCase.hostnames) > 0 || len(config.AllowNondistributableArtifactsHostnames) > 0) && !reflect.DeepEqual(testCase.hostnames, config.AllowNondistributableArtifactsHostnames) {
  109. t.Fatalf("expect AllowNondistributableArtifactsHostnames to be '%+v', got '%+v'", testCase.hostnames, config.AllowNondistributableArtifactsHostnames)
  110. }
  111. } else {
  112. if err == nil {
  113. t.Fatalf("expect error '%s', got no error", testCase.err)
  114. }
  115. if !strings.Contains(err.Error(), testCase.err) {
  116. t.Fatalf("expect error '%s', got '%s'", testCase.err, err)
  117. }
  118. }
  119. }
  120. }
  121. func TestValidateMirror(t *testing.T) {
  122. valid := []string{
  123. "http://mirror-1.com",
  124. "http://mirror-1.com/",
  125. "https://mirror-1.com",
  126. "https://mirror-1.com/",
  127. "http://localhost",
  128. "https://localhost",
  129. "http://localhost:5000",
  130. "https://localhost:5000",
  131. "http://127.0.0.1",
  132. "https://127.0.0.1",
  133. "http://127.0.0.1:5000",
  134. "https://127.0.0.1:5000",
  135. }
  136. invalid := []string{
  137. "!invalid!://%as%",
  138. "ftp://mirror-1.com",
  139. "http://mirror-1.com/?q=foo",
  140. "http://mirror-1.com/v1/",
  141. "http://mirror-1.com/v1/?q=foo",
  142. "http://mirror-1.com/v1/?q=foo#frag",
  143. "http://mirror-1.com?q=foo",
  144. "https://mirror-1.com#frag",
  145. "https://mirror-1.com/#frag",
  146. "http://foo:bar@mirror-1.com/",
  147. "https://mirror-1.com/v1/",
  148. "https://mirror-1.com/v1/#",
  149. "https://mirror-1.com?q",
  150. }
  151. for _, address := range valid {
  152. if ret, err := ValidateMirror(address); err != nil || ret == "" {
  153. t.Errorf("ValidateMirror(`"+address+"`) got %s %s", ret, err)
  154. }
  155. }
  156. for _, address := range invalid {
  157. if ret, err := ValidateMirror(address); err == nil || ret != "" {
  158. t.Errorf("ValidateMirror(`"+address+"`) got %s %s", ret, err)
  159. }
  160. }
  161. }
  162. func TestLoadInsecureRegistries(t *testing.T) {
  163. testCases := []struct {
  164. registries []string
  165. index string
  166. err string
  167. }{
  168. {
  169. registries: []string{"127.0.0.1"},
  170. index: "127.0.0.1",
  171. },
  172. {
  173. registries: []string{"127.0.0.1:8080"},
  174. index: "127.0.0.1:8080",
  175. },
  176. {
  177. registries: []string{"2001:db8::1"},
  178. index: "2001:db8::1",
  179. },
  180. {
  181. registries: []string{"[2001:db8::1]:80"},
  182. index: "[2001:db8::1]:80",
  183. },
  184. {
  185. registries: []string{"http://mytest.com"},
  186. index: "mytest.com",
  187. },
  188. {
  189. registries: []string{"https://mytest.com"},
  190. index: "mytest.com",
  191. },
  192. {
  193. registries: []string{"HTTP://mytest.com"},
  194. index: "mytest.com",
  195. },
  196. {
  197. registries: []string{"svn://mytest.com"},
  198. err: "insecure registry svn://mytest.com should not contain '://'",
  199. },
  200. {
  201. registries: []string{"-invalid-registry"},
  202. err: "Cannot begin or end with a hyphen",
  203. },
  204. {
  205. registries: []string{`mytest-.com`},
  206. err: `insecure registry mytest-.com is not valid: invalid host "mytest-.com"`,
  207. },
  208. {
  209. registries: []string{`1200:0000:AB00:1234:0000:2552:7777:1313:8080`},
  210. 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"`,
  211. },
  212. {
  213. registries: []string{`mytest.com:500000`},
  214. err: `insecure registry mytest.com:500000 is not valid: invalid port "500000"`,
  215. },
  216. {
  217. registries: []string{`"mytest.com"`},
  218. err: `insecure registry "mytest.com" is not valid: invalid host "\"mytest.com\""`,
  219. },
  220. {
  221. registries: []string{`"mytest.com:5000"`},
  222. err: `insecure registry "mytest.com:5000" is not valid: invalid host "\"mytest.com"`,
  223. },
  224. }
  225. for _, testCase := range testCases {
  226. config := emptyServiceConfig
  227. err := config.LoadInsecureRegistries(testCase.registries)
  228. if testCase.err == "" {
  229. if err != nil {
  230. t.Fatalf("expect no error, got '%s'", err)
  231. }
  232. match := false
  233. for index := range config.IndexConfigs {
  234. if index == testCase.index {
  235. match = true
  236. }
  237. }
  238. if !match {
  239. t.Fatalf("expect index configs to contain '%s', got %+v", testCase.index, config.IndexConfigs)
  240. }
  241. } else {
  242. if err == nil {
  243. t.Fatalf("expect error '%s', got no error", testCase.err)
  244. }
  245. if !strings.Contains(err.Error(), testCase.err) {
  246. t.Fatalf("expect error '%s', got '%s'", testCase.err, err)
  247. }
  248. }
  249. }
  250. }
  251. func TestNewServiceConfig(t *testing.T) {
  252. testCases := []struct {
  253. opts ServiceOptions
  254. errStr string
  255. }{
  256. {
  257. ServiceOptions{},
  258. "",
  259. },
  260. {
  261. ServiceOptions{
  262. Mirrors: []string{"example.com:5000"},
  263. },
  264. `invalid mirror: unsupported scheme "example.com" in "example.com:5000"`,
  265. },
  266. {
  267. ServiceOptions{
  268. Mirrors: []string{"http://example.com:5000"},
  269. },
  270. "",
  271. },
  272. {
  273. ServiceOptions{
  274. InsecureRegistries: []string{"[fe80::]/64"},
  275. },
  276. `insecure registry [fe80::]/64 is not valid: invalid host "[fe80::]/64"`,
  277. },
  278. {
  279. ServiceOptions{
  280. InsecureRegistries: []string{"102.10.8.1/24"},
  281. },
  282. "",
  283. },
  284. {
  285. ServiceOptions{
  286. AllowNondistributableArtifacts: []string{"[fe80::]/64"},
  287. },
  288. `allow-nondistributable-artifacts registry [fe80::]/64 is not valid: invalid host "[fe80::]/64"`,
  289. },
  290. {
  291. ServiceOptions{
  292. AllowNondistributableArtifacts: []string{"102.10.8.1/24"},
  293. },
  294. "",
  295. },
  296. }
  297. for _, testCase := range testCases {
  298. _, err := newServiceConfig(testCase.opts)
  299. if testCase.errStr != "" {
  300. assert.EqualError(t, err, testCase.errStr)
  301. } else {
  302. assert.Nil(t, err)
  303. }
  304. }
  305. }
  306. func TestValidateIndexName(t *testing.T) {
  307. valid := []struct {
  308. index string
  309. expect string
  310. }{
  311. {
  312. index: "index.docker.io",
  313. expect: "docker.io",
  314. },
  315. {
  316. index: "example.com",
  317. expect: "example.com",
  318. },
  319. {
  320. index: "127.0.0.1:8080",
  321. expect: "127.0.0.1:8080",
  322. },
  323. {
  324. index: "mytest-1.com",
  325. expect: "mytest-1.com",
  326. },
  327. {
  328. index: "mirror-1.com/v1/?q=foo",
  329. expect: "mirror-1.com/v1/?q=foo",
  330. },
  331. }
  332. for _, testCase := range valid {
  333. result, err := ValidateIndexName(testCase.index)
  334. if assert.NoError(t, err) {
  335. assert.Equal(t, testCase.expect, result)
  336. }
  337. }
  338. }
  339. func TestValidateIndexNameWithError(t *testing.T) {
  340. invalid := []struct {
  341. index string
  342. err string
  343. }{
  344. {
  345. index: "docker.io-",
  346. err: "invalid index name (docker.io-). Cannot begin or end with a hyphen",
  347. },
  348. {
  349. index: "-example.com",
  350. err: "invalid index name (-example.com). Cannot begin or end with a hyphen",
  351. },
  352. {
  353. index: "mirror-1.com/v1/?q=foo-",
  354. err: "invalid index name (mirror-1.com/v1/?q=foo-). Cannot begin or end with a hyphen",
  355. },
  356. }
  357. for _, testCase := range invalid {
  358. _, err := ValidateIndexName(testCase.index)
  359. assert.EqualError(t, err, testCase.err)
  360. }
  361. }