networking.scm 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Thomas Danckaert <post@thomasdanckaert.be>
  3. ;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu tests networking)
  20. #:use-module (gnu tests)
  21. #:use-module (gnu system)
  22. #:use-module (gnu system vm)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services networking)
  25. #:use-module (guix gexp)
  26. #:use-module (guix store)
  27. #:use-module (guix monads)
  28. #:use-module (gnu packages bash)
  29. #:use-module (gnu packages networking)
  30. #:use-module (gnu services shepherd)
  31. #:export (%test-inetd %test-openvswitch))
  32. (define %inetd-os
  33. ;; Operating system with 2 inetd services.
  34. (simple-operating-system
  35. (dhcp-client-service)
  36. (service inetd-service-type
  37. (inetd-configuration
  38. (entries (list
  39. (inetd-entry
  40. (name "echo")
  41. (socket-type 'stream)
  42. (protocol "tcp")
  43. (wait? #f)
  44. (user "root"))
  45. (inetd-entry
  46. (name "dict")
  47. (socket-type 'stream)
  48. (protocol "tcp")
  49. (wait? #f)
  50. (user "root")
  51. (program (file-append bash
  52. "/bin/bash"))
  53. (arguments
  54. (list "bash" (plain-file "my-dict.sh" "\
  55. while read line
  56. do
  57. if [[ $line =~ ^DEFINE\\ (.*)$ ]]
  58. then
  59. case ${BASH_REMATCH[1]} in
  60. Guix)
  61. echo GNU Guix is a package management tool for the GNU system.
  62. ;;
  63. G-expression)
  64. echo Like an S-expression but with a G.
  65. ;;
  66. *)
  67. echo NO DEFINITION FOUND
  68. ;;
  69. esac
  70. else
  71. echo ERROR
  72. fi
  73. done" ))))))))))
  74. (define* (run-inetd-test)
  75. "Run tests in %INETD-OS, where the inetd service provides an echo service on
  76. port 7, and a dict service on port 2628."
  77. (define os
  78. (marionette-operating-system %inetd-os))
  79. (define vm
  80. (virtual-machine
  81. (operating-system os)
  82. (port-forwardings `((8007 . 7)
  83. (8628 . 2628)))))
  84. (define test
  85. (with-imported-modules '((gnu build marionette))
  86. #~(begin
  87. (use-modules (ice-9 rdelim)
  88. (srfi srfi-64)
  89. (gnu build marionette))
  90. (define marionette
  91. (make-marionette (list #$vm)))
  92. (mkdir #$output)
  93. (chdir #$output)
  94. (test-begin "inetd")
  95. ;; Make sure the PID file is created.
  96. (test-assert "PID file"
  97. (marionette-eval
  98. '(file-exists? "/var/run/inetd.pid")
  99. marionette))
  100. ;; Test the echo service.
  101. (test-equal "echo response"
  102. "Hello, Guix!"
  103. (let ((echo (socket PF_INET SOCK_STREAM 0))
  104. (addr (make-socket-address AF_INET INADDR_LOOPBACK 8007)))
  105. (connect echo addr)
  106. (display "Hello, Guix!\n" echo)
  107. (let ((response (read-line echo)))
  108. (close echo)
  109. response)))
  110. ;; Test the dict service
  111. (test-equal "dict response"
  112. "GNU Guix is a package management tool for the GNU system."
  113. (let ((dict (socket PF_INET SOCK_STREAM 0))
  114. (addr (make-socket-address AF_INET INADDR_LOOPBACK 8628)))
  115. (connect dict addr)
  116. (display "DEFINE Guix\n" dict)
  117. (let ((response (read-line dict)))
  118. (close dict)
  119. response)))
  120. (test-end)
  121. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  122. (gexp->derivation "inetd-test" test))
  123. (define %test-inetd
  124. (system-test
  125. (name "inetd")
  126. (description "Connect to a host with an INETD server.")
  127. (value (run-inetd-test))))
  128. ;;;
  129. ;;; Open vSwitch
  130. ;;;
  131. (define setup-openvswitch
  132. #~(let ((ovs-vsctl (lambda (str)
  133. (zero? (apply system*
  134. #$(file-append openvswitch "/bin/ovs-vsctl")
  135. (string-tokenize str)))))
  136. (add-native-port (lambda (if)
  137. (string-append "--may-exist add-port br0 " if
  138. " vlan_mode=native-untagged"
  139. " -- set Interface " if
  140. " type=internal"))))
  141. (and (ovs-vsctl "--may-exist add-br br0")
  142. ;; Connect eth0 as an "untagged" port (no VLANs).
  143. (ovs-vsctl "--may-exist add-port br0 eth0 vlan_mode=native-untagged")
  144. (ovs-vsctl (add-native-port "ovs0")))))
  145. (define openvswitch-configuration-service
  146. (simple-service 'openvswitch-configuration shepherd-root-service-type
  147. (list (shepherd-service
  148. (provision '(openvswitch-configuration))
  149. (requirement '(vswitchd))
  150. (start #~(lambda ()
  151. #$setup-openvswitch))
  152. (respawn? #f)))))
  153. (define %openvswitch-os
  154. (simple-operating-system
  155. (static-networking-service "ovs0" "10.1.1.1"
  156. #:netmask "255.255.255.252"
  157. #:requirement '(openvswitch-configuration))
  158. (service openvswitch-service-type
  159. (openvswitch-configuration
  160. (package openvswitch)))
  161. openvswitch-configuration-service))
  162. (define (run-openvswitch-test)
  163. (define os
  164. (marionette-operating-system %openvswitch-os
  165. #:imported-modules '((gnu services herd))))
  166. (define test
  167. (with-imported-modules '((gnu build marionette))
  168. #~(begin
  169. (use-modules (gnu build marionette)
  170. (ice-9 popen)
  171. (ice-9 rdelim)
  172. (srfi srfi-64))
  173. (define marionette
  174. (make-marionette (list #$(virtual-machine os))))
  175. (mkdir #$output)
  176. (chdir #$output)
  177. (test-begin "openvswitch")
  178. ;; Make sure the bridge is created.
  179. (test-assert "br0 exists"
  180. (marionette-eval
  181. '(zero? (system* "ovs-vsctl" "br-exists" "br0"))
  182. marionette))
  183. ;; Make sure eth0 is connected to the bridge.
  184. (test-equal "eth0 is connected to br0"
  185. "br0"
  186. (marionette-eval
  187. '(begin
  188. (use-modules (ice-9 popen) (ice-9 rdelim))
  189. (let* ((port (open-pipe*
  190. OPEN_READ
  191. (string-append #$openvswitch "/bin/ovs-vsctl")
  192. "port-to-br" "eth0"))
  193. (output (read-line port)))
  194. (close-pipe port)
  195. output))
  196. marionette))
  197. ;; Make sure the virtual interface got a static IP.
  198. (test-assert "networking has started on ovs0"
  199. (marionette-eval
  200. '(begin
  201. (use-modules (gnu services herd)
  202. (srfi srfi-1))
  203. (live-service-running
  204. (find (lambda (live)
  205. (memq 'networking-ovs0
  206. (live-service-provision live)))
  207. (current-services))))
  208. marionette))
  209. (test-end)
  210. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  211. (gexp->derivation "openvswitch-test" test))
  212. (define %test-openvswitch
  213. (system-test
  214. (name "openvswitch")
  215. (description "Test a running OpenvSwitch configuration.")
  216. (value (run-openvswitch-test))))