guix.scm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu tests guix)
  19. #:use-module (gnu tests)
  20. #:use-module (gnu system)
  21. #:use-module (gnu system file-systems)
  22. #:use-module (gnu system shadow)
  23. #:use-module (gnu system vm)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services guix)
  26. #:use-module (gnu services databases)
  27. #:use-module (gnu services shepherd)
  28. #:use-module (gnu services networking)
  29. #:use-module (gnu packages databases)
  30. #:use-module (guix packages)
  31. #:use-module (guix modules)
  32. #:use-module (guix records)
  33. #:use-module (guix gexp)
  34. #:use-module (guix store)
  35. #:use-module (guix utils)
  36. #:use-module (ice-9 match)
  37. #:export (%test-guix-data-service))
  38. ;;;
  39. ;;; Guix Data Service
  40. ;;;
  41. (define guix-data-service-initial-database-setup-service
  42. (let ((user "guix_data_service")
  43. (name "guix_data_service"))
  44. (define start-gexp
  45. #~(lambda ()
  46. (let ((pid (primitive-fork))
  47. (postgres (getpwnam "postgres")))
  48. (if (eq? pid 0)
  49. (dynamic-wind
  50. (const #t)
  51. (lambda ()
  52. (setgid (passwd:gid postgres))
  53. (setuid (passwd:uid postgres))
  54. (primitive-exit
  55. (if (and
  56. (zero?
  57. (system* #$(file-append postgresql "/bin/createuser")
  58. #$user))
  59. (zero?
  60. (system* #$(file-append postgresql "/bin/createdb")
  61. "-O" #$user #$name)))
  62. 0
  63. 1)))
  64. (lambda ()
  65. (primitive-exit 1)))
  66. (zero? (cdr (waitpid pid)))))))
  67. (shepherd-service
  68. (requirement '(postgres))
  69. (provision '(guix-data-service-initial-database-setup))
  70. (start start-gexp)
  71. (stop #~(const #f))
  72. (respawn? #f)
  73. (one-shot? #t)
  74. (documentation "Setup Guix Data Service database."))))
  75. (define %guix-data-service-os
  76. (simple-operating-system
  77. (service dhcp-client-service-type)
  78. (service postgresql-service-type
  79. (postgresql-configuration
  80. (config-file
  81. (postgresql-config-file
  82. (hba-file
  83. (plain-file "pg_hba.conf"
  84. "
  85. local all all trust
  86. host all all 127.0.0.1/32 trust
  87. host all all ::1/128 trust"))))))
  88. (service guix-data-service-type
  89. (guix-data-service-configuration
  90. (host "0.0.0.0")))
  91. (simple-service 'guix-data-service-database-setup
  92. shepherd-root-service-type
  93. (list guix-data-service-initial-database-setup-service))))
  94. (define (run-guix-data-service-test)
  95. (define os
  96. (marionette-operating-system
  97. %guix-data-service-os
  98. #:imported-modules '((gnu services herd)
  99. (guix combinators))))
  100. (define forwarded-port 8080)
  101. (define vm
  102. (virtual-machine
  103. (operating-system os)
  104. (memory-size 1024)
  105. (port-forwardings `((,forwarded-port . 8765)))))
  106. (define test
  107. (with-imported-modules '((gnu build marionette))
  108. #~(begin
  109. (use-modules (srfi srfi-11) (srfi srfi-64)
  110. (gnu build marionette)
  111. (web uri)
  112. (web client)
  113. (web response))
  114. (define marionette
  115. (make-marionette (list #$vm)))
  116. (mkdir #$output)
  117. (chdir #$output)
  118. (test-begin "guix-data-service")
  119. (test-assert "service running"
  120. (marionette-eval
  121. '(begin
  122. (use-modules (gnu services herd))
  123. (match (start-service 'guix-data-service)
  124. (#f #f)
  125. (('service response-parts ...)
  126. (match (assq-ref response-parts 'running)
  127. ((pid) (number? pid))))))
  128. marionette))
  129. (test-assert "process jobs service running"
  130. (marionette-eval
  131. '(begin
  132. (use-modules (gnu services herd))
  133. (match (start-service 'guix-data-service-process-jobs)
  134. (#f #f)
  135. (('service response-parts ...)
  136. (match (assq-ref response-parts 'running)
  137. ((pid) (number? pid))))))
  138. marionette))
  139. (test-equal "http-get"
  140. 200
  141. (let-values
  142. (((response text)
  143. (http-get #$(simple-format
  144. #f "http://localhost:~A/healthcheck" forwarded-port)
  145. #:decode-body? #t)))
  146. (response-code response)))
  147. (test-end)
  148. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  149. (gexp->derivation "guix-data-service-test" test))
  150. (define %test-guix-data-service
  151. (system-test
  152. (name "guix-data-service")
  153. (description "Connect to a running Guix Data Service.")
  154. (value (run-guix-data-service-test))))