nixpkgs/nixos/tests/nats.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
1.8 KiB
Nix
Raw Normal View History

2021-06-26 11:58:40 +00:00
let
port = 4222;
username = "client";
password = "password";
topic = "foo.bar";
in
import ./make-test-python.nix (
{ pkgs, lib, ... }:
{
name = "nats";
meta = with pkgs.lib; {
maintainers = with maintainers; [ c0deaddict ];
};
2021-06-26 11:58:40 +00:00
nodes =
let
client =
2021-06-26 11:58:40 +00:00
{ pkgs, ... }:
{
environment.systemPackages = with pkgs; [ natscli ];
};
in
{
server =
2021-06-26 11:58:40 +00:00
{ pkgs, ... }:
{
2021-06-26 11:58:40 +00:00
networking.firewall.allowedTCPPorts = [ port ];
services.nats = {
inherit port;
enable = true;
settings = {
authorization = {
users = [
{
2021-06-26 11:58:40 +00:00
user = username;
inherit password;
}
];
2021-06-26 11:58:40 +00:00
};
};
};
};
2021-06-26 11:58:40 +00:00
client1 = client;
client2 = client;
};
testScript =
let
file = "/tmp/msg";
in
''
def nats_cmd(*args):
return (
"nats "
"--server=nats://server:${toString port} "
"--user=${username} "
"--password=${password} "
"{}"
).format(" ".join(args))
2022-02-19 15:08:06 +00:00
def parallel(*fns):
from threading import Thread
threads = [ Thread(target=fn) for fn in fns ]
for t in threads: t.start()
for t in threads: t.join()
2021-06-26 11:58:40 +00:00
start_all()
server.wait_for_unit("nats.service")
2022-02-19 15:08:06 +00:00
with subtest("pub sub"):
parallel(
lambda: client1.succeed(nats_cmd("sub", "--count", "1", "${topic}")),
lambda: client2.succeed("sleep 2 && {}".format(nats_cmd("pub", "${topic}", "hello"))),
)
2021-06-26 11:58:40 +00:00
'';
}
)