Skip to content

Commit

Permalink
Cardano-db-sync test [not yet] passes
Browse files Browse the repository at this point in the history
  • Loading branch information
zmrocze committed Apr 16, 2024
1 parent 662058a commit 9d71ab7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 56 deletions.
77 changes: 43 additions & 34 deletions modules/cardano-db-sync.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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`.";
}
];
};
}
5 changes: 5 additions & 0 deletions modules/http.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
};
};
Expand Down
31 changes: 9 additions & 22 deletions tests/cardano-db-sync.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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)
'';
};
};
Expand Down

0 comments on commit 9d71ab7

Please sign in to comment.