Gunnar

mikronova/draupnir

tests/live_oci_published_port.rs

//! **LIVE podman integration — a published-port, resource-capped container is
//! host-reachable.** This is the real end of the korp default-infra chain: jera
//! (job) → draupnir (`ContainerBoot`, this crate) → a running container with a
//! PUBLISHED port the host can connect to, brought up with full-machine resource
//! caps (`--cpus`).
//!
//! `#[ignore]` by default: it needs the `backend-oci` feature AND a live podman
//! socket AND the FalkorDB image, so a normal `cargo test` (CI, no daemon) skips it.
//! Run it on a box with podman:
//!
//! ```text
//! CARGO_TARGET_DIR=/mnt/work4t/cargo-target/draupnir \
//!   cargo test --features backend-oci --test live_oci_published_port -- --ignored --nocapture
//! ```
#![cfg(feature = "backend-oci")]

use std::net::TcpStream;
use std::time::{Duration, Instant};

use draupnir::container::{ContainerBoot, ContainerControl, ContainerState};
use draupnir::{Boot, BootSpec};

fn port_accepts(port: u16, timeout: Duration) -> bool {
    let start = Instant::now();
    let addr = format!("127.0.0.1:{port}");
    while start.elapsed() < timeout {
        if TcpStream::connect_timeout(&addr.parse().unwrap(), Duration::from_millis(300)).is_ok() {
            return true;
        }
        std::thread::sleep(Duration::from_millis(300));
    }
    false
}

#[test]
#[ignore = "needs a live podman socket + the FalkorDB image"]
fn falkordb_published_port_is_host_reachable_with_cpu_cap() {
    // The exact korp demo-zone default-infra shape: FalkorDB v4.20.0, published
    // :6379 on the default (non-isolated) network, all host cores.
    let cores = std::thread::available_parallelism()
        .map(|n| n.get() as f64)
        .unwrap_or(4.0);
    let spec = BootSpec::container("live-falkordb", "docker.io/falkordb/falkordb:v4.20.0")
        .with_port(6379)
        .with_cpus(cores)
        .with_mem_limit_mb(16384);
    spec.validate().expect("hot FalkorDB spec is valid");

    let boot = ContainerBoot::new();
    let machine = boot
        .boot(&spec)
        .expect("boot FalkorDB through draupnir's OCI engine");

    // The container process is up...
    assert_eq!(
        boot.container_state(&machine),
        ContainerState::Running,
        "container running"
    );
    // ...and its PUBLISHED port accepts a host TCP connection (the whole point:
    // no more "Connection refused" — the port is reachable on the host).
    let reachable = port_accepts(6379, Duration::from_secs(30));
    boot.stop(&machine); // always clean up (idempotent remove)
    assert!(
        reachable,
        "published :6379 accepts a host connection (FalkorDB reachable)"
    );

    println!("LIVE OK: FalkorDB published :6379 host-reachable, --cpus {cores}, --memory 16384MiB");
}

#[test]
#[ignore = "needs a live podman socket + the FalkorDB image"]
fn falkordb_distinct_host_port_map_is_reachable_on_the_host_port() {
    // The korp **test-zone** shape: FalkorDB always listens on 6379 INSIDE, but
    // the test zone publishes it on host 6380 (`-p 6380:6379`). Before the fix,
    // publishing 6380:6380 left the container unreachable; this proves the DISTINCT
    // map actually accepts on the HOST port (6380) while nothing binds 6380 inside.
    let spec = BootSpec::container("live-falkordb-test", "docker.io/falkordb/falkordb:v4.20.0")
        .with_port_map(6380, 6379); // host 6380 -> container 6379 (the zone offset)
    spec.validate().expect("test-zone FalkorDB spec is valid");

    let boot = ContainerBoot::new();
    let machine = boot
        .boot(&spec)
        .expect("boot FalkorDB through draupnir's OCI engine");

    assert_eq!(
        boot.container_state(&machine),
        ContainerState::Running,
        "container running"
    );
    // The HOST port is the zone offset (6380); the container never listens on 6380
    // internally, so a bad host==container publish would NOT accept here.
    let reachable = port_accepts(6380, Duration::from_secs(30));
    boot.stop(&machine);
    assert!(
        reachable,
        "distinct map -p 6380:6379 accepts on host 6380 (test zone reachable)"
    );

    println!("LIVE OK: FalkorDB distinct map host 6380 -> container 6379 host-reachable on :6380");
}