2017-09-16 16:31:15 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-05-19 18:31:26 -04:00
|
|
|
import pendulum
|
2016-05-18 16:08:09 -04:00
|
|
|
import pytest
|
2016-05-19 15:35:39 -04:00
|
|
|
|
2017-09-15 05:35:21 -04:00
|
|
|
from tsstats import events
|
2017-09-14 16:37:56 -04:00
|
|
|
from tsstats.log import TimedLog, _bundle_logs, _parse_line, parse_logs
|
2016-11-22 17:08:49 -05:00
|
|
|
from tsstats.template import render_servers
|
2016-05-18 16:08:09 -04:00
|
|
|
|
2016-06-12 10:39:41 -04:00
|
|
|
testlog_path = 'tsstats/tests/res/test.log'
|
|
|
|
|
2023-01-14 13:02:53 -05:00
|
|
|
static_timestamp = pendulum.datetime(2015, 5, 18, 15, 52, 52, 685612)
|
2017-09-15 05:35:21 -04:00
|
|
|
|
2016-05-18 16:08:09 -04:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def clients():
|
2017-09-09 18:04:11 -04:00
|
|
|
return list(parse_logs(testlog_path, online_dc=False))[0].clients
|
2016-05-18 16:08:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_log_client_count(clients):
|
2016-05-19 09:41:19 -04:00
|
|
|
assert len(clients) == 3
|
2016-05-18 16:08:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_log_onlinetime(clients):
|
2023-01-14 13:02:53 -05:00
|
|
|
assert clients['1'].onlinetime == pendulum.duration(
|
2017-05-19 18:31:26 -04:00
|
|
|
seconds=402, microseconds=149208)
|
2023-01-14 13:02:53 -05:00
|
|
|
assert clients['2'].onlinetime == pendulum.duration(
|
2017-05-19 18:31:26 -04:00
|
|
|
seconds=19, microseconds=759644)
|
2016-05-18 16:08:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_log_kicks(clients):
|
|
|
|
assert clients['UIDClient1'].kicks == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_log_pkicks(clients):
|
|
|
|
assert clients['2'].pkicks == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_log_bans(clients):
|
|
|
|
assert clients['UIDClient1'].bans == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_log_pbans(clients):
|
|
|
|
assert clients['2'].pbans == 1
|
2016-05-19 15:35:39 -04:00
|
|
|
|
|
|
|
|
2016-06-20 15:59:22 -04:00
|
|
|
@pytest.mark.parametrize("logs,bundled", [
|
|
|
|
(
|
|
|
|
['l1.log', 'l2.log'],
|
|
|
|
{'': [TimedLog('l1.log', None), TimedLog('l2.log', None)]}
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[
|
|
|
|
'ts3server_2016-06-06__14_22_09.527229_1.log',
|
|
|
|
'ts3server_2017-07-07__15_23_10.638340_1.log'
|
|
|
|
],
|
|
|
|
{
|
|
|
|
'1': [
|
2017-05-19 18:31:26 -04:00
|
|
|
TimedLog(
|
|
|
|
'ts3server_2016-06-06__14_22_09.527229_1.log',
|
2023-01-14 13:02:53 -05:00
|
|
|
pendulum.datetime(
|
2017-05-19 18:31:26 -04:00
|
|
|
year=2016, month=6, day=6, hour=14, minute=22,
|
|
|
|
second=9, microsecond=527229
|
|
|
|
)
|
|
|
|
),
|
|
|
|
TimedLog(
|
|
|
|
'ts3server_2017-07-07__15_23_10.638340_1.log',
|
2023-01-14 13:02:53 -05:00
|
|
|
pendulum.datetime(
|
2017-05-19 18:31:26 -04:00
|
|
|
year=2017, month=7, day=7, hour=15, minute=23,
|
|
|
|
second=10, microsecond=638340
|
|
|
|
)
|
|
|
|
)
|
2016-06-20 15:59:22 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
])
|
|
|
|
def test_log_bundle(logs, bundled):
|
|
|
|
assert _bundle_logs(logs) == bundled
|
|
|
|
|
|
|
|
|
2016-06-12 10:38:28 -04:00
|
|
|
def test_log_client_online():
|
2017-05-19 18:31:26 -04:00
|
|
|
current_time = pendulum.now()
|
|
|
|
|
|
|
|
pendulum.set_test_now(current_time)
|
2017-09-09 18:04:11 -04:00
|
|
|
clients = list(parse_logs(testlog_path))[0].clients
|
2016-06-12 11:55:52 -04:00
|
|
|
old_onlinetime = int(clients['1'].onlinetime.total_seconds())
|
2017-05-19 18:31:26 -04:00
|
|
|
|
|
|
|
pendulum.set_test_now(current_time.add(seconds=2)) # add 2s to .now()
|
2017-09-09 18:04:11 -04:00
|
|
|
clients = list(parse_logs(testlog_path))[0].clients
|
2016-06-12 11:55:52 -04:00
|
|
|
assert int(clients['1'].onlinetime.total_seconds()) == old_onlinetime + 2
|
2016-06-22 14:40:55 -04:00
|
|
|
|
|
|
|
|
2016-10-26 18:02:17 -04:00
|
|
|
def test_parse_groups():
|
2017-09-09 18:04:11 -04:00
|
|
|
server = list(parse_logs('tsstats/tests/res/test.log.groups'))
|
|
|
|
assert len(server) == 0
|
2016-11-07 14:24:08 -05:00
|
|
|
|
|
|
|
|
2017-04-28 17:24:31 -04:00
|
|
|
def test_parse_utf8(output):
|
2016-11-22 17:08:49 -05:00
|
|
|
servers = parse_logs(testlog_path + '.utf8')
|
2017-04-28 17:24:31 -04:00
|
|
|
render_servers(servers, output)
|
2017-09-14 16:37:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_invalid_line():
|
|
|
|
assert _parse_line('INVALID') == []
|
2017-09-15 05:35:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('line,expected_events', [
|
|
|
|
(
|
|
|
|
"client connected 'Client1'(id:1) from 1.2.3.4:1234",
|
|
|
|
[
|
|
|
|
events.connect(static_timestamp, '1')
|
|
|
|
]
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"client disconnected 'Client1'(id:1) reason 'reasonmsg=ByeBye!'",
|
|
|
|
[
|
|
|
|
events.disconnect(static_timestamp, '1')
|
|
|
|
]
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"client disconnected 'Client1'(id:1) reason 'invokerid=1"
|
|
|
|
" invokername=Client2 invokeruid=UIDClient2 reasonmsg'",
|
|
|
|
[
|
|
|
|
events.disconnect(static_timestamp, '1'),
|
|
|
|
events.nick(None, 'UIDClient2', 'Client2'),
|
|
|
|
events.kick(None, 'UIDClient2', '1')
|
|
|
|
]
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"client disconnected 'Client1'(id:1) reason 'invokerid=2 "
|
|
|
|
"invokername=Client2 invokeruid=UIDClient2 reasonmsg bantime=0'",
|
|
|
|
[
|
|
|
|
events.disconnect(static_timestamp, '1'),
|
|
|
|
events.nick(None, 'UIDClient2', 'Client2'),
|
|
|
|
events.ban(None, 'UIDClient2', '1')
|
|
|
|
]
|
|
|
|
)
|
|
|
|
])
|
|
|
|
def test_parse_line(line, expected_events):
|
|
|
|
line = '2015-05-18 15:52:52.685612|INFO |VirtualServerBase| 3| ' + line
|
|
|
|
expected_events.insert(0, events.nick(None, '1', 'Client1'))
|
|
|
|
expected_events = [
|
|
|
|
event._replace(timestamp=static_timestamp) for event in expected_events
|
|
|
|
]
|
|
|
|
assert _parse_line(line) == expected_events
|