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 2 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 @@ -257,6 +258,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
123 changes: 123 additions & 0 deletions tests/gelf_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/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
num_received_logs = 0
last_received_log = ""
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, clienaddr = s.recvfrom(BUFFER_SIZE)
jgiszczak marked this conversation as resolved.
Show resolved Hide resolved
message = zlib.decompress(data, zlib.MAX_WBITS|32)
entry = json.loads(message.decode())
global num_received_logs, last_received_log
num_received_logs += 1
last_received_log = entry["short_message"]
linh2931 marked this conversation as resolved.
Show resolved Hide resolved
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:
lines = f.readlines()

assert len(lines)-1 == num_received_logs, "number of log entry received from GELF does not match stderr"
jgiszczak marked this conversation as resolved.
Show resolved Hide resolved
assert last_received_log in lines[-1], "last received GELF log entry does not match that of from stderr"

if os.path.exists(Utils.DataPath):
shutil.rmtree(Utils.DataPath)
jgiszczak marked this conversation as resolved.
Show resolved Hide resolved