version-control.scm 18 KB

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