Skip to main content

cortexkit_bus_testkit/
lib.rs

1//! Private, disposable broker and daemon processes for module integration tests.
2//!
3//! `CK_SUBC_BIN` and `CK_BUS_BIN` must name test builds, never installed binaries.
4//! Every executable is staged through `cortexkit_test_support::ckdev_binary`.
5//! See the crate README for a complete setup and the missing-binary skip policy.
6
7mod binaries;
8mod bus;
9mod config;
10mod daemon;
11mod process;
12pub mod signer;
13pub mod trust;
14
15pub use binaries::{ck_bus_binary, ck_subc_binary, nats_server_bin};
16pub use bus::{BusOptions, BusServer, PreloadedAccount, LOOPBACK};
17pub use config::{subc_config, Module};
18pub use daemon::{new_machine_id, write_machine_id, DaemonOptions, PrivateDaemon};
19
20/// A missing prerequisite is skippable; a broken rig is not.
21#[derive(Debug)]
22pub enum RigError {
23    MissingBinary {
24        what: &'static str,
25        env_var: &'static str,
26        how_to_fix: String,
27    },
28    Io(std::io::Error),
29    Invalid(String),
30    Timeout {
31        waiting_for: String,
32        details: String,
33    },
34}
35
36impl std::fmt::Display for RigError {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match self {
39            Self::MissingBinary {
40                what,
41                env_var,
42                how_to_fix,
43            } => write!(f, "missing {what} ({env_var}); {how_to_fix}"),
44            Self::Io(error) => write!(f, "{error}"),
45            Self::Invalid(message) => write!(f, "{message}"),
46            Self::Timeout {
47                waiting_for,
48                details,
49            } => write!(f, "timed out waiting for {waiting_for}: {details}"),
50        }
51    }
52}
53
54impl std::error::Error for RigError {
55    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
56        match self {
57            Self::Io(error) => Some(error),
58            _ => None,
59        }
60    }
61}
62
63impl From<std::io::Error> for RigError {
64    fn from(error: std::io::Error) -> Self {
65        Self::Io(error)
66    }
67}
68
69/// Call in an error arm, then return from the test. CI can require prerequisites
70/// with `CK_BUS_TESTKIT_REQUIRE=1`; all non-prerequisite errors always panic.
71pub fn skip_or_fail(error: RigError) {
72    if matches!(error, RigError::MissingBinary { .. })
73        && std::env::var_os("CK_BUS_TESTKIT_REQUIRE").as_deref() != Some(std::ffi::OsStr::new("1"))
74    {
75        eprintln!("SKIP cortexkit-bus-testkit: {error}");
76    } else {
77        panic!("cortexkit-bus-testkit: {error}");
78    }
79}