Commit | Line | Data |
---|---|---|
9260b9d1 TD |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2017 Thomas Danckaert <post@thomasdanckaert.be> | |
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 | ||
19 | (define-module (gnu tests networking) | |
20 | #:use-module (gnu tests) | |
21 | #:use-module (gnu system) | |
22 | #:use-module (gnu system grub) | |
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 base) | |
28 | #:use-module (gnu services networking) | |
29 | #:use-module (guix gexp) | |
30 | #:use-module (guix store) | |
31 | #:use-module (guix monads) | |
32 | #:use-module (gnu packages bash) | |
33 | #:export (%test-inetd)) | |
34 | ||
35 | (define %inetd-os | |
36 | ;; Operating system with 2 inetd services. | |
37 | (operating-system | |
38 | (host-name "komputilo") | |
39 | (timezone "Europe/Brussels") | |
40 | (locale "en_US.utf8") | |
41 | ||
42 | (bootloader (grub-configuration (device "/dev/sdX"))) | |
43 | (file-systems %base-file-systems) | |
44 | (firmware '()) | |
45 | (users %base-user-accounts) | |
46 | (services (cons* (dhcp-client-service) | |
47 | (service inetd-service-type | |
48 | (inetd-configuration | |
49 | (entries (list | |
50 | (inetd-entry | |
51 | (name "echo") | |
52 | (socket-type 'stream) | |
53 | (protocol "tcp") | |
54 | (wait? #f) | |
55 | (user "root")) | |
56 | (inetd-entry | |
57 | (name "dict") | |
58 | (socket-type 'stream) | |
59 | (protocol "tcp") | |
60 | (wait? #f) | |
61 | (user "root") | |
62 | (program (file-append bash | |
63 | "/bin/bash")) | |
64 | (arguments | |
65 | (list "bash" (plain-file "my-dict.sh" "\ | |
66 | while read line | |
67 | do | |
68 | if [[ $line =~ ^DEFINE\\ (.*)$ ]] | |
69 | then | |
70 | case ${BASH_REMATCH[1]} in | |
71 | Guix) | |
72 | echo GNU Guix is a package management tool for the GNU system. | |
73 | ;; | |
74 | G-expression) | |
75 | echo Like an S-expression but with a G. | |
76 | ;; | |
77 | *) | |
78 | echo NO DEFINITION FOUND | |
79 | ;; | |
80 | esac | |
81 | else | |
82 | echo ERROR | |
83 | fi | |
84 | done" )))))))) | |
85 | %base-services)))) | |
86 | ||
87 | (define* (run-inetd-test) | |
88 | "Run tests in %INETD-OS, where the inetd service provides an echo service on | |
89 | port 7, and a dict service on port 2628." | |
90 | (mlet* %store-monad ((os -> (marionette-operating-system %inetd-os)) | |
91 | (command (system-qemu-image/shared-store-script | |
92 | os #:graphic? #f))) | |
93 | (define test | |
94 | (with-imported-modules '((gnu build marionette)) | |
95 | #~(begin | |
96 | (use-modules (ice-9 rdelim) | |
97 | (srfi srfi-64) | |
98 | (gnu build marionette)) | |
99 | (define marionette | |
100 | ;; Forward guest ports 7 and 2628 to host ports 8007 and 8628. | |
101 | (make-marionette (list #$command "-net" | |
102 | (string-append | |
103 | "user" | |
104 | ",hostfwd=tcp::8007-:7" | |
105 | ",hostfwd=tcp::8628-:2628")))) | |
106 | ||
107 | (mkdir #$output) | |
108 | (chdir #$output) | |
109 | ||
110 | (test-begin "inetd") | |
111 | ||
112 | ;; Make sure the PID file is created. | |
113 | (test-assert "PID file" | |
114 | (marionette-eval | |
115 | '(file-exists? "/var/run/inetd.pid") | |
116 | marionette)) | |
117 | ||
118 | ;; Test the echo service. | |
119 | (test-equal "echo response" | |
120 | "Hello, Guix!" | |
121 | (let ((echo (socket PF_INET SOCK_STREAM 0)) | |
122 | (addr (make-socket-address AF_INET INADDR_LOOPBACK 8007))) | |
123 | (connect echo addr) | |
124 | (display "Hello, Guix!\n" echo) | |
125 | (let ((response (read-line echo))) | |
126 | (close echo) | |
127 | response))) | |
128 | ||
129 | ;; Test the dict service | |
130 | (test-equal "dict response" | |
131 | "GNU Guix is a package management tool for the GNU system." | |
132 | (let ((dict (socket PF_INET SOCK_STREAM 0)) | |
133 | (addr (make-socket-address AF_INET INADDR_LOOPBACK 8628))) | |
134 | (connect dict addr) | |
135 | (display "DEFINE Guix\n" dict) | |
136 | (let ((response (read-line dict))) | |
137 | (close dict) | |
138 | response))) | |
139 | ||
140 | (test-end) | |
141 | (exit (= (test-runner-fail-count (test-runner-current)) 0))))) | |
142 | ||
143 | (gexp->derivation "inetd-test" test))) | |
144 | ||
145 | (define %test-inetd | |
146 | (system-test | |
147 | (name "inetd") | |
148 | (description "Connect to a host with an INETD server.") | |
149 | (value (run-inetd-test)))) |