swh.scm 4.8 KB

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