Skip to main content

isoloom_render/
checks.rs

1//! What the backends' check runners share: where a probe reaches its host, and how a VM target
2//! runs a project check script.
3
4use isoloom_plan::Environment;
5use isoloom_plan::checks::Host;
6
7use crate::steps::PROJECT_DIR;
8
9/// A probe's host at its address: a machine where a probe reaches it on the network
10/// ([`Environment::probe_address`]), a literal as written. For the targets that keep the
11/// plan's addresses; one that hands addresses out itself (Kubernetes) reaches machines by name.
12#[must_use]
13pub fn address_of(env: &Environment, host: &Host) -> String {
14    match host {
15        Host::Machine { name, network } => env.probe_address(name, network),
16        Host::Literal(literal) => literal.clone(),
17    }
18}
19
20/// The shell command a VM target runs a project check script with: from the project copy at
21/// [`PROJECT_DIR`], then `sh <path>` (what a backend hands [`isoloom_plan::checks::Render::script`]).
22#[must_use]
23pub fn script_command(path: &str) -> String {
24    format!("cd {PROJECT_DIR} && sh {path}")
25}
26
27#[cfg(test)]
28mod tests {
29    use isoloom_plan::Environment;
30    use isoloom_plan::checks::Host;
31    use isoloom_spec::model::Target;
32
33    #[test]
34    fn a_machine_is_reached_at_its_address_and_a_literal_as_written() {
35        let lab = isoloom_plan::Lab::parse(
36            "version: 1\nname: t\nnetworks: { lan: { cidr: 10.9.0.0/24 } }\nmachines:\n  web: { networks: { lan: 10 }, os: debian@12 }\n",
37        )
38        .expect("parses");
39        let env = Environment::build(&lab.for_target(Target::Vagrant), Target::Vagrant, None);
40        let web = Host::Machine {
41            name: "web".into(),
42            network: "lan".into(),
43        };
44        assert_eq!(super::address_of(&env, &web), "10.9.0.10");
45        assert_eq!(super::address_of(&env, &Host::Literal("example.com".into())), "example.com");
46    }
47}