version-control.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Oleg Pykhalov <go.wigust@gmail.com>
  3. ;;; Copyright © 2017, 2018 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. ;; Make sure we can clone the repo from the host.
  252. (test-equal "clone"
  253. '#$README-contents
  254. (begin
  255. (invoke #$(file-append git "/bin/git") "clone" "-v"
  256. "http://localhost:8080/git/test" "/tmp/clone")
  257. (call-with-input-file "/tmp/clone/README"
  258. get-string-all)))
  259. (test-end)
  260. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  261. (gexp->derivation "git-http" test))
  262. (define %test-git-http
  263. (system-test
  264. (name "git-http")
  265. (description "Connect to a running Git HTTP server.")
  266. (value (run-git-http-test))))
  267. ;;;
  268. ;;; Gitolite.
  269. ;;;
  270. (define %gitolite-test-admin-keypair
  271. (computed-file
  272. "gitolite-test-admin-keypair"
  273. (with-imported-modules (source-module-closure
  274. '((guix build utils)))
  275. #~(begin
  276. (use-modules (ice-9 match) (srfi srfi-26)
  277. (guix build utils))
  278. (mkdir #$output)
  279. (invoke #$(file-append openssh "/bin/ssh-keygen")
  280. "-f" (string-append #$output "/test-admin")
  281. "-t" "rsa"
  282. "-q"
  283. "-N" "")))))
  284. (define %gitolite-os
  285. (simple-operating-system
  286. (service dhcp-client-service-type)
  287. (service openssh-service-type)
  288. (service gitolite-service-type
  289. (gitolite-configuration
  290. (admin-pubkey
  291. (file-append %gitolite-test-admin-keypair "/test-admin.pub"))))))
  292. (define (run-gitolite-test)
  293. (define os
  294. (marionette-operating-system
  295. %gitolite-os
  296. #:imported-modules '((gnu services herd)
  297. (guix combinators))))
  298. (define vm
  299. (virtual-machine
  300. (operating-system os)
  301. (port-forwardings `((2222 . 22)))))
  302. (define test
  303. (with-imported-modules '((gnu build marionette)
  304. (guix build utils))
  305. #~(begin
  306. (use-modules (srfi srfi-64)
  307. (rnrs io ports)
  308. (gnu build marionette)
  309. (guix build utils))
  310. (define marionette
  311. (make-marionette (list #$vm)))
  312. (mkdir #$output)
  313. (chdir #$output)
  314. (test-begin "gitolite")
  315. ;; Wait for sshd to be up and running.
  316. (test-assert "service running"
  317. (marionette-eval
  318. '(begin
  319. (use-modules (gnu services herd))
  320. (start-service 'ssh-daemon))
  321. marionette))
  322. (display #$%gitolite-test-admin-keypair)
  323. (setenv "GIT_SSH_VARIANT" "ssh")
  324. (setenv "GIT_SSH_COMMAND"
  325. (string-join
  326. '(#$(file-append openssh "/bin/ssh")
  327. "-i" #$(file-append %gitolite-test-admin-keypair
  328. "/test-admin")
  329. "-o" "UserKnownHostsFile=/dev/null"
  330. "-o" "StrictHostKeyChecking=no")))
  331. (test-assert "cloning the admin repository"
  332. (invoke #$(file-append git "/bin/git")
  333. "clone" "-v"
  334. "ssh://git@localhost:2222/gitolite-admin"
  335. "/tmp/clone"))
  336. (test-assert "admin key exists"
  337. (file-exists? "/tmp/clone/keydir/test-admin.pub"))
  338. (with-directory-excursion "/tmp/clone"
  339. (invoke #$(file-append git "/bin/git")
  340. "-c" "user.name=Guix" "-c" "user.email=guix"
  341. "commit"
  342. "-m" "Test commit"
  343. "--allow-empty")
  344. (test-assert "pushing, and the associated hooks"
  345. (invoke #$(file-append git "/bin/git") "push")))
  346. (test-end)
  347. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  348. (gexp->derivation "gitolite" test))
  349. (define %test-gitolite
  350. (system-test
  351. (name "gitolite")
  352. (description "Clone the Gitolite admin repository.")
  353. (value (run-gitolite-test))))