From 9d71ab7ae495e9dfc43b144c8b85e34e3c9776a8 Mon Sep 17 00:00:00 2001 From: zmrocze Date: Tue, 16 Apr 2024 11:55:45 +0200 Subject: [PATCH] Cardano-db-sync test [not yet] passes --- modules/cardano-db-sync.nix | 77 +++++++++++++++++++++---------------- modules/http.nix | 5 +++ tests/cardano-db-sync.nix | 31 +++++---------- 3 files changed, 57 insertions(+), 56 deletions(-) diff --git a/modules/cardano-db-sync.nix b/modules/cardano-db-sync.nix index ea739d1..0a54224 100644 --- a/modules/cardano-db-sync.nix +++ b/modules/cardano-db-sync.nix @@ -63,43 +63,52 @@ in { }; }; - config = mkIf cfg.enable { - services.cardano-db-sync = { - enable = true; - environment = cfg._environment; - inherit (config.cardano.node) socketPath; - postgres = { - inherit (config.services.postgresql.settings) port; - inherit (cfg.postgres) database; - user = cfg.postgres.database; - socketdir = "/var/run/postgresql"; + config = let + inherit (cfg.postgres) database; + in + mkIf cfg.enable { + services.cardano-db-sync = { + enable = true; + environment = cfg._environment; + inherit (config.cardano.node) socketPath; + postgres = { + inherit (config.services.postgresql.settings) port; + inherit database; + user = database; + socketdir = "/var/run/postgresql"; + }; + stateDir = "/var/lib/${database}"; + inherit (cfg) explorerConfig logConfig disableLedger takeSnapshot restoreSnapshot restoreSnapshotSha; }; - inherit (cfg) explorerConfig logConfig disableLedger takeSnapshot restoreSnapshot restoreSnapshotSha; - }; - services.postgresql = { - enable = true; - ensureDatabases = [cfg.postgres.database]; - ensureUsers = [ + services.postgresql = { + enable = true; + ensureDatabases = [database]; + ensureUsers = [ + { + name = "${database}"; + ensureDBOwnership = true; + } + ]; + authentication = + # type database DBuser auth-method optional_ident_map + '' + local sameuser ${database} peer + ''; + }; + systemd.services.cardano-db-sync = mkIf (config.cardano.node.enable or false) { + after = ["cardano-node-socket.service"]; + requires = ["cardano-node-socket.service"]; + serviceConfig = { + DynamicUser = true; + User = database; + Group = database; + }; + }; + assertions = [ { - name = "${cfg.postgres.database}"; - ensureDBOwnership = true; + assertion = config.cardano.node.enable; + message = "Cardano db sync requires `cardano.node.enable`."; } ]; }; - systemd.services.cardano-db-sync = mkIf (config.cardano.node.enable or false) { - after = ["cardano-node-socket.service"]; - requires = ["cardano-node-socket.service"]; - serviceConfig = { - DynamicUser = true; - User = cfg.postgres.database; - Group = cfg.postgres.database; - }; - }; - assertions = [ - { - assertion = config.cardano.node.enable; - message = "Cardano db sync requires `cardano.node.enable`."; - } - ]; - }; } diff --git a/modules/http.nix b/modules/http.nix index 35f90e3..e5b71cf 100644 --- a/modules/http.nix +++ b/modules/http.nix @@ -26,6 +26,11 @@ in { inherit (config.services.ogmios) port; inherit (config.services.ogmios.package) version; }; + # it's cardano-db-sync db, so: + cardano-db = { + inherit (config.services.cardano-db-sync.postgres) port; + inherit (config.services.cardano-db-sync.package) version; + }; }; }; }; diff --git a/tests/cardano-db-sync.nix b/tests/cardano-db-sync.nix index 22cd9ba..7e92f00 100644 --- a/tests/cardano-db-sync.nix +++ b/tests/cardano-db-sync.nix @@ -12,29 +12,16 @@ environment.systemPackages = with pkgs; [jq bc curl postgresql]; }; - testScript = _: - # let - # cfg = nodes.machine; - # dbname = cfg.cardano.cardano-db-sync.postgres.database; - # host = "localhost"; - # sql = '' - # select - # 100 * (extract (epoch from (max (time) at time zone 'UTC')) - extract (epoch from (min (time) at time zone 'UTC'))) - # / (extract (epoch from (now () at time zone 'UTC')) - extract (epoch from (min (time) at time zone 'UTC'))) - # as sync_percent - # from block ; - # sync_percent - # ''; - # print(machine.succeed("systemd-analyze security cardano-db-sync")) - # print(machine.succeed("""psql --no-password -h '${host}' -U '${dbname}' -d '${dbname}' -c '${sql}' <<< '*\n' """)) - # in - '' + testScript = {nodes, ...}: let + cfg = nodes.machine; + dbname = cfg.cardano.cardano-db-sync.postgres.database; + inherit (cfg.services.cardano-db-sync.postgres) socketdir; + # get sync percentage, return true if it's above 0.000001 + sql = "select (100 * (extract (epoch from (max (time) at time zone 'UTC')) - extract (epoch from (min (time) at time zone 'UTC'))) / (extract (epoch from (now () at time zone 'UTC')) - extract (epoch from (min (time) at time zone 'UTC')))) > 0.000001 from block limit 1;"; + output = "?column?\\n----------\\nt\\n(1 row)"; # postgres "true" result + in '' machine.wait_for_unit("cardano-db-sync") - - print(machine.execute("pwd")) - print(machine.execute("ls /var/lib")) - print(machine.execute("users")) - print(machine.execute("cat /etc/passwd")) + machine.wait_until_succeeds(r"""[[ $(sudo -u postgres psql --no-password "host=${socketdir} user=postgres dbname=${dbname}" -c "${sql}") = "${output}" ]] """, 150) ''; }; };