Ejemplo n.º 1
0
def test_status_rabbitmq_not_ok(
    mock_workers_status,
    mock_rabbitmq_ok,
    mock_database_ok,
    mock_athens_ok,
    mock_nexus_ok,
    mock_get_worker_config,
    short,
    test_app,
):
    config = mock_worker_config()

    mock_get_worker_config.return_value = config
    mock_nexus_ok.return_value = (True, None)
    mock_athens_ok.return_value = (True, None)
    mock_database_ok.return_value = (True, None)
    mock_rabbitmq_ok.return_value = (False, "failed to resolve broker hostname")

    if short:
        err_msg = "rabbitmq unavailable: failed to resolve broker hostname"
        with pytest.raises(CachitoError, match=err_msg):
            status.status(short=True)
    else:
        result = status.status(short=False)
        assert result["workers"] == []

    # Test that that worker status is not checked if rabbitmq is not ok
    mock_workers_status.assert_not_called()
Ejemplo n.º 2
0
def get_status_short():
    """Return 200 if all workers and services seem to be ok, 503 otherwise."""
    try:
        status(short=True)
        retval = {"ok": True}
    except CachitoError as e:
        retval = {"ok": False, "reason": str(e)}

    return flask.jsonify(retval), 200 if retval["ok"] else 503
Ejemplo n.º 3
0
def test_status_worker_not_ok(
    mock_can_process,
    mock_workers_status,
    mock_rabbitmq_ok,
    mock_database_ok,
    mock_athens_ok,
    mock_nexus_ok,
    mock_get_worker_config,
    short,
    workers_status_result,
    expect_any_worker_ok,
    test_app,
):
    config = mock_worker_config()

    mock_get_worker_config.return_value = config
    mock_nexus_ok.return_value = (True, None)
    mock_athens_ok.return_value = (True, None)
    mock_database_ok.return_value = (True, None)
    mock_rabbitmq_ok.return_value = (True, None)
    mock_workers_status.return_value = workers_status_result

    if short and not expect_any_worker_ok:
        with pytest.raises(CachitoError, match="no workers are available"):
            status.status(short=True)
        return

    result = status.status(short=False)

    expect_services = [
        {"name": "nexus", "ok": True},
        {"name": "athens", "ok": True},
        {"name": "database", "ok": True},
        {"name": "rabbitmq", "ok": True},
    ]

    assert result == {
        "can_process": mock_can_process.return_value,
        "services": expect_services,
        "workers": mock_workers_status.return_value,
    }

    mock_get_worker_config.assert_called_once()
    mock_nexus_ok.assert_called_once_with(config.cachito_nexus_url)
    mock_athens_ok.assert_called_once_with(config.cachito_athens_url)
    mock_database_ok.assert_called_once()
    mock_rabbitmq_ok.assert_called_once_with(config.broker_url)
    mock_workers_status.assert_called_once_with(retries=2)
    mock_can_process.assert_called_once_with(
        TEST_PACKAGE_MANAGERS, expect_services, expect_any_worker_ok
    )
Ejemplo n.º 4
0
def test_status_service_not_ok(
    mock_can_process,
    mock_workers_status,
    mock_rabbitmq_ok,
    mock_database_ok,
    mock_athens_ok,
    mock_nexus_ok,
    mock_get_worker_config,
    short,
    test_app,
):
    config = mock_worker_config()

    mock_get_worker_config.return_value = config
    mock_nexus_ok.return_value = (True, None)
    mock_athens_ok.return_value = (False, "Athens is currently at war with Sparta")
    mock_database_ok.return_value = (True, None)
    mock_rabbitmq_ok.return_value = (True, None)
    mock_workers_status.return_value = [{"name": "celery@123456", "ok": True}]

    if short:
        err_msg = "athens unavailable: Athens is currently at war with Sparta"
        with pytest.raises(CachitoError, match=err_msg):
            status.status(short=True)
        return

    result = status.status(short=False)

    expect_services = [
        {"name": "nexus", "ok": True},
        {"name": "athens", "ok": False, "reason": "Athens is currently at war with Sparta"},
        {"name": "database", "ok": True},
        {"name": "rabbitmq", "ok": True},
    ]

    assert result == {
        "can_process": mock_can_process.return_value,
        "services": expect_services,
        "workers": mock_workers_status.return_value,
    }

    mock_get_worker_config.assert_called_once()
    mock_nexus_ok.assert_called_once_with(config.cachito_nexus_url)
    mock_athens_ok.assert_called_once_with(config.cachito_athens_url)
    assert mock_database_ok.call_count == (0 if short else 1)
    assert mock_rabbitmq_ok.call_count == (0 if short else 1)
    assert mock_workers_status.call_count == (0 if short else 1)
    mock_can_process.assert_called_once_with(TEST_PACKAGE_MANAGERS, expect_services, True)
Ejemplo n.º 5
0
def test_status_all_ok(
    mock_can_process,
    mock_workers_status,
    mock_rabbitmq_ok,
    mock_database_ok,
    mock_athens_ok,
    mock_nexus_ok,
    mock_get_worker_config,
    nexus_hoster,
    short,
    test_app,
):
    config = mock_worker_config(nexus_hoster=nexus_hoster)

    mock_get_worker_config.return_value = config
    mock_nexus_ok.return_value = (True, None)
    mock_athens_ok.return_value = (True, None)
    mock_database_ok.return_value = (True, None)
    mock_rabbitmq_ok.return_value = (True, None)
    mock_workers_status.return_value = [{"name": "celery@123456", "ok": True}]

    result = status.status(short=short)

    expect_services = [
        {"name": "nexus", "ok": True},
        {"name": "athens", "ok": True},
        {"name": "database", "ok": True},
        {"name": "rabbitmq", "ok": True},
    ]
    if nexus_hoster:
        expect_services.insert(1, {"name": "nexus-hoster", "ok": True})

    assert result == {
        "can_process": mock_can_process.return_value,
        "services": expect_services,
        "workers": mock_workers_status.return_value,
    }

    nexus_ok_calls = [mock.call(config.cachito_nexus_url)]
    if nexus_hoster:
        nexus_ok_calls.append(mock.call(config.cachito_nexus_hoster_url))

    mock_get_worker_config.assert_called_once()
    mock_nexus_ok.assert_has_calls(nexus_ok_calls)
    assert mock_nexus_ok.call_count == len(nexus_ok_calls)
    mock_athens_ok.assert_called_once_with(config.cachito_athens_url)
    mock_database_ok.assert_called_once()
    mock_rabbitmq_ok.assert_called_once_with(config.broker_url)
    mock_workers_status.assert_called_once_with(retries=2)
    mock_can_process.assert_called_once_with(TEST_PACKAGE_MANAGERS, expect_services, True)
Ejemplo n.º 6
0
def get_status():
    """Return status of Cachito workers and services that Cachito depends on."""
    return flask.jsonify(status())