summary refs log tree commit diff
path: root/nixos/tests/mosquitto.nix
blob: b4c897c3ab5db37b735ab9536f49bc5014bc3068 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import ./make-test.nix ({ pkgs, ... }:

let
  port = 1888;
  username = "mqtt";
  password = "VERY_secret";
  topic = "test/foo";

  cmd = bin: pkgs.lib.concatStringsSep " " [
    "${pkgs.mosquitto}/bin/mosquitto_${bin}"
    "-V mqttv311"
    "-h server"
    "-p ${toString port}"
    "-u ${username}"
    "-P '${password}'"
    "-t ${topic}"
  ];

in {
  name = "mosquitto";
  meta = with pkgs.stdenv.lib; {
    maintainers = with maintainers; [ peterhoeg ];
  };

  nodes = let
    client = { pkgs, ... }: {
      environment.systemPackages = with pkgs; [ mosquitto ];
    };
  in {
    server = { pkgs, ... }: {
      networking.firewall.allowedTCPPorts = [ port ];
      services.mosquitto = {
        inherit port;
        enable = true;
        host = "0.0.0.0";
        checkPasswords = true;
        users.${username} = {
          inherit password;
          acl = [
            "topic readwrite ${topic}"
          ];
        };
      };
    };

    client1 = client;
    client2 = client;
  };

  testScript = let
    file = "/tmp/msg";
    sub = args:
      "(${cmd "sub"} -C 1 ${args} | tee ${file} &)";
  in ''
    startAll;
    $server->waitForUnit("mosquitto.service");

    $server->fail("test -f ${file}");
    $client1->fail("test -f ${file}");
    $client2->fail("test -f ${file}");


    # QoS = 0, so only one subscribers should get it
    $server->execute("${sub "-q 0"}");

    # we need to give the subscribers some time to connect
    $client2->execute("sleep 5");
    $client2->succeed("${cmd "pub"} -m FOO -q 0");

    $server->waitUntilSucceeds("grep -q FOO ${file}");
    $server->execute("rm ${file}");


    # QoS = 1, so both subscribers should get it
    $server->execute("${sub "-q 1"}");
    $client1->execute("${sub "-q 1"}");

    # we need to give the subscribers some time to connect
    $client2->execute("sleep 5");
    $client2->succeed("${cmd "pub"} -m BAR -q 1");

    $server->waitUntilSucceeds("grep -q BAR ${file}");
    $server->execute("rm ${file}");

    $client1->waitUntilSucceeds("grep -q BAR ${file}");
    $client1->execute("rm ${file}");
  '';
})