version-control.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Oleg Pykhalov <go.wigust@gmail.com>
  3. ;;; Copyright © 2017, 2018, 2020, 2021 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. (marionette-eval
  143. '(begin
  144. (use-modules (gnu services herd))
  145. (start-service 'nginx))
  146. marionette))
  147. ;; Wait for fcgiwrap to be up and running.
  148. (test-assert "fcgiwrap running"
  149. (marionette-eval
  150. '(begin
  151. (use-modules (gnu services herd))
  152. (start-service 'fcgiwrap))
  153. marionette))
  154. ;; Make sure the PID file is created.
  155. (test-assert "PID file"
  156. (marionette-eval
  157. '(file-exists? "/var/run/nginx/pid")
  158. marionette))
  159. ;; Make sure the configuration file is created.
  160. (test-assert "configuration file"
  161. (marionette-eval
  162. '(file-exists? "/etc/cgitrc")
  163. marionette))
  164. ;; Make sure Git test repository is created.
  165. (test-assert "Git test repository"
  166. (marionette-eval
  167. '(file-exists? "/srv/git/test")
  168. marionette))
  169. ;; Make sure we can access pages that correspond to our repository.
  170. (letrec-syntax ((test-url
  171. (syntax-rules ()
  172. ((_ path code)
  173. (test-equal (string-append "GET " path)
  174. code
  175. (let-values (((response body)
  176. (http-get (string-append
  177. "http://localhost:8080"
  178. path))))
  179. (response-code response))))
  180. ((_ path)
  181. (test-url path 200)))))
  182. (test-url "/")
  183. (test-url "/test")
  184. (test-url "/test/log")
  185. (test-url "/test/tree")
  186. (test-url "/test/tree/README")
  187. (test-url "/test/does-not-exist" 404)
  188. (test-url "/test/tree/does-not-exist" 404)
  189. (test-url "/does-not-exist" 404))
  190. (test-end))))
  191. (gexp->derivation "cgit-test" test))
  192. (define %test-cgit
  193. (system-test
  194. (name "cgit")
  195. (description "Connect to a running Cgit server.")
  196. (value (run-cgit-test))))
  197. ;;;
  198. ;;; Git server.
  199. ;;;
  200. (define %git-nginx-configuration
  201. (nginx-configuration
  202. (server-blocks
  203. (list
  204. (nginx-server-configuration
  205. (listen '("19418"))
  206. (ssl-certificate #f)
  207. (ssl-certificate-key #f)
  208. (locations
  209. (list (git-http-nginx-location-configuration
  210. (git-http-configuration (export-all? #t)
  211. (uri-path "/git"))))))))))
  212. (define %git-http-os
  213. (simple-operating-system
  214. (service dhcp-client-service-type)
  215. (service fcgiwrap-service-type)
  216. (service nginx-service-type %git-nginx-configuration)
  217. %test-repository-service))
  218. (define* (run-git-http-test #:optional (http-port 19418))
  219. (define os
  220. (marionette-operating-system
  221. %git-http-os
  222. #:imported-modules '((gnu services herd)
  223. (guix combinators))))
  224. (define vm
  225. (virtual-machine
  226. (operating-system os)
  227. (port-forwardings `((8080 . ,http-port)))))
  228. (define test
  229. (with-imported-modules '((gnu build marionette)
  230. (guix build utils))
  231. #~(begin
  232. (use-modules (srfi srfi-64)
  233. (rnrs io ports)
  234. (gnu build marionette)
  235. (guix build utils))
  236. (define marionette
  237. (make-marionette (list #$vm)))
  238. (test-runner-current (system-test-runner #$output))
  239. (test-begin "git-http")
  240. ;; Wait for nginx to be up and running.
  241. (test-assert "nginx running"
  242. (marionette-eval
  243. '(begin
  244. (use-modules (gnu services herd))
  245. (start-service 'nginx))
  246. marionette))
  247. ;; Make sure Git test repository is created.
  248. (test-assert "Git test repository"
  249. (marionette-eval
  250. '(file-exists? "/srv/git/test")
  251. marionette))
  252. (test-assert "fcgiwrap listens"
  253. ;; Wait for fcgiwrap to be ready before cloning.
  254. (wait-for-tcp-port 9000 marionette))
  255. ;; Make sure we can clone the repo from the host.
  256. (test-equal "clone"
  257. '#$README-contents
  258. (begin
  259. (invoke #$(file-append git "/bin/git") "clone" "-v"
  260. "http://localhost:8080/git/test" "/tmp/clone")
  261. (call-with-input-file "/tmp/clone/README"
  262. get-string-all)))
  263. (test-end))))
  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. (test-runner-current (system-test-runner #$output))
  316. (test-begin "gitolite")
  317. ;; Wait for sshd to be up and running.
  318. (test-assert "service running"
  319. (marionette-eval
  320. '(begin
  321. (use-modules (gnu services herd))
  322. (start-service 'ssh-daemon))
  323. marionette))
  324. (display #$%gitolite-test-admin-keypair)
  325. (setenv "GIT_SSH_VARIANT" "ssh")
  326. (setenv "GIT_SSH_COMMAND"
  327. (string-join
  328. '(#$(file-append openssh "/bin/ssh")
  329. "-i" #$(file-append %gitolite-test-admin-keypair
  330. "/test-admin")
  331. "-o" "UserKnownHostsFile=/dev/null"
  332. "-o" "StrictHostKeyChecking=no")))
  333. (test-assert "cloning the admin repository"
  334. (invoke #$(file-append git "/bin/git")
  335. "clone" "-v"
  336. "ssh://git@localhost:2222/gitolite-admin"
  337. "/tmp/clone"))
  338. (test-assert "admin key exists"
  339. (file-exists? "/tmp/clone/keydir/test-admin.pub"))
  340. (with-directory-excursion "/tmp/clone"
  341. (invoke #$(file-append git "/bin/git")
  342. "-c" "user.name=Guix" "-c" "user.email=guix"
  343. "commit"
  344. "-m" "Test commit"
  345. "--allow-empty")
  346. (test-assert "pushing, and the associated hooks"
  347. (invoke #$(file-append git "/bin/git") "push")))
  348. (test-end))))
  349. (gexp->derivation "gitolite" test))
  350. (define %test-gitolite
  351. (system-test
  352. (name "gitolite")
  353. (description "Clone the Gitolite admin repository.")
  354. (value (run-gitolite-test))))
  355. ;;;
  356. ;;; Gitile.
  357. ;;;
  358. (define %gitile-configuration-nginx
  359. (nginx-server-configuration
  360. (root "/does/not/exists")
  361. (try-files (list "$uri" "=404"))
  362. (listen '("19418"))
  363. (ssl-certificate #f)
  364. (ssl-certificate-key #f)))
  365. (define %gitile-os
  366. ;; Operating system under test.
  367. (simple-operating-system
  368. (service dhcp-client-service-type)
  369. (simple-service 'srv-git activation-service-type
  370. #~(mkdir-p "/srv/git"))
  371. (service gitile-service-type
  372. (gitile-configuration
  373. (base-git-url "http://localhost")
  374. (repositories "/srv/git")
  375. (nginx %gitile-configuration-nginx)))
  376. %test-repository-service))
  377. (define* (run-gitile-test #:optional (http-port 19418))
  378. "Run tests in %GITOLITE-OS, which has nginx running and listening on
  379. HTTP-PORT."
  380. (define os
  381. (marionette-operating-system
  382. %gitile-os
  383. #:imported-modules '((gnu services herd)
  384. (guix combinators))))
  385. (define vm
  386. (virtual-machine
  387. (operating-system os)
  388. (port-forwardings `((8081 . ,http-port)))
  389. (memory-size 1024)))
  390. (define test
  391. (with-imported-modules '((gnu build marionette))
  392. #~(begin
  393. (use-modules (srfi srfi-11) (srfi srfi-64)
  394. (gnu build marionette)
  395. (web uri)
  396. (web client)
  397. (web response))
  398. (define marionette
  399. (make-marionette (list #$vm)))
  400. (test-runner-current (system-test-runner #$output))
  401. (test-begin "gitile")
  402. ;; XXX: Shepherd reads the config file *before* binding its control
  403. ;; socket, so /var/run/shepherd/socket might not exist yet when the
  404. ;; 'marionette' service is started.
  405. (test-assert "shepherd socket ready"
  406. (marionette-eval
  407. `(begin
  408. (use-modules (gnu services herd))
  409. (let loop ((i 10))
  410. (cond ((file-exists? (%shepherd-socket-file))
  411. #t)
  412. ((> i 0)
  413. (sleep 1)
  414. (loop (- i 1)))
  415. (else
  416. 'failure))))
  417. marionette))
  418. ;; Wait for nginx to be up and running.
  419. (test-assert "nginx running"
  420. (marionette-eval
  421. '(begin
  422. (use-modules (gnu services herd))
  423. (start-service 'nginx))
  424. marionette))
  425. ;; Make sure the PID file is created.
  426. (test-assert "PID file"
  427. (marionette-eval
  428. '(file-exists? "/var/run/nginx/pid")
  429. marionette))
  430. ;; Make sure Git test repository is created.
  431. (test-assert "Git test repository"
  432. (marionette-eval
  433. '(file-exists? "/srv/git/test")
  434. marionette))
  435. (sleep 2)
  436. ;; Make sure we can access pages that correspond to our repository.
  437. (letrec-syntax ((test-url
  438. (syntax-rules ()
  439. ((_ path code)
  440. (test-equal (string-append "GET " path)
  441. code
  442. (let-values (((response body)
  443. (http-get (string-append
  444. "http://localhost:8081"
  445. path))))
  446. (response-code response))))
  447. ((_ path)
  448. (test-url path 200)))))
  449. (test-url "/")
  450. (test-url "/css/gitile.css")
  451. (test-url "/test")
  452. (test-url "/test/commits")
  453. (test-url "/test/tree" 404)
  454. (test-url "/test/tree/-")
  455. (test-url "/test/tree/-/README")
  456. (test-url "/test/does-not-exist" 404)
  457. (test-url "/test/tree/-/does-not-exist" 404)
  458. (test-url "/does-not-exist" 404))
  459. (test-end))))
  460. (gexp->derivation "gitile-test" test))
  461. (define %test-gitile
  462. (system-test
  463. (name "gitile")
  464. (description "Connect to a running Gitile server.")
  465. (value (run-gitile-test))))