From 4771a29c1c475f1a71114c215a2ff167f215948c Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Fri, 15 Jun 2018 16:37:40 +0200 Subject: Add the ability to assert daemon log messages Change-Id: I392dc677061223d4990a12517606e9ef9142d515 Signed-off-by: Romain Forlot --- conf.d/project/lua.d/aft.lua | 117 ++++++++++++++++++++++++++---------- conf.d/project/lua.d/helloworld.lua | 7 +++ 2 files changed, 92 insertions(+), 32 deletions(-) (limited to 'conf.d/project') diff --git a/conf.d/project/lua.d/aft.lua b/conf.d/project/lua.d/aft.lua index c0f047a..5f548ce 100644 --- a/conf.d/project/lua.d/aft.lua +++ b/conf.d/project/lua.d/aft.lua @@ -34,52 +34,89 @@ function _AFT.enableEventHistory() _AFT.event_history = true end +function _AFT.setJunitFile(filePath) + lu.LuaUnit.fname = filePath +end + --[[ - Events listener and assertion function to test correctness of received + Events listener and assertion functions to test correctness of received event data. + + Check are in 2 times. First you need to register the event that you want to + monitor then you test that it has been correctly received. + + Notice that there is a difference between log and event. Logs are daemon + messages normally handled by the host log system (journald, syslog...) and + events are generated by the apis to communicate and send informations to the + subscribed listeners. ]] -function _evt_catcher_ (source, action, eventObj) +function _AFT.addEventToMonitor(eventName, callback) + _AFT.monitored_events[eventName] = { cb = callback } +end + +function _AFT.addLogToMonitor(api, type, message, callback) + _AFT.monitored_events[message] = { api = api, type = type, cb = callback } +end + +function _AFT.incrementCount(dict) + if dict.receivedCount then + dict.receivedCount = dict.receivedCount + 1 + else + dict.receivedCount = 1 + end +end + +function _AFT.registerData(dict, eventData) + if dict.data and type(dict.data) == 'table' then + if _AFT.event_history == true then + table.insert(dict.data, eventData, 1) + else + dict.data[1] = eventData + end + else + dict.data = {} + table.insert(dict.data, eventData) + end +end + +function _AFT.daemonEventHandler(eventObj) + local eventName = eventObj.data.message + local log = _AFT.monitored_events[eventName] + if log and log.api == eventObj.daemon.api and log.type == eventObj.data.type then + _AFT.incrementCount(_AFT.monitored_events[eventName]) + _AFT.registerData(_AFT.monitored_events[eventName], eventObj.data) + end +end + +function _AFT.bindingEventHandler(eventObj) local eventName = eventObj.event.name local eventListeners = eventObj.data.result -- Remove from event to hold the bare event data and be able to assert it eventObj.data.result = nil - local eventData = eventObj.data if type(_AFT.monitored_events[eventName]) == 'table' then _AFT.monitored_events[eventName].eventListeners = eventListeners - if _AFT.monitored_events[eventName].receivedCount then - _AFT.monitored_events[eventName].receivedCount = _AFT.monitored_events[eventName].receivedCount + 1 - else - _AFT.monitored_events[eventName].receivedCount = 1 - end - - if _AFT.monitored_events[eventName].data and type(_AFT.monitored_events[eventName].data) == 'table' then - if _AFT.event_history == true then - table.insert(_AFT.monitored_events[eventName].data, eventObj, 1) - else - _AFT.monitored_events[eventName].data[1] = eventData - end - else - _AFT.monitored_events[eventName].data = {} - table.insert(_AFT.monitored_events[eventName].data, eventData) - end + _AFT.incrementCount(_AFT.monitored_events[eventName]) + _AFT.registerData(_AFT.monitored_events[eventName], eventObj.data) end end -function _AFT.addEventToMonitor(eventName, callback) - AFB:servsync(_AFT.context, "monitor", "set", { verbosity = "debug" }) - --AFB:servsync(_AFT.context, "monitor", "trace", { add = { event = "push_before", event = "push_after" }}) - AFB:servsync(_AFT.context, "monitor", "trace", { add = { event = "push_after" }}) - if callback then - _AFT.monitored_events[eventName] = { cb = callback } - else - _AFT.monitored_events[eventName] = { cb = EvtReceived } +function _evt_catcher_ (source, action, eventObj) + print(Dump_Table(eventObj)) + if eventObj.type == "event" then + _AFT.bindingEventHandler(eventObj) + elseif eventObj.type == "daemon" then + _AFT.daemonEventHandler(eventObj) end end +--[[ + Assert and test functions about the event part. +]] + function _AFT.assertEvtReceived(eventName) local count = 0 if _AFT.monitored_events[eventName].receivedCount then @@ -101,7 +138,7 @@ function _AFT.testEvtReceived(testName, eventName, timeout) end --[[ - Assert Wrapping function meant to tests API Verbs calls + Assert function meant to tests API Verbs calls ]] local function assertVerbCallParameters(src, api, verb, args) @@ -172,7 +209,7 @@ end aliases. ]] -local luaunit_list_of_funcs = { +local luaunit_list_of_assert = { -- official function name from luaunit test framework -- general assertions @@ -316,6 +353,10 @@ local luaunit_list_of_funcs = { 'assertNotIsThread', } +local luaunit_list_of_functions = { + "setOutputType", +} + local _AFT_list_of_funcs = { -- AF Binder generic assertions { 'assertVerb', 'assertVerbStatusSuccess' }, @@ -324,21 +365,29 @@ local _AFT_list_of_funcs = { { 'assertVerbError', 'assertVerbStatusError' }, { 'assertVerbError', 'assertVerbResponseEqualsError' }, { 'assertVerbError', 'assertVerbCbError' }, + { 'assertEvtReceived', 'assertLogReceived' }, { 'testVerb', 'testVerbStatusSuccess' }, { 'testVerb', 'testVerbResponseEquals' }, { 'testVerb', 'testVerbCb' }, { 'testVerbError', 'testVerbStatusError' }, { 'testVerbError', 'testVerbResponseEqualsError' }, { 'testVerbError', 'testVerbCbError' }, + { 'testEvtReceived', 'testLogReceived' }, } --- Create all aliases in M -for _, v in pairs( luaunit_list_of_funcs ) do +-- Import all luaunit assertion function to _AFT object +for _, v in pairs( luaunit_list_of_assert ) do local funcname = v _AFT[funcname] = lu[funcname] end --- Create all aliases in M +-- Import specific luaunit configuration functions to _AFT object +for _, v in pairs( luaunit_list_of_functions ) do + local funcname = v + _AFT[funcname] = lu.LuaUnit[funcname] +end + +-- Create all aliases in _AFT for _, v in pairs( _AFT_list_of_funcs ) do local funcname, alias = v[1], v[2] _AFT[alias] = _AFT[funcname] @@ -346,8 +395,12 @@ end function _launch_test(context, args) _AFT.context = context + AFB:servsync(_AFT.context, "monitor", "set", { verbosity = "debug" }) + --AFB:servsync(_AFT.context, "monitor", "trace", { add = { event = "push_before", event = "push_after" }}) + AFB:servsync(_AFT.context, "monitor", "trace", { add = { daemon = "vverbose", event = "push_after" }}) for _,f in pairs(args.files) do dofile('var/'..f) end + lu.LuaUnit:runSuiteByInstances(_AFT.tests_list) end diff --git a/conf.d/project/lua.d/helloworld.lua b/conf.d/project/lua.d/helloworld.lua index ccb1c67..dc883f2 100644 --- a/conf.d/project/lua.d/helloworld.lua +++ b/conf.d/project/lua.d/helloworld.lua @@ -32,6 +32,7 @@ end _AFT.addEventToMonitor("hello/anEvent") _AFT.addEventToMonitor("hello/anotherEvent", _callbackEvent) +_AFT.addLogToMonitor("test", "warning", "CtlDispatchEvent: fail to find uid=hello/anEvent in action event section") _AFT.testVerbStatusSuccess('testPingSuccess','hello', 'ping', {}) _AFT.testVerbResponseEquals('testPingSuccess','hello', 'ping', {}, "Some String") @@ -52,3 +53,9 @@ _AFT.testVerbStatusSuccess('testEventPush', 'hello', 'eventpush', {tag = 'evt', _AFT.testEvtReceived("testEvent", "hello/anEvent") _AFT.testEvtReceived("testEventCb", "hello/anotherEvent") + +_AFT.testLogReceived("LogReceived", "CtlDispatchEvent: fail to find uid=hello/anEvent in action event section") + +_AFT.testCustom("mytest", function() + _AFT.assertEquals(false, false) +end) -- cgit 1.2.3-korg