cuirass.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020, 2021 Mathieu Othacehe <othacehe@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 (gnu tests cuirass)
  19. #:use-module (gnu tests)
  20. #:use-module (gnu system)
  21. #:use-module (gnu system vm)
  22. #:use-module (gnu system install)
  23. #:use-module (gnu packages databases)
  24. #:use-module (gnu packages guile)
  25. #:use-module (gnu packages version-control)
  26. #:use-module (gnu services)
  27. #:use-module (gnu services avahi)
  28. #:use-module (gnu services base)
  29. #:use-module (gnu services cuirass)
  30. #:use-module (gnu services databases)
  31. #:use-module (gnu services networking)
  32. #:use-module (gnu system nss)
  33. #:use-module (guix channels)
  34. #:use-module (guix gexp)
  35. #:use-module (guix store)
  36. #:export (%cuirass-test
  37. %cuirass-remote-test
  38. %cuirass-simple-test))
  39. (define %derivation-file
  40. (scheme-file
  41. "derivation.scm"
  42. '(begin
  43. (use-modules (guix)
  44. (srfi srfi-1)
  45. (ice-9 match))
  46. (define (derivation->alist store drv)
  47. `((#:derivation . ,(derivation-file-name drv))
  48. (#:log . ,(log-file store (derivation-file-name drv)))
  49. (#:outputs . ,(filter-map (lambda (res)
  50. (match res
  51. ((name . path)
  52. `(,name . ,path))))
  53. (derivation->output-paths drv)))
  54. (#:nix-name . ,(derivation-name drv))
  55. (#:system . ,(derivation-system drv))
  56. (#:max-silent-time . 3600)
  57. (#:timeout . 3600)))
  58. (define (cuirass-jobs store arguments)
  59. (let* ((file (plain-file "test" "this is a test derivation"))
  60. (job-name "test-job")
  61. (drv (run-with-store store
  62. (gexp->derivation
  63. job-name
  64. #~(begin
  65. (mkdir #$output)
  66. (symlink #$file
  67. (string-append #$output "/file")))))))
  68. (list (lambda ()
  69. `((#:job-name . ,job-name)
  70. ,@(derivation->alist store drv)))))))))
  71. (define git-service
  72. ;; Create a Git repository to host Cuirass' specification.
  73. (simple-service
  74. 'create-git-directory activation-service-type
  75. #~(begin
  76. (let* ((git (string-append #$git "/bin/git"))
  77. (main "/tmp/cuirass-main")
  78. (file (string-append main "/build-aux/cuirass/gnu-system.scm")))
  79. (mkdir-p (dirname file))
  80. (with-directory-excursion main
  81. (copy-file #$%derivation-file file)
  82. (invoke git "config" "--global" "user.email"
  83. "charlie@example.org")
  84. (invoke git "config" "--global" "user.name" "A U Thor")
  85. (invoke git "init")
  86. (invoke git "add" ".")
  87. (invoke git "commit" "-m" "That's a commit."))))))
  88. (define cow-service
  89. ;; The Guix-daemon & Cuirass will complain if the store is
  90. ;; read-only. Create a store overlay to solve this issue.
  91. (simple-service
  92. 'mount-cow-store activation-service-type
  93. #~(begin
  94. (use-modules (guix build syscalls)
  95. (guix build utils))
  96. (mkdir-p "/rw-store")
  97. (mount "none" "/rw-store" "tmpfs")
  98. (mkdir-p "/rw-store/upper")
  99. (mkdir-p "/rw-store/work")
  100. (mount "none" "/gnu/store" "overlay" 0
  101. "lowerdir=/gnu/store,upperdir=/rw-store/upper,workdir=/rw-store/work"))))
  102. (define %cuirass-specs
  103. #~(list
  104. '((#:name . "test")
  105. (#:load-path-inputs . ())
  106. (#:package-path-inputs . ())
  107. (#:proc-input . "main")
  108. (#:proc-file . "build-aux/cuirass/gnu-system.scm")
  109. (#:proc . cuirass-jobs)
  110. (#:proc-args . ())
  111. (#:inputs . (((#:name . "main")
  112. (#:url . "file:///tmp/cuirass-main/")
  113. (#:load-path . ".")
  114. (#:branch . "master")
  115. (#:no-compile? . #t))))
  116. (#:build-outputs . ())
  117. (#:priority . 1))))
  118. (define* (cuirass-services #:key remote-build?)
  119. (list
  120. (service cuirass-service-type
  121. (cuirass-configuration
  122. (specifications %cuirass-specs)
  123. (remote-server (and remote-build?
  124. (cuirass-remote-server-configuration)))
  125. (host "0.0.0.0")
  126. (use-substitutes? #t)))))
  127. (define (run-cuirass-test name os)
  128. (define os*
  129. (let ((modules '((gnu services herd)
  130. (guix combinators)
  131. (guix build syscalls)
  132. (guix build utils))))
  133. (marionette-operating-system
  134. os
  135. #:imported-modules modules)))
  136. (define cuirass-web-port 8081)
  137. (define forward-port 5000)
  138. (define vm
  139. (virtual-machine
  140. (operating-system os*)
  141. (memory-size 1024)
  142. (port-forwardings `((,forward-port . ,cuirass-web-port)))))
  143. (define test
  144. (with-extensions (list guile-json-4)
  145. (with-imported-modules '((gnu build marionette))
  146. #~(begin
  147. (use-modules (srfi srfi-11) (srfi srfi-64)
  148. (gnu build marionette)
  149. (ice-9 match)
  150. (ice-9 rdelim)
  151. (json)
  152. (rnrs bytevectors)
  153. (web client) (web response))
  154. (define marionette
  155. (make-marionette (list #$vm)))
  156. (define (query path)
  157. (http-get
  158. (format #f "http://localhost:~a~a"
  159. #$(number->string forward-port)
  160. path)))
  161. (define* (retry f #:key times delay)
  162. (let loop ((attempt 1))
  163. (let ((result (f)))
  164. (cond
  165. (result result)
  166. (else
  167. (if (>= attempt times)
  168. #f
  169. (begin
  170. (sleep delay)
  171. (loop (+ 1 attempt)))))))))
  172. (mkdir #$output)
  173. (chdir #$output)
  174. (test-begin "cuirass")
  175. ;; XXX: Shepherd reads the config file *before* binding its
  176. ;; control socket, so /var/run/shepherd/socket might not exist yet
  177. ;; when the 'marionette' service is started.
  178. (test-assert "shepherd socket ready"
  179. (marionette-eval
  180. `(begin
  181. (use-modules (gnu services herd))
  182. (let loop ((i 10))
  183. (cond ((file-exists? (%shepherd-socket-file))
  184. #t)
  185. ((> i 0)
  186. (sleep 1)
  187. (loop (- i 1)))
  188. (else
  189. 'failure))))
  190. marionette))
  191. ;; Wait for cuirass to be up and running.
  192. (test-assert "cuirass running"
  193. (marionette-eval
  194. '(begin
  195. (use-modules (gnu services herd))
  196. (start-service 'cuirass)
  197. #t)
  198. marionette))
  199. (test-assert "cuirass-web running"
  200. (begin
  201. (wait-for-tcp-port #$cuirass-web-port marionette)
  202. (retry
  203. (lambda ()
  204. (let-values (((response text)
  205. (query "/")))
  206. (eq? (response-code response) 200)))
  207. #:times 5
  208. #:delay 5)))
  209. (test-assert "cuirass-web evaluation"
  210. (begin
  211. (retry
  212. (lambda ()
  213. (let-values (((response text)
  214. (query "/api/evaluation?id=1")))
  215. (let ((result
  216. (false-if-exception
  217. (json-string->scm
  218. (utf8->string text)))))
  219. (eq? (and result
  220. (assoc-ref result "id"))
  221. 1))))
  222. #:times 5
  223. #:delay 5)))
  224. ;; Even though there's a store overlay, the Guix database is not
  225. ;; initialized, meaning that we won't be able to perform the
  226. ;; build. Check at least that it is queued.
  227. (test-assert "cuirass-web build queued"
  228. (begin
  229. (retry
  230. (lambda ()
  231. (let-values (((response text)
  232. (query "/api/queue?nr=1")))
  233. (let ((result
  234. (json-string->scm
  235. (utf8->string text))))
  236. (match (vector->list result)
  237. ((build)
  238. (string=? (assoc-ref build "job") "test-job"))
  239. (else #f)))))
  240. #:times 5
  241. #:delay 10)))
  242. (test-end)
  243. (exit (= (test-runner-fail-count (test-runner-current)) 0))))))
  244. (gexp->derivation name test))
  245. (define %cuirass-test
  246. (let ((os (operating-system
  247. (inherit %simple-os)
  248. (services
  249. (append (list cow-service
  250. (service dhcp-client-service-type)
  251. git-service)
  252. (cuirass-services)
  253. (operating-system-user-services %simple-os))))))
  254. (system-test
  255. (name "cuirass")
  256. (description "Connect to a Cuirass server.")
  257. (value
  258. (run-cuirass-test name os)))))
  259. (define %cuirass-remote-test
  260. (let ((os (operating-system
  261. (inherit %simple-os)
  262. (name-service-switch %mdns-host-lookup-nss)
  263. (services
  264. (append (list (service avahi-service-type)
  265. cow-service
  266. (service dhcp-client-service-type)
  267. git-service)
  268. (cuirass-services #:remote-build? #t)
  269. (operating-system-user-services %simple-os))))))
  270. (system-test
  271. (name "cuirass-remote")
  272. (description "Connect to a Cuirass server with remote build.")
  273. (value (run-cuirass-test name os)))))
  274. (define simple-cuirass-service
  275. (service cuirass-service-type
  276. (cuirass-configuration
  277. (specifications
  278. (simple-cuirass-configuration->specs
  279. (simple-cuirass-configuration
  280. (build 'all)
  281. (channels
  282. (list (channel
  283. (name 'guix)
  284. (url "file:///tmp/cuirass-main/")))))))
  285. (host "0.0.0.0")
  286. (use-substitutes? #t))))
  287. (define %cuirass-simple-test
  288. (let ((os (operating-system
  289. (inherit %simple-os)
  290. (services
  291. (append
  292. (list cow-service
  293. (service dhcp-client-service-type)
  294. git-service
  295. simple-cuirass-service)
  296. (operating-system-user-services %simple-os))))))
  297. (system-test
  298. (name "cuirass-simple")
  299. (description "Connect to a simple Cuirass server.")
  300. (value
  301. (run-cuirass-test name os)))))