Commit 9273d8d9 authored by Matt Traudt's avatar Matt Traudt Committed by Matt Traudt
Browse files

Moar tests for cleanup command

parent f8ac4869
......@@ -113,11 +113,11 @@ def test_cleanup_only_compress_stale(time_mock, empty_dotsbws_datadir, caplog,
j(sub_a, '2002-10-01bbbb.txt'),
j(sub_b, '2002-10-10-cccc.txt'),
j(sub_a, '2002-10-10.dddd.txt'),
j(sub_ab, '2002-11-30.txt'),
]
should_ignore_fnames = [
j(sub_b, '2002-10-10.nottxt'), # wrong ext, should be ignored
j(sub_a, '200j-10-10.txt'), # not YYYY-MM-DD*.txt, should be ignored
j(sub_ab, '2002-11-30.txt'), # too deep, should be ignored
]
for fname in should_ignore_fnames + should_compress_fnames:
touch_file(fname)
......@@ -135,8 +135,8 @@ def test_cleanup_only_compress_stale(time_mock, empty_dotsbws_datadir, caplog,
@patch('time.time')
def test_cleanup_only_delete_stale(time_mock, empty_dotsbws_datadir, caplog,
parser):
def test_cleanup_only_delete_rotten(time_mock, empty_dotsbws_datadir, caplog,
parser):
caplog.set_level(logging.DEBUG)
dotsbws = empty_dotsbws_datadir
args = parser.parse_args(
......@@ -161,6 +161,7 @@ def test_cleanup_only_delete_stale(time_mock, empty_dotsbws_datadir, caplog,
j(sub_a, '2000-10-10.dddd.txt'),
j(sub_a, '2000-10-11.eeee.txt.gz'),
j(dd, '2000-10-12.txt.gz'),
j(sub_ab, '2000-11-30.txt'),
]
should_ignore_fnames = [
j(dd, '2002-12-31.txt'), # too new, should be ignored
......@@ -168,7 +169,7 @@ def test_cleanup_only_delete_stale(time_mock, empty_dotsbws_datadir, caplog,
j(dd, '2003-02-10.txt'), # in the future, should be ignored
j(sub_b, '2000-10-10.nottxt'), # wrong ext, should be ignored
j(sub_a, '200j-10-10.txt'), # not YYYY-MM-DD*.txt, should be ignored
j(sub_ab, '2000-11-30.txt'), # too deep, should be ignored
j(dd, '1999-1*-11.txt.gz'), # not YYYY-MM-DD*.txt.gz, should ignore
]
for fname in should_ignore_fnames + should_delete_fnames:
touch_file(fname)
......@@ -181,3 +182,86 @@ def test_cleanup_only_delete_stale(time_mock, empty_dotsbws_datadir, caplog,
expected_fnames.sort()
existing_fnames.sort()
assert expected_fnames == existing_fnames
def test_cleanup_nothing_to_do(empty_dotsbws_datadir, caplog, parser):
caplog.set_level(logging.DEBUG)
dotsbws = empty_dotsbws_datadir
args = parser.parse_args(
'-d {} --log-level DEBUG cleanup'.format(dotsbws.name).split())
conf = get_config(args)
try:
sbws.core.cleanup.main(args, conf)
except Exception as e:
assert None, 'Nothing bad should have happened, but this did: {}'\
.format(e)
@patch('time.time')
def test_cleanup_compress_barely_stale(time_mock, empty_dotsbws_datadir,
caplog, parser):
caplog.set_level(logging.DEBUG)
dotsbws = empty_dotsbws_datadir
args = parser.parse_args(
'-d {} --log-level DEBUG cleanup'.format(dotsbws.name).split())
conf = get_config(args)
conf['general']['data_period'] = '1'
conf['cleanup']['stale_days'] = '10'
conf['cleanup']['rotten_days'] = '30'
now = 1443571200 # 1,443,571,200 is 30 Sep 2015 00:00:00 UTC
time_mock.side_effect = monotonic_time(start=now)
dd = os.path.join(dotsbws.name, 'datadir')
fname_compress1 = os.path.join(dd, '2015-09-19.txt')
fname_compress2 = os.path.join(dd, '2015-09-20.txt')
fname_leave = os.path.join(dd, '2015-09-21.txt')
touch_file(fname_compress1)
touch_file(fname_compress2)
touch_file(fname_leave)
sbws.core.cleanup.main(args, conf)
expected_fnames = [
fname_compress1 + '.gz',
fname_compress2 + '.gz',
fname_leave,
os.path.join(dd, '.lockfile'),
]
existing_fnames = []
for root, dirs, files in os.walk(dd):
for fname in files:
existing_fnames.append(os.path.join(root, fname))
expected_fnames.sort()
existing_fnames.sort()
assert expected_fnames == existing_fnames
@patch('time.time')
def test_cleanup_delete_barely_rotten(time_mock, empty_dotsbws_datadir,
caplog, parser):
caplog.set_level(logging.DEBUG)
dotsbws = empty_dotsbws_datadir
args = parser.parse_args(
'-d {} --log-level DEBUG cleanup'.format(dotsbws.name).split())
conf = get_config(args)
conf['general']['data_period'] = '1'
conf['cleanup']['stale_days'] = '5'
conf['cleanup']['rotten_days'] = '20'
now = 1443571200 # 1,443,571,200 is 30 Sep 2015 00:00:00 UTC
time_mock.side_effect = monotonic_time(start=now)
dd = os.path.join(dotsbws.name, 'datadir')
fname_rotten1 = os.path.join(dd, '2015-09-09.txt')
fname_rotten2 = os.path.join(dd, '2015-09-10.txt')
fname_leave = os.path.join(dd, '2015-09-11.txt')
touch_file(fname_rotten1)
touch_file(fname_rotten2)
touch_file(fname_leave)
sbws.core.cleanup.main(args, conf)
expected_fnames = [
fname_leave + '.gz',
os.path.join(dd, '.lockfile'),
]
existing_fnames = []
for root, dirs, files in os.walk(dd):
for fname in files:
existing_fnames.append(os.path.join(root, fname))
expected_fnames.sort()
existing_fnames.sort()
assert expected_fnames == existing_fnames
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment