http.scm 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. ;;; http.scm -- tests for (cuirass http) module
  2. ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
  3. ;;; Copyright © 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2017, 2020, 2021 Mathieu Othacehe <othacehe@gnu.org>
  5. ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
  6. ;;;
  7. ;;; This file is part of Cuirass.
  8. ;;;
  9. ;;; Cuirass is free software: you can redistribute it and/or modify
  10. ;;; it under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation, either version 3 of the License, or
  12. ;;; (at your option) any later version.
  13. ;;;
  14. ;;; Cuirass is distributed in the hope that it will be useful,
  15. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with Cuirass. If not, see <http://www.gnu.org/licenses/>.
  21. (use-modules (cuirass http)
  22. (cuirass database)
  23. (cuirass specification)
  24. (cuirass utils)
  25. (tests common)
  26. (guix channels)
  27. (json)
  28. (fibers)
  29. (squee)
  30. (web uri)
  31. (web client)
  32. (web response)
  33. (rnrs bytevectors)
  34. (srfi srfi-1)
  35. (srfi srfi-64)
  36. (ice-9 threads)
  37. (ice-9 match))
  38. (define (http-get-body uri)
  39. (call-with-values (lambda () (http-get uri))
  40. (lambda (response body) body)))
  41. (define (wait-until-ready port)
  42. ;; Wait until the server is accepting connections.
  43. (let ((conn (socket PF_INET SOCK_STREAM 0)))
  44. (let loop ()
  45. (unless (false-if-exception
  46. (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
  47. (loop)))))
  48. (define (test-cuirass-uri route)
  49. (string-append "http://localhost:6688" route))
  50. (define build-query-result
  51. '((#:id . 1)
  52. (#:evaluation . 1)
  53. (#:jobset . "guix")
  54. (#:job . "fake-job")
  55. (#:timestamp . 1501347493)
  56. (#:starttime . 1501347493)
  57. (#:stoptime . 1501347493)
  58. (#:derivation . "/gnu/store/fake.drv")
  59. (#:buildoutputs . ((out ("path" . "/gnu/store/fake-1.0"))))
  60. (#:system . "x86_64-linux")
  61. (#:nixname . "fake-1.0")
  62. (#:buildstatus . 0)
  63. (#:weather . -1)
  64. (#:busy . 0)
  65. (#:priority . 9)
  66. (#:finished . 1)
  67. (#:buildproducts . #())))
  68. (define evaluations-query-result
  69. #(((#:id . 2)
  70. (#:specification . "guix")
  71. (#:status . -1)
  72. (#:timestamp . 1501347493)
  73. (#:checkouttime . 0)
  74. (#:evaltime . 0)
  75. (#:checkouts . #(((#:commit . "fakesha2")
  76. (#:channel . "guix")
  77. (#:directory . "dir3")))))))
  78. (test-group-with-cleanup "http"
  79. (test-assert "object->json-string"
  80. (lset= equal?
  81. (call-with-input-string
  82. (string-append "{"
  83. "\"boolean\" : false,"
  84. "\"string\" : \"guix\","
  85. "\"alist\" : {\"subset\" : \"hello\"},"
  86. "\"list\" : [1, \"2\", \"three\"],"
  87. "\"symbol\" : \"hydra-jobs\","
  88. "\"number\" : 1"
  89. "}")
  90. json->scm)
  91. (call-with-input-string
  92. (object->json-string '((#:number . 1)
  93. (string . "guix")
  94. ("symbol" . hydra-jobs)
  95. (#:alist . ((subset . "hello")))
  96. (list . #(1 "2" #:three))
  97. ("boolean" . #f)))
  98. json->scm)))
  99. (test-assert "db-init"
  100. (begin
  101. (test-init-db!)
  102. #t))
  103. (test-assert "cuirass-run"
  104. (call-with-new-thread
  105. (lambda ()
  106. (run-fibers
  107. (lambda ()
  108. (run-cuirass-server #:port 6688))
  109. #:drain? #t))))
  110. (test-assert "wait-server"
  111. (wait-until-ready 6688))
  112. (test-assert "fill-db"
  113. (let* ((build1
  114. `((#:derivation . "/gnu/store/fake.drv")
  115. (#:eval-id . 1)
  116. (#:job-name . "fake-job")
  117. (#:system . "x86_64-linux")
  118. (#:nix-name . "fake-1.0")
  119. (#:log . "unused so far")
  120. (#:status . ,(build-status succeeded))
  121. (#:outputs . (("out" . "/gnu/store/fake-1.0")))
  122. (#:timestamp . 1501347493)
  123. (#:starttime . 1501347493)
  124. (#:stoptime . 1501347493)))
  125. (build2
  126. `((#:derivation . "/gnu/store/fake2.drv")
  127. (#:eval-id . 1)
  128. (#:job-name . "fake-job")
  129. (#:system . "x86_64-linux")
  130. (#:nix-name . "fake-2.0")
  131. (#:log . "unused so far")
  132. (#:status . ,(build-status scheduled))
  133. (#:outputs . (("out" . "/gnu/store/fake-2.0")))
  134. (#:timestamp . 1501347493)
  135. (#:starttime . 0)
  136. (#:stoptime . 0)))
  137. (spec
  138. (specification
  139. (name "guix")
  140. (build 'hello)
  141. (channels
  142. (list (channel
  143. (name 'guix)
  144. (url "https://gitlab.com/mothacehe/guix.git")
  145. (branch "master"))
  146. (channel
  147. (name 'packages)
  148. (url "https://gitlab.com/mothacehe/guix.git")
  149. (branch "master"))))))
  150. (checkouts1
  151. (list
  152. (checkout->channel-instance "dir1"
  153. #:name 'guix
  154. #:url "url1"
  155. #:commit "fakesha1")
  156. (checkout->channel-instance "dir2"
  157. #:name 'packages
  158. #:url "url2"
  159. #:commit "fakesha3")))
  160. (checkouts2
  161. (list
  162. (checkout->channel-instance "dir3"
  163. #:name 'guix
  164. #:url "dir3"
  165. #:commit "fakesha2")
  166. (checkout->channel-instance "dir4"
  167. #:name 'packages
  168. #:url "dir4"
  169. #:commit "fakesha3"))))
  170. (db-add-or-update-specification spec)
  171. (db-add-evaluation "guix" checkouts1
  172. #:timestamp 1501347493)
  173. (db-add-evaluation "guix" checkouts2
  174. #:timestamp 1501347493)
  175. (db-add-build build1)
  176. (db-add-build build2)))
  177. (test-assert "/specifications"
  178. (match (call-with-input-string
  179. (utf8->string
  180. (http-get-body (test-cuirass-uri "/specifications")))
  181. json->scm)
  182. (#(spec)
  183. (string=? (assoc-ref spec "name") "guix"))))
  184. (test-assert "/build/1"
  185. (lset= equal?
  186. (call-with-input-string
  187. (utf8->string
  188. (http-get-body (test-cuirass-uri "/build/1")))
  189. json->scm)
  190. (call-with-input-string
  191. (object->json-string build-query-result)
  192. json->scm)))
  193. (test-equal "/build/42"
  194. 404
  195. (response-code (http-get (test-cuirass-uri "/build/42"))))
  196. (test-equal "/build/42)"
  197. 404
  198. (response-code (http-get (test-cuirass-uri "/build/42)"))))
  199. (test-equal "/build/42/log/raw"
  200. 404
  201. (response-code (http-get (test-cuirass-uri "/build/42/log/raw"))))
  202. (test-equal "/build/42xx/log/raw"
  203. 404
  204. (response-code (http-get (test-cuirass-uri "/build/42xx/log/raw"))))
  205. (test-equal "/build/42/details"
  206. 404
  207. (response-code (http-get (test-cuirass-uri "/build/42/details"))))
  208. (test-equal "/build/42xx/details"
  209. 404
  210. (response-code (http-get (test-cuirass-uri "/build/42xx/details"))))
  211. (test-equal "/api/latestbuilds"
  212. 500
  213. (response-code (http-get (test-cuirass-uri "/api/latestbuilds"))))
  214. (test-assert "/api/latestbuilds?nr=1&jobset=guix"
  215. (match (json-string->scm
  216. (utf8->string
  217. (http-get-body
  218. (test-cuirass-uri
  219. "/api/latestbuilds?nr=1&jobset=guix"))))
  220. (#(build)
  221. (lset= equal? build
  222. (json-string->scm
  223. (object->json-string build-query-result))))))
  224. (test-equal "/api/latestbuilds?nr=1&jobset=gnu"
  225. #() ;the result should be an empty JSON array
  226. (json-string->scm
  227. (utf8->string
  228. (http-get-body
  229. (test-cuirass-uri
  230. "/api/latestbuilds?nr=1&jobset=gnu")))))
  231. (test-equal "/api/latestbuilds?nr&jobset=gnu"
  232. 500
  233. (response-code
  234. (http-get
  235. (test-cuirass-uri
  236. "/api/latestbuilds?nr&jobset=gnu"))))
  237. (test-equal "/api/queue?nr=100"
  238. `("fake-2.0" ,(build-status scheduled))
  239. (match (json-string->scm
  240. (utf8->string
  241. (http-get-body
  242. (test-cuirass-uri "/api/queue?nr=100"))))
  243. (#(dictionary)
  244. (list (assoc-ref dictionary "nixname")
  245. (assoc-ref dictionary "buildstatus")))))
  246. (test-equal "/api/evaluations?nr=1"
  247. (json-string->scm
  248. (object->json-string evaluations-query-result))
  249. (json-string->scm
  250. (utf8->string
  251. (http-get-body (test-cuirass-uri "/api/evaluations?nr=1")))))
  252. (test-assert "db-close"
  253. (begin
  254. (db-close (%db))
  255. #t)))