version-control.scm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Oleg Pykhalov <go.wigust@gmail.com>
  3. ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gnu tests version-control)
  21. #:use-module (gnu tests)
  22. #:use-module (gnu system)
  23. #:use-module (gnu system file-systems)
  24. #:use-module (gnu system shadow)
  25. #:use-module (gnu system vm)
  26. #:use-module (gnu services)
  27. #:use-module (gnu services version-control)
  28. #:use-module (gnu services web)
  29. #:use-module (gnu services networking)
  30. #:use-module (gnu packages version-control)
  31. #:use-module (guix gexp)
  32. #:use-module (guix store)
  33. #:use-module (guix modules)
  34. #:export (%test-cgit
  35. %test-git-http))
  36. (define README-contents
  37. "Hello! This is what goes inside the 'README' file.")
  38. (define %make-git-repository
  39. ;; Create Git repository in /srv/git/test.
  40. (with-imported-modules (source-module-closure
  41. '((guix build utils)))
  42. #~(begin
  43. (use-modules (guix build utils))
  44. (let ((git (string-append #$git "/bin/git")))
  45. (mkdir-p "/tmp/test-repo")
  46. (with-directory-excursion "/tmp/test-repo"
  47. (call-with-output-file "/tmp/test-repo/README"
  48. (lambda (port)
  49. (display #$README-contents port)))
  50. (invoke git "config" "--global" "user.email" "charlie@example.org")
  51. (invoke git "config" "--global" "user.name" "A U Thor")
  52. (invoke git "init")
  53. (invoke git "add" ".")
  54. (invoke git "commit" "-m" "That's a commit."))
  55. (mkdir-p "/srv/git")
  56. (rename-file "/tmp/test-repo/.git" "/srv/git/test")))))
  57. (define %test-repository-service
  58. ;; Service that creates /srv/git/test.
  59. (simple-service 'make-git-repository activation-service-type
  60. %make-git-repository))
  61. (define %cgit-configuration-nginx
  62. (list
  63. (nginx-server-configuration
  64. (root cgit)
  65. (locations
  66. (list
  67. (nginx-location-configuration
  68. (uri "@cgit")
  69. (body '("fastcgi_param SCRIPT_FILENAME $document_root/lib/cgit/cgit.cgi;"
  70. "fastcgi_param PATH_INFO $uri;"
  71. "fastcgi_param QUERY_STRING $args;"
  72. "fastcgi_param HTTP_HOST $server_name;"
  73. "fastcgi_pass 127.0.0.1:9000;")))))
  74. (try-files (list "$uri" "@cgit"))
  75. (listen '("19418"))
  76. (ssl-certificate #f)
  77. (ssl-certificate-key #f))))
  78. (define %cgit-os
  79. ;; Operating system under test.
  80. (let ((base-os
  81. (simple-operating-system
  82. (dhcp-client-service)
  83. (service nginx-service-type)
  84. (service fcgiwrap-service-type)
  85. (service cgit-service-type
  86. (cgit-configuration
  87. (nginx %cgit-configuration-nginx)))
  88. %test-repository-service)))
  89. (operating-system
  90. (inherit base-os)
  91. (packages (cons* git
  92. (operating-system-packages base-os))))))
  93. (define* (run-cgit-test #:optional (http-port 19418))
  94. "Run tests in %CGIT-OS, which has nginx running and listening on
  95. HTTP-PORT."
  96. (define os
  97. (marionette-operating-system
  98. %cgit-os
  99. #:imported-modules '((gnu services herd)
  100. (guix combinators))))
  101. (define vm
  102. (virtual-machine
  103. (operating-system os)
  104. (port-forwardings `((8080 . ,http-port)))))
  105. (define test
  106. (with-imported-modules '((gnu build marionette))
  107. #~(begin
  108. (use-modules (srfi srfi-11) (srfi srfi-64)
  109. (gnu build marionette)
  110. (web uri)
  111. (web client)
  112. (web response))
  113. (define marionette
  114. (make-marionette (list #$vm)))
  115. (mkdir #$output)
  116. (chdir #$output)
  117. (test-begin "cgit")
  118. ;; Wait for nginx to be up and running.
  119. (test-eq "service running"
  120. 'running!
  121. (marionette-eval
  122. '(begin
  123. (use-modules (gnu services herd))
  124. (start-service 'nginx)
  125. 'running!)
  126. marionette))
  127. ;; Wait for fcgiwrap to be up and running.
  128. (test-eq "service running"
  129. 'running!
  130. (marionette-eval
  131. '(begin
  132. (use-modules (gnu services herd))
  133. (start-service 'fcgiwrap)
  134. 'running!)
  135. marionette))
  136. ;; Make sure the PID file is created.
  137. (test-assert "PID file"
  138. (marionette-eval
  139. '(file-exists? "/var/run/nginx/pid")
  140. marionette))
  141. ;; Make sure the configuration file is created.
  142. (test-assert "configuration file"
  143. (marionette-eval
  144. '(file-exists? "/etc/cgitrc")
  145. marionette))
  146. ;; Make sure Git test repository is created.
  147. (test-assert "Git test repository"
  148. (marionette-eval
  149. '(file-exists? "/srv/git/test")
  150. marionette))
  151. ;; Make sure we can access pages that correspond to our repository.
  152. (letrec-syntax ((test-url
  153. (syntax-rules ()
  154. ((_ path code)
  155. (test-equal (string-append "GET " path)
  156. code
  157. (let-values (((response body)
  158. (http-get (string-append
  159. "http://localhost:8080"
  160. path))))
  161. (response-code response))))
  162. ((_ path)
  163. (test-url path 200)))))
  164. (test-url "/")
  165. (test-url "/test")
  166. (test-url "/test/log")
  167. (test-url "/test/tree")
  168. (test-url "/test/tree/README")
  169. (test-url "/test/does-not-exist" 404)
  170. (test-url "/test/tree/does-not-exist" 404)
  171. (test-url "/does-not-exist" 404))
  172. (test-end)
  173. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  174. (gexp->derivation "cgit-test" test))
  175. (define %test-cgit
  176. (system-test
  177. (name "cgit")
  178. (description "Connect to a running Cgit server.")
  179. (value (run-cgit-test))))
  180. ;;;
  181. ;;; Git server.
  182. ;;;
  183. (define %git-nginx-configuration
  184. (nginx-configuration
  185. (server-blocks
  186. (list
  187. (nginx-server-configuration
  188. (listen '("19418"))
  189. (ssl-certificate #f)
  190. (ssl-certificate-key #f)
  191. (locations
  192. (list (git-http-nginx-location-configuration
  193. (git-http-configuration (export-all? #t)
  194. (uri-path "/git"))))))))))
  195. (define %git-http-os
  196. (simple-operating-system
  197. (dhcp-client-service)
  198. (service fcgiwrap-service-type)
  199. (service nginx-service-type %git-nginx-configuration)
  200. %test-repository-service))
  201. (define* (run-git-http-test #:optional (http-port 19418))
  202. (define os
  203. (marionette-operating-system
  204. %git-http-os
  205. #:imported-modules '((gnu services herd)
  206. (guix combinators))))
  207. (define vm
  208. (virtual-machine
  209. (operating-system os)
  210. (port-forwardings `((8080 . ,http-port)))))
  211. (define test
  212. (with-imported-modules '((gnu build marionette)
  213. (guix build utils))
  214. #~(begin
  215. (use-modules (srfi srfi-64)
  216. (rnrs io ports)
  217. (gnu build marionette)
  218. (guix build utils))
  219. (define marionette
  220. (make-marionette (list #$vm)))
  221. (mkdir #$output)
  222. (chdir #$output)
  223. (test-begin "git-http")
  224. ;; Wait for nginx to be up and running.
  225. (test-eq "nginx running"
  226. 'running!
  227. (marionette-eval
  228. '(begin
  229. (use-modules (gnu services herd))
  230. (start-service 'nginx)
  231. 'running!)
  232. marionette))
  233. ;; Make sure Git test repository is created.
  234. (test-assert "Git test repository"
  235. (marionette-eval
  236. '(file-exists? "/srv/git/test")
  237. marionette))
  238. ;; Make sure we can clone the repo from the host.
  239. (test-equal "clone"
  240. '#$README-contents
  241. (begin
  242. (invoke #$(file-append git "/bin/git") "clone" "-v"
  243. "http://localhost:8080/git/test" "/tmp/clone")
  244. (call-with-input-file "/tmp/clone/README"
  245. get-string-all)))
  246. (test-end)
  247. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  248. (gexp->derivation "git-http" test))
  249. (define %test-git-http
  250. (system-test
  251. (name "git-http")
  252. (description "Connect to a running Git HTTP server.")
  253. (value (run-git-http-test))))