def test_cleanup(app): """The cleanup task deletes jobs older than 7 days""" job_status = mock.Mock(spec=execution.JobStatus) with app.app_context(): session = app.db.session('relengapi') task = tables.BadpennyTask(name='foo') session.add(task) def newjob(id, created_at, log=None): job = tables.BadpennyJob(id=id, task=task, created_at=created_at) session.add(job) if log: session.add(tables.BadpennyJobLog(id=id, content=log)) newjob(1, dt(2014, 9, 4)) newjob(2, dt(2014, 9, 20)) newjob(3, dt(2014, 9, 15), log="Hello, world\n") newjob(4, dt(2014, 9, 10)) newjob(5, dt(2014, 9, 5), log="Hello, world\n") session.commit() with mock.patch( 'relengapi.blueprints.badpenny.cleanup.time.now') as now: now.return_value = dt(2014, 9, 16) cleanup.cleanup_old_jobs(job_status) eq_(sorted([j.id for j in tables.BadpennyJob.query.all()]), sorted([2, 3, 4])) # 1, 5 are gone eq_(sorted([j.id for j in tables.BadpennyJobLog.query.all()]), sorted([3])) # log for 5 is gone
def create_job(app): with app.app_context(): session = app.db.session('relengapi') task = tables.BadpennyTask(id=10, name='test.task') session.add(task) job = tables.BadpennyJob(task_id=10, created_at=dt(2014, 9, 16)) session.add(job) session.commit() return job.id
def test_cron_run(): """The `relengapi badpenny-cron` script syncs tasks, gets the list of runnable tasks, and then runs them.""" cmd = cron.BadpennyCron() with mock.patch.multiple('relengapi.blueprints.badpenny.cron.BadpennyCron', sync_tasks=mock.DEFAULT, runnable_tasks=mock.DEFAULT, run_task=mock.DEFAULT) as mocks: tasks = [tables.BadpennyTask(name=n) for n in '01'] mocks['runnable_tasks'].return_value = tasks cmd.run(None, None) mocks['sync_tasks'].assert_called_with() mocks['runnable_tasks'].assert_called_with(mock.ANY) eq_(mocks['run_task'].mock_calls, [mock.call(tasks[0]), mock.call(tasks[1])])
def test_cron_task(): """The cron_task decorator takes a cron spec argument and creates a task""" with empty_registry(): @badpenny.cron_task('13 * * * *') def run_me(js): pass t = badpenny.Task.get('{}.run_me'.format(__name__)) eq_(t.schedule, 'cron: 13 * * * *') when = datetime.datetime(2014, 8, 12, 15, 59, 17) bpt = tables.BadpennyTask(jobs=[ tables.BadpennyJob(created_at=when), ]) assert not t.runnable_now( bpt, datetime.datetime(2014, 8, 12, 15, 59, 17)) assert t.runnable_now(bpt, datetime.datetime(2014, 8, 12, 16, 13, 0))
def test_periodic_task(): """The periodic_task decorator takes a `seconds` argument and creates a task""" with empty_registry(): @badpenny.periodic_task(seconds=10) def run_me(js): pass t = badpenny.Task.get('{}.run_me'.format(__name__)) eq_(t.schedule, 'every 10 seconds') when = datetime.datetime(2014, 8, 12, 15, 59, 17) bpt = tables.BadpennyTask(jobs=[ tables.BadpennyJob(created_at=when), ]) assert not t.runnable_now(bpt, when) assert not t.runnable_now(bpt, when + delta(5)) assert t.runnable_now(bpt, when + delta(10))
def insert_task(app, name): session = app.db.session('relengapi') t = tables.BadpennyTask(name=name) session.add(t) session.commit() return t