1# NixOS test for usbrelayd
2#
3# It is not stored in nixos/tests directory, because it requires the
4# USB relay connected to the host computer and as such, it cannot be
5# run automatically.
6#
7# Run this test as:
8#
9# nix-build test.nix -A driverInteractive && ./result/bin/nixos-test-driver --no-interactive
10#
11# The interactive driver is required because the default
12# (non-interactive) driver uses qemu without support for passing USB
13# devices to the guest (see
14# https://discourse.nixos.org/t/hardware-dependent-nixos-tests/18564
15# for discussion of other alternatives).
16
17import ../../../../nixos/tests/make-test-python.nix (
18 { pkgs, ... }:
19 {
20 name = "usbrelayd";
21
22 nodes.machine = {
23 virtualisation.qemu.options = [
24 "-device qemu-xhci"
25 "-device usb-host,vendorid=0x16c0,productid=0x05df"
26 ];
27 services.usbrelayd.enable = true;
28 systemd.services.usbrelayd = {
29 after = [ "mosquitto.service" ];
30 };
31 services.mosquitto = {
32 enable = true;
33 listeners = [
34 {
35 acl = [ "pattern readwrite #" ];
36 omitPasswordAuth = true;
37 settings.allow_anonymous = true;
38 }
39 ];
40 };
41 environment.systemPackages = [
42 pkgs.usbrelay
43 pkgs.mosquitto
44 ];
45 documentation.nixos.enable = false; # building nixos manual takes long time
46 };
47
48 testScript = ''
49 import os
50 if os.waitstatus_to_exitcode(os.system("lsusb -d 16c0:05df")) != 0:
51 print("No USB relay detected, skipping test")
52 import sys
53 sys.exit(2)
54 machine.start()
55 # usbrelayd is started by udev when an relay is detected
56 machine.wait_for_unit("usbrelayd.service")
57
58 stdout = machine.succeed("usbrelay")
59 relay_id = stdout.split(sep="_")[0]
60 assert relay_id != ""
61 import time
62 time.sleep(1)
63 machine.succeed(f"mosquitto_pub -h localhost -t cmnd/{relay_id}/1 -m ON")
64 time.sleep(1)
65 machine.succeed(f"mosquitto_pub -h localhost -t cmnd/{relay_id}/1 -m OFF")
66 print("Did you see the relay switching on and off?")
67 '';
68 }
69)