From 4d08ed2fa6664da13b1cbb17014b3ffb864ae30e Mon Sep 17 00:00:00 2001 From: trueptolemy <823220586@qq.com> Date: Tue, 4 Jun 2019 15:42:43 +0800 Subject: [PATCH] pytest: Add a test for the 'warning' subscription and notification 1. Create a plugin: ./lightning/tests/plugins/pretend_badlog.py This plugin subscribes 'warning' notification and log the payload of 'warning'; 2. Add a new test: tests/test_plugin.py::test_warning_notification This test runs the plugin-pretend_badlog.py and check if 'warning' notification can be normal triggered and subscribed. --- tests/plugins/pretend_badlog.py | 32 ++++++++++++++++++++++++++++++ tests/test_plugin.py | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100755 tests/plugins/pretend_badlog.py diff --git a/tests/plugins/pretend_badlog.py b/tests/plugins/pretend_badlog.py new file mode 100755 index 000000000..854ec6c3c --- /dev/null +++ b/tests/plugins/pretend_badlog.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +"""This plugin is used to check that warning(unusual/broken level log) calls are working correctly. +""" +from lightning import Plugin + +plugin = Plugin() + + +@plugin.init() +def init(configuration, options, plugin): + plugin.log("initialized") + + +@plugin.subscribe("warning") +def notify_warning(plugin, warning): + plugin.log("Received warning") + plugin.log("level: {}".format(warning['level'])) + plugin.log("time: {}".format(warning['time'])) + plugin.log("source: {}".format(warning['source'])) + plugin.log("log: {}".format(warning['log'])) + + +@plugin.method("pretendbad") +def pretend_bad(event, level, plugin): + """Log an specified level entry. + And in plugin, we use 'warn'/'error' instead of + 'unusual'/'broken' + """ + plugin.log("{}".format(event), level) + + +plugin.run() diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 3977467fe..4278097e7 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -127,7 +127,9 @@ def test_plugin_hook(node_factory, executor): assert(end_time >= start_time + 20) -def test_plugin_notifications(node_factory): +def test_plugin_connect_notifications(node_factory): + """ test 'connect' and 'disconnect' notifications + """ l1, l2 = node_factory.get_nodes(2, opts={'plugin': 'contrib/plugins/helloworld.py'}) l1.connect(l2) @@ -418,3 +420,34 @@ def test_htlc_accepted_hook_forward_restart(node_factory, executor): l2.restart() f1.result() + + +def test_warning_notification(node_factory): + """ test 'warning' notifications + """ + l1 = node_factory.get_node(options={'plugin': 'tests/plugins/pretend_badlog.py'}) + + # 1. test 'warn' level + event = "Test warning notification(for unusual event)" + l1.rpc.call('pretendbad', {'event': event, 'level': 'warn'}) + + # ensure an unusual log_entry was produced by 'pretendunusual' method + l1.daemon.wait_for_log('plugin-pretend_badlog.py Test warning notification\\(for unusual event\\)') + + # now wait for notification + l1.daemon.wait_for_log('plugin-pretend_badlog.py Received warning') + l1.daemon.wait_for_log('plugin-pretend_badlog.py level: warn') + l1.daemon.wait_for_log('plugin-pretend_badlog.py time: *') + l1.daemon.wait_for_log('plugin-pretend_badlog.py source: plugin-pretend_badlog.py') + l1.daemon.wait_for_log('plugin-pretend_badlog.py log: Test warning notification\\(for unusual event\\)') + + # 2. test 'error' level, steps like above + event = "Test warning notification(for broken event)" + l1.rpc.call('pretendbad', {'event': event, 'level': 'error'}) + l1.daemon.wait_for_log('plugin-pretend_badlog.py Test warning notification\\(for broken event\\)') + + l1.daemon.wait_for_log('plugin-pretend_badlog.py Received warning') + l1.daemon.wait_for_log('plugin-pretend_badlog.py level: error') + l1.daemon.wait_for_log('plugin-pretend_badlog.py time: *') + l1.daemon.wait_for_log('plugin-pretend_badlog.py source: plugin-pretend_badlog.py') + l1.daemon.wait_for_log('plugin-pretend_badlog.py log: Test warning notification\\(for broken event\\)')