Skip to content

Commit

Permalink
WIP cardano-db-sync test
Browse files Browse the repository at this point in the history
  • Loading branch information
zmrocze committed Apr 9, 2024
1 parent 6920f5c commit 059b6de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
26 changes: 10 additions & 16 deletions modules/cardano-db-sync.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ in {
default = "cdbsync";
description = "Used for postgresql database and the cardano-db-sync service user.";
};
_environment = mkOption {
default = config.services.cardano-node.environments.${config.cardano.network};
internal = true;
type = attrs;
};
# `services.cardano-db-sync` module options:
explorerConfig = mkOption {
type = attrs;
default = cfg.environment.dbSyncConfig;
default = cfg._environment.dbSyncConfig;
};
logConfig = mkOption {
type = attrs;
Expand Down Expand Up @@ -61,20 +66,15 @@ in {
config = mkIf cfg.enable {
services.cardano-db-sync = {
enable = true;
environment = config.services.cardano-node.environments.${config.cardano.network};
environment = cfg._environment;
inherit (config.cardano.node) socketPath;
postgres = {
inherit (config.services.postgresql) port;
inherit (config.services.postgresql.settings) port;
inherit (cfg.postgres) database;
user = cfg.postgres.database;
socketdir = "/var/run/postgresql";
};
inherit (cfg) explorerConfig;
inherit (cfg) logConfig;
inherit (cfg) disableLedger;
inherit (cfg) takeSnapshot;
inherit (cfg) restoreSnapshot;
inherit (cfg) restoreSnapshotSha;
inherit (cfg) explorerConfig logConfig disableLedger takeSnapshot restoreSnapshot restoreSnapshotSha;
};
services.postgresql = {
enable = true;
Expand All @@ -86,19 +86,13 @@ in {
}
];
};
# users.users = {
# "${cfg.postgres.database}" = {
# group = "${cfg.postgres.database}";
# isSystemUser = true;
# };
# };
# users.groups."${cfg.postgres.database}" = {};
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 = [
Expand Down
28 changes: 13 additions & 15 deletions tests/cardano-db-sync.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,27 @@
network = "preview";
cli.enable = true;
node.enable = true;
# cardano-db-sync.enable = true;
};
services.postgresql = {
enable = true;
ensureUsers = [
{
name = "karol";
ensureDBOwnership = true;
}
];
cardano-db-sync.enable = true;
};
environment.systemPackages = with pkgs; [jq bc curl postgresql];
};

testScript = {nodes, ...}: let
magic = toString nodes.machine.config.cardano.networkNumber;
cfg = nodes.machine.config;
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
'';
in ''
machine.wait_for_unit("cardano-node")
machine.wait_for_unit("cardano-node-socket")
machine.wait_until_succeeds("""[[ $(echo "$(cardano-cli query tip --testnet-magic ${magic} | jq '.syncProgress' --raw-output) > 0.001" | bc) == "1" ]]""")
machine.wait_for_unit("cardano-db-sync")
print(machine.succeed("systemd-analyze security cardano-db-sync"))
print(machine.succeed("""psql --no-password -h '${host}' -U '${dbname}' -d '${dbname}' -c '${sql}' <<< '*\n' """))
'';
};
};
Expand Down

0 comments on commit 059b6de

Please sign in to comment.