networking.scm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Thomas Danckaert <post@thomasdanckaert.be>
  3. ;;; Copyright © 2017, 2020 Marius Bakke <marius@gnu.org>
  4. ;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
  5. ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
  6. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  7. ;;; Copyright © 2021, 2023 Ludovic Courtès <ludo@gnu.org>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (gnu tests networking)
  24. #:use-module (gnu tests)
  25. #:use-module (gnu system)
  26. #:use-module (gnu system vm)
  27. #:use-module (gnu services)
  28. #:use-module (gnu services base)
  29. #:use-module (gnu services networking)
  30. #:use-module (guix gexp)
  31. #:use-module (guix store)
  32. #:use-module (guix monads)
  33. #:use-module (guix modules)
  34. #:use-module (gnu packages bash)
  35. #:use-module (gnu packages linux)
  36. #:use-module (gnu packages networking)
  37. #:use-module (gnu packages guile)
  38. #:use-module (gnu services shepherd)
  39. #:use-module (ice-9 match)
  40. #:export (%test-static-networking
  41. %test-inetd
  42. %test-openvswitch
  43. %test-dhcpd
  44. %test-tor
  45. %test-iptables
  46. %test-ipfs))
  47. ;;;
  48. ;;; Static networking.
  49. ;;;
  50. (define (run-static-networking-test vm)
  51. (define test
  52. (with-imported-modules '((gnu build marionette)
  53. (guix build syscalls))
  54. #~(begin
  55. (use-modules (gnu build marionette)
  56. (guix build syscalls)
  57. (srfi srfi-64))
  58. (define marionette
  59. (make-marionette
  60. '(#$vm "-nic" "user,model=virtio-net-pci")))
  61. (test-runner-current (system-test-runner #$output))
  62. (test-begin "static-networking")
  63. (test-assert "service is up"
  64. (marionette-eval
  65. '(begin
  66. (use-modules (gnu services herd))
  67. (start-service 'networking))
  68. marionette))
  69. (test-assert "network interfaces"
  70. (marionette-eval
  71. '(begin
  72. (use-modules (guix build syscalls))
  73. (network-interface-names))
  74. marionette))
  75. (test-equal "address of eth0"
  76. "10.0.2.15"
  77. (marionette-eval
  78. '(let* ((sock (socket AF_INET SOCK_STREAM 0))
  79. (addr (network-interface-address sock "eth0")))
  80. (close-port sock)
  81. (inet-ntop (sockaddr:fam addr) (sockaddr:addr addr)))
  82. marionette))
  83. (test-equal "netmask of eth0"
  84. "255.255.255.0"
  85. (marionette-eval
  86. '(let* ((sock (socket AF_INET SOCK_STREAM 0))
  87. (mask (network-interface-netmask sock "eth0")))
  88. (close-port sock)
  89. (inet-ntop (sockaddr:fam mask) (sockaddr:addr mask)))
  90. marionette))
  91. (test-equal "eth0 is up"
  92. IFF_UP
  93. (marionette-eval
  94. '(let* ((sock (socket AF_INET SOCK_STREAM 0))
  95. (flags (network-interface-flags sock "eth0")))
  96. (logand flags IFF_UP))
  97. marionette))
  98. (test-end))))
  99. (gexp->derivation "static-networking" test))
  100. (define %test-static-networking
  101. (system-test
  102. (name "static-networking")
  103. (description "Test the 'static-networking' service.")
  104. (value
  105. (let ((os (marionette-operating-system
  106. (simple-operating-system
  107. (service static-networking-service-type
  108. (list %qemu-static-networking)))
  109. #:imported-modules '((gnu services herd)
  110. (guix combinators)))))
  111. (run-static-networking-test (virtual-machine os))))))
  112. ;;;
  113. ;;; Inetd.
  114. ;;;
  115. (define %inetd-os
  116. ;; Operating system with 2 inetd services.
  117. (simple-operating-system
  118. (service dhcp-client-service-type)
  119. (service inetd-service-type
  120. (inetd-configuration
  121. (entries (list
  122. (inetd-entry
  123. (name "echo")
  124. (socket-type 'stream)
  125. (protocol "tcp")
  126. (wait? #f)
  127. (user "root"))
  128. (inetd-entry
  129. (name "dict")
  130. (socket-type 'stream)
  131. (protocol "tcp")
  132. (wait? #f)
  133. (user "root")
  134. (program (file-append bash
  135. "/bin/bash"))
  136. (arguments
  137. (list "bash" (plain-file "my-dict.sh" "\
  138. while read line
  139. do
  140. if [[ $line =~ ^DEFINE\\ (.*)$ ]]
  141. then
  142. case ${BASH_REMATCH[1]} in
  143. Guix)
  144. echo GNU Guix is a package management tool for the GNU system.
  145. ;;
  146. G-expression)
  147. echo Like an S-expression but with a G.
  148. ;;
  149. *)
  150. echo NO DEFINITION FOUND
  151. ;;
  152. esac
  153. else
  154. echo ERROR
  155. fi
  156. done" ))))))))))
  157. (define* (run-inetd-test)
  158. "Run tests in %INETD-OS, where the inetd service provides an echo service on
  159. port 7, and a dict service on port 2628."
  160. (define os
  161. (marionette-operating-system %inetd-os))
  162. (define vm
  163. (virtual-machine
  164. (operating-system os)
  165. (port-forwardings `((8007 . 7)
  166. (8628 . 2628)))))
  167. (define test
  168. (with-imported-modules '((gnu build marionette))
  169. #~(begin
  170. (use-modules (ice-9 rdelim)
  171. (srfi srfi-64)
  172. (gnu build marionette))
  173. (define marionette
  174. (make-marionette (list #$vm)))
  175. (test-runner-current (system-test-runner #$output))
  176. (test-begin "inetd")
  177. ;; Make sure the PID file is created.
  178. (test-assert "PID file"
  179. (marionette-eval
  180. '(file-exists? "/var/run/inetd.pid")
  181. marionette))
  182. ;; Test the echo service.
  183. (test-equal "echo response"
  184. "Hello, Guix!"
  185. (let ((echo (socket PF_INET SOCK_STREAM 0))
  186. (addr (make-socket-address AF_INET INADDR_LOOPBACK 8007)))
  187. (connect echo addr)
  188. (display "Hello, Guix!\n" echo)
  189. (let ((response (read-line echo)))
  190. (close echo)
  191. response)))
  192. ;; Test the dict service
  193. (test-equal "dict response"
  194. "GNU Guix is a package management tool for the GNU system."
  195. (let ((dict (socket PF_INET SOCK_STREAM 0))
  196. (addr (make-socket-address AF_INET INADDR_LOOPBACK 8628)))
  197. (connect dict addr)
  198. (display "DEFINE Guix\n" dict)
  199. (let ((response (read-line dict)))
  200. (close dict)
  201. response)))
  202. (test-end))))
  203. (gexp->derivation "inetd-test" test))
  204. (define %test-inetd
  205. (system-test
  206. (name "inetd")
  207. (description "Connect to a host with an INETD server.")
  208. (value (run-inetd-test))))
  209. ;;;
  210. ;;; Open vSwitch
  211. ;;;
  212. (define setup-openvswitch
  213. #~(let ((ovs-vsctl (lambda (str)
  214. (zero? (apply system*
  215. #$(file-append openvswitch "/bin/ovs-vsctl")
  216. (string-tokenize str)))))
  217. (add-native-port (lambda (if)
  218. (string-append "--may-exist add-port br0 " if
  219. " vlan_mode=native-untagged"
  220. " -- set Interface " if
  221. " type=internal"))))
  222. (and (ovs-vsctl "--may-exist add-br br0")
  223. ;; Connect eth0 as an "untagged" port (no VLANs).
  224. (ovs-vsctl "--may-exist add-port br0 eth0 vlan_mode=native-untagged")
  225. (ovs-vsctl (add-native-port "ovs0")))))
  226. (define openvswitch-configuration-service
  227. (simple-service 'openvswitch-configuration shepherd-root-service-type
  228. (list (shepherd-service
  229. (provision '(openvswitch-configuration))
  230. (requirement '(vswitchd))
  231. (start #~(lambda ()
  232. #$setup-openvswitch))
  233. (respawn? #f)))))
  234. (define %openvswitch-os
  235. (operating-system
  236. (inherit (simple-operating-system
  237. (simple-service 'openswitch-networking
  238. static-networking-service-type
  239. (list (static-networking
  240. (addresses (list (network-address
  241. (value "10.1.1.1/24")
  242. (device "ovs0"))))
  243. (requirement '(openvswitch-configuration)))))
  244. (service openvswitch-service-type)
  245. openvswitch-configuration-service))
  246. ;; Ensure the interface name does not change depending on the driver.
  247. (kernel-arguments (cons "net.ifnames=0" %default-kernel-arguments))))
  248. (define (run-openvswitch-test)
  249. (define os
  250. (marionette-operating-system %openvswitch-os
  251. #:imported-modules '((gnu services herd)
  252. (guix build syscalls))))
  253. (define test
  254. (with-imported-modules '((gnu build marionette)
  255. (guix build syscalls))
  256. #~(begin
  257. (use-modules (gnu build marionette)
  258. (guix build syscalls)
  259. (ice-9 popen)
  260. (ice-9 rdelim)
  261. (srfi srfi-64))
  262. (define marionette
  263. (make-marionette (list #$(virtual-machine os))))
  264. (test-runner-current (system-test-runner #$output))
  265. (test-begin "openvswitch")
  266. ;; Wait for our configuration to be active (it sets up br0).
  267. (test-assert "openvswitch-configuration is running"
  268. (marionette-eval
  269. '(begin
  270. (use-modules (gnu services herd))
  271. (wait-for-service 'openvswitch-configuration))
  272. marionette))
  273. ;; Make sure the bridge is created.
  274. (test-assert "br0 exists"
  275. (marionette-eval
  276. '(zero? (system* #$(file-append openvswitch "/bin/ovs-vsctl")
  277. "br-exists" "br0"))
  278. marionette))
  279. ;; Make sure eth0 is connected to the bridge.
  280. (test-equal "eth0 is connected to br0"
  281. "br0"
  282. (marionette-eval
  283. '(begin
  284. (use-modules (ice-9 popen) (ice-9 rdelim))
  285. (let* ((port (open-pipe*
  286. OPEN_READ
  287. (string-append #$openvswitch "/bin/ovs-vsctl")
  288. "port-to-br" "eth0"))
  289. (output (read-line port)))
  290. (close-pipe port)
  291. output))
  292. marionette))
  293. ;; Make sure the virtual interface got a static IP.
  294. (test-assert "networking has started on ovs0"
  295. (marionette-eval
  296. '(begin
  297. (use-modules (gnu services herd)
  298. (srfi srfi-1))
  299. (live-service-running
  300. (find (lambda (live)
  301. (memq 'networking
  302. (live-service-provision live)))
  303. (current-services))))
  304. marionette))
  305. (test-equal "ovs0 is up"
  306. IFF_UP
  307. (marionette-eval
  308. '(begin
  309. (use-modules (guix build syscalls))
  310. (let* ((sock (socket AF_INET SOCK_STREAM 0))
  311. (flags (network-interface-flags sock "ovs0")))
  312. (close-port sock)
  313. (logand flags IFF_UP)))
  314. marionette))
  315. (test-end))))
  316. (gexp->derivation "openvswitch-test" test))
  317. (define %test-openvswitch
  318. (system-test
  319. (name "openvswitch")
  320. (description "Test a running OpenvSwitch configuration.")
  321. (value (run-openvswitch-test))))
  322. ;;;
  323. ;;; DHCP Daemon
  324. ;;;
  325. (define minimal-dhcpd-v4-config-file
  326. (plain-file "dhcpd.conf"
  327. "\
  328. default-lease-time 600;
  329. max-lease-time 7200;
  330. subnet 192.168.1.0 netmask 255.255.255.0 {
  331. range 192.168.1.100 192.168.1.200;
  332. option routers 192.168.1.1;
  333. option domain-name-servers 192.168.1.2, 192.168.1.3;
  334. option domain-name \"dummy.domain.name.abc123xyz\";
  335. }
  336. "))
  337. (define dhcpd-v4-configuration
  338. (dhcpd-configuration
  339. (config-file minimal-dhcpd-v4-config-file)
  340. (version "4")
  341. (interfaces '("ens3"))))
  342. (define %dhcpd-os
  343. (simple-operating-system
  344. (service static-networking-service-type
  345. (list (static-networking
  346. (addresses (list (network-address
  347. (value "192.168.1.4/24")
  348. (device "ens3"))))
  349. (routes (list (network-route
  350. (destination "default")
  351. (gateway "192.168.1.1"))))
  352. (name-servers '("192.168.1.2" "192.168.1.3")))))
  353. (service dhcpd-service-type dhcpd-v4-configuration)))
  354. (define (run-dhcpd-test)
  355. (define os
  356. (marionette-operating-system %dhcpd-os
  357. #:imported-modules '((gnu services herd))))
  358. (define test
  359. (with-imported-modules '((gnu build marionette))
  360. #~(begin
  361. (use-modules (gnu build marionette)
  362. (srfi srfi-64))
  363. (define marionette
  364. (make-marionette (list #$(virtual-machine os))))
  365. (test-runner-current (system-test-runner #$output))
  366. (test-begin "dhcpd")
  367. (test-assert "pid file exists"
  368. (wait-for-file
  369. '#$(dhcpd-configuration-pid-file dhcpd-v4-configuration)
  370. marionette))
  371. (test-assert "lease file exists"
  372. (wait-for-file
  373. '#$(dhcpd-configuration-lease-file dhcpd-v4-configuration)
  374. marionette
  375. #:read '(@ (ice-9 textual-ports) get-string-all)))
  376. (test-assert "run directory exists"
  377. (marionette-eval
  378. '(file-exists?
  379. #$(dhcpd-configuration-run-directory dhcpd-v4-configuration))
  380. marionette))
  381. (test-assert "dhcpd is alive"
  382. (marionette-eval
  383. '(begin
  384. (use-modules (gnu services herd))
  385. (wait-for-service 'dhcpv4-daemon))
  386. marionette))
  387. (test-end))))
  388. (gexp->derivation "dhcpd-test" test))
  389. (define %test-dhcpd
  390. (system-test
  391. (name "dhcpd")
  392. (description "Test a running DHCP daemon configuration.")
  393. (value (run-dhcpd-test))))
  394. ;;;
  395. ;;; Services related to Tor
  396. ;;;
  397. (define %tor-os
  398. (simple-operating-system
  399. (service tor-service-type)))
  400. (define %tor-os/unix-socks-socket
  401. (simple-operating-system
  402. (service tor-service-type
  403. (tor-configuration
  404. (socks-socket-type 'unix)))))
  405. (define (run-tor-test)
  406. (define os
  407. (marionette-operating-system %tor-os
  408. #:imported-modules '((gnu services herd))
  409. #:requirements '(tor)))
  410. (define os/unix-socks-socket
  411. (marionette-operating-system %tor-os/unix-socks-socket
  412. #:imported-modules '((gnu services herd))
  413. #:requirements '(tor)))
  414. (define test
  415. (with-imported-modules '((gnu build marionette))
  416. #~(begin
  417. (use-modules (gnu build marionette)
  418. (ice-9 popen)
  419. (ice-9 rdelim)
  420. (srfi srfi-64))
  421. (define marionette
  422. (make-marionette (list #$(virtual-machine os))))
  423. (define (tor-is-alive? marionette)
  424. (marionette-eval
  425. '(begin
  426. (use-modules (gnu services herd)
  427. (srfi srfi-1))
  428. (live-service-running
  429. (find (lambda (live)
  430. (memq 'tor
  431. (live-service-provision live)))
  432. (current-services))))
  433. marionette))
  434. (test-runner-current (system-test-runner #$output))
  435. (test-begin "tor")
  436. ;; Test the usual Tor service.
  437. (test-assert "tor is alive"
  438. (tor-is-alive? marionette))
  439. (test-assert "tor is listening"
  440. (let ((default-port 9050))
  441. (wait-for-tcp-port default-port marionette)))
  442. ;; Don't run two VMs at once.
  443. (marionette-control "quit" marionette)
  444. ;; Test the Tor service using a SOCKS socket.
  445. (let* ((socket-directory "/tmp/more-sockets")
  446. (_ (mkdir socket-directory))
  447. (marionette/unix-socks-socket
  448. (make-marionette
  449. (list #$(virtual-machine os/unix-socks-socket))
  450. ;; We can't use the same socket directory as the first
  451. ;; marionette.
  452. #:socket-directory socket-directory)))
  453. (test-assert "tor is alive, even when using a SOCKS socket"
  454. (tor-is-alive? marionette/unix-socks-socket))
  455. (test-assert "tor is listening, even when using a SOCKS socket"
  456. (wait-for-unix-socket "/var/run/tor/socks-sock"
  457. marionette/unix-socks-socket)))
  458. (test-end))))
  459. (gexp->derivation "tor-test" test))
  460. (define %test-tor
  461. (system-test
  462. (name "tor")
  463. (description "Test a running Tor daemon configuration.")
  464. (value (run-tor-test))))
  465. (define* (run-iptables-test)
  466. "Run tests of 'iptables-service-type'."
  467. (define iptables-rules
  468. "*filter
  469. :INPUT ACCEPT
  470. :FORWARD ACCEPT
  471. :OUTPUT ACCEPT
  472. -A INPUT -p tcp -m tcp --dport 7 -j REJECT --reject-with icmp-port-unreachable
  473. COMMIT
  474. ")
  475. (define ip6tables-rules
  476. "*filter
  477. :INPUT ACCEPT
  478. :FORWARD ACCEPT
  479. :OUTPUT ACCEPT
  480. -A INPUT -p tcp -m tcp --dport 7 -j REJECT --reject-with icmp6-port-unreachable
  481. COMMIT
  482. ")
  483. (define inetd-echo-port 7)
  484. (define os
  485. (marionette-operating-system
  486. (simple-operating-system
  487. (service dhcp-client-service-type)
  488. (service inetd-service-type
  489. (inetd-configuration
  490. (entries (list
  491. (inetd-entry
  492. (name "echo")
  493. (socket-type 'stream)
  494. (protocol "tcp")
  495. (wait? #f)
  496. (user "root"))))))
  497. (service iptables-service-type
  498. (iptables-configuration
  499. (ipv4-rules (plain-file "iptables.rules" iptables-rules))
  500. (ipv6-rules (plain-file "ip6tables.rules" ip6tables-rules)))))
  501. #:imported-modules '((gnu services herd))
  502. #:requirements '(inetd iptables)))
  503. (define test
  504. (with-imported-modules '((gnu build marionette))
  505. #~(begin
  506. (use-modules (srfi srfi-64)
  507. (gnu build marionette))
  508. (define marionette
  509. (make-marionette (list #$(virtual-machine os))))
  510. (define (dump-iptables iptables-save marionette)
  511. (marionette-eval
  512. `(begin
  513. (use-modules (ice-9 popen)
  514. (ice-9 rdelim)
  515. (ice-9 regex))
  516. (call-with-output-string
  517. (lambda (out)
  518. (call-with-port
  519. (open-pipe* OPEN_READ ,iptables-save)
  520. (lambda (in)
  521. (let loop ((line (read-line in)))
  522. ;; iptables-save does not output rules in the exact
  523. ;; same format we loaded using iptables-restore. It
  524. ;; adds comments, packet counters, etc. We remove
  525. ;; these additions.
  526. (unless (eof-object? line)
  527. (cond
  528. ;; Remove comments
  529. ((string-match "^#" line) #t)
  530. ;; Remove packet counters
  531. ((string-match "^:([A-Z]*) ([A-Z]*) .*" line)
  532. => (lambda (match-record)
  533. (format out ":~a ~a~%"
  534. (match:substring match-record 1)
  535. (match:substring match-record 2))))
  536. ;; Pass other lines without modification
  537. (else (display line out)
  538. (newline out)))
  539. (loop (read-line in)))))))))
  540. marionette))
  541. (test-runner-current (system-test-runner #$output))
  542. (test-begin "iptables")
  543. (test-equal "iptables-save dumps the same rules that were loaded"
  544. (dump-iptables #$(file-append iptables "/sbin/iptables-save")
  545. marionette)
  546. #$iptables-rules)
  547. (test-equal "ip6tables-save dumps the same rules that were loaded"
  548. (dump-iptables #$(file-append iptables "/sbin/ip6tables-save")
  549. marionette)
  550. #$ip6tables-rules)
  551. (test-error "iptables firewall blocks access to inetd echo service"
  552. 'misc-error
  553. (wait-for-tcp-port inetd-echo-port marionette #:timeout 5))
  554. ;; TODO: This test freezes up at the login prompt without any
  555. ;; relevant messages on the console. Perhaps it is waiting for some
  556. ;; timeout. Find and fix this issue.
  557. ;; (test-assert "inetd echo service is accessible after iptables firewall is stopped"
  558. ;; (begin
  559. ;; (marionette-eval
  560. ;; '(begin
  561. ;; (use-modules (gnu services herd))
  562. ;; (stop-service 'iptables))
  563. ;; marionette)
  564. ;; (wait-for-tcp-port inetd-echo-port marionette #:timeout 5)))
  565. (test-end))))
  566. (gexp->derivation "iptables" test))
  567. (define %test-iptables
  568. (system-test
  569. (name "iptables")
  570. (description "Test a running iptables daemon.")
  571. (value (run-iptables-test))))
  572. ;;;
  573. ;;; IPFS service
  574. ;;;
  575. (define %ipfs-os
  576. (simple-operating-system
  577. (service ipfs-service-type)))
  578. (define (run-ipfs-test)
  579. (define os
  580. (marionette-operating-system %ipfs-os
  581. #:imported-modules (source-module-closure
  582. '((gnu services herd)
  583. (guix ipfs)))
  584. #:extensions (list guile-json-4)
  585. #:requirements '(ipfs)))
  586. (define test
  587. (with-imported-modules '((gnu build marionette))
  588. #~(begin
  589. (use-modules (gnu build marionette)
  590. (rnrs bytevectors)
  591. (srfi srfi-64)
  592. (ice-9 binary-ports))
  593. (define marionette
  594. (make-marionette (list #$(virtual-machine os))))
  595. (define (ipfs-is-alive?)
  596. (marionette-eval
  597. '(begin
  598. (use-modules (gnu services herd)
  599. (srfi srfi-1))
  600. (live-service-running
  601. (find (lambda (live)
  602. (memq 'ipfs
  603. (live-service-provision live)))
  604. (current-services))))
  605. marionette))
  606. ;; The default API endpoint port 5001 is used,
  607. ;; so there is no need to parameterize %ipfs-base-url.
  608. (define (add-data data)
  609. (marionette-eval `(content-name (add-data ,data)) marionette))
  610. (define (read-contents object)
  611. (marionette-eval
  612. `(let* ((input (read-contents ,object))
  613. (all-input (get-bytevector-all input)))
  614. (close-port input)
  615. all-input)
  616. marionette))
  617. (marionette-eval '(use-modules (guix ipfs)) marionette)
  618. (test-runner-current (system-test-runner #$output))
  619. (test-begin "ipfs")
  620. ;; Test the IPFS service.
  621. (test-assert "ipfs is alive" (ipfs-is-alive?))
  622. (test-assert "ipfs is listening on the gateway"
  623. (let ((default-port 8082))
  624. (wait-for-tcp-port default-port marionette)))
  625. (test-assert "ipfs is listening on the API endpoint"
  626. (let ((default-port 5001))
  627. (wait-for-tcp-port default-port marionette)))
  628. (define test-bv (string->utf8 "hello ipfs!"))
  629. (test-equal "can upload and download a file to/from ipfs"
  630. test-bv
  631. (read-contents (add-data test-bv)))
  632. (test-end))))
  633. (gexp->derivation "ipfs-test" test))
  634. (define %test-ipfs
  635. (system-test
  636. (name "ipfs")
  637. (description "Test a running IPFS daemon configuration.")
  638. (value (run-ipfs-test))))