version-control.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Oleg Pykhalov <go.wigust@gmail.com>
  3. ;;; Copyright © 2017, 2018, 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
  5. ;;; Copyright © 2018 Christopher Baines <mail@cbaines.net>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; 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 GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu tests version-control)
  22. #:use-module (gnu tests)
  23. #:use-module (gnu system)
  24. #:use-module (gnu system file-systems)
  25. #:use-module (gnu system shadow)
  26. #:use-module (gnu system vm)
  27. #:use-module (gnu services)
  28. #:use-module (gnu services version-control)
  29. #:use-module (gnu services cgit)
  30. #:use-module (gnu services ssh)
  31. #:use-module (gnu services web)
  32. #:use-module (gnu services networking)
  33. #:use-module (gnu packages version-control)
  34. #:use-module (gnu packages ssh)
  35. #:use-module (guix gexp)
  36. #:use-module (guix store)
  37. #:use-module (guix modules)
  38. #:export (%test-cgit
  39. %test-git-http
  40. %test-gitolite))
  41. (define README-contents
  42. "Hello! This is what goes inside the 'README' file.")
  43. (define %make-git-repository
  44. ;; Create Git repository in /srv/git/test.
  45. (with-imported-modules (source-module-closure
  46. '((guix build utils)))
  47. #~(begin
  48. (use-modules (guix build utils))
  49. (let ((git (string-append #$git "/bin/git")))
  50. (mkdir-p "/tmp/test-repo")
  51. (with-directory-excursion "/tmp/test-repo"
  52. (call-with-output-file "/tmp/test-repo/README"
  53. (lambda (port)
  54. (display #$README-contents port)))
  55. (invoke git "config" "--global" "user.email" "charlie@example.org")
  56. (invoke git "config" "--global" "user.name" "A U Thor")
  57. (invoke git "init")
  58. (invoke git "add" ".")
  59. (invoke git "commit" "-m" "That's a commit."))
  60. (mkdir-p "/srv/git")
  61. (rename-file "/tmp/test-repo/.git" "/srv/git/test")))))
  62. (define %test-repository-service
  63. ;; Service that creates /srv/git/test.
  64. (simple-service 'make-git-repository activation-service-type
  65. %make-git-repository))
  66. (define %cgit-configuration-nginx
  67. (list
  68. (nginx-server-configuration
  69. (root cgit)
  70. (locations
  71. (list
  72. (nginx-location-configuration
  73. (uri "@cgit")
  74. (body '("fastcgi_param SCRIPT_FILENAME $document_root/lib/cgit/cgit.cgi;"
  75. "fastcgi_param PATH_INFO $uri;"
  76. "fastcgi_param QUERY_STRING $args;"
  77. "fastcgi_param HTTP_HOST $server_name;"
  78. "fastcgi_pass 127.0.0.1:9000;")))))
  79. (try-files (list "$uri" "@cgit"))
  80. (listen '("19418"))
  81. (ssl-certificate #f)
  82. (ssl-certificate-key #f))))
  83. (define %cgit-os
  84. ;; Operating system under test.
  85. (let ((base-os
  86. (simple-operating-system
  87. (service dhcp-client-service-type)
  88. (service cgit-service-type
  89. (cgit-configuration
  90. (nginx %cgit-configuration-nginx)))
  91. %test-repository-service)))
  92. (operating-system
  93. (inherit base-os)
  94. (packages (cons* git
  95. (operating-system-packages base-os))))))
  96. (define* (run-cgit-test #:optional (http-port 19418))
  97. "Run tests in %CGIT-OS, which has nginx running and listening on
  98. HTTP-PORT."
  99. (define os
  100. (marionette-operating-system
  101. %cgit-os
  102. #:imported-modules '((gnu services herd)
  103. (guix combinators))))
  104. (define vm
  105. (virtual-machine
  106. (operating-system os)
  107. (port-forwardings `((8080 . ,http-port)))))
  108. (define test
  109. (with-imported-modules '((gnu build marionette))
  110. #~(begin
  111. (use-modules (srfi srfi-11) (srfi srfi-64)
  112. (gnu build marionette)
  113. (web uri)
  114. (web client)
  115. (web response))
  116. (define marionette
  117. (make-marionette (list #$vm)))
  118. (mkdir #$output)
  119. (chdir #$output)
  120. (test-begin "cgit")
  121. ;; XXX: Shepherd reads the config file *before* binding its control
  122. ;; socket, so /var/run/shepherd/socket might not exist yet when the
  123. ;; 'marionette' service is started.
  124. (test-assert "shepherd socket ready"
  125. (marionette-eval
  126. `(begin
  127. (use-modules (gnu services herd))
  128. (let loop ((i 10))
  129. (cond ((file-exists? (%shepherd-socket-file))
  130. #t)
  131. ((> i 0)
  132. (sleep 1)
  133. (loop (- i 1)))
  134. (else
  135. 'failure))))
  136. marionette))
  137. ;; Wait for nginx to be up and running.
  138. (test-assert "nginx running"
  139. (marionette-eval
  140. '(begin
  141. (use-modules (gnu services herd))
  142. (start-service 'nginx))
  143. marionette))
  144. ;; Wait for fcgiwrap to be up and running.
  145. (test-assert "fcgiwrap running"
  146. (marionette-eval
  147. '(begin
  148. (use-modules (gnu services herd))
  149. (start-service 'fcgiwrap))
  150. marionette))
  151. ;; Make sure the PID file is created.
  152. (test-assert "PID file"
  153. (marionette-eval
  154. '(file-exists? "/var/run/nginx/pid")
  155. marionette))
  156. ;; Make sure the configuration file is created.
  157. (test-assert "configuration file"
  158. (marionette-eval
  159. '(file-exists? "/etc/cgitrc")
  160. marionette))
  161. ;; Make sure Git test repository is created.
  162. (test-assert "Git test repository"
  163. (marionette-eval
  164. '(file-exists? "/srv/git/test")
  165. marionette))
  166. ;; Make sure we can access pages that correspond to our repository.
  167. (letrec-syntax ((test-url
  168. (syntax-rules ()
  169. ((_ path code)
  170. (test-equal (string-append "GET " path)
  171. code
  172. (let-values (((response body)
  173. (http-get (string-append
  174. "http://localhost:8080"
  175. path))))
  176. (response-code response))))
  177. ((_ path)
  178. (test-url path 200)))))
  179. (test-url "/")
  180. (test-url "/test")
  181. (test-url "/test/log")
  182. (test-url "/test/tree")
  183. (test-url "/test/tree/README")
  184. (test-url "/test/does-not-exist" 404)
  185. (test-url "/test/tree/does-not-exist" 404)
  186. (test-url "/does-not-exist" 404))
  187. (test-end)
  188. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  189. (gexp->derivation "cgit-test" test))
  190. (define %test-cgit
  191. (system-test
  192. (name "cgit")
  193. (description "Connect to a running Cgit server.")
  194. (value (run-cgit-test))))
  195. ;;;
  196. ;;; Git server.
  197. ;;;
  198. (define %git-nginx-configuration
  199. (nginx-configuration
  200. (server-blocks
  201. (list
  202. (nginx-server-configuration
  203. (listen '("19418"))
  204. (ssl-certificate #f)
  205. (ssl-certificate-key #f)
  206. (locations
  207. (list (git-http-nginx-location-configuration
  208. (git-http-configuration (export-all? #t)
  209. (uri-path "/git"))))))))))
  210. (define %git-http-os
  211. (simple-operating-system
  212. (service dhcp-client-service-type)
  213. (service fcgiwrap-service-type)
  214. (service nginx-service-type %git-nginx-configuration)
  215. %test-repository-service))
  216. (define* (run-git-http-test #:optional (http-port 19418))
  217. (define os
  218. (marionette-operating-system
  219. %git-http-os
  220. #:imported-modules '((gnu services herd)
  221. (guix combinators))))
  222. (define vm
  223. (virtual-machine
  224. (operating-system os)
  225. (port-forwardings `((8080 . ,http-port)))))
  226. (define test
  227. (with-imported-modules '((gnu build marionette)
  228. (guix build utils))
  229. #~(begin
  230. (use-modules (srfi srfi-64)
  231. (rnrs io ports)
  232. (gnu build marionette)
  233. (guix build utils))
  234. (define marionette
  235. (make-marionette (list #$vm)))
  236. (mkdir #$output)
  237. (chdir #$output)
  238. (test-begin "git-http")
  239. ;; Wait for nginx to be up and running.
  240. (test-assert "nginx running"
  241. (marionette-eval
  242. '(begin
  243. (use-modules (gnu services herd))
  244. (start-service 'nginx))
  245. marionette))
  246. ;; Make sure Git test repository is created.
  247. (test-assert "Git test repository"
  248. (marionette-eval
  249. '(file-exists? "/srv/git/test")
  250. marionette))
  251. (test-assert "fcgiwrap listens"
  252. ;; Wait for fcgiwrap to be ready before cloning.
  253. (wait-for-tcp-port 9000 marionette))
  254. ;; Make sure we can clone the repo from the host.
  255. (test-equal "clone"
  256. '#$README-contents
  257. (begin
  258. (invoke #$(file-append git "/bin/git") "clone" "-v"
  259. "http://localhost:8080/git/test" "/tmp/clone")
  260. (call-with-input-file "/tmp/clone/README"
  261. get-string-all)))
  262. (test-end)
  263. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  264. (gexp->derivation "git-http" test))
  265. (define %test-git-http
  266. (system-test
  267. (name "git-http")
  268. (description "Connect to a running Git HTTP server.")
  269. (value (run-git-http-test))))
  270. ;;;
  271. ;;; Gitolite.
  272. ;;;
  273. (define %gitolite-test-admin-keypair
  274. (computed-file
  275. "gitolite-test-admin-keypair"
  276. (with-imported-modules (source-module-closure
  277. '((guix build utils)))
  278. #~(begin
  279. (use-modules (ice-9 match) (srfi srfi-26)
  280. (guix build utils))
  281. (mkdir #$output)
  282. (invoke #$(file-append openssh "/bin/ssh-keygen")
  283. "-f" (string-append #$output "/test-admin")
  284. "-t" "rsa"
  285. "-q"
  286. "-N" "")))))
  287. (define %gitolite-os
  288. (simple-operating-system
  289. (service dhcp-client-service-type)
  290. (service openssh-service-type)
  291. (service gitolite-service-type
  292. (gitolite-configuration
  293. (admin-pubkey
  294. (file-append %gitolite-test-admin-keypair "/test-admin.pub"))))))
  295. (define (run-gitolite-test)
  296. (define os
  297. (marionette-operating-system
  298. %gitolite-os
  299. #:imported-modules '((gnu services herd)
  300. (guix combinators))))
  301. (define vm
  302. (virtual-machine
  303. (operating-system os)
  304. (port-forwardings `((2222 . 22)))))
  305. (define test
  306. (with-imported-modules '((gnu build marionette)
  307. (guix build utils))
  308. #~(begin
  309. (use-modules (srfi srfi-64)
  310. (rnrs io ports)
  311. (gnu build marionette)
  312. (guix build utils))
  313. (define marionette
  314. (make-marionette (list #$vm)))
  315. (mkdir #$output)
  316. (chdir #$output)
  317. (test-begin "gitolite")
  318. ;; Wait for sshd to be up and running.
  319. (test-assert "service running"
  320. (marionette-eval
  321. '(begin
  322. (use-modules (gnu services herd))
  323. (start-service 'ssh-daemon))
  324. marionette))
  325. (display #$%gitolite-test-admin-keypair)
  326. (setenv "GIT_SSH_VARIANT" "ssh")
  327. (setenv "GIT_SSH_COMMAND"
  328. (string-join
  329. '(#$(file-append openssh "/bin/ssh")
  330. "-i" #$(file-append %gitolite-test-admin-keypair
  331. "/test-admin")
  332. "-o" "UserKnownHostsFile=/dev/null"
  333. "-o" "StrictHostKeyChecking=no")))
  334. (test-assert "cloning the admin repository"
  335. (invoke #$(file-append git "/bin/git")
  336. "clone" "-v"
  337. "ssh://git@localhost:2222/gitolite-admin"
  338. "/tmp/clone"))
  339. (test-assert "admin key exists"
  340. (file-exists? "/tmp/clone/keydir/test-admin.pub"))
  341. (with-directory-excursion "/tmp/clone"
  342. (invoke #$(file-append git "/bin/git")
  343. "-c" "user.name=Guix" "-c" "user.email=guix"
  344. "commit"
  345. "-m" "Test commit"
  346. "--allow-empty")
  347. (test-assert "pushing, and the associated hooks"
  348. (invoke #$(file-append git "/bin/git") "push")))
  349. (test-end)
  350. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  351. (gexp->derivation "gitolite" test))
  352. (define %test-gitolite
  353. (system-test
  354. (name "gitolite")
  355. (description "Clone the Gitolite admin repository.")
  356. (value (run-gitolite-test))))