def test_http_unassignment(db, default_account, mock_queue_client):
    purge_other_accounts(default_account)
    qp = QueuePopulator(zone='testzone')
    qp.queue_client = mock_queue_client
    qp.enqueue_new_accounts()
    s = patched_sync_service(db, mock_queue_client)
    s.poll()

    frontend = HTTPFrontend(s, 16384, False, False)
    app = frontend._create_app()
    app.config['TESTING'] = True
    with app.test_client() as c:
        resp = c.post(
            '/unassign', data=json.dumps({'account_id': default_account.id}),
            content_type='application/json')
        assert resp.status_code == 200
    db.session.expire_all()
    assert default_account.sync_host is None

    # Check that 409 is returned if account is not actually assigned to
    # process.
    with app.test_client() as c:
        resp = c.post(
            '/unassign', data=json.dumps({'account_id': default_account.id}),
            content_type='application/json')
        assert resp.status_code == 409
Example #2
0
def test_http_unassignment(db, default_account, mock_queue_client):
    purge_other_accounts(default_account)
    qp = QueuePopulator(zone='testzone')
    qp.queue_client = mock_queue_client
    qp.enqueue_new_accounts()
    s = patched_sync_service(db, mock_queue_client)
    s.poll()

    frontend = HTTPFrontend(s, 16384, False, False)
    app = frontend._create_app()
    app.config['TESTING'] = True
    with app.test_client() as c:
        resp = c.post('/unassign',
                      data=json.dumps({'account_id': default_account.id}),
                      content_type='application/json')
        assert resp.status_code == 200
    db.session.expire_all()
    assert default_account.sync_host is None

    # Check that 409 is returned if account is not actually assigned to
    # process.
    with app.test_client() as c:
        resp = c.post('/unassign',
                      data=json.dumps({'account_id': default_account.id}),
                      content_type='application/json')
        assert resp.status_code == 409
def test_queue_population_limited_by_zone(db, default_account,
                                          mock_queue_client):
    purge_other_accounts(default_account)
    qp = QueuePopulator(zone='otherzone')
    qp.queue_client = mock_queue_client
    s = patched_sync_service(db, mock_queue_client)
    qp.enqueue_new_accounts()
    s.poll()
    assert s.start_sync.call_count == 0
Example #4
0
def test_queue_population_limited_by_zone(monkeypatch, db, default_account,
                                          mock_queue_client):
    monkeypatch.setattr('psutil.cpu_percent',
                        lambda *args, **kwargs: [10.0, 25.0])

    purge_other_accounts(default_account)
    qp = QueuePopulator(zone='otherzone')
    qp.queue_client = mock_queue_client
    s = patched_sync_service(db, mock_queue_client)
    qp.enqueue_new_accounts()
    s.poll()
    assert s.start_sync.call_count == 0
def test_queue_population_limited_by_zone(monkeypatch, db, default_account,
                                          mock_queue_client):
    monkeypatch.setattr('psutil.cpu_percent',
                        lambda *args, **kwargs: [10.0, 25.0])

    purge_other_accounts(default_account)
    qp = QueuePopulator(zone='otherzone')
    qp.queue_client = mock_queue_client
    s = patched_sync_service(db, mock_queue_client)
    qp.enqueue_new_accounts()
    s.poll()
    assert s.start_sync.call_count == 0
def test_start_accounts_w_sync_should_run_set(db, default_account,
                                              config,
                                              mock_queue_client, sync_state):
    purge_other_accounts(default_account)
    config['SYNC_STEAL_ACCOUNTS'] = True
    default_account.sync_should_run = True
    default_account.sync_state = sync_state
    db.session.commit()

    qp = QueuePopulator(zone='testzone')
    qp.queue_client = mock_queue_client
    qp.enqueue_new_accounts()
    s = patched_sync_service(db, mock_queue_client)

    s.poll()
    assert s.start_sync.call_count == 1
