Browse Source

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.
htlc_accepted_hook
trueptolemy 6 years ago
committed by Rusty Russell
parent
commit
4d08ed2fa6
  1. 32
      tests/plugins/pretend_badlog.py
  2. 35
      tests/test_plugin.py

32
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()

35
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\\)')

Loading…
Cancel
Save