admin.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 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 admin)
  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 admin)
  26. #:use-module (gnu services networking)
  27. #:use-module (guix gexp)
  28. #:use-module (guix store)
  29. #:use-module (guix monads)
  30. #:export (%test-tailon))
  31. (define %tailon-os
  32. ;; Operating system under test.
  33. (simple-operating-system
  34. (dhcp-client-service)
  35. (service tailon-service-type
  36. (tailon-configuration
  37. (config-file
  38. (tailon-configuration-file
  39. (bind "0.0.0.0:8080")))))))
  40. (define* (run-tailon-test #:optional (http-port 8081))
  41. "Run tests in %TAILON-OS, which has tailon running and listening on
  42. HTTP-PORT."
  43. (define os
  44. (marionette-operating-system
  45. %tailon-os
  46. #:imported-modules '((gnu services herd)
  47. (guix combinators))))
  48. (define vm
  49. (virtual-machine
  50. (operating-system os)
  51. (port-forwardings `((,http-port . 8080)))))
  52. (define test
  53. (with-imported-modules '((gnu build marionette))
  54. #~(begin
  55. (use-modules (srfi srfi-11) (srfi srfi-64)
  56. (ice-9 match)
  57. (gnu build marionette)
  58. (web uri)
  59. (web client)
  60. (web response))
  61. (define marionette
  62. ;; Forward the guest's HTTP-PORT, where tailon is listening, to
  63. ;; port 8080 in the host.
  64. (make-marionette (list #$vm)))
  65. (mkdir #$output)
  66. (chdir #$output)
  67. (test-begin "tailon")
  68. (test-eq "service running"
  69. 'running!
  70. (marionette-eval
  71. '(begin
  72. (use-modules (gnu services herd))
  73. (start-service 'tailon)
  74. 'running!)
  75. marionette))
  76. (define* (retry-on-error f #:key times delay)
  77. (let loop ((attempt 1))
  78. (match (catch
  79. #t
  80. (lambda ()
  81. (cons #t
  82. (f)))
  83. (lambda args
  84. (cons #f
  85. args)))
  86. ((#t . return-value)
  87. return-value)
  88. ((#f . error-args)
  89. (if (>= attempt times)
  90. error-args
  91. (begin
  92. (sleep delay)
  93. (loop (+ 1 attempt))))))))
  94. (test-equal "http-get"
  95. 200
  96. (retry-on-error
  97. (lambda ()
  98. (let-values (((response text)
  99. (http-get #$(format
  100. #f
  101. "http://localhost:~A/"
  102. http-port)
  103. #:decode-body? #t)))
  104. (response-code response)))
  105. #:times 10
  106. #:delay 5))
  107. (test-end)
  108. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  109. (gexp->derivation "tailon-test" test))
  110. (define %test-tailon
  111. (system-test
  112. (name "tailon")
  113. (description "Connect to a running Tailon server.")
  114. (value (run-tailon-test))))