From patchwork Fri May 5 00:18:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bruno Victal X-Patchwork-Id: 49805 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 0E84827BBEA; Fri, 5 May 2023 01:21:36 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id BC43727BBE9 for ; Fri, 5 May 2023 01:21:34 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pujC4-0006yT-GG; Thu, 04 May 2023 20:21:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pujC3-0006y2-0j for guix-patches@gnu.org; Thu, 04 May 2023 20:21:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pujC2-0004sK-OQ for guix-patches@gnu.org; Thu, 04 May 2023 20:21:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pujC2-0001PD-JY for guix-patches@gnu.org; Thu, 04 May 2023 20:21:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#60788] [PATCH v10 3/3] tests: Add vnstat tests. Resent-From: Bruno Victal Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 05 May 2023 00:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60788 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 60788@debbugs.gnu.org Cc: ludo@gnu.org, Bruno Victal , maxim.cournoyer@gmail.com Received: via spool by 60788-submit@debbugs.gnu.org id=B60788.16832460235322 (code B ref 60788); Fri, 05 May 2023 00:21:02 +0000 Received: (at 60788) by debbugs.gnu.org; 5 May 2023 00:20:23 +0000 Received: from localhost ([127.0.0.1]:52361 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pujBO-0001Nm-IV for submit@debbugs.gnu.org; Thu, 04 May 2023 20:20:23 -0400 Received: from smtpm4.myservices.hosting ([185.26.105.235]:44664) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pujBM-0001Nc-Rn for 60788@debbugs.gnu.org; Thu, 04 May 2023 20:20:21 -0400 Received: from mail1.netim.hosting (unknown [185.26.106.173]) by smtpm4.myservices.hosting (Postfix) with ESMTP id A897F20B8B; Fri, 5 May 2023 02:20:19 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail1.netim.hosting (Postfix) with ESMTP id 349BA8009A; Fri, 5 May 2023 02:19:26 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mail1.netim.hosting Received: from mail1.netim.hosting ([127.0.0.1]) by localhost (mail1-2.netim.hosting [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id C2YjvVFtg2Zs; Fri, 5 May 2023 02:19:11 +0200 (CEST) Received: from guix-nuc.home.arpa (unknown [10.192.1.83]) (Authenticated sender: lumen@makinata.eu) by mail1.netim.hosting (Postfix) with ESMTPSA id B750F80060; Fri, 5 May 2023 02:19:10 +0200 (CEST) From: Bruno Victal Date: Fri, 5 May 2023 01:18:39 +0100 Message-Id: <2b1c8fa1c767cb2bcdec87109402fc42be17753d.1683245610.git.mirai@makinata.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: <95b646eb6b23dec213cba43b6e4e7ddc4a601d0f.1673640404.git.mirai@makinata.eu> References: <95b646eb6b23dec213cba43b6e4e7ddc4a601d0f.1673640404.git.mirai@makinata.eu> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * gnu/tests/vnstat.scm: New file. * gnu/local.mk: Register it. --- gnu/local.mk | 1 + gnu/tests/vnstat.scm | 159 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 gnu/tests/vnstat.scm diff --git a/gnu/local.mk b/gnu/local.mk index 5f5de953d7..c462c41b9d 100644 --- a/gnu/local.mk +++ b/gnu/local.mk @@ -795,6 +795,7 @@ GNU_SYSTEM_MODULES = \ %D%/tests/version-control.scm \ %D%/tests/virtualization.scm \ %D%/tests/vnc.scm \ + %D%/tests/vnstat.scm \ %D%/tests/web.scm INSTALLER_MODULES = \ diff --git a/gnu/tests/vnstat.scm b/gnu/tests/vnstat.scm new file mode 100644 index 0000000000..d1be98e342 --- /dev/null +++ b/gnu/tests/vnstat.scm @@ -0,0 +1,159 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2023 Bruno Victal . +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (gnu tests vnstat) + #:use-module (gnu tests) + #:use-module ((gnu packages networking) #:select (socat vnstat)) + #:use-module (gnu services) + #:use-module (gnu services networking) + #:use-module (gnu services monitoring) + #:use-module (gnu system) + #:use-module (gnu system vm) + #:use-module (guix gexp) + #:use-module (ice-9 format) + #:export (%test-vnstat)) + + +(define (run-vnstat-test) + "Run tests in a vm which has vnstat running." + + (define vnstat-config + (vnstat-configuration + (max-bandwidth 0) + (time-sync-wait 0) + (bandwidth-detection-interval 0))) + + (define inetd-service-entry-config + (inetd-entry + (name "discard") + (socket-type 'stream) + (protocol "tcp") + (wait? #t) + (user "nobody"))) + + (define os + (marionette-operating-system + (simple-operating-system + (service dhcp-client-service-type) + (service vnstat-service-type + vnstat-config) + (service inetd-service-type + (inetd-configuration + (entries + (list inetd-service-entry-config))))) + #:imported-modules '((gnu services herd)))) + + (define forwarded-port 9999) + + (define vm + (let* ((inetd-service-name "discard") + (inetd-service-proto + (inetd-entry-protocol inetd-service-entry-config)) + (guest-port + (servent:port (getservbyname inetd-service-name + inetd-service-proto)))) + (virtual-machine + (operating-system os) + (port-forwardings `((,forwarded-port . ,guest-port)))))) + + ;; The test duration is inconsistent, at times a test may complete under + ;; 2 minutes and at times it may take up to 5 minutes. + (define test-timeout (* 60 5)) + + (define test + (with-imported-modules '((gnu build marionette)) + #~(begin + (use-modules (gnu build marionette) + (srfi srfi-64)) + + (let ((marionette (make-marionette (list #$vm))) + (pid-file #$(vnstat-configuration-pid-file vnstat-config))) + + (test-runner-current (system-test-runner #$output)) + (test-begin "vnstat") + + (test-assert "service is running" + (marionette-eval + '(begin + (use-modules (gnu services herd)) + (start-service 'vnstatd)) + marionette)) + + (test-assert "vnstatd ready" + (wait-for-file pid-file marionette)) + + ;; Pump garbage into the 'discard' inetd service within the vm. + (let* ((socat #$(file-append socat "/bin/socat")) + (dest-addr #$(format #f "TCP4:localhost:~d" + forwarded-port)) + (args `("socat" "-u" "/dev/zero" ,dest-addr)) + ;; XXX: Guile bug (22/03/2023, Guile 3.0.9) + ;; Fixed in main: + ;; FIXME: re-add #:output (%make-void-port "w") below on + ;; next Guile release. + (garbage-pump-pid + (spawn socat args))) + (test-group-with-cleanup "Logging" + ;; To aid debugging, this test returns #t on success + ;; and either #f or 'timed-out otherwise. + (test-eq "vnstatd is logging" + #t + (marionette-eval + '(begin + (use-modules (ice-9 popen) + (ice-9 match) + (sxml simple) + (sxml xpath)) + + (define selector + (let ((xpath '(vnstat interface traffic total))) + (compose (node-pos 1) (sxpath xpath)))) + + (let loop ((i 0)) + (let* ((vnstat #$(file-append vnstat "/bin/vnstat")) + (query-cmd (format #f "~a --xml" vnstat)) + (proc (compose selector xml->sxml)) + (result + (call-with-port + (open-input-pipe query-cmd) proc))) + (match result + ;; Counter still warming up. + ((('total ('rx "0") ('tx "0"))) + (sleep 1) + (if (< i #$test-timeout) + (loop (+ i 1)) + 'timed-out)) + ;; Count of bytes on iface was non-zero. + ((('total ('rx rx) ('tx tx))) + #t) + ;; Unknown data encountered, perhaps the + ;; data format changed? + (_ #f))))) + marionette)) + ;; Cleanup: shutdown garbage pump. + (kill garbage-pump-pid SIGTERM))) + + (test-end))))) + + (gexp->derivation "vnstat-test" test)) + +(define %test-vnstat + (system-test + (name "vnstat") + (description "Basic tests for vnstat service.") + (value (run-vnstat-test))))