from datetime import datetime, timedelta from data import model from data.database import LogEntry3 from workers.logrotateworker import LogRotateWorker from test.fixtures import * def test_logrotateworker(initialized_db): worker = LogRotateWorker() logs = list(LogEntry3.select()) # Ensure there are logs. assert logs # Ensure we don't find any to archive that are new. assert not worker._perform_archiving(LogEntry3, datetime.utcnow() - timedelta(weeks=6)) assert len(list(LogEntry3.select())) == len(logs) # Archive all the logs. assert worker._perform_archiving(LogEntry3, datetime.utcnow() + timedelta(days=1)) # Ensure all the logs were archived. assert not list(LogEntry3.select()) def test_logrotateworker_withcutoff(initialized_db): # Delete all existing logs. LogEntry3.delete().execute() # Create a new set of logs. start_timestamp = datetime.now() for day in range(0, 100): model.log.log_action('push_repo', 'devtable', timestamp=start_timestamp + timedelta(days=day)) logs = list(LogEntry3.select()) # Sort and find a midpoint in the logs. logs.sort(key=lambda l: l.datetime) midpoint = logs[0:len(logs)/2] assert midpoint assert len(midpoint) < len(logs) # Archive the earlier logs. worker = LogRotateWorker() assert worker._perform_archiving(LogEntry3, midpoint[-1].datetime) # Ensure the earlier logs were archived. for log in midpoint: try: LogEntry3.get(id=log.id) assert False, "Found unexpected log" except LogEntry3.DoesNotExist: pass # Ensure the later logs were not. for log in logs[len(logs)/2:]: LogEntry3.get(id=log.id)