swh.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2022 Maxime Devos <maximedevos@telenet.be>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-swh)
  20. #:use-module (guix swh)
  21. #:use-module (guix tests http)
  22. #:use-module (web response)
  23. #:use-module (srfi srfi-19)
  24. #:use-module (srfi srfi-64)
  25. #:use-module (ice-9 match))
  26. ;; Test the JSON mapping machinery used in (guix swh).
  27. (define %origin
  28. "{ \"origin_visits_url\": \"/visits/42\",
  29. \"type\": \"git\",
  30. \"url\": \"http://example.org/guix.git\" }")
  31. (define %visits
  32. ;; A single visit where 'snapshot_url' is null.
  33. ;; See <https://bugs.gnu.org/45615>.
  34. "[ {
  35. \"origin\": \"https://github.com/Genivia/ugrep\",
  36. \"visit\": 1,
  37. \"date\": \"2020-05-17T21:43:45.422977+00:00\",
  38. \"status\": \"ongoing\",
  39. \"snapshot\": null,
  40. \"metadata\": {},
  41. \"type\": \"git\",
  42. \"origin_visit_url\": \"https://archive.softwareheritage.org/api/1/origin/https://github.com/Genivia/ugrep/visit/1/\",
  43. \"snapshot_url\": null
  44. } ]")
  45. (define %directory-entries
  46. "[ { \"name\": \"one\",
  47. \"type\": \"regular\",
  48. \"length\": 123,
  49. \"dir_id\": 1 },
  50. { \"name\": \"two\",
  51. \"type\": \"regular\",
  52. \"length\": 456,
  53. \"dir_id\": 2 } ]")
  54. (define-syntax-rule (with-json-result where str exp ...)
  55. (with-http-server `((,where 200 ,str))
  56. (parameterize ((%swh-base-url (%local-url "")))
  57. exp ...)))
  58. (test-begin "swh")
  59. (test-equal "lookup-origin"
  60. (list "git" "http://example.org/guix.git")
  61. (with-json-result "/api/1/origin/http://example.org/guix.git/get/" %origin
  62. (let ((origin (lookup-origin "http://example.org/guix.git")))
  63. (list (origin-type origin)
  64. (origin-url origin)))))
  65. (test-equal "lookup-origin, not found"
  66. #f
  67. (with-http-server `(("/api/1/origin/http://example.org/whatever/get/"
  68. 404 "Nope."))
  69. (parameterize ((%swh-base-url (%local-url "")))
  70. (lookup-origin "http://example.org/whatever"))))
  71. (test-equal "origin-visit, no snapshots"
  72. '("https://github.com/Genivia/ugrep"
  73. "2020-05-17T21:43:45Z"
  74. #f) ;see <https://bugs.gnu.org/45615>
  75. (with-http-server `(("/api/1/origin/http://example.org/whatever/get/"
  76. 200 ,%origin)
  77. ("/visits/42/" 200 ,%visits))
  78. (parameterize ((%swh-base-url (%local-url "")))
  79. (let ((origin (lookup-origin "http://example.org/whatever")))
  80. (match (origin-visits origin)
  81. ((visit)
  82. (list (visit-origin visit)
  83. (date->string (visit-date visit) "~4")
  84. (visit-snapshot-url visit))))))))
  85. (test-equal "lookup-directory"
  86. '(("one" 123) ("two" 456))
  87. (with-json-result "/api/1/directory/123/" %directory-entries
  88. (map (lambda (entry)
  89. (list (directory-entry-name entry)
  90. (directory-entry-length entry)))
  91. (lookup-directory "123"))))
  92. (test-equal "rate limit reached"
  93. 3000000000
  94. (let ((too-many (build-response
  95. #:code 429
  96. #:reason-phrase "Too many requests"
  97. ;; Pretend we've reached the limit and it'll be reset in
  98. ;; June 2065.
  99. #:headers '((x-ratelimit-remaining . "0")
  100. (x-ratelimit-reset . "3000000000")))))
  101. (with-http-server `(("/api/1/origin/http://example.org/guix.git/get/"
  102. ,too-many "Too bad."))
  103. (parameterize ((%swh-base-url (%local-url "")))
  104. (catch 'swh-error
  105. (lambda ()
  106. (lookup-origin "http://example.org/guix.git"))
  107. (lambda (key url method response)
  108. ;; Ensure the reset time was recorded.
  109. (@@ (guix swh) %general-rate-limit-reset-time)))))))
  110. (test-assert "%allow-request? and request-rate-limit-reached?"
  111. ;; Here we test two things: that the rate limit set above is in effect and
  112. ;; that %ALLOW-REQUEST? is called, and that 'request-rate-limit-reached?'
  113. ;; returns true.
  114. (let* ((key (gensym "skip-request"))
  115. (skip-if-limit-reached
  116. (lambda (url method)
  117. (or (not (request-rate-limit-reached? url method))
  118. (throw key #t)))))
  119. (parameterize ((%allow-request? skip-if-limit-reached))
  120. (catch key
  121. (lambda ()
  122. (lookup-origin "http://example.org/guix.git")
  123. #f)
  124. (const #t)))))
  125. (test-end "swh")
  126. ;; Local Variables:
  127. ;; eval: (put 'with-json-result 'scheme-indent-function 2)
  128. ;; eval: (put 'with-http-server 'scheme-indent-function 1)
  129. ;; End: