| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 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 | |
| 65 | |
| 66 | (let ((guest-port 9))
|
| 67 | (virtual-machine
|
| 68 | (operating-system os)
|
| 69 | (port-forwardings `((,forwarded-port . ,guest-port))))))
|
| 70 |
|
| 71 | |
| 72 | |
| 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 | |
| 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 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | (garbage-pump-pid
|
| 107 | (spawn socat args)))
|
| 108 | (test-group-with-cleanup "Logging"
|
| 109 | |
| 110 | |
| 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 | |
| 133 | ((('total ('rx "0") ('tx "0")))
|
| 134 | (sleep 1)
|
| 135 | (if (< i #$test-timeout)
|
| 136 | (loop (+ i 1))
|
| 137 | 'timed-out))
|
| 138 | |
| 139 | ((('total ('rx rx) ('tx tx)))
|
| 140 | #t)
|
| 141 | |
| 142 | |
| 143 | (_ #f)))))
|
| 144 | marionette))
|
| 145 | |
| 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))))
|