1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (gnu tests messaging)
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 messaging)
29 #:use-module (gnu services networking)
30 #:use-module (gnu packages messaging)
31 #:use-module (guix gexp)
32 #:use-module (guix store)
33 #:use-module (guix monads)
34 #:export (%test-prosody))
38 (host-name "komputilo")
39 (timezone "Europe/Berlin")
40 (locale "en_US.UTF-8")
42 (bootloader (grub-configuration (device "/dev/sdX")))
43 (file-systems %base-file-systems)
45 (users %base-user-accounts)
46 (services (cons (dhcp-client-service)
49 (define (os-with-service service)
50 "Return a test operating system that runs SERVICE."
53 (services (cons service
54 (operating-system-user-services %base-os)))))
56 (define (run-xmpp-test name xmpp-service pid-file create-account)
57 "Run a test of an OS running XMPP-SERVICE, which writes its PID to PID-FILE."
58 (mlet* %store-monad ((os -> (marionette-operating-system
59 (os-with-service xmpp-service)
60 #:imported-modules '((gnu services herd))))
61 (command (system-qemu-image/shared-store-script
64 (server -> "localhost")
65 (jid -> (string-append username "@" server))
66 (password -> "correct horse battery staple")
68 (message -> "hello world")
69 (witness -> "/tmp/freetalk-witness"))
75 (define (handle-received-message time from nickname message)
76 (define (touch file-name)
77 (call-with-output-file file-name (const #t)))
78 (when (equal? message #$message)
80 (add-hook! ft-message-receive-hook handle-received-message)
83 (ft-set-password! #$password)
84 (ft-set-server! #$server)
88 (ft-send-message #$jid #$message)
94 (with-imported-modules '((gnu build marionette))
96 (use-modules (gnu build marionette)
100 ;; Enable TCP forwarding of the guest's port 5222.
101 (make-marionette (list #$command "-net"
102 (string-append "user,hostfwd=tcp::"
103 (number->string #$port)
106 (define (guest-wait-for-file file)
107 ;; Wait until FILE exists in the guest; 'read' its content and
111 (cond ((file-exists? ,file)
112 (call-with-input-file ,file read))
118 (error "file didn't show up" ,file))))
121 (define (host-wait-for-file file)
122 ;; Wait until FILE exists in the host.
124 (cond ((file-exists? file)
131 (error "file didn't show up" file)))))
138 ;; Wait for XMPP service to be up and running.
139 (test-eq "service running"
143 (use-modules (gnu services herd))
144 (start-service 'xmpp-daemon)
148 ;; Check XMPP service's PID.
149 (test-assert "service process id"
150 (let ((pid (number->string (guest-wait-for-file #$pid-file))))
151 (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
154 ;; Alice sends an XMPP message to herself, with Freetalk.
155 (test-assert "client-to-server communication"
156 (let ((freetalk-bin (string-append #$freetalk "/bin/freetalk")))
157 (marionette-eval '(system* #$create-account #$jid #$password)
159 ;; Freetalk requires write access to $HOME.
160 (setenv "HOME" "/tmp")
161 (system* freetalk-bin "-s" #$script.ft)
162 (host-wait-for-file #$witness)))
165 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
167 (gexp->derivation name test)))
169 (define %create-prosody-account
173 (use-modules (ice-9 match))
174 (match (command-line)
175 ((command jid password)
176 (let ((password-input (format #f "\"~a~%~a\"" password password))
177 (prosodyctl #$(file-append prosody "/bin/prosodyctl")))
179 `("echo" ,password-input "|" ,prosodyctl "adduser" ,jid)
182 (define %test-prosody
183 (let* ((config (prosody-configuration
186 (virtualhost-configuration
187 (domain "localhost")))))))
190 (description "Connect to a running Prosody daemon.")
191 (value (run-xmpp-test name
192 (service prosody-service-type config)
193 (prosody-configuration-pidfile config)
194 %create-prosody-account)))))