2017-09-26 05:09:27 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
|
|
|
2018-02-01 15:17:23 -05:00
|
|
|
|
|
|
|
import shutil
|
|
|
|
|
2017-09-26 05:09:27 -04:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from tsstats.cache import Cache, CachedLog, _calculate_hash
|
|
|
|
from tsstats.log import parse_logs
|
|
|
|
from tsstats.tests.test_log import testlog_path
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def cache_path(request):
|
2018-02-01 15:01:01 -05:00
|
|
|
cache_path = 'tsstats/tests/res/tsstats.cache.{}'.format(
|
|
|
|
request.function.__name__
|
|
|
|
)
|
2017-09-26 05:09:27 -04:00
|
|
|
|
|
|
|
def clean():
|
|
|
|
if os.path.exists(cache_path):
|
|
|
|
os.remove(cache_path)
|
|
|
|
|
|
|
|
request.addfinalizer(clean)
|
|
|
|
yield cache_path
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def cache(cache_path):
|
|
|
|
return Cache(cache_path)
|
|
|
|
|
|
|
|
|
|
|
|
# UNIT
|
|
|
|
def test_cache_add_get_remove_iter_len(cache):
|
|
|
|
expected_cachedlog = CachedLog(
|
|
|
|
testlog_path, _calculate_hash(testlog_path), []
|
|
|
|
)
|
|
|
|
cache[testlog_path] = []
|
|
|
|
assert cache.store[testlog_path] == expected_cachedlog
|
|
|
|
assert cache[testlog_path] == expected_cachedlog
|
|
|
|
assert next(iter(cache)) == testlog_path
|
|
|
|
assert len(cache) == 1
|
|
|
|
del cache[testlog_path]
|
|
|
|
assert len(cache) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_cache_read_write(cache):
|
|
|
|
cache[testlog_path] = []
|
|
|
|
cache.write()
|
|
|
|
cache2 = Cache.read(cache.path)
|
|
|
|
assert cache2[testlog_path] == CachedLog(
|
|
|
|
testlog_path, _calculate_hash(testlog_path), []
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-02-01 15:17:23 -05:00
|
|
|
def test_cache_needs_parsing(cache, tmpdir):
|
|
|
|
tmplog_path = str(tmpdir.mkdir('cache').join('test.log'))
|
|
|
|
# copy logfile to temporary location
|
|
|
|
shutil.copy(testlog_path, tmplog_path)
|
|
|
|
|
|
|
|
assert cache.needs_parsing(tmplog_path)
|
|
|
|
cache[tmplog_path] = []
|
|
|
|
assert not cache.needs_parsing(tmplog_path)
|
|
|
|
with open(tmplog_path, 'a') as f:
|
|
|
|
f.writelines(['content'])
|
|
|
|
assert cache.needs_parsing(tmplog_path)
|
2017-09-26 05:09:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
# INTEGRATION
|
2018-02-01 10:13:04 -05:00
|
|
|
def test_cache_integration(cache_path):
|
2018-02-01 10:15:29 -05:00
|
|
|
first_run = list(parse_logs(
|
|
|
|
testlog_path, online_dc=False, cache_path=cache_path
|
|
|
|
))
|
|
|
|
second_run = list(parse_logs(
|
|
|
|
testlog_path, online_dc=False, cache_path=cache_path
|
|
|
|
))
|
|
|
|
assert first_run == second_run
|