status.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019, 2022 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-status)
  19. #:use-module (guix status)
  20. #:use-module (srfi srfi-1)
  21. #:use-module (srfi srfi-64)
  22. #:use-module (srfi srfi-71)
  23. #:use-module (rnrs bytevectors)
  24. #:use-module (rnrs io ports)
  25. #:use-module (ice-9 match))
  26. (test-begin "status")
  27. (test-equal "compute-status, no-op"
  28. (build-status)
  29. (let ((port get-status (build-event-output-port compute-status)))
  30. (display "foo\nbar\n\baz\n" port)
  31. (get-status)))
  32. (test-equal "compute-status, builds + substitutes"
  33. (list (build-status
  34. (building (list (build "foo.drv" "x86_64-linux")))
  35. (downloading (list (download "bar" "http://example.org/bar"
  36. #:size 500
  37. #:start 'now))))
  38. (build-status
  39. (building (list (build "foo.drv" "x86_64-linux")))
  40. (downloading (list (download "bar" "http://example.org/bar"
  41. #:size 500
  42. #:transferred 42
  43. #:start 'now))))
  44. (build-status
  45. (builds-completed (list (build "foo.drv" "x86_64-linux")))
  46. (downloads-completed (list (download "bar" "http://example.org/bar"
  47. #:size 500
  48. #:transferred 500
  49. #:start 'now
  50. #:end 'now)))))
  51. (let ((port get-status
  52. (build-event-output-port (lambda (event status)
  53. (compute-status event status
  54. #:current-time
  55. (const 'now))))))
  56. (display "@ build-started foo.drv - x86_64-linux \n" port)
  57. (display "@ substituter-started bar\n" port)
  58. (display "@ download-started bar http://example.org/bar 500\n" port)
  59. (display "various\nthings\nget\nwritten\n" port)
  60. (let ((first (get-status)))
  61. (display "@ download-progress bar http://example.org/bar 500 42\n"
  62. port)
  63. (let ((second (get-status)))
  64. (display "@ download-progress bar http://example.org/bar 500 84\n"
  65. port)
  66. (display "@ build-succeeded foo.drv\n" port)
  67. (display "@ download-succeeded bar http://example.org/bar 500\n" port)
  68. (display "Almost done!\n" port)
  69. (display "@ substituter-succeeded bar\n" port)
  70. (list first second (get-status))))))
  71. (test-equal "compute-status, missing events"
  72. (list (build-status
  73. (building (list (build "foo.drv" "x86_64-linux"
  74. #:log-file "foo.log")))
  75. (downloading (list (download "baz" "http://example.org/baz"
  76. #:size 500
  77. #:transferred 42
  78. #:start 'now)
  79. (download "bar" "http://example.org/bar"
  80. #:size 999
  81. #:transferred 0
  82. #:start 'now))))
  83. (build-status
  84. (builds-completed (list (build "foo.drv" "x86_64-linux"
  85. #:log-file "foo.log")))
  86. (downloads-completed (list (download "baz" "http://example.org/baz"
  87. #:size 500
  88. #:transferred 500
  89. #:start 'now
  90. #:end 'now)
  91. (download "bar" "http://example.org/bar"
  92. #:size 999
  93. #:transferred 999
  94. #:start 'now
  95. #:end 'now)))))
  96. ;; Below we omit 'substituter-started' events and the like.
  97. (let ((port get-status
  98. (build-event-output-port (lambda (event status)
  99. (compute-status event status
  100. #:current-time
  101. (const 'now))))))
  102. (display "@ build-started foo.drv - x86_64-linux foo.log\n" port)
  103. (display "@ download-started bar http://example.org/bar 999\n" port)
  104. (display "various\nthings\nget\nwritten\n" port)
  105. (display "@ download-progress baz http://example.org/baz 500 42\n"
  106. port)
  107. (let ((first (get-status)))
  108. (display "@ build-succeeded foo.drv\n" port)
  109. (display "@ download-succeeded bar http://example.org/bar 999\n" port)
  110. (display "Almost done!\n" port)
  111. (display "@ substituter-succeeded baz\n" port)
  112. (list first (get-status)))))
  113. (test-equal "build-output-port, UTF-8"
  114. '((build-log #f "lambda is λ!\n"))
  115. (let ((port get-status (build-event-output-port cons '()))
  116. (bv (string->utf8 "lambda is λ!\n")))
  117. (put-bytevector port bv)
  118. (force-output port)
  119. (get-status)))
  120. (test-equal "build-output-port, daemon messages with LF"
  121. '((build-log #f "updating substitutes... 0%\r")
  122. (build-log #f "updating substitutes... 50%\r")
  123. (build-log #f "updating substitutes... 100%\r"))
  124. (let ((port get-status (build-event-output-port cons '())))
  125. (for-each (lambda (suffix)
  126. (let ((bv (string->utf8
  127. (string-append "updating substitutes... "
  128. suffix "\r"))))
  129. (put-bytevector port bv)
  130. (force-output port)))
  131. '("0%" "50%" "100%"))
  132. (reverse (get-status))))
  133. (test-equal "current-build-output-port, UTF-8 + garbage"
  134. ;; What about a mixture of UTF-8 + garbage?
  135. (let ((replacement "�"))
  136. `((build-log #f ,(string-append "garbage: " replacement "lambda: λ\n"))))
  137. (let ((port get-status (build-event-output-port cons '())))
  138. (display "garbage: " port)
  139. (put-bytevector port #vu8(128))
  140. (put-bytevector port (string->utf8 "lambda: λ\n"))
  141. (force-output port)
  142. (get-status)))
  143. (test-equal "compute-status, multiplexed build output"
  144. (list (build-status
  145. (building (list (build "foo.drv" "x86_64-linux" #:id 121)))
  146. (downloading (list (download "bar" "http://example.org/bar"
  147. #:size 999
  148. #:start 'now))))
  149. (build-status
  150. (building (list (build "foo.drv" "x86_64-linux" #:id 121)))
  151. (downloading (list (download "bar" "http://example.org/bar"
  152. #:size 999
  153. #:transferred 42
  154. #:start 'now))))
  155. (build-status
  156. ;; "bar" is now only listed as a download.
  157. (builds-completed (list (build "foo.drv" "x86_64-linux" #:id 121)))
  158. (downloads-completed (list (download "bar" "http://example.org/bar"
  159. #:size 999
  160. #:transferred 999
  161. #:start 'now
  162. #:end 'now)))))
  163. (let ((port get-status
  164. (build-event-output-port (lambda (event status)
  165. (compute-status event status
  166. #:current-time
  167. (const 'now)
  168. #:derivation-path->output-path
  169. (match-lambda
  170. ("bar.drv" "bar")))))))
  171. (display "@ build-started foo.drv - x86_64-linux 121\n" port)
  172. (display "@ build-started bar.drv - armhf-linux bar.log 144\n" port)
  173. (display "@ build-log 121 6\nHello!" port)
  174. (display "@ build-log 144 50
  175. @ download-started bar http://example.org/bar 999\n" port)
  176. (let ((first (get-status)))
  177. (display "@ build-log 121 30\n@ build-started FAKE!.drv 555\n")
  178. (display "@ build-log 144 54
  179. @ download-progress bar http://example.org/bar 999 42\n"
  180. port)
  181. (let ((second (get-status)))
  182. (display "@ download-succeeded bar http://example.org/bar 999\n" port)
  183. (display "@ build-succeeded foo.drv\n" port)
  184. (display "@ build-succeeded bar.drv\n" port)
  185. (list first second (get-status))))))
  186. (test-equal "compute-status, build completion"
  187. (list (build-status
  188. (building (list (build "foo.drv" "x86_64-linux" #:id 121))))
  189. (build-status
  190. (building (list (build "foo.drv" "x86_64-linux" #:id 121
  191. #:completion 0.))))
  192. (build-status
  193. (building (list (build "foo.drv" "x86_64-linux" #:id 121
  194. #:completion 50.))))
  195. (build-status
  196. (builds-completed (list (build "foo.drv" "x86_64-linux" #:id 121
  197. #:completion 100.)))))
  198. (let ((port get-status
  199. (build-event-output-port (lambda (event status)
  200. (compute-status event status
  201. #:current-time
  202. (const 'now))))))
  203. (display "@ build-started foo.drv - x86_64-linux 121\n" port)
  204. (display "@ build-log 121 6\nHello!" port)
  205. (let ((first (get-status)))
  206. (display "@ build-log 121 20\n[ 0/100] building X\n" port)
  207. (display "@ build-log 121 6\nHello!" port)
  208. (let ((second (get-status)))
  209. (display "@ build-log 121 20\n[50/100] building Y\n" port)
  210. (display "@ build-log 121 6\nHello!" port)
  211. (let ((third (get-status)))
  212. (display "@ build-log 121 21\n[100/100] building Z\n" port)
  213. (display "@ build-log 121 6\nHello!" port)
  214. (display "@ build-succeeded foo.drv\n" port)
  215. (list first second third (get-status)))))))
  216. (test-equal "compute-status, build phase"
  217. (list (build-status
  218. (building (list (build "foo.drv" "x86_64-linux" #:id 121
  219. #:phase 'configure))))
  220. (build-status
  221. (building (list (build "foo.drv" "x86_64-linux" #:id 121
  222. #:phase 'configure
  223. #:completion 50.))))
  224. (build-status
  225. (building (list (build "foo.drv" "x86_64-linux" #:id 121
  226. #:phase 'install))))
  227. (build-status
  228. (builds-completed (list (build "foo.drv" "x86_64-linux" #:id 121
  229. #:phase 'install)))))
  230. (let ((port get-status
  231. (build-event-output-port (lambda (event status)
  232. (compute-status event status
  233. #:current-time
  234. (const 'now))))))
  235. (display "@ build-started foo.drv - x86_64-linux 121\n" port)
  236. (display "@ build-log 121 27\nstarting phase `configure'\n" port)
  237. (display "@ build-log 121 6\nabcde!" port)
  238. (let ((first (get-status)))
  239. (display "@ build-log 121 20\n[50/100] building Y\n" port)
  240. (display "@ build-log 121 6\nfghik!" port)
  241. (let ((second (get-status)))
  242. (display "@ build-log 121 21\n[100/100] building Z\n" port)
  243. (display "@ build-log 121 25\nstarting phase `install'\n" port)
  244. (display "@ build-log 121 6\nlmnop!" port)
  245. (let ((third (get-status)))
  246. (display "@ build-succeeded foo.drv\n" port)
  247. (list first second third (get-status)))))))
  248. (test-end "status")