-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathgelf_test.py
executable file
·127 lines (109 loc) · 3.36 KB
/
gelf_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
import atexit, os, signal, shlex, shutil, time
import socket, threading
import zlib, json, glob
from pathlib import Path
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": "127.0.0.1:GELF_PORT",
"host": "127.0.0.1",
"_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()
data_dir = Path(Utils.getNodeDataDir(node_id))
config_dir = Path(Utils.getNodeConfigDir(node_id))
start_nodeos_cmd = shlex.split(f"{Utils.EosServerPath} -e -p eosio --data-dir={data_dir} --config-dir={config_dir}")
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)
nodeos = Node(TestHelper.LOCAL_HOST, TestHelper.DEFAULT_PORT, node_id, config_dir, data_dir, start_nodeos_cmd, unstarted=True)
with open(config_dir / 'logging.json', 'w') as textFile:
print(logging,file=textFile)
stop_threads = False
t1 = threading.Thread(target = gelfServer, args =(lambda : stop_threads, ))
try:
@atexit.register
def cleanup():
nodeos.kill(signal.SIGINT)
global stop_threads
stop_threads = True
t1.join()
t1.start()
nodeos.launchUnstarted()
time.sleep(nodeos_run_time_in_sec)
finally:
cleanup()
stderr_file = data_dir / 'stderr.txt'
with open(stderr_file, "r") as f:
stderr_txt = f.read().rstrip()
assert len(received_logs) > 10, "Not enough gelf logs are received"
for received_log in received_logs:
assert received_log in stderr_txt, "received GELF log entry does not match that of stderr"
if os.path.exists(Utils.DataPath):
shutil.rmtree(Utils.DataPath)