search_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. package registry
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "net/http/httputil"
  8. "testing"
  9. "github.com/docker/distribution/registry/client/transport"
  10. "github.com/docker/docker/api/types/filters"
  11. "github.com/docker/docker/api/types/registry"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. )
  15. func spawnTestRegistrySession(t *testing.T) *session {
  16. authConfig := &registry.AuthConfig{}
  17. endpoint, err := newV1Endpoint(makeIndex("/v1/"), nil)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. userAgent := "docker test client"
  22. var tr http.RoundTripper = debugTransport{newTransport(nil), t.Log}
  23. tr = transport.NewTransport(newAuthTransport(tr, authConfig, false), Headers(userAgent, nil)...)
  24. client := httpClient(tr)
  25. if err := authorizeClient(client, authConfig, endpoint); err != nil {
  26. t.Fatal(err)
  27. }
  28. r := newSession(client, endpoint)
  29. // In a normal scenario for the v1 registry, the client should send a `X-Docker-Token: true`
  30. // header while authenticating, in order to retrieve a token that can be later used to
  31. // perform authenticated actions.
  32. //
  33. // The mock v1 registry does not support that, (TODO(tiborvass): support it), instead,
  34. // it will consider authenticated any request with the header `X-Docker-Token: fake-token`.
  35. //
  36. // Because we know that the client's transport is an `*authTransport` we simply cast it,
  37. // in order to set the internal cached token to the fake token, and thus send that fake token
  38. // upon every subsequent requests.
  39. r.client.Transport.(*authTransport).token = []string{"fake-token"}
  40. return r
  41. }
  42. type debugTransport struct {
  43. http.RoundTripper
  44. log func(...interface{})
  45. }
  46. func (tr debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  47. dump, err := httputil.DumpRequestOut(req, false)
  48. if err != nil {
  49. tr.log("could not dump request")
  50. }
  51. tr.log(string(dump))
  52. resp, err := tr.RoundTripper.RoundTrip(req)
  53. if err != nil {
  54. return nil, err
  55. }
  56. dump, err = httputil.DumpResponse(resp, false)
  57. if err != nil {
  58. tr.log("could not dump response")
  59. }
  60. tr.log(string(dump))
  61. return resp, err
  62. }
  63. func TestSearchRepositories(t *testing.T) {
  64. r := spawnTestRegistrySession(t)
  65. results, err := r.searchRepositories("fakequery", 25)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. if results == nil {
  70. t.Fatal("Expected non-nil SearchResults object")
  71. }
  72. assert.Equal(t, results.NumResults, 1, "Expected 1 search results")
  73. assert.Equal(t, results.Query, "fakequery", "Expected 'fakequery' as query")
  74. assert.Equal(t, results.Results[0].StarCount, 42, "Expected 'fakeimage' to have 42 stars")
  75. }
  76. func TestSearchErrors(t *testing.T) {
  77. errorCases := []struct {
  78. filtersArgs filters.Args
  79. shouldReturnError bool
  80. expectedError string
  81. }{
  82. {
  83. expectedError: "Unexpected status code 500",
  84. shouldReturnError: true,
  85. },
  86. {
  87. filtersArgs: filters.NewArgs(filters.Arg("type", "custom")),
  88. expectedError: "invalid filter 'type'",
  89. },
  90. {
  91. filtersArgs: filters.NewArgs(filters.Arg("is-automated", "invalid")),
  92. expectedError: "invalid filter 'is-automated=[invalid]'",
  93. },
  94. {
  95. filtersArgs: filters.NewArgs(
  96. filters.Arg("is-automated", "true"),
  97. filters.Arg("is-automated", "false"),
  98. ),
  99. expectedError: "invalid filter 'is-automated",
  100. },
  101. {
  102. filtersArgs: filters.NewArgs(filters.Arg("is-official", "invalid")),
  103. expectedError: "invalid filter 'is-official=[invalid]'",
  104. },
  105. {
  106. filtersArgs: filters.NewArgs(
  107. filters.Arg("is-official", "true"),
  108. filters.Arg("is-official", "false"),
  109. ),
  110. expectedError: "invalid filter 'is-official",
  111. },
  112. {
  113. filtersArgs: filters.NewArgs(filters.Arg("stars", "invalid")),
  114. expectedError: "invalid filter 'stars=invalid'",
  115. },
  116. {
  117. filtersArgs: filters.NewArgs(
  118. filters.Arg("stars", "1"),
  119. filters.Arg("stars", "invalid"),
  120. ),
  121. expectedError: "invalid filter 'stars=invalid'",
  122. },
  123. }
  124. for _, tc := range errorCases {
  125. tc := tc
  126. t.Run(tc.expectedError, func(t *testing.T) {
  127. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  128. if !tc.shouldReturnError {
  129. t.Errorf("unexpected HTTP request")
  130. }
  131. http.Error(w, "no search for you", http.StatusInternalServerError)
  132. }))
  133. defer srv.Close()
  134. // Construct the search term by cutting the 'http://' prefix off srv.URL.
  135. term := srv.URL[7:] + "/term"
  136. reg, err := NewService(ServiceOptions{})
  137. assert.NilError(t, err)
  138. _, err = reg.Search(context.Background(), tc.filtersArgs, term, 0, nil, map[string][]string{})
  139. assert.ErrorContains(t, err, tc.expectedError)
  140. if tc.shouldReturnError {
  141. assert.Check(t, errdefs.IsUnknown(err), "got: %T: %v", err, err)
  142. return
  143. }
  144. assert.Check(t, errdefs.IsInvalidParameter(err), "got: %T: %v", err, err)
  145. })
  146. }
  147. }
  148. func TestSearch(t *testing.T) {
  149. const term = "term"
  150. successCases := []struct {
  151. name string
  152. filtersArgs filters.Args
  153. registryResults []registry.SearchResult
  154. expectedResults []registry.SearchResult
  155. }{
  156. {
  157. name: "empty results",
  158. registryResults: []registry.SearchResult{},
  159. expectedResults: []registry.SearchResult{},
  160. },
  161. {
  162. name: "no filter",
  163. registryResults: []registry.SearchResult{
  164. {
  165. Name: "name",
  166. Description: "description",
  167. },
  168. },
  169. expectedResults: []registry.SearchResult{
  170. {
  171. Name: "name",
  172. Description: "description",
  173. },
  174. },
  175. },
  176. {
  177. name: "is-automated=true, no results",
  178. filtersArgs: filters.NewArgs(filters.Arg("is-automated", "true")),
  179. registryResults: []registry.SearchResult{
  180. {
  181. Name: "name",
  182. Description: "description",
  183. },
  184. },
  185. expectedResults: []registry.SearchResult{},
  186. },
  187. {
  188. name: "is-automated=true",
  189. filtersArgs: filters.NewArgs(filters.Arg("is-automated", "true")),
  190. registryResults: []registry.SearchResult{
  191. {
  192. Name: "name",
  193. Description: "description",
  194. IsAutomated: true, //nolint:staticcheck // ignore SA1019 (field is deprecated).
  195. },
  196. },
  197. expectedResults: []registry.SearchResult{
  198. {
  199. Name: "name",
  200. Description: "description",
  201. IsAutomated: true, //nolint:staticcheck // ignore SA1019 (field is deprecated).
  202. },
  203. },
  204. },
  205. {
  206. name: "is-automated=false, no results",
  207. filtersArgs: filters.NewArgs(filters.Arg("is-automated", "false")),
  208. registryResults: []registry.SearchResult{
  209. {
  210. Name: "name",
  211. Description: "description",
  212. IsAutomated: true, //nolint:staticcheck // ignore SA1019 (field is deprecated).
  213. },
  214. },
  215. expectedResults: []registry.SearchResult{},
  216. },
  217. {
  218. name: "is-automated=false",
  219. filtersArgs: filters.NewArgs(filters.Arg("is-automated", "false")),
  220. registryResults: []registry.SearchResult{
  221. {
  222. Name: "name",
  223. Description: "description",
  224. },
  225. },
  226. expectedResults: []registry.SearchResult{
  227. {
  228. Name: "name",
  229. Description: "description",
  230. },
  231. },
  232. },
  233. {
  234. name: "is-official=true, no results",
  235. filtersArgs: filters.NewArgs(filters.Arg("is-official", "true")),
  236. registryResults: []registry.SearchResult{
  237. {
  238. Name: "name",
  239. Description: "description",
  240. },
  241. },
  242. expectedResults: []registry.SearchResult{},
  243. },
  244. {
  245. name: "is-official=true",
  246. filtersArgs: filters.NewArgs(filters.Arg("is-official", "true")),
  247. registryResults: []registry.SearchResult{
  248. {
  249. Name: "name",
  250. Description: "description",
  251. IsOfficial: true,
  252. },
  253. },
  254. expectedResults: []registry.SearchResult{
  255. {
  256. Name: "name",
  257. Description: "description",
  258. IsOfficial: true,
  259. },
  260. },
  261. },
  262. {
  263. name: "is-official=false, no results",
  264. filtersArgs: filters.NewArgs(filters.Arg("is-official", "false")),
  265. registryResults: []registry.SearchResult{
  266. {
  267. Name: "name",
  268. Description: "description",
  269. IsOfficial: true,
  270. },
  271. },
  272. expectedResults: []registry.SearchResult{},
  273. },
  274. {
  275. name: "is-official=false",
  276. filtersArgs: filters.NewArgs(filters.Arg("is-official", "false")),
  277. registryResults: []registry.SearchResult{
  278. {
  279. Name: "name",
  280. Description: "description",
  281. IsOfficial: false,
  282. },
  283. },
  284. expectedResults: []registry.SearchResult{
  285. {
  286. Name: "name",
  287. Description: "description",
  288. IsOfficial: false,
  289. },
  290. },
  291. },
  292. {
  293. name: "stars=0",
  294. filtersArgs: filters.NewArgs(filters.Arg("stars", "0")),
  295. registryResults: []registry.SearchResult{
  296. {
  297. Name: "name",
  298. Description: "description",
  299. StarCount: 0,
  300. },
  301. },
  302. expectedResults: []registry.SearchResult{
  303. {
  304. Name: "name",
  305. Description: "description",
  306. StarCount: 0,
  307. },
  308. },
  309. },
  310. {
  311. name: "stars=0, no results",
  312. filtersArgs: filters.NewArgs(filters.Arg("stars", "1")),
  313. registryResults: []registry.SearchResult{
  314. {
  315. Name: "name",
  316. Description: "description",
  317. StarCount: 0,
  318. },
  319. },
  320. expectedResults: []registry.SearchResult{},
  321. },
  322. {
  323. name: "stars=1",
  324. filtersArgs: filters.NewArgs(filters.Arg("stars", "1")),
  325. registryResults: []registry.SearchResult{
  326. {
  327. Name: "name0",
  328. Description: "description0",
  329. StarCount: 0,
  330. },
  331. {
  332. Name: "name1",
  333. Description: "description1",
  334. StarCount: 1,
  335. },
  336. },
  337. expectedResults: []registry.SearchResult{
  338. {
  339. Name: "name1",
  340. Description: "description1",
  341. StarCount: 1,
  342. },
  343. },
  344. },
  345. {
  346. name: "stars=1, is-official=true, is-automated=true",
  347. filtersArgs: filters.NewArgs(
  348. filters.Arg("stars", "1"),
  349. filters.Arg("is-official", "true"),
  350. filters.Arg("is-automated", "true"),
  351. ),
  352. registryResults: []registry.SearchResult{
  353. {
  354. Name: "name0",
  355. Description: "description0",
  356. StarCount: 0,
  357. IsOfficial: true,
  358. IsAutomated: true, //nolint:staticcheck // ignore SA1019 (field is deprecated).
  359. },
  360. {
  361. Name: "name1",
  362. Description: "description1",
  363. StarCount: 1,
  364. IsOfficial: false,
  365. IsAutomated: true, //nolint:staticcheck // ignore SA1019 (field is deprecated).
  366. },
  367. {
  368. Name: "name2",
  369. Description: "description2",
  370. StarCount: 1,
  371. IsOfficial: true,
  372. },
  373. {
  374. Name: "name3",
  375. Description: "description3",
  376. StarCount: 2,
  377. IsOfficial: true,
  378. IsAutomated: true, //nolint:staticcheck // ignore SA1019 (field is deprecated).
  379. },
  380. },
  381. expectedResults: []registry.SearchResult{
  382. {
  383. Name: "name3",
  384. Description: "description3",
  385. StarCount: 2,
  386. IsOfficial: true,
  387. IsAutomated: true, //nolint:staticcheck // ignore SA1019 (field is deprecated).
  388. },
  389. },
  390. },
  391. }
  392. for _, tc := range successCases {
  393. tc := tc
  394. t.Run(tc.name, func(t *testing.T) {
  395. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  396. w.Header().Set("Content-type", "application/json")
  397. json.NewEncoder(w).Encode(registry.SearchResults{
  398. Query: term,
  399. NumResults: len(tc.registryResults),
  400. Results: tc.registryResults,
  401. })
  402. }))
  403. defer srv.Close()
  404. // Construct the search term by cutting the 'http://' prefix off srv.URL.
  405. searchTerm := srv.URL[7:] + "/" + term
  406. reg, err := NewService(ServiceOptions{})
  407. assert.NilError(t, err)
  408. results, err := reg.Search(context.Background(), tc.filtersArgs, searchTerm, 0, nil, map[string][]string{})
  409. assert.NilError(t, err)
  410. assert.DeepEqual(t, results, tc.expectedResults)
  411. })
  412. }
  413. }