version-control.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Oleg Pykhalov <go.wigust@gmail.com>
  3. ;;; Copyright © 2017-2018, 2020-2022 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. %test-gitile))
  42. (define README-contents
  43. "Hello! This is what goes inside the 'README' file.")
  44. (define %make-git-repository
  45. ;; Create Git repository in /srv/git/test.
  46. (with-imported-modules (source-module-closure
  47. '((guix build utils)))
  48. #~(begin
  49. (use-modules (guix build utils))
  50. (let ((git (string-append #$git "/bin/git")))
  51. (mkdir-p "/tmp/test-repo")
  52. (with-directory-excursion "/tmp/test-repo"
  53. (call-with-output-file "/tmp/test-repo/README"
  54. (lambda (port)
  55. (display #$README-contents port)))
  56. (invoke git "config" "--global" "user.email" "charlie@example.org")
  57. (invoke git "config" "--global" "user.name" "A U Thor")
  58. (invoke git "init")
  59. (invoke git "add" ".")
  60. (invoke git "commit" "-m" "That's a commit."))
  61. (mkdir-p "/srv/git")
  62. (rename-file "/tmp/test-repo/.git" "/srv/git/test")
  63. (with-output-to-file "/srv/git/test/git-daemon-export-ok"
  64. (lambda _
  65. (display "")))))))
  66. (define %test-repository-service
  67. ;; Service that creates /srv/git/test.
  68. (simple-service 'make-git-repository activation-service-type
  69. %make-git-repository))
  70. (define %cgit-configuration-nginx
  71. (list
  72. (nginx-server-configuration
  73. (root cgit)
  74. (locations
  75. (list
  76. (nginx-location-configuration
  77. (uri "@cgit")
  78. (body '("fastcgi_param SCRIPT_FILENAME $document_root/lib/cgit/cgit.cgi;"
  79. "fastcgi_param PATH_INFO $uri;"
  80. "fastcgi_param QUERY_STRING $args;"
  81. "fastcgi_param HTTP_HOST $server_name;"
  82. "fastcgi_pass 127.0.0.1:9000;")))))
  83. (try-files (list "$uri" "@cgit"))
  84. (listen '("19418"))
  85. (ssl-certificate #f)
  86. (ssl-certificate-key #f))))
  87. (define %cgit-os
  88. ;; Operating system under test.
  89. (let ((base-os
  90. (simple-operating-system
  91. (service dhcp-client-service-type)
  92. (service cgit-service-type
  93. (cgit-configuration
  94. (nginx %cgit-configuration-nginx)))
  95. %test-repository-service)))
  96. (operating-system
  97. (inherit base-os)
  98. (packages (cons* git
  99. (operating-system-packages base-os))))))
  100. (define* (run-cgit-test #:optional (http-port 19418))
  101. "Run tests in %CGIT-OS, which has nginx running and listening on
  102. HTTP-PORT."
  103. (define os
  104. (marionette-operating-system
  105. %cgit-os
  106. #:imported-modules '((gnu services herd)
  107. (guix combinators))))
  108. (define vm
  109. (virtual-machine
  110. (operating-system os)
  111. (port-forwardings `((8080 . ,http-port)))))
  112. (define test
  113. (with-imported-modules '((gnu build marionette))
  114. #~(begin
  115. (use-modules (srfi srfi-11) (srfi srfi-64)
  116. (gnu build marionette)
  117. (web uri)
  118. (web client)
  119. (web response))
  120. (define marionette
  121. (make-marionette (list #$vm)))
  122. (test-runner-current (system-test-runner #$output))
  123. (test-begin "cgit")
  124. ;; XXX: Shepherd reads the config file *before* binding its control
  125. ;; socket, so /var/run/shepherd/socket might not exist yet when the
  126. ;; 'marionette' service is started.
  127. (test-assert "shepherd socket ready"
  128. (marionette-eval
  129. `(begin
  130. (use-modules (gnu services herd))
  131. (let loop ((i 10))
  132. (cond ((file-exists? (%shepherd-socket-file))
  133. #t)
  134. ((> i 0)
  135. (sleep 1)
  136. (loop (- i 1)))
  137. (else
  138. 'failure))))
  139. marionette))
  140. ;; Wait for nginx to be up and running.
  141. (test-assert "nginx running"
  142. (wait-for-file "/var/run/nginx/pid" marionette))
  143. ;; Wait for fcgiwrap to be up and running.
  144. (test-assert "fcgiwrap running"
  145. (wait-for-tcp-port 9000 marionette))
  146. ;; Make sure the PID file is created.
  147. (test-assert "PID file"
  148. (marionette-eval
  149. '(file-exists? "/var/run/nginx/pid")
  150. marionette))
  151. ;; Make sure the configuration file is created.
  152. (test-assert "configuration file"
  153. (marionette-eval
  154. '(file-exists? "/etc/cgitrc")
  155. marionette))
  156. ;; Make sure Git test repository is created.
  157. (test-assert "Git test repository"
  158. (marionette-eval
  159. '(file-exists? "/srv/git/test")
  160. marionette))
  161. ;; Make sure we can access pages that correspond to our repository.
  162. (letrec-syntax ((test-url
  163. (syntax-rules ()
  164. ((_ path code)
  165. (test-equal (string-append "GET " path)
  166. code
  167. (let-values (((response body)
  168. (http-get (string-append
  169. "http://localhost:8080"
  170. path))))
  171. (response-code response))))
  172. ((_ path)
  173. (test-url path 200)))))
  174. (test-url "/")
  175. (test-url "/test")
  176. (test-url "/test/log")
  177. (test-url "/test/tree")
  178. (test-url "/test/tree/README")
  179. (test-url "/test/does-not-exist" 404)
  180. (test-url "/test/tree/does-not-exist" 404)
  181. (test-url "/does-not-exist" 404))
  182. (test-end))))
  183. (gexp->derivation "cgit-test" test))
  184. (define %test-cgit
  185. (system-test
  186. (name "cgit")
  187. (description "Connect to a running Cgit server.")
  188. (value (run-cgit-test))))
  189. ;;;
  190. ;;; Git server.
  191. ;;;
  192. (define %git-nginx-configuration
  193. (nginx-configuration
  194. (server-blocks
  195. (list
  196. (nginx-server-configuration
  197. (listen '("19418"))
  198. (ssl-certificate #f)
  199. (ssl-certificate-key #f)
  200. (locations
  201. (list (git-http-nginx-location-configuration
  202. (git-http-configuration (export-all? #t)
  203. (uri-path "/git"))))))))))
  204. (define %git-http-os
  205. (simple-operating-system
  206. (service dhcp-client-service-type)
  207. (service fcgiwrap-service-type)
  208. (service nginx-service-type %git-nginx-configuration)
  209. %test-repository-service))
  210. (define* (run-git-http-test #:optional (http-port 19418))
  211. (define os
  212. (marionette-operating-system
  213. %git-http-os
  214. #:imported-modules '((gnu services herd)
  215. (guix combinators))))
  216. (define vm
  217. (virtual-machine
  218. (operating-system os)
  219. (port-forwardings `((8080 . ,http-port)))))
  220. (define test
  221. (with-imported-modules '((gnu build marionette)
  222. (guix build utils))
  223. #~(begin
  224. (use-modules (srfi srfi-64)
  225. (rnrs io ports)
  226. (gnu build marionette)
  227. (guix build utils))
  228. (define marionette
  229. (make-marionette (list #$vm)))
  230. (test-runner-current (system-test-runner #$output))
  231. (test-begin "git-http")
  232. ;; Wait for nginx to be up and running.
  233. (test-assert "nginx running"
  234. (wait-for-file "/var/run/nginx/pid" marionette))
  235. ;; Make sure Git test repository is created.
  236. (test-assert "Git test repository"
  237. (marionette-eval
  238. '(file-exists? "/srv/git/test")
  239. marionette))
  240. (test-assert "fcgiwrap listens"
  241. ;; Wait for fcgiwrap to be ready before cloning.
  242. (wait-for-tcp-port 9000 marionette))
  243. ;; Make sure we can clone the repo from the host.
  244. (test-equal "clone"
  245. '#$README-contents
  246. (begin
  247. (invoke #$(file-append git "/bin/git") "clone" "-v"
  248. "http://localhost:8080/git/test" "/tmp/clone")
  249. (call-with-input-file "/tmp/clone/README"
  250. get-string-all)))
  251. (test-end))))
  252. (gexp->derivation "git-http" test))
  253. (define %test-git-http
  254. (system-test
  255. (name "git-http")
  256. (description "Connect to a running Git HTTP server.")
  257. (value (run-git-http-test))))
  258. ;;;
  259. ;;; Gitolite.
  260. ;;;
  261. (define %gitolite-test-admin-keypair
  262. (computed-file
  263. "gitolite-test-admin-keypair"
  264. (with-imported-modules (source-module-closure
  265. '((guix build utils)))
  266. #~(begin
  267. (use-modules (ice-9 match) (srfi srfi-26)
  268. (guix build utils))
  269. (mkdir #$output)
  270. (invoke #$(file-append openssh "/bin/ssh-keygen")
  271. "-f" (string-append #$output "/test-admin")
  272. "-t" "rsa"
  273. "-q"
  274. "-N" "")))))
  275. (define %gitolite-os
  276. (simple-operating-system
  277. (service dhcp-client-service-type)
  278. (service openssh-service-type)
  279. (service gitolite-service-type
  280. (gitolite-configuration
  281. (admin-pubkey
  282. (file-append %gitolite-test-admin-keypair "/test-admin.pub"))))))
  283. (define (run-gitolite-test)
  284. (define os
  285. (marionette-operating-system
  286. %gitolite-os
  287. #:imported-modules '((gnu services herd)
  288. (guix combinators))))
  289. (define vm
  290. (virtual-machine
  291. (operating-system os)
  292. (port-forwardings `((2222 . 22)))))
  293. (define test
  294. (with-imported-modules '((gnu build marionette)
  295. (guix build utils))
  296. #~(begin
  297. (use-modules (srfi srfi-64)
  298. (rnrs io ports)
  299. (gnu build marionette)
  300. (guix build utils))
  301. (define marionette
  302. (make-marionette (list #$vm)))
  303. (test-runner-current (system-test-runner #$output))
  304. (test-begin "gitolite")
  305. ;; Wait for sshd to be up and running.
  306. (test-assert "service running"
  307. (marionette-eval
  308. '(begin
  309. (use-modules (gnu services herd))
  310. (start-service 'ssh-daemon))
  311. marionette))
  312. (display #$%gitolite-test-admin-keypair)
  313. (setenv "GIT_SSH_VARIANT" "ssh")
  314. (setenv "GIT_SSH_COMMAND"
  315. (string-join
  316. '(#$(file-append openssh "/bin/ssh")
  317. "-i" #$(file-append %gitolite-test-admin-keypair
  318. "/test-admin")
  319. "-o" "UserKnownHostsFile=/dev/null"
  320. "-o" "StrictHostKeyChecking=no")))
  321. (test-assert "cloning the admin repository"
  322. (invoke #$(file-append git "/bin/git")
  323. "clone" "-v"
  324. "ssh://git@localhost:2222/gitolite-admin"
  325. "/tmp/clone"))
  326. (test-assert "admin key exists"
  327. (file-exists? "/tmp/clone/keydir/test-admin.pub"))
  328. (with-directory-excursion "/tmp/clone"
  329. (invoke #$(file-append git "/bin/git")
  330. "-c" "user.name=Guix" "-c" "user.email=guix"
  331. "commit"
  332. "-m" "Test commit"
  333. "--allow-empty")
  334. (test-assert "pushing, and the associated hooks"
  335. (invoke #$(file-append git "/bin/git") "push")))
  336. (test-end))))
  337. (gexp->derivation "gitolite" test))
  338. (define %test-gitolite
  339. (system-test
  340. (name "gitolite")
  341. (description "Clone the Gitolite admin repository.")
  342. (value (run-gitolite-test))))
  343. ;;;
  344. ;;; Gitile.
  345. ;;;
  346. (define %gitile-configuration-nginx
  347. (nginx-server-configuration
  348. (root "/does/not/exists")
  349. (try-files (list "$uri" "=404"))
  350. (listen '("19418"))
  351. (ssl-certificate #f)
  352. (ssl-certificate-key #f)))
  353. (define %gitile-os
  354. ;; Operating system under test.
  355. (simple-operating-system
  356. (service dhcp-client-service-type)
  357. (simple-service 'srv-git activation-service-type
  358. #~(mkdir-p "/srv/git"))
  359. (service gitile-service-type
  360. (gitile-configuration
  361. (base-git-url "http://localhost")
  362. (repositories "/srv/git")
  363. (nginx %gitile-configuration-nginx)))
  364. %test-repository-service))
  365. (define* (run-gitile-test #:optional (http-port 19418))
  366. "Run tests in %GITOLITE-OS, which has nginx running and listening on
  367. HTTP-PORT."
  368. (define os
  369. (marionette-operating-system
  370. %gitile-os
  371. #:imported-modules '((gnu services herd)
  372. (guix combinators))))
  373. (define vm
  374. (virtual-machine
  375. (operating-system os)
  376. (port-forwardings `((8081 . ,http-port)))
  377. (memory-size 1024)))
  378. (define test
  379. (with-imported-modules '((gnu build marionette))
  380. #~(begin
  381. (use-modules (srfi srfi-11) (srfi srfi-64)
  382. (gnu build marionette)
  383. (web uri)
  384. (web client)
  385. (web response))
  386. (define marionette
  387. (make-marionette (list #$vm)))
  388. (test-runner-current (system-test-runner #$output))
  389. (test-begin "gitile")
  390. ;; XXX: Shepherd reads the config file *before* binding its control
  391. ;; socket, so /var/run/shepherd/socket might not exist yet when the
  392. ;; 'marionette' service is started.
  393. (test-assert "shepherd socket ready"
  394. (marionette-eval
  395. `(begin
  396. (use-modules (gnu services herd))
  397. (let loop ((i 10))
  398. (cond ((file-exists? (%shepherd-socket-file))
  399. #t)
  400. ((> i 0)
  401. (sleep 1)
  402. (loop (- i 1)))
  403. (else
  404. 'failure))))
  405. marionette))
  406. ;; Wait for nginx to be up and running.
  407. (test-assert "nginx running"
  408. (wait-for-file "/var/run/nginx/pid" marionette))
  409. ;; Make sure Git test repository is created.
  410. (test-assert "Git test repository"
  411. (marionette-eval
  412. '(file-exists? "/srv/git/test")
  413. marionette))
  414. (sleep 2)
  415. ;; Make sure we can access pages that correspond to our repository.
  416. (letrec-syntax ((test-url
  417. (syntax-rules ()
  418. ((_ path code)
  419. (test-equal (string-append "GET " path)
  420. code
  421. (let-values (((response body)
  422. (http-get (string-append
  423. "http://localhost:8081"
  424. path))))
  425. (response-code response))))
  426. ((_ path)
  427. (test-url path 200)))))
  428. (test-url "/")
  429. (test-url "/css/gitile.css")
  430. (test-url "/test")
  431. (test-url "/test/commits")
  432. (test-url "/test/tree" 404)
  433. (test-url "/test/tree/-")
  434. (test-url "/test/tree/-/README")
  435. (test-url "/test/does-not-exist" 404)
  436. (test-url "/test/tree/-/does-not-exist" 404)
  437. (test-url "/does-not-exist" 404))
  438. (test-end))))
  439. (gexp->derivation "gitile-test" test))
  440. (define %test-gitile
  441. (system-test
  442. (name "gitile")
  443. (description "Connect to a running Gitile server.")
  444. (value (run-gitile-test))))