From 896da4fc654166eda99aaadc3e441892b24c1211 Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Fri, 18 Jun 2021 11:03:15 +0200 Subject: [PATCH] pytest: rescan restarts plugin on update --- tests/test_plugin.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 342e4381e..4d5319aa5 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -21,6 +21,7 @@ import random import re import signal import sqlite3 +import stat import subprocess import time import unittest @@ -2488,3 +2489,51 @@ def test_custom_notification_topics(node_factory): # The plugin just dist what previously was a fatal mistake (emit # an unknown notification), make sure we didn't kill it. assert 'custom_notifications.py' in [p['name'] for p in l1.rpc.listconfigs()['plugins']] + + +def test_restart_on_update(node_factory): + """Tests if plugin rescan restarts modified plugins + """ + # we need to write plugin content dynamically + content = """#!/usr/bin/env python3 +from pyln.client import Plugin +import time +plugin = Plugin() +@plugin.init() +def init(options, configuration, plugin): + plugin.log("test_restart_on_update %s") +plugin.run() + """ + + # get a node that is not started so we can put a plugin in its lightning_dir + n = node_factory.get_node(start=False) + lndir = n.daemon.lightning_dir + + # write hello world plugin to lndir/plugins + os.makedirs(os.path.join(lndir, 'plugins'), exist_ok=True) + path = os.path.join(lndir, 'plugins', 'test_restart_on_update.py') + file = open(path, 'w+') + file.write(content % "1") + file.close() + os.chmod(path, os.stat(path).st_mode | stat.S_IEXEC) + + # now fire up the node and wait for the plugin to print hello + n.daemon.start() + n.daemon.logsearch_start = 0 + n.daemon.wait_for_log(r"test_restart_on_update 1") + + # a rescan should not yet reload the plugin on the same file + n.rpc.plugin_rescan() + assert not n.daemon.is_in_log(r"Plugin changed, needs restart.") + + # modify the file + file = open(path, 'w+') + file.write(content % "2") + file.close() + os.chmod(path, os.stat(path).st_mode | stat.S_IEXEC) + + # rescan and check + n.rpc.plugin_rescan() + n.daemon.wait_for_log(r"Plugin changed, needs restart.") + n.daemon.wait_for_log(r"test_restart_on_update 2") + n.stop()