Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/holger

server/lib/tests/admin_capacity_measures_the_data_root.rs

//! **`AdminService.Capacity` measures the data root it was given.**
//!
//! Called the way tonic calls it — through the generated `AdminService` trait
//! on `Arc<HolgerGrpc>` — against a tempdir installed as the data root. Three
//! assertions on a real filesystem, and one on the refusal: a `HolgerGrpc`
//! nobody handed a root to is `FailedPrecondition` BY NAME, never a capacity of
//! zero.

use std::sync::Arc;

use holger_server_lib::exposed::fast_routes::FastRoutes;
use holger_server_lib::grpc::holger_proto::admin_service_server::AdminService;
use holger_server_lib::grpc::holger_proto::CapacityRequest;
use holger_server_lib::grpc::HolgerGrpc;
use holger_server_lib::metering::NO_FILESYSTEM_SOURCE;
use tonic::{Code, Request};

#[tokio::test]
async fn capacity_measures_the_installed_data_root() {
    let root = tempfile::tempdir().unwrap();
    let svc = Arc::new(HolgerGrpc::new(FastRoutes::new(Vec::new())).with_data_root(root.path()));

    let reply = svc
        .capacity(Request::new(CapacityRequest {}))
        .await
        .expect("Capacity answers against a real tempdir")
        .into_inner();

    assert!(
        reply.total_bytes > 0,
        "a real filesystem has a size: {reply:?}"
    );
    assert!(
        reply.available_bytes <= reply.total_bytes,
        "servable can never exceed total: {reply:?}"
    );
    assert_eq!(reply.path, root.path().display().to_string());
    // No repository is served, so nothing is used by holger — and this is a
    // measured zero over an empty route table, not a refusal read as zero.
    assert_eq!(reply.used_by_holger_bytes, 0);
    // ★ The election's other two axes (stinky SB #37): the installer elects the
    // weakest box on cores, RAM and data room, so the capacity says all three.
    assert!(reply.cpu_cores >= 1, "a box has a core: {reply:?}");
    assert!(reply.ram_bytes > 0, "a box has memory: {reply:?}");
    assert_eq!(
        u64::from(reply.cpu_cores),
        std::thread::available_parallelism().unwrap().get() as u64
    );
}

#[tokio::test]
async fn capacity_without_a_data_root_is_refused_by_name() {
    let svc = Arc::new(HolgerGrpc::new(FastRoutes::new(Vec::new())));
    let status = svc
        .capacity(Request::new(CapacityRequest {}))
        .await
        .expect_err("no data root installed, so there is nothing to measure");
    assert_eq!(status.code(), Code::FailedPrecondition);
    // The same words, now with the capability code on both channels (lane C3).
    assert_eq!(
        status.message(),
        holger_errcode::SERVER_CAPABILITY_NOT_CONFIGURED.tag(NO_FILESYSTEM_SOURCE)
    );
    assert_eq!(
        status
            .metadata()
            .get(holger_errcode::WIRE_CODE_KEY)
            .map(|v| v.to_str().unwrap()),
        Some(holger_errcode::SERVER_CAPABILITY_NOT_CONFIGURED.code)
    );
}

#[tokio::test]
async fn capacity_over_a_vanished_root_is_unavailable_with_the_path() {
    let root = tempfile::tempdir().unwrap();
    let gone = root.path().join("vanished");
    let svc = Arc::new(HolgerGrpc::new(FastRoutes::new(Vec::new())).with_data_root(&gone));
    let status = svc
        .capacity(Request::new(CapacityRequest {}))
        .await
        .expect_err("the kernel cannot describe a path that does not exist");
    assert_eq!(status.code(), Code::Unavailable);
    assert!(
        status.message().contains("vanished"),
        "{}",
        status.message()
    );
    assert_eq!(
        holger_errcode::parse(status.message()),
        Some(&holger_errcode::SERVER_UNAVAILABLE)
    );
}