summaryrefslogtreecommitdiffstats
path: root/nixos/tests/centrifugo.nix
blob: 5867297c3d30f652c38f04492851f38b96f42ddc (plain)
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
89
let
  redisPort = 6379;
  centrifugoPort = 8080;
  nodes = [
    "centrifugo1"
    "centrifugo2"
    "centrifugo3"
  ];
in
{ lib, ... }:
{
  name = "centrifugo";
  meta.maintainers = [
    lib.maintainers.tie
    lib.maintainers.valodim
  ];

  nodes = lib.listToAttrs (
    lib.imap0 (index: name: {
      inherit name;
      value =
        { config, ... }:
        {
          services.centrifugo = {
            enable = true;
            settings = {
              node = {
                inherit name;
              };
              http_server.port = centrifugoPort;
              http_api.insecure = true;
              usage_stats.disabled = true;

              engine.type = "redis";
              engine.redis.address =
                let
                  toRedisAddresses = map (name: "${name}:${toString redisPort}");
                in
                toRedisAddresses (lib.take index nodes)
                ++ [
                  "unix://${config.services.redis.servers.centrifugo.unixSocket}"
                ]
                ++ toRedisAddresses (lib.drop (index + 1) nodes);
            };
            extraGroups = [
              config.services.redis.servers.centrifugo.user
            ];
          };
          services.redis.servers.centrifugo = {
            enable = true;
            bind = null; # all interfaces
            port = redisPort;
            openFirewall = true;
            settings.protected-mode = false;
          };
        };
    }) nodes
  );

  testScript = ''
    import json

    redisPort = ${toString redisPort}
    centrifugoPort = ${toString centrifugoPort}

    start_all()

    for machine in machines:
      machine.wait_for_unit("redis-centrifugo.service")
      machine.wait_for_open_port(redisPort)

    for machine in machines:
      machine.wait_for_unit("centrifugo.service")
      machine.wait_for_open_port(centrifugoPort)

    # See https://centrifugal.dev/docs/server/server_api#info
    def list_nodes(machine):
      curl = "curl --fail-with-body --silent"
      body = "{}"
      resp = json.loads(machine.succeed(f"{curl} -d '{body}' http://localhost:{centrifugoPort}/api/info"))
      return resp["result"]["nodes"]
    machineNames = {m.name for m in machines}
    for machine in machines:
      nodes = list_nodes(machine)
      assert len(nodes) == len(machines)
      nodeNames = {n['name'] for n in nodes}
      assert machineNames == nodeNames
  '';
}