Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gelf test #1145

Merged
merged 5 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/large-lib-test.py ${CMAKE_CURRENT_BIN
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/launcher.py ${CMAKE_CURRENT_BINARY_DIR}/launcher.py COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/auto_bp_peering_test.py ${CMAKE_CURRENT_BINARY_DIR}/auto_bp_peering_test.py COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/auto_bp_peering_test_shape.json ${CMAKE_CURRENT_BINARY_DIR}/auto_bp_peering_test_shape.json COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/gelf_test.py ${CMAKE_CURRENT_BINARY_DIR}/gelf_test.py COPYONLY)

if(DEFINED ENV{GITHUB_ACTIONS})
set(UNSHARE "--unshared")
Expand Down Expand Up @@ -261,6 +262,9 @@ set_property(TEST light_validation_sync_test PROPERTY LABELS nonparallelizable_t
add_test(NAME auto_bp_peering_test COMMAND tests/auto_bp_peering_test.py ${UNSHARE} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_property(TEST auto_bp_peering_test PROPERTY LABELS long_running_tests)

add_test(NAME gelf_test COMMAND tests/gelf_test.py ${UNSHARE} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_property(TEST gelf_test PROPERTY LABELS nonparallelizable_tests)

if(ENABLE_COVERAGE_TESTING)

set(Coverage_NAME ${PROJECT_NAME}_coverage)
Expand Down
125 changes: 125 additions & 0 deletions tests/gelf_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env python3
import os, shutil, time
import socket, threading
import zlib, json, glob
from TestHarness import Node, TestHelper, Utils

###############################################################################################
# This test starts nodeos process which is configured with GELF logging endpoint as localhost,
# receives data from the GELF loggging UDP PORT, and checks if the received log entries match
# those in stderr log file.
###########################################################################################


GELF_PORT = 24081

# We need debug level to get more information about nodeos process
logging="""{
"includes": [],
"appenders": [{
"name": "stderr",
"type": "console",
"args": {
"stream": "std_error",
"level_colors": [{
"level": "debug",
"color": "green"
},{
"level": "warn",
"color": "brown"
},{
"level": "error",
"color": "red"
}
],
"flush": true
},
"enabled": true
},{
"name": "net",
"type": "gelf",
"args": {
"endpoint": "localhost:GELF_PORT",
"host": "localhost",
"_network": "testnet"
},
"enabled": true
}
],
"loggers": [{
"name": "default",
"level": "debug",
"enabled": true,
"additivity": false,
"appenders": [
"stderr",
"net"
]
}
]
}"""

logging = logging.replace("GELF_PORT", str(GELF_PORT))

nodeos_run_time_in_sec = 5

node_id = 1
received_logs = []
BUFFER_SIZE = 1024

def gelfServer(stop):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1)
s.bind((TestHelper.LOCAL_HOST, GELF_PORT))
while not stop():
try:
data, _ = s.recvfrom(BUFFER_SIZE)
message = zlib.decompress(data, zlib.MAX_WBITS|32)
entry = json.loads(message.decode())
global num_received_logs, last_received_log
received_logs.append(entry["short_message"])
except socket.timeout:
pass
s.close()


nodeos = Node(TestHelper.LOCAL_HOST, TestHelper.DEFAULT_PORT, node_id)
data_dir = Utils.getNodeDataDir(node_id)
config_dir = Utils.getNodeConfigDir(node_id)
if os.path.exists(data_dir):
shutil.rmtree(data_dir)
os.makedirs(data_dir)
if not os.path.exists(config_dir):
os.makedirs(config_dir)

with open(config_dir+"/logging.json", "w") as textFile:
print(logging,file=textFile)

try:
stop_threads = False
t1 = threading.Thread(target = gelfServer, args =(lambda : stop_threads, ))
t1.start()

start_nodeos_cmd = f"{Utils.EosServerPath} -e -p eosio --data-dir={data_dir} --config-dir={config_dir}"
nodeos.launchCmd(start_nodeos_cmd, node_id)
time.sleep(nodeos_run_time_in_sec)
finally:
#clean up
Node.killAllNodeos()
stop_threads = True
t1.join()

# find the stderr log file
stderr_file = glob.glob(os.path.join(data_dir, 'stderr.*.txt'))
with open(stderr_file[0], "r") as f:
stderr_lines = f.readlines()

#Less one line because the GELF appender does not transmit 'opened GELF socket to endpoint...
assert len(stderr_lines)-1 == len(received_logs), "number of log entry received from GELF does not match stderr"

stderr_lines.pop(0)
for (stderr_line, received_log) in zip(stderr_lines, received_logs):
assert received_log in stderr_line, "received GELF log entry does not match that of from stderr"

if os.path.exists(Utils.DataPath):
shutil.rmtree(Utils.DataPath)