registry_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. package registry
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "strings"
  7. "testing"
  8. "github.com/docker/docker/cliconfig"
  9. "github.com/docker/docker/pkg/requestdecorator"
  10. )
  11. var (
  12. token = []string{"fake-token"}
  13. )
  14. const (
  15. imageID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
  16. REPO = "foo42/bar"
  17. )
  18. func spawnTestRegistrySession(t *testing.T) *Session {
  19. authConfig := &cliconfig.AuthConfig{}
  20. endpoint, err := NewEndpoint(makeIndex("/v1/"))
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. r, err := NewSession(authConfig, requestdecorator.NewRequestFactory(), endpoint, true)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. return r
  29. }
  30. func TestPublicSession(t *testing.T) {
  31. authConfig := &cliconfig.AuthConfig{}
  32. getSessionDecorators := func(index *IndexInfo) int {
  33. endpoint, err := NewEndpoint(index)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. r, err := NewSession(authConfig, requestdecorator.NewRequestFactory(), endpoint, true)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. return len(r.reqFactory.GetDecorators())
  42. }
  43. decorators := getSessionDecorators(makeIndex("/v1/"))
  44. assertEqual(t, decorators, 0, "Expected no decorator on http session")
  45. decorators = getSessionDecorators(makeHttpsIndex("/v1/"))
  46. assertNotEqual(t, decorators, 0, "Expected decorator on https session")
  47. decorators = getSessionDecorators(makePublicIndex())
  48. assertEqual(t, decorators, 0, "Expected no decorator on public session")
  49. }
  50. func TestPingRegistryEndpoint(t *testing.T) {
  51. testPing := func(index *IndexInfo, expectedStandalone bool, assertMessage string) {
  52. ep, err := NewEndpoint(index)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. regInfo, err := ep.Ping()
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. assertEqual(t, regInfo.Standalone, expectedStandalone, assertMessage)
  61. }
  62. testPing(makeIndex("/v1/"), true, "Expected standalone to be true (default)")
  63. testPing(makeHttpsIndex("/v1/"), true, "Expected standalone to be true (default)")
  64. testPing(makePublicIndex(), false, "Expected standalone to be false for public index")
  65. }
  66. func TestEndpoint(t *testing.T) {
  67. // Simple wrapper to fail test if err != nil
  68. expandEndpoint := func(index *IndexInfo) *Endpoint {
  69. endpoint, err := NewEndpoint(index)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. return endpoint
  74. }
  75. assertInsecureIndex := func(index *IndexInfo) {
  76. index.Secure = true
  77. _, err := NewEndpoint(index)
  78. assertNotEqual(t, err, nil, index.Name+": Expected error for insecure index")
  79. assertEqual(t, strings.Contains(err.Error(), "insecure-registry"), true, index.Name+": Expected insecure-registry error for insecure index")
  80. index.Secure = false
  81. }
  82. assertSecureIndex := func(index *IndexInfo) {
  83. index.Secure = true
  84. _, err := NewEndpoint(index)
  85. assertNotEqual(t, err, nil, index.Name+": Expected cert error for secure index")
  86. assertEqual(t, strings.Contains(err.Error(), "certificate signed by unknown authority"), true, index.Name+": Expected cert error for secure index")
  87. index.Secure = false
  88. }
  89. index := &IndexInfo{}
  90. index.Name = makeURL("/v1/")
  91. endpoint := expandEndpoint(index)
  92. assertEqual(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
  93. if endpoint.Version != APIVersion1 {
  94. t.Fatal("Expected endpoint to be v1")
  95. }
  96. assertInsecureIndex(index)
  97. index.Name = makeURL("")
  98. endpoint = expandEndpoint(index)
  99. assertEqual(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
  100. if endpoint.Version != APIVersion1 {
  101. t.Fatal("Expected endpoint to be v1")
  102. }
  103. assertInsecureIndex(index)
  104. httpURL := makeURL("")
  105. index.Name = strings.SplitN(httpURL, "://", 2)[1]
  106. endpoint = expandEndpoint(index)
  107. assertEqual(t, endpoint.String(), httpURL+"/v1/", index.Name+": Expected endpoint to be "+httpURL+"/v1/")
  108. if endpoint.Version != APIVersion1 {
  109. t.Fatal("Expected endpoint to be v1")
  110. }
  111. assertInsecureIndex(index)
  112. index.Name = makeHttpsURL("/v1/")
  113. endpoint = expandEndpoint(index)
  114. assertEqual(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
  115. if endpoint.Version != APIVersion1 {
  116. t.Fatal("Expected endpoint to be v1")
  117. }
  118. assertSecureIndex(index)
  119. index.Name = makeHttpsURL("")
  120. endpoint = expandEndpoint(index)
  121. assertEqual(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
  122. if endpoint.Version != APIVersion1 {
  123. t.Fatal("Expected endpoint to be v1")
  124. }
  125. assertSecureIndex(index)
  126. httpsURL := makeHttpsURL("")
  127. index.Name = strings.SplitN(httpsURL, "://", 2)[1]
  128. endpoint = expandEndpoint(index)
  129. assertEqual(t, endpoint.String(), httpsURL+"/v1/", index.Name+": Expected endpoint to be "+httpsURL+"/v1/")
  130. if endpoint.Version != APIVersion1 {
  131. t.Fatal("Expected endpoint to be v1")
  132. }
  133. assertSecureIndex(index)
  134. badEndpoints := []string{
  135. "http://127.0.0.1/v1/",
  136. "https://127.0.0.1/v1/",
  137. "http://127.0.0.1",
  138. "https://127.0.0.1",
  139. "127.0.0.1",
  140. }
  141. for _, address := range badEndpoints {
  142. index.Name = address
  143. _, err := NewEndpoint(index)
  144. checkNotEqual(t, err, nil, "Expected error while expanding bad endpoint")
  145. }
  146. }
  147. func TestGetRemoteHistory(t *testing.T) {
  148. r := spawnTestRegistrySession(t)
  149. hist, err := r.GetRemoteHistory(imageID, makeURL("/v1/"), token)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. assertEqual(t, len(hist), 2, "Expected 2 images in history")
  154. assertEqual(t, hist[0], imageID, "Expected "+imageID+"as first ancestry")
  155. assertEqual(t, hist[1], "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
  156. "Unexpected second ancestry")
  157. }
  158. func TestLookupRemoteImage(t *testing.T) {
  159. r := spawnTestRegistrySession(t)
  160. err := r.LookupRemoteImage(imageID, makeURL("/v1/"), token)
  161. assertEqual(t, err, nil, "Expected error of remote lookup to nil")
  162. if err := r.LookupRemoteImage("abcdef", makeURL("/v1/"), token); err == nil {
  163. t.Fatal("Expected error of remote lookup to not nil")
  164. }
  165. }
  166. func TestGetRemoteImageJSON(t *testing.T) {
  167. r := spawnTestRegistrySession(t)
  168. json, size, err := r.GetRemoteImageJSON(imageID, makeURL("/v1/"), token)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. assertEqual(t, size, 154, "Expected size 154")
  173. if len(json) <= 0 {
  174. t.Fatal("Expected non-empty json")
  175. }
  176. _, _, err = r.GetRemoteImageJSON("abcdef", makeURL("/v1/"), token)
  177. if err == nil {
  178. t.Fatal("Expected image not found error")
  179. }
  180. }
  181. func TestGetRemoteImageLayer(t *testing.T) {
  182. r := spawnTestRegistrySession(t)
  183. data, err := r.GetRemoteImageLayer(imageID, makeURL("/v1/"), token, 0)
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. if data == nil {
  188. t.Fatal("Expected non-nil data result")
  189. }
  190. _, err = r.GetRemoteImageLayer("abcdef", makeURL("/v1/"), token, 0)
  191. if err == nil {
  192. t.Fatal("Expected image not found error")
  193. }
  194. }
  195. func TestGetRemoteTags(t *testing.T) {
  196. r := spawnTestRegistrySession(t)
  197. tags, err := r.GetRemoteTags([]string{makeURL("/v1/")}, REPO, token)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. assertEqual(t, len(tags), 1, "Expected one tag")
  202. assertEqual(t, tags["latest"], imageID, "Expected tag latest to map to "+imageID)
  203. _, err = r.GetRemoteTags([]string{makeURL("/v1/")}, "foo42/baz", token)
  204. if err == nil {
  205. t.Fatal("Expected error when fetching tags for bogus repo")
  206. }
  207. }
  208. func TestGetRepositoryData(t *testing.T) {
  209. r := spawnTestRegistrySession(t)
  210. parsedURL, err := url.Parse(makeURL("/v1/"))
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. host := "http://" + parsedURL.Host + "/v1/"
  215. data, err := r.GetRepositoryData("foo42/bar")
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. assertEqual(t, len(data.ImgList), 2, "Expected 2 images in ImgList")
  220. assertEqual(t, len(data.Endpoints), 2,
  221. fmt.Sprintf("Expected 2 endpoints in Endpoints, found %d instead", len(data.Endpoints)))
  222. assertEqual(t, data.Endpoints[0], host,
  223. fmt.Sprintf("Expected first endpoint to be %s but found %s instead", host, data.Endpoints[0]))
  224. assertEqual(t, data.Endpoints[1], "http://test.example.com/v1/",
  225. fmt.Sprintf("Expected first endpoint to be http://test.example.com/v1/ but found %s instead", data.Endpoints[1]))
  226. }
  227. func TestPushImageJSONRegistry(t *testing.T) {
  228. r := spawnTestRegistrySession(t)
  229. imgData := &ImgData{
  230. ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
  231. Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
  232. }
  233. err := r.PushImageJSONRegistry(imgData, []byte{0x42, 0xdf, 0x0}, makeURL("/v1/"), token)
  234. if err != nil {
  235. t.Fatal(err)
  236. }
  237. }
  238. func TestPushImageLayerRegistry(t *testing.T) {
  239. r := spawnTestRegistrySession(t)
  240. layer := strings.NewReader("")
  241. _, _, err := r.PushImageLayerRegistry(imageID, layer, makeURL("/v1/"), token, []byte{})
  242. if err != nil {
  243. t.Fatal(err)
  244. }
  245. }
  246. func TestValidateRepositoryName(t *testing.T) {
  247. validRepoNames := []string{
  248. "docker/docker",
  249. "library/debian",
  250. "debian",
  251. "docker.io/docker/docker",
  252. "docker.io/library/debian",
  253. "docker.io/debian",
  254. "index.docker.io/docker/docker",
  255. "index.docker.io/library/debian",
  256. "index.docker.io/debian",
  257. "127.0.0.1:5000/docker/docker",
  258. "127.0.0.1:5000/library/debian",
  259. "127.0.0.1:5000/debian",
  260. "thisisthesongthatneverendsitgoesonandonandonthisisthesongthatnev",
  261. }
  262. invalidRepoNames := []string{
  263. "https://github.com/docker/docker",
  264. "docker/Docker",
  265. "docker///docker",
  266. "docker.io/docker/Docker",
  267. "docker.io/docker///docker",
  268. "1a3f5e7d9c1b3a5f7e9d1c3b5a7f9e1d3c5b7a9f1e3d5d7c9b1a3f5e7d9c1b3a",
  269. "docker.io/1a3f5e7d9c1b3a5f7e9d1c3b5a7f9e1d3c5b7a9f1e3d5d7c9b1a3f5e7d9c1b3a",
  270. }
  271. for _, name := range invalidRepoNames {
  272. err := ValidateRepositoryName(name)
  273. assertNotEqual(t, err, nil, "Expected invalid repo name: "+name)
  274. }
  275. for _, name := range validRepoNames {
  276. err := ValidateRepositoryName(name)
  277. assertEqual(t, err, nil, "Expected valid repo name: "+name)
  278. }
  279. err := ValidateRepositoryName(invalidRepoNames[0])
  280. assertEqual(t, err, ErrInvalidRepositoryName, "Expected ErrInvalidRepositoryName: "+invalidRepoNames[0])
  281. }
  282. func TestParseRepositoryInfo(t *testing.T) {
  283. expectedRepoInfos := map[string]RepositoryInfo{
  284. "fooo/bar": {
  285. Index: &IndexInfo{
  286. Name: IndexServerName(),
  287. Official: true,
  288. },
  289. RemoteName: "fooo/bar",
  290. LocalName: "fooo/bar",
  291. CanonicalName: "fooo/bar",
  292. Official: false,
  293. },
  294. "library/ubuntu": {
  295. Index: &IndexInfo{
  296. Name: IndexServerName(),
  297. Official: true,
  298. },
  299. RemoteName: "library/ubuntu",
  300. LocalName: "ubuntu",
  301. CanonicalName: "ubuntu",
  302. Official: true,
  303. },
  304. "nonlibrary/ubuntu": {
  305. Index: &IndexInfo{
  306. Name: IndexServerName(),
  307. Official: true,
  308. },
  309. RemoteName: "nonlibrary/ubuntu",
  310. LocalName: "nonlibrary/ubuntu",
  311. CanonicalName: "nonlibrary/ubuntu",
  312. Official: false,
  313. },
  314. "ubuntu": {
  315. Index: &IndexInfo{
  316. Name: IndexServerName(),
  317. Official: true,
  318. },
  319. RemoteName: "library/ubuntu",
  320. LocalName: "ubuntu",
  321. CanonicalName: "ubuntu",
  322. Official: true,
  323. },
  324. "other/library": {
  325. Index: &IndexInfo{
  326. Name: IndexServerName(),
  327. Official: true,
  328. },
  329. RemoteName: "other/library",
  330. LocalName: "other/library",
  331. CanonicalName: "other/library",
  332. Official: false,
  333. },
  334. "127.0.0.1:8000/private/moonbase": {
  335. Index: &IndexInfo{
  336. Name: "127.0.0.1:8000",
  337. Official: false,
  338. },
  339. RemoteName: "private/moonbase",
  340. LocalName: "127.0.0.1:8000/private/moonbase",
  341. CanonicalName: "127.0.0.1:8000/private/moonbase",
  342. Official: false,
  343. },
  344. "127.0.0.1:8000/privatebase": {
  345. Index: &IndexInfo{
  346. Name: "127.0.0.1:8000",
  347. Official: false,
  348. },
  349. RemoteName: "privatebase",
  350. LocalName: "127.0.0.1:8000/privatebase",
  351. CanonicalName: "127.0.0.1:8000/privatebase",
  352. Official: false,
  353. },
  354. "localhost:8000/private/moonbase": {
  355. Index: &IndexInfo{
  356. Name: "localhost:8000",
  357. Official: false,
  358. },
  359. RemoteName: "private/moonbase",
  360. LocalName: "localhost:8000/private/moonbase",
  361. CanonicalName: "localhost:8000/private/moonbase",
  362. Official: false,
  363. },
  364. "localhost:8000/privatebase": {
  365. Index: &IndexInfo{
  366. Name: "localhost:8000",
  367. Official: false,
  368. },
  369. RemoteName: "privatebase",
  370. LocalName: "localhost:8000/privatebase",
  371. CanonicalName: "localhost:8000/privatebase",
  372. Official: false,
  373. },
  374. "example.com/private/moonbase": {
  375. Index: &IndexInfo{
  376. Name: "example.com",
  377. Official: false,
  378. },
  379. RemoteName: "private/moonbase",
  380. LocalName: "example.com/private/moonbase",
  381. CanonicalName: "example.com/private/moonbase",
  382. Official: false,
  383. },
  384. "example.com/privatebase": {
  385. Index: &IndexInfo{
  386. Name: "example.com",
  387. Official: false,
  388. },
  389. RemoteName: "privatebase",
  390. LocalName: "example.com/privatebase",
  391. CanonicalName: "example.com/privatebase",
  392. Official: false,
  393. },
  394. "example.com:8000/private/moonbase": {
  395. Index: &IndexInfo{
  396. Name: "example.com:8000",
  397. Official: false,
  398. },
  399. RemoteName: "private/moonbase",
  400. LocalName: "example.com:8000/private/moonbase",
  401. CanonicalName: "example.com:8000/private/moonbase",
  402. Official: false,
  403. },
  404. "example.com:8000/privatebase": {
  405. Index: &IndexInfo{
  406. Name: "example.com:8000",
  407. Official: false,
  408. },
  409. RemoteName: "privatebase",
  410. LocalName: "example.com:8000/privatebase",
  411. CanonicalName: "example.com:8000/privatebase",
  412. Official: false,
  413. },
  414. "localhost/private/moonbase": {
  415. Index: &IndexInfo{
  416. Name: "localhost",
  417. Official: false,
  418. },
  419. RemoteName: "private/moonbase",
  420. LocalName: "localhost/private/moonbase",
  421. CanonicalName: "localhost/private/moonbase",
  422. Official: false,
  423. },
  424. "localhost/privatebase": {
  425. Index: &IndexInfo{
  426. Name: "localhost",
  427. Official: false,
  428. },
  429. RemoteName: "privatebase",
  430. LocalName: "localhost/privatebase",
  431. CanonicalName: "localhost/privatebase",
  432. Official: false,
  433. },
  434. IndexServerName() + "/public/moonbase": {
  435. Index: &IndexInfo{
  436. Name: IndexServerName(),
  437. Official: true,
  438. },
  439. RemoteName: "public/moonbase",
  440. LocalName: "public/moonbase",
  441. CanonicalName: "public/moonbase",
  442. Official: false,
  443. },
  444. "index." + IndexServerName() + "/public/moonbase": {
  445. Index: &IndexInfo{
  446. Name: IndexServerName(),
  447. Official: true,
  448. },
  449. RemoteName: "public/moonbase",
  450. LocalName: "public/moonbase",
  451. CanonicalName: "public/moonbase",
  452. Official: false,
  453. },
  454. IndexServerName() + "/public/moonbase": {
  455. Index: &IndexInfo{
  456. Name: IndexServerName(),
  457. Official: true,
  458. },
  459. RemoteName: "public/moonbase",
  460. LocalName: "public/moonbase",
  461. CanonicalName: "public/moonbase",
  462. Official: false,
  463. },
  464. "ubuntu-12.04-base": {
  465. Index: &IndexInfo{
  466. Name: IndexServerName(),
  467. Official: true,
  468. },
  469. RemoteName: "library/ubuntu-12.04-base",
  470. LocalName: "ubuntu-12.04-base",
  471. CanonicalName: "ubuntu-12.04-base",
  472. Official: true,
  473. },
  474. IndexServerName() + "/ubuntu-12.04-base": {
  475. Index: &IndexInfo{
  476. Name: IndexServerName(),
  477. Official: true,
  478. },
  479. RemoteName: "library/ubuntu-12.04-base",
  480. LocalName: "ubuntu-12.04-base",
  481. CanonicalName: "ubuntu-12.04-base",
  482. Official: true,
  483. },
  484. IndexServerName() + "/ubuntu-12.04-base": {
  485. Index: &IndexInfo{
  486. Name: IndexServerName(),
  487. Official: true,
  488. },
  489. RemoteName: "library/ubuntu-12.04-base",
  490. LocalName: "ubuntu-12.04-base",
  491. CanonicalName: "ubuntu-12.04-base",
  492. Official: true,
  493. },
  494. "index." + IndexServerName() + "/ubuntu-12.04-base": {
  495. Index: &IndexInfo{
  496. Name: IndexServerName(),
  497. Official: true,
  498. },
  499. RemoteName: "library/ubuntu-12.04-base",
  500. LocalName: "ubuntu-12.04-base",
  501. CanonicalName: "ubuntu-12.04-base",
  502. Official: true,
  503. },
  504. }
  505. for reposName, expectedRepoInfo := range expectedRepoInfos {
  506. repoInfo, err := ParseRepositoryInfo(reposName)
  507. if err != nil {
  508. t.Error(err)
  509. } else {
  510. checkEqual(t, repoInfo.Index.Name, expectedRepoInfo.Index.Name, reposName)
  511. checkEqual(t, repoInfo.RemoteName, expectedRepoInfo.RemoteName, reposName)
  512. checkEqual(t, repoInfo.LocalName, expectedRepoInfo.LocalName, reposName)
  513. checkEqual(t, repoInfo.CanonicalName, expectedRepoInfo.CanonicalName, reposName)
  514. checkEqual(t, repoInfo.Index.Official, expectedRepoInfo.Index.Official, reposName)
  515. checkEqual(t, repoInfo.Official, expectedRepoInfo.Official, reposName)
  516. }
  517. }
  518. }
  519. func TestNewIndexInfo(t *testing.T) {
  520. testIndexInfo := func(config *ServiceConfig, expectedIndexInfos map[string]*IndexInfo) {
  521. for indexName, expectedIndexInfo := range expectedIndexInfos {
  522. index, err := config.NewIndexInfo(indexName)
  523. if err != nil {
  524. t.Fatal(err)
  525. } else {
  526. checkEqual(t, index.Name, expectedIndexInfo.Name, indexName+" name")
  527. checkEqual(t, index.Official, expectedIndexInfo.Official, indexName+" is official")
  528. checkEqual(t, index.Secure, expectedIndexInfo.Secure, indexName+" is secure")
  529. checkEqual(t, len(index.Mirrors), len(expectedIndexInfo.Mirrors), indexName+" mirrors")
  530. }
  531. }
  532. }
  533. config := NewServiceConfig(nil)
  534. noMirrors := make([]string, 0)
  535. expectedIndexInfos := map[string]*IndexInfo{
  536. IndexServerName(): {
  537. Name: IndexServerName(),
  538. Official: true,
  539. Secure: true,
  540. Mirrors: noMirrors,
  541. },
  542. "index." + IndexServerName(): {
  543. Name: IndexServerName(),
  544. Official: true,
  545. Secure: true,
  546. Mirrors: noMirrors,
  547. },
  548. "example.com": {
  549. Name: "example.com",
  550. Official: false,
  551. Secure: true,
  552. Mirrors: noMirrors,
  553. },
  554. "127.0.0.1:5000": {
  555. Name: "127.0.0.1:5000",
  556. Official: false,
  557. Secure: false,
  558. Mirrors: noMirrors,
  559. },
  560. }
  561. testIndexInfo(config, expectedIndexInfos)
  562. publicMirrors := []string{"http://mirror1.local", "http://mirror2.local"}
  563. config = makeServiceConfig(publicMirrors, []string{"example.com"})
  564. expectedIndexInfos = map[string]*IndexInfo{
  565. IndexServerName(): {
  566. Name: IndexServerName(),
  567. Official: true,
  568. Secure: true,
  569. Mirrors: publicMirrors,
  570. },
  571. "index." + IndexServerName(): {
  572. Name: IndexServerName(),
  573. Official: true,
  574. Secure: true,
  575. Mirrors: publicMirrors,
  576. },
  577. "example.com": {
  578. Name: "example.com",
  579. Official: false,
  580. Secure: false,
  581. Mirrors: noMirrors,
  582. },
  583. "example.com:5000": {
  584. Name: "example.com:5000",
  585. Official: false,
  586. Secure: true,
  587. Mirrors: noMirrors,
  588. },
  589. "127.0.0.1": {
  590. Name: "127.0.0.1",
  591. Official: false,
  592. Secure: false,
  593. Mirrors: noMirrors,
  594. },
  595. "127.0.0.1:5000": {
  596. Name: "127.0.0.1:5000",
  597. Official: false,
  598. Secure: false,
  599. Mirrors: noMirrors,
  600. },
  601. "other.com": {
  602. Name: "other.com",
  603. Official: false,
  604. Secure: true,
  605. Mirrors: noMirrors,
  606. },
  607. }
  608. testIndexInfo(config, expectedIndexInfos)
  609. config = makeServiceConfig(nil, []string{"42.42.0.0/16"})
  610. expectedIndexInfos = map[string]*IndexInfo{
  611. "example.com": {
  612. Name: "example.com",
  613. Official: false,
  614. Secure: false,
  615. Mirrors: noMirrors,
  616. },
  617. "example.com:5000": {
  618. Name: "example.com:5000",
  619. Official: false,
  620. Secure: false,
  621. Mirrors: noMirrors,
  622. },
  623. "127.0.0.1": {
  624. Name: "127.0.0.1",
  625. Official: false,
  626. Secure: false,
  627. Mirrors: noMirrors,
  628. },
  629. "127.0.0.1:5000": {
  630. Name: "127.0.0.1:5000",
  631. Official: false,
  632. Secure: false,
  633. Mirrors: noMirrors,
  634. },
  635. "other.com": {
  636. Name: "other.com",
  637. Official: false,
  638. Secure: true,
  639. Mirrors: noMirrors,
  640. },
  641. }
  642. testIndexInfo(config, expectedIndexInfos)
  643. }
  644. func TestPushRegistryTag(t *testing.T) {
  645. r := spawnTestRegistrySession(t)
  646. err := r.PushRegistryTag("foo42/bar", imageID, "stable", makeURL("/v1/"), token)
  647. if err != nil {
  648. t.Fatal(err)
  649. }
  650. }
  651. func TestPushImageJSONIndex(t *testing.T) {
  652. r := spawnTestRegistrySession(t)
  653. imgData := []*ImgData{
  654. {
  655. ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
  656. Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
  657. },
  658. {
  659. ID: "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
  660. Checksum: "sha256:bea7bf2e4bacd479344b737328db47b18880d09096e6674165533aa994f5e9f2",
  661. },
  662. }
  663. repoData, err := r.PushImageJSONIndex("foo42/bar", imgData, false, nil)
  664. if err != nil {
  665. t.Fatal(err)
  666. }
  667. if repoData == nil {
  668. t.Fatal("Expected RepositoryData object")
  669. }
  670. repoData, err = r.PushImageJSONIndex("foo42/bar", imgData, true, []string{r.indexEndpoint.String()})
  671. if err != nil {
  672. t.Fatal(err)
  673. }
  674. if repoData == nil {
  675. t.Fatal("Expected RepositoryData object")
  676. }
  677. }
  678. func TestSearchRepositories(t *testing.T) {
  679. r := spawnTestRegistrySession(t)
  680. results, err := r.SearchRepositories("fakequery")
  681. if err != nil {
  682. t.Fatal(err)
  683. }
  684. if results == nil {
  685. t.Fatal("Expected non-nil SearchResults object")
  686. }
  687. assertEqual(t, results.NumResults, 1, "Expected 1 search results")
  688. assertEqual(t, results.Query, "fakequery", "Expected 'fakequery' as query")
  689. assertEqual(t, results.Results[0].StarCount, 42, "Expected 'fakeimage' a ot hae 42 stars")
  690. }
  691. func TestValidRemoteName(t *testing.T) {
  692. validRepositoryNames := []string{
  693. // Sanity check.
  694. "docker/docker",
  695. // Allow 64-character non-hexadecimal names (hexadecimal names are forbidden).
  696. "thisisthesongthatneverendsitgoesonandonandonthisisthesongthatnev",
  697. // Allow embedded hyphens.
  698. "docker-rules/docker",
  699. // Allow underscores everywhere (as opposed to hyphens).
  700. "____/____",
  701. //Username doc and image name docker being tested.
  702. "doc/docker",
  703. }
  704. for _, repositoryName := range validRepositoryNames {
  705. if err := validateRemoteName(repositoryName); err != nil {
  706. t.Errorf("Repository name should be valid: %v. Error: %v", repositoryName, err)
  707. }
  708. }
  709. invalidRepositoryNames := []string{
  710. // Disallow capital letters.
  711. "docker/Docker",
  712. // Only allow one slash.
  713. "docker///docker",
  714. // Disallow 64-character hexadecimal.
  715. "1a3f5e7d9c1b3a5f7e9d1c3b5a7f9e1d3c5b7a9f1e3d5d7c9b1a3f5e7d9c1b3a",
  716. // Disallow leading and trailing hyphens in namespace.
  717. "-docker/docker",
  718. "docker-/docker",
  719. "-docker-/docker",
  720. // Disallow consecutive hyphens.
  721. "dock--er/docker",
  722. // No repository.
  723. "docker/",
  724. //namespace too short
  725. "d/docker",
  726. //namespace too long
  727. "this_is_not_a_valid_namespace_because_its_lenth_is_greater_than_255_this_is_not_a_valid_namespace_because_its_lenth_is_greater_than_255_this_is_not_a_valid_namespace_because_its_lenth_is_greater_than_255_this_is_not_a_valid_namespace_because_its_lenth_is_greater_than_255/docker",
  728. }
  729. for _, repositoryName := range invalidRepositoryNames {
  730. if err := validateRemoteName(repositoryName); err == nil {
  731. t.Errorf("Repository name should be invalid: %v", repositoryName)
  732. }
  733. }
  734. }
  735. func TestTrustedLocation(t *testing.T) {
  736. for _, url := range []string{"http://example.com", "https://example.com:7777", "http://docker.io", "http://test.docker.com", "https://fakedocker.com"} {
  737. req, _ := http.NewRequest("GET", url, nil)
  738. if trustedLocation(req) == true {
  739. t.Fatalf("'%s' shouldn't be detected as a trusted location", url)
  740. }
  741. }
  742. for _, url := range []string{"https://docker.io", "https://test.docker.com:80"} {
  743. req, _ := http.NewRequest("GET", url, nil)
  744. if trustedLocation(req) == false {
  745. t.Fatalf("'%s' should be detected as a trusted location", url)
  746. }
  747. }
  748. }
  749. func TestAddRequiredHeadersToRedirectedRequests(t *testing.T) {
  750. for _, urls := range [][]string{
  751. {"http://docker.io", "https://docker.com"},
  752. {"https://foo.docker.io:7777", "http://bar.docker.com"},
  753. {"https://foo.docker.io", "https://example.com"},
  754. } {
  755. reqFrom, _ := http.NewRequest("GET", urls[0], nil)
  756. reqFrom.Header.Add("Content-Type", "application/json")
  757. reqFrom.Header.Add("Authorization", "super_secret")
  758. reqTo, _ := http.NewRequest("GET", urls[1], nil)
  759. AddRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
  760. if len(reqTo.Header) != 1 {
  761. t.Fatalf("Expected 1 headers, got %d", len(reqTo.Header))
  762. }
  763. if reqTo.Header.Get("Content-Type") != "application/json" {
  764. t.Fatal("'Content-Type' should be 'application/json'")
  765. }
  766. if reqTo.Header.Get("Authorization") != "" {
  767. t.Fatal("'Authorization' should be empty")
  768. }
  769. }
  770. for _, urls := range [][]string{
  771. {"https://docker.io", "https://docker.com"},
  772. {"https://foo.docker.io:7777", "https://bar.docker.com"},
  773. } {
  774. reqFrom, _ := http.NewRequest("GET", urls[0], nil)
  775. reqFrom.Header.Add("Content-Type", "application/json")
  776. reqFrom.Header.Add("Authorization", "super_secret")
  777. reqTo, _ := http.NewRequest("GET", urls[1], nil)
  778. AddRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
  779. if len(reqTo.Header) != 2 {
  780. t.Fatalf("Expected 2 headers, got %d", len(reqTo.Header))
  781. }
  782. if reqTo.Header.Get("Content-Type") != "application/json" {
  783. t.Fatal("'Content-Type' should be 'application/json'")
  784. }
  785. if reqTo.Header.Get("Authorization") != "super_secret" {
  786. t.Fatal("'Authorization' should be 'super_secret'")
  787. }
  788. }
  789. }
  790. func TestIsSecureIndex(t *testing.T) {
  791. tests := []struct {
  792. addr string
  793. insecureRegistries []string
  794. expected bool
  795. }{
  796. {IndexServerName(), nil, true},
  797. {"example.com", []string{}, true},
  798. {"example.com", []string{"example.com"}, false},
  799. {"localhost", []string{"localhost:5000"}, false},
  800. {"localhost:5000", []string{"localhost:5000"}, false},
  801. {"localhost", []string{"example.com"}, false},
  802. {"127.0.0.1:5000", []string{"127.0.0.1:5000"}, false},
  803. {"localhost", nil, false},
  804. {"localhost:5000", nil, false},
  805. {"127.0.0.1", nil, false},
  806. {"localhost", []string{"example.com"}, false},
  807. {"127.0.0.1", []string{"example.com"}, false},
  808. {"example.com", nil, true},
  809. {"example.com", []string{"example.com"}, false},
  810. {"127.0.0.1", []string{"example.com"}, false},
  811. {"127.0.0.1:5000", []string{"example.com"}, false},
  812. {"example.com:5000", []string{"42.42.0.0/16"}, false},
  813. {"example.com", []string{"42.42.0.0/16"}, false},
  814. {"example.com:5000", []string{"42.42.42.42/8"}, false},
  815. {"127.0.0.1:5000", []string{"127.0.0.0/8"}, false},
  816. {"42.42.42.42:5000", []string{"42.1.1.1/8"}, false},
  817. {"invalid.domain.com", []string{"42.42.0.0/16"}, true},
  818. {"invalid.domain.com", []string{"invalid.domain.com"}, false},
  819. {"invalid.domain.com:5000", []string{"invalid.domain.com"}, true},
  820. {"invalid.domain.com:5000", []string{"invalid.domain.com:5000"}, false},
  821. }
  822. for _, tt := range tests {
  823. config := makeServiceConfig(nil, tt.insecureRegistries)
  824. if sec := config.isSecureIndex(tt.addr); sec != tt.expected {
  825. t.Errorf("isSecureIndex failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec)
  826. }
  827. }
  828. }