2016-05-10 22:50:34 +02:00
|
|
|
import logging
|
2015-07-31 21:55:45 +02:00
|
|
|
|
2017-05-20 00:31:26 +02:00
|
|
|
import pendulum
|
2016-05-09 20:09:16 +02:00
|
|
|
import pytest
|
2015-09-04 23:20:35 +02:00
|
|
|
|
2017-09-09 17:35:55 +02:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from tsstats.log import parse_logs
|
2016-11-23 21:00:35 +01:00
|
|
|
from tsstats.template import render_servers
|
2016-11-11 18:41:51 +01:00
|
|
|
from tsstats.utils import filter_threshold, seconds_to_text, sort_clients
|
2015-09-04 23:20:35 +02:00
|
|
|
|
2017-09-09 17:35:55 +02:00
|
|
|
servers = list(parse_logs('tsstats/tests/res/test.log', online_dc=False))
|
|
|
|
servers[0] = servers[0]._replace(sid=1) # add missing sid to server object
|
|
|
|
clients = servers[0].clients
|
2015-07-31 21:55:45 +02:00
|
|
|
|
2016-05-10 22:50:34 +02:00
|
|
|
logger = logging.getLogger('tsstats')
|
|
|
|
|
2015-07-31 21:55:45 +02:00
|
|
|
|
2016-06-10 15:36:32 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def soup(output):
|
2017-04-28 23:24:31 +02:00
|
|
|
render_servers(servers, output)
|
|
|
|
return BeautifulSoup(open(output), 'html.parser')
|
2016-06-10 15:36:32 +02:00
|
|
|
|
|
|
|
|
2016-05-09 20:09:16 +02:00
|
|
|
def test_debug(output):
|
2016-05-10 22:50:34 +02:00
|
|
|
logger.setLevel(logging.DEBUG)
|
2017-04-28 23:24:31 +02:00
|
|
|
render_servers(servers, output)
|
2016-05-10 22:50:34 +02:00
|
|
|
logger.setLevel(logging.INFO)
|
2017-04-28 23:24:31 +02:00
|
|
|
soup = BeautifulSoup(open(output), 'html.parser')
|
2016-06-10 16:22:52 +02:00
|
|
|
# check debug-label presence
|
2016-11-23 21:04:51 +01:00
|
|
|
assert soup.find_all(style='color: red; padding-right: 10px;')
|
2016-11-23 21:06:55 +01:00
|
|
|
for client_item in soup.find('ul', id='1.onlinetime').find_all('li'):
|
2016-06-10 16:22:52 +02:00
|
|
|
nick = client_item.find('span').text
|
|
|
|
# check for right identifier
|
|
|
|
nick, encl_identifier = nick.split()
|
|
|
|
identifier = encl_identifier.replace('(', '').replace(')', '')
|
|
|
|
assert clients[identifier].nick == nick
|
2016-05-09 20:09:16 +02:00
|
|
|
|
|
|
|
|
2016-06-10 15:36:32 +02:00
|
|
|
def test_onlinetime(soup):
|
2016-11-23 21:06:11 +01:00
|
|
|
items = soup.find('ul', id='1.onlinetime').find_all('li')
|
2016-06-10 16:45:41 +02:00
|
|
|
assert len(items) == 2
|
2017-03-03 23:59:24 +01:00
|
|
|
for item in items:
|
|
|
|
nick, onlinetime = item.find_all('span')
|
|
|
|
nick = nick.text
|
|
|
|
onlinetime = onlinetime.text
|
|
|
|
# find corresponding client-object
|
2017-03-04 00:02:41 +01:00
|
|
|
client = list(filter(
|
2017-05-20 00:31:26 +02:00
|
|
|
lambda c: c.nick == nick and c.onlinetime > pendulum.Interval(),
|
2017-03-03 23:59:24 +01:00
|
|
|
clients
|
2017-03-04 00:02:41 +01:00
|
|
|
))
|
2017-03-03 23:59:24 +01:00
|
|
|
# assert existence
|
|
|
|
assert client
|
|
|
|
client = client[0]
|
|
|
|
# compare onlinetimes
|
|
|
|
client_onlinetime_text = seconds_to_text(
|
|
|
|
int(client.onlinetime.total_seconds())
|
|
|
|
)
|
|
|
|
assert onlinetime == client_onlinetime_text
|
2016-11-11 18:41:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_filter_threshold():
|
|
|
|
sorted_clients = sort_clients(
|
|
|
|
clients, lambda c: c.onlinetime.total_seconds())
|
|
|
|
assert len(filter_threshold(sorted_clients, -1)) == len(sorted_clients)
|
|
|
|
assert len(filter_threshold(sorted_clients, 20)) == 1
|
|
|
|
assert len(filter_threshold(sorted_clients, 500)) == 0
|