projects/guix·blob python-team · refs

156 lines · 5.8 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2023 Bruno Victal <[email protected]>.
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 vnstat)
20 #:use-module (gnu tests)
21 #:use-module ((gnu packages networking) #:select (socat vnstat))
22 #:use-module (gnu services)
23 #:use-module (gnu services networking)
24 #:use-module (gnu services monitoring)
25 #:use-module (gnu system)
26 #:use-module (gnu system vm)
27 #:use-module (guix gexp)
28 #:use-module (ice-9 format)
29 #:export (%test-vnstat))
30
31
32(define (run-vnstat-test)
33 "Run tests in a vm which has vnstat running."
34
35 (define vnstat-config
36 (vnstat-configuration
37 (max-bandwidth 0)
38 (time-sync-wait 0)
39 (bandwidth-detection-interval 0)))
40
41 (define inetd-service-entry-config
42 (inetd-entry
43 (name "discard")
44 (socket-type 'stream)
45 (protocol "tcp")
46 (wait? #t)
47 (user "nobody")))
48
49 (define os
50 (marionette-operating-system
51 (simple-operating-system
52 (service dhcpcd-service-type)
53 (service vnstat-service-type
54 vnstat-config)
55 (service inetd-service-type
56 (inetd-configuration
57 (entries
58 (list inetd-service-entry-config)))))
59 #:imported-modules '((gnu services herd))))
60
61 (define forwarded-port 9999)
62
63 (define vm
64 ;; The 'discard' port is 9. Avoid using 'getservbyname' as that might
65 ;; fail depending on what /etc/services has (if it's available).
66 (let ((guest-port 9))
67 (virtual-machine
68 (operating-system os)
69 (port-forwardings `((,forwarded-port . ,guest-port))))))
70
71 ;; The test duration is inconsistent, at times a test may complete under
72 ;; 2 minutes and at times it may take up to 5 minutes.
73 (define test-timeout (* 60 5))
74
75 (define test
76 (with-imported-modules '((gnu build marionette))
77 #~(begin
78 (use-modules (gnu build marionette)
79 (srfi srfi-64))
80
81 (let ((marionette (make-marionette (list #$vm)))
82 (pid-file #$(vnstat-configuration-pid-file vnstat-config)))
83
84 (test-runner-current (system-test-runner #$output))
85 (test-begin "vnstat")
86
87 (test-assert "service is running"
88 (marionette-eval
89 '(begin
90 (use-modules (gnu services herd))
91 (start-service 'vnstatd))
92 marionette))
93
94 (test-assert "vnstatd ready"
95 (wait-for-file pid-file marionette))
96
97 ;; Pump garbage into the 'discard' inetd service within the vm.
98 (let* ((socat #$(file-append socat "/bin/socat"))
99 (dest-addr #$(format #f "TCP4:localhost:~d"
100 forwarded-port))
101 (args `("socat" "-u" "/dev/zero" ,dest-addr))
102 ;; XXX: Guile bug (22/03/2023, Guile 3.0.9)
103 ;; Fixed in main: <https://issues.guix.gnu.org/61073>
104 ;; FIXME: re-add #:output (%make-void-port "w") below on
105 ;; next Guile release.
106 (garbage-pump-pid
107 (spawn socat args)))
108 (test-group-with-cleanup "Logging"
109 ;; To aid debugging, this test returns #t on success
110 ;; and either #f or 'timed-out otherwise.
111 (test-eq "vnstatd is logging"
112 #t
113 (marionette-eval
114 '(begin
115 (use-modules (ice-9 popen)
116 (ice-9 match)
117 (sxml simple)
118 (sxml xpath))
119
120 (define selector
121 (let ((xpath '(vnstat interface traffic total)))
122 (compose (node-pos 1) (sxpath xpath))))
123
124 (let loop ((i 0))
125 (let* ((vnstat #$(file-append vnstat "/bin/vnstat"))
126 (query-cmd (format #f "~a --xml" vnstat))
127 (proc (compose selector xml->sxml))
128 (result
129 (call-with-port
130 (open-input-pipe query-cmd) proc)))
131 (match result
132 ;; Counter still warming up.
133 ((('total ('rx "0") ('tx "0")))
134 (sleep 1)
135 (if (< i #$test-timeout)
136 (loop (+ i 1))
137 'timed-out))
138 ;; Count of bytes on iface was non-zero.
139 ((('total ('rx rx) ('tx tx)))
140 #t)
141 ;; Unknown data encountered, perhaps the
142 ;; data format changed?
143 (_ #f)))))
144 marionette))
145 ;; Cleanup: shutdown garbage pump.
146 (kill garbage-pump-pid SIGTERM)))
147
148 (test-end)))))
149
150 (gexp->derivation "vnstat-test" test))
151
152(define %test-vnstat
153 (system-test
154 (name "vnstat")
155 (description "Basic tests for vnstat service.")
156 (value (run-vnstat-test))))