Example #7
0
def test_external_sync_disabling(monkeypatch, db, mock_queue_client):
    monkeypatch.setattr('psutil.cpu_percent',
                        lambda *args, **kwargs: [10.0, 25.0])

    purge_other_accounts()
    account = add_generic_imap_account(db.session,
                                       email_address='*****@*****.**')
    other_account = add_generic_imap_account(db.session,
                                             email_address='*****@*****.**')
    qp = QueuePopulator(zone='testzone')
    qp.queue_client = mock_queue_client
    s = patched_sync_service(db, mock_queue_client)

    qp.enqueue_new_accounts()
    s.poll()
    s.poll()
    assert len(s.syncing_accounts) == 2

    account.mark_deleted()
    db.session.commit()
    assert account.sync_should_run is False
    assert account._sync_status['sync_disabled_reason'] == 'account deleted'

    account.mark_invalid()
    db.session.commit()
    assert account.sync_should_run is False
    assert account.sync_state == 'invalid'
    assert account._sync_status['sync_disabled_reason'] == \
        'invalid credentials'

    qp.unassign_disabled_accounts()
    s.poll()
    assert s.syncing_accounts == {other_account.id}
def test_external_sync_disabling(db, mock_queue_client):
    purge_other_accounts()
    account = add_fake_account(db.session, email_address='*****@*****.**')
    other_account = add_fake_account(db.session,
                                     email_address='*****@*****.**')
    qp = QueuePopulator(zone='testzone')
    qp.queue_client = mock_queue_client
    s = patched_sync_service(mock_queue_client)

    qp.enqueue_new_accounts()
    s.poll()
    s.poll()
    assert len(s.syncing_accounts) == 2

    account.mark_deleted()
    db.session.commit()
    assert account.sync_should_run is False
    assert account._sync_status['sync_disabled_reason'] == 'account deleted'

    account.mark_invalid()
    db.session.commit()
    assert account.sync_should_run is False
    assert account.sync_state == 'invalid'
    assert account._sync_status['sync_disabled_reason'] == \
        'invalid credentials'

    qp.unassign_disabled_accounts()
    s.poll()
    assert s.syncing_accounts == {other_account.id}
Example #9
0
def test_dont_start_accounts_when_sync_should_run_is_none(
        monkeypatch, db, default_account, config, mock_queue_client,
        sync_state):
    monkeypatch.setattr('psutil.cpu_percent',
                        lambda *args, **kwargs: [10.0, 25.0])

    purge_other_accounts(default_account)
    config['SYNC_STEAL_ACCOUNTS'] = True
    default_account.sync_should_run = False
    default_account.sync_state = sync_state
    db.session.commit()

    qp = QueuePopulator(zone='testzone')
    qp.queue_client = mock_queue_client
    qp.enqueue_new_accounts()
    s = patched_sync_service(db, mock_queue_client)

    s.poll()
    assert s.start_sync.call_count == 0
def test_start_accounts_w_sync_should_run_set(monkeypatch, db, default_account,
                                              config,
                                              mock_queue_client, sync_state):
    monkeypatch.setattr('psutil.cpu_percent',
                        lambda *args, **kwargs: [10.0, 25.0])

    purge_other_accounts(default_account)
    config['SYNC_STEAL_ACCOUNTS'] = True
    default_account.sync_should_run = True
    default_account.sync_state = sync_state
    db.session.commit()

    qp = QueuePopulator(zone='testzone')
    qp.queue_client = mock_queue_client
    qp.enqueue_new_accounts()
    s = patched_sync_service(db, mock_queue_client)

    s.poll()
    assert s.start_sync.call_count == 1
Example #11
0
def main():
    maybe_enable_rollbar()

    setproctitle("scheduler")
    zones = {h.get("ZONE") for h in config["DATABASE_HOSTS"]}
    threads = []
    for zone in zones:
        populator = QueuePopulator(zone)
        threads.append(gevent.spawn(populator.run))

    gevent.joinall(threads)
def test_external_sync_disabling(db, mock_queue_client):
    purge_other_accounts()
    account = add_generic_imap_account(db.session, email_address='*****@*****.**')
    other_account = add_generic_imap_account(
        db.session, email_address='*****@*****.**')
    qp = QueuePopulator(zone='testzone')
    qp.queue_client = mock_queue_client
    s = patched_sync_service(db, mock_queue_client)

    qp.enqueue_new_accounts()
    s.poll()
    s.poll()
    assert len(s.syncing_accounts) == 2

    account.mark_deleted()
    db.session.commit()
    assert account.sync_should_run is False
    assert account._sync_status['sync_disabled_reason'] == 'account deleted'

    account.mark_invalid()
    db.session.commit()
    assert account.sync_should_run is False
    assert account.sync_state == 'invalid'
    assert account._sync_status['sync_disabled_reason'] == \
        'invalid credentials'

    qp.unassign_disabled_accounts()
    s.poll()
    assert s.syncing_accounts == {other_account.id}