1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-11-19 04:02:10 +00:00
nixpkgs/nixos/tests/mosquitto.nix

89 lines
2.1 KiB
Nix
Raw Normal View History

2019-04-24 09:23:13 +01:00
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}"
];
2019-08-13 22:52:01 +01:00
in {
2019-04-24 09:23:13 +01:00
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;
2019-08-13 22:52:01 +01:00
users.${username} = {
2019-04-24 09:23:13 +01:00
inherit password;
acl = [
"topic readwrite ${topic}"
];
};
};
};
client1 = client;
client2 = client;
};
testScript = let
file = "/tmp/msg";
2019-06-24 06:59:53 +01:00
sub = args:
"(${cmd "sub"} -C 1 ${args} | tee ${file} &)";
2019-04-24 09:23:13 +01:00
in ''
startAll;
$server->waitForUnit("mosquitto.service");
$server->fail("test -f ${file}");
$client1->fail("test -f ${file}");
2019-06-24 06:59:53 +01:00
$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"}");
2019-04-24 09:23:13 +01:00
2019-06-24 06:59:53 +01:00
# we need to give the subscribers some time to connect
$client2->execute("sleep 5");
$client2->succeed("${cmd "pub"} -m BAR -q 1");
2019-04-24 09:23:13 +01:00
2019-06-24 06:59:53 +01:00
$server->waitUntilSucceeds("grep -q BAR ${file}");
$server->execute("rm ${file}");
2019-04-24 09:23:13 +01:00
2019-06-24 06:59:53 +01:00
$client1->waitUntilSucceeds("grep -q BAR ${file}");
$client1->execute("rm ${file}");
2019-04-24 09:23:13 +01:00
'';
})