From 7ccf3af51d4bb2f562853f20cf2aebdbd4a8e5a5 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 18 Mar 2019 13:10:32 +1030 Subject: [PATCH] pytest: test the db hook plugin. Signed-off-by: Rusty Russell --- tests/plugins/dblog.py | 39 +++++++++++++++++++++++++++++++++++++++ tests/test_plugin.py | 27 +++++++++++++++++++++++++++ wallet/db.c | 10 +++------- wallet/test/run-db.c | 4 ++++ wallet/test/run-wallet.c | 3 +++ 5 files changed, 76 insertions(+), 7 deletions(-) create mode 100755 tests/plugins/dblog.py diff --git a/tests/plugins/dblog.py b/tests/plugins/dblog.py new file mode 100755 index 000000000..1cf601075 --- /dev/null +++ b/tests/plugins/dblog.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +"""This plugin is used to check that db_write calls are working correctly. +""" +from lightning import Plugin, RpcError +import sqlite3 + +plugin = Plugin() +plugin.sqlite_pre_init_cmds = [] +plugin.initted = False + + +@plugin.init() +def init(configuration, options, plugin): + if not plugin.get_option('dblog-file'): + raise RpcError("No dblog-file specified") + plugin.conn = sqlite3.connect(plugin.get_option('dblog-file'), + isolation_level=None) + plugin.log("replaying pre-init data:") + for c in plugin.sqlite_pre_init_cmds: + plugin.conn.execute(c) + plugin.log("{}".format(c)) + plugin.initted = True + plugin.log("initialized") + + +@plugin.hook('db_write') +def db_write(plugin, writes): + if not plugin.initted: + plugin.log("deferring {} commands".format(len(writes))) + plugin.sqlite_pre_init_cmds += writes + else: + for c in writes: + plugin.conn.execute(c) + plugin.log("{}".format(c)) + return True + + +plugin.add_option('dblog-file', None, 'The db file to create.') +plugin.run() diff --git a/tests/test_plugin.py b/tests/test_plugin.py index cf278c86c..d6074d1c7 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -3,7 +3,9 @@ from fixtures import * # noqa: F401,F403 from lightning import RpcError, Millisatoshi from utils import only_one +import os import pytest +import sqlite3 import subprocess import time @@ -190,6 +192,31 @@ def test_async_rpcmethod(node_factory, executor): assert [r.result() for r in results] == [42] * len(results) +def test_db_hook(node_factory, executor): + """This tests the db hook.""" + dbfile = os.path.join(node_factory.directory, "dblog.sqlite3") + l1 = node_factory.get_node(options={'plugin': 'tests/plugins/dblog.py', + 'dblog-file': dbfile}) + + # It should see the db being created, and sometime later actually get + # initted. + # This precedes startup, so needle already past + assert l1.daemon.is_in_log('plugin-dblog.py deferring 1 commands') + l1.daemon.logsearch_start = 0 + l1.daemon.wait_for_log('plugin-dblog.py replaying pre-init data:') + l1.daemon.wait_for_log('plugin-dblog.py PRAGMA foreign_keys = ON;') + l1.daemon.wait_for_log('plugin-dblog.py CREATE TABLE version \\(version INTEGER\\)') + l1.daemon.wait_for_log('plugin-dblog.py initialized') + + l1.stop() + + # Databases should be identical. + db1 = sqlite3.connect(os.path.join(l1.daemon.lightning_dir, 'lightningd.sqlite3')) + db2 = sqlite3.connect(dbfile) + + assert [x for x in db1.iterdump()] == [x for x in db2.iterdump()] + + def test_utf8_passthrough(node_factory, executor): l1 = node_factory.get_node(options={'plugin': 'tests/plugins/utf8.py', 'log-level': 'io'}) diff --git a/wallet/db.c b/wallet/db.c index 879f5b2b6..707d2e19a 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -7,6 +7,7 @@ #include #include #include +#include #define DB_FILE "lightningd.sqlite3" @@ -578,13 +579,8 @@ static void db_report_changes(struct db *db, const char *final, size_t min) assert(db->changes); assert(tal_count(db->changes) >= min); - if (tal_count(db->changes) > min) { - fprintf(stderr, "Committing db changes:\n"); - for (size_t i = 0; i < tal_count(db->changes); i++) - fprintf(stderr, " %zu: %s\n", i, db->changes[i]); - if (final) - fprintf(stderr, " %s\n", final); - } + if (tal_count(db->changes) > min) + plugin_hook_db_sync(db, db->changes, final); db->changes = tal_free(db->changes); } diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index 916478dee..25cfbf393 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -37,6 +37,10 @@ static void db_test_fatal(const char *fmt, ...) va_end(ap); } +void plugin_hook_db_sync(struct db *db UNNEEDED, const char **changes UNNEEDED, const char *final UNNEEDED) +{ +} + static struct db *create_test_db(void) { struct db *db; diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index fb4411c2a..caa399b82 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -564,6 +564,9 @@ u8 *wire_sync_read(const tal_t *ctx UNNEEDED, int fd UNNEEDED) { return NULL; } +void plugin_hook_db_sync(struct db *db UNNEEDED, const char **changes UNNEEDED, const char *final UNNEEDED) +{ +} bool fromwire_hsm_get_channel_basepoints_reply(const void *p UNNEEDED, struct basepoints *basepoints, struct pubkey *funding_pubkey)