def test_task_status_when_pending_expired(app, client):
    now = datetime.datetime(2015, 7, 14, 23, 19, 42, tzinfo=pytz.UTC)  # freeze time
    pending_expiry = now + datetime.timedelta(seconds=60)
    new_expected_pending_expiry = now + datetime.timedelta(seconds=121)
    expired_future = now + datetime.timedelta(seconds=61)
    task_id = "mozilla-central-9213957d166d.tar.gz_testing_mozharness"
    create_fake_tracker_row(app, task_id, created_at=now,
                            pending_expires_at=pending_expiry)
    expected_response = EXPECTED_TASK_STATUS_PENDING_RESPONSE

    with app.app_context():
        with mock.patch('relengapi.blueprints.archiver.now') as time_traveller, \
                mock.patch("relengapi.blueprints.archiver.create_and_upload_archive") as caua:
            caua.AsyncResult.return_value = fake_expired_task_status()
            time_traveller.return_value = expired_future
            response = client.get('/archiver/status/{task_id}'.format(task_id=task_id))
            # status will change state to RETRY
            expected_response['state'] = "RETRY"
            expected_response['status'] = "Task has expired from pending for too long. " \
                                          "Re-creating task."
            eq_(cmp(json.loads(response.data)['result'], expected_response), 0,
                "An expired task status check does not equal expected status.")
            tracker = tables.ArchiverTask.query.filter(
                tables.ArchiverTask.task_id == task_id
            ).first()
            eq_(tracker.pending_expires_at, new_expected_pending_expiry,
                "New pending expiry does not match expected")
def test_task_status_when_pending_but_not_expired(app, client):
    now = datetime.datetime(2015, 7, 14, 23, 19, 42, tzinfo=pytz.UTC)  # freeze time
    pending_expiry = now + datetime.timedelta(seconds=60)
    future = now + datetime.timedelta(seconds=59)
    task_id = "mozilla-central-9213957d166d.tar.gz_testing_mozharness"
    create_fake_tracker_row(app, task_id, created_at=now,
                            pending_expires_at=pending_expiry)
    expected_response = EXPECTED_TASK_STATUS_PENDING_RESPONSE

    with app.app_context():
        with mock.patch('relengapi.blueprints.archiver.now') as time_traveller, \
                mock.patch("relengapi.blueprints.archiver.create_and_upload_archive") as caua:
            caua.AsyncResult.return_value = fake_expired_task_status()
            time_traveller.return_value = future
            response = client.get('/archiver/status/{task_id}'.format(task_id=task_id))
            eq_(cmp(json.loads(response.data)['result'], expected_response), 0,
                "A pending task that has not expired does not equal expected status.")
            tracker = tables.ArchiverTask.query.filter(
                tables.ArchiverTask.task_id == task_id
            ).first()
            eq_(tracker.pending_expires_at, pending_expiry,
                "Tracker does not match original pending expiry.")