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

nordisk/holger

server/cli/tests/release_has_no_test_runner.rs

//! **No shipped build can run a job in-process** (lane HOL-SANDBOX-TRAIT).
//!
//! The in-process test runner (`holger-job-runner`'s `test-runner`, turned on only by
//! server-lib's `sandbox-off` and this crate's `sandbox-off`) runs a job's program in the
//! server's own process: for unit tests and for the tests that drive the real
//! `holger-downstream` in the toolchain image, which has no container engine. Every
//! shipped feature set is resolved here with `cargo tree` (normal edges: no
//! dev-dependency) and must carry neither feature; without them a release binary refuses
//! `sandbox: off` at boot (`holger-sandbox-6`), so it cannot select the test runner.

use std::path::Path;
use std::process::Command;

fn repo_root() -> &'static Path {
    Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."))
}

/// The features `package` (in the workspace of `manifest`) resolves for `invert` with
/// `features` on, as `<crate> feature "<f>"` lines (the features a package is built with show
/// in the INVERTED tree).
fn features_of(
    manifest: &str,
    package: &str,
    features: &str,
    default: bool,
    invert: &str,
) -> String {
    let mut args = vec![
        "tree",
        "--manifest-path",
        manifest,
        "-p",
        package,
        "-e",
        "normal,features",
        "--prefix",
        "none",
        "-i",
        invert,
    ];
    if !features.is_empty() {
        args.extend(["--features", features]);
    }
    if !default {
        args.push("--no-default-features");
    }
    let out = Command::new(env!("CARGO"))
        .current_dir(repo_root())
        .args(&args)
        .output()
        .expect("cargo tree runs");
    assert!(
        out.status.success(),
        "cargo tree -p {package} --features {features}: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8_lossy(&out.stdout).into_owned()
}

/// The shipped builds with an intake chain (their bundles are verified in jobs):
/// `(manifest, package, features, default features)`. `holger` (the server) and its MVP row,
/// and `holger-downstream` (a workspace of its own) one-way, fetching and over WireGuard.
const SHIPPED: &[(&str, &str, &str, bool)] = &[
    ("Cargo.toml", "holger", "", true),
    ("Cargo.toml", "holger", "mvp-server", false),
    (
        "holger-downstream/Cargo.toml",
        "holger-downstream",
        "",
        true,
    ),
    (
        "holger-downstream/Cargo.toml",
        "holger-downstream",
        "fetch",
        true,
    ),
    (
        "holger-downstream/Cargo.toml",
        "holger-downstream",
        "downstream-wg",
        true,
    ),
];

#[test]
fn no_shipped_build_carries_the_in_process_test_runner() {
    for (manifest, package, features, default) in SHIPPED {
        let runner = features_of(manifest, package, features, *default, "holger-job-runner");
        assert!(
            runner.contains("holger-job-runner v"),
            "{package} `{features}` runs its jobs through holger-job-runner:\n{runner}"
        );
        assert!(
            !runner.contains("holger-job-runner feature \"test-runner\""),
            "{package} `{features}` carries the in-process test runner:\n{runner}"
        );
        let lib = features_of(manifest, package, features, *default, "holger-server-lib");
        assert!(
            !lib.contains("feature \"sandbox-off\""),
            "{package} `{features}` turns `sandbox-off` on:\n{lib}"
        );
    }
}

#[test]
fn sandbox_off_is_what_brings_the_test_runner() {
    let tree = features_of(
        "holger-downstream/Cargo.toml",
        "holger-downstream",
        "sandbox-off",
        true,
        "holger-job-runner",
    );
    assert!(
        tree.contains("holger-job-runner feature \"test-runner\""),
        "non-vacuous: `sandbox-off` carries the test runner:\n{tree}"
    );
}