shep-daemon 0.25.2

The supervision engine behind shep: process spawning, restart policy, the log plane, watch and cron restarts, and the RPC server
Documentation
//! Shared test harness for `rpc`: a `dispatch` that hands out a fresh
//! [`ConnId`] per call, the `envelope`/`reply_of` wire-shape helpers,
//! and everything the test submodules pull in with `use super::*`.

use super::*;
use crate::fake::{FIRST_SCRIPTED_PID, ProcScript};
use crate::limits::MEMORY_POLL_INTERVAL;
use crate::testing::{
    Harness, SCRIPTED_TREE_BYTES, harness, harness_identifying, harness_with_stats, identity,
};
use shep_core::config::{AppConfig, ApplyGroup, DeclaredApp, ResetDepth, apply_group};
use shep_core::protocol::{
    ActionOutcome, ActionReply, DogSource, HostUsage, ProcessEventKind, Request, Response,
    RpcErrorCode, SelectorSpec,
};
use shep_core::values::UpDuration;
use std::collections::{BTreeMap, BTreeSet};
use tokio::time::Instant;

/// Dispatches on a connection of its own, shadowing [`super::dispatch`]
/// so no case here has to name a [`ConnId`]. One fresh id per call:
/// nothing here spans two requests on the same connection.
async fn dispatch(envelope: Envelope, ctx: &RpcContext) -> Outcome {
    super::dispatch(envelope, ConnId::next(), None, ctx).await
}

fn envelope(id: u64, body: Request) -> Envelope {
    Envelope {
        id,
        deadline_ms: None,
        body,
    }
}

fn reply_of(outcome: Outcome) -> Reply {
    match outcome {
        Outcome::Reply(reply)
        | Outcome::Subscribe { reply, .. }
        | Outcome::Attach { reply, .. }
        | Outcome::Shutdown(reply) => reply,
    }
}

/// Starts `web` through `h`, asserting the start landed: a fixture that
/// never came up has to fail here, not at whatever assertion reads the
/// flock afterwards.
///
/// # Panics
///
/// If the start reply is an error.
async fn start_web(h: &Harness) {
    let started = reply_of(
        dispatch(
            envelope(
                1,
                Request::Start {
                    apps: vec![AppConfig::minimal("web", "./srv")],
                },
            ),
            &h.ctx,
        )
        .await,
    );
    assert!(
        started.result.is_ok(),
        "web must start: {:?}",
        started.result
    );
}

/// The flock a `Describe` of `selector` on `ctx` answers with.
///
/// # Panics
///
/// If the reply is anything but `Described`, which is a fixture bug.
async fn describe(ctx: &RpcContext, id: u64, selector: SelectorSpec) -> Vec<ProcessInfo> {
    let reply = reply_of(dispatch(envelope(id, Request::Describe { selector }), ctx).await);
    let Ok(Response::Described(infos)) = reply.result else {
        panic!("expected Described, got {:?}", reply.result)
    };
    infos
}

/// Sends `entries` for `namespace` in `environment` as a `PutSecrets`,
/// and hands back the whole reply: every case here reads either its
/// `Ok` or its `Err`, so neither half is thrown away.
async fn put_secrets(
    ctx: &RpcContext,
    id: u64,
    namespace: &str,
    environment: &str,
    entries: &[(&str, &str)],
) -> Reply {
    reply_of(
        dispatch(
            envelope(
                id,
                Request::PutSecrets {
                    namespace: namespace.to_string(),
                    environment: environment.to_string(),
                    entries: entries
                        .iter()
                        .map(|(key, value)| ((*key).to_string(), (*value).to_string().into()))
                        .collect(),
                },
            ),
            ctx,
        )
        .await,
    )
}

/// The flock a `ListFlock` on `ctx` answers with.
///
/// # Panics
///
/// If the reply is anything but `Flock`, which is a fixture bug.
async fn list_flock(ctx: &RpcContext, id: u64) -> Vec<ProcessInfo> {
    let reply = reply_of(dispatch(envelope(id, Request::ListFlock), ctx).await);
    let Ok(Response::Flock(infos)) = reply.result else {
        panic!("expected Flock, got {:?}", reply.result)
    };
    infos
}

/// Enables `name` as a built-in dog through the real dispatch path,
/// returning the entry it registered.
async fn enable_dog(ctx: &RpcContext, id: u64, name: &str) -> ProcessInfo {
    let reply = reply_of(
        dispatch(
            envelope(
                id,
                Request::EnableDog {
                    name: name.to_string(),
                    env: BTreeMap::default(),
                    source: DogSource::BuiltIn {
                        grants: None,
                        events: Vec::new(),
                    },
                },
            ),
            ctx,
        )
        .await,
    );
    let Ok(Response::DogStarted(info)) = reply.result else {
        panic!("expected DogStarted, got {:?}", reply.result)
    };
    info
}

/// Starts one sheep named `web` carrying a secret env value, which is
/// the fixture the config-pane cases below all want.
async fn start_web_with_a_secret(ctx: &RpcContext) {
    let mut config = AppConfig::minimal("web", "./srv");
    config
        .env
        .insert("DB_PASS".to_string(), "hunter2".to_string());
    let started = reply_of(dispatch(envelope(1, Request::Start { apps: vec![config] }), ctx).await);
    assert!(started.result.is_ok(), "{:?}", started.result);
}

/// Reads one sheep's config view, for the cases that assert on it.
async fn sheep_config_view(
    ctx: &RpcContext,
    id: u64,
    name: &str,
) -> shep_core::protocol::SheepConfigView {
    let reply = reply_of(
        dispatch(
            envelope(
                id,
                Request::SheepConfig {
                    name: name.to_string(),
                },
            ),
            ctx,
        )
        .await,
    );
    match reply.result {
        Ok(Response::SheepConfig(view)) => *view,
        other => panic!("expected SheepConfig, got {other:?}"),
    }
}

/// Sends `request` on the launch link of `sheep` through `dog`.
async fn on_launch_link(h: &Harness, dog: &str, sheep: &str, request: Request) -> Reply {
    let link = Link::Launch { dog, sheep };
    reply_of(super::dispatch(envelope(1, request), ConnId::next(), Some(link), &h.ctx).await)
}

fn reads(sheep: &str, dog: &str) -> Request {
    Request::SheepDogSettingsResolved {
        name: sheep.to_string(),
        dog: dog.to_string(),
    }
}

#[tokio::test(start_paused = true)]
async fn a_launch_link_reads_its_own_sheeps_table() {
    let h = harness(vec![ProcScript::never_exits(); 2]);
    let jobs = dog_tables::table(serde_json::json!({ "network": "none" }));
    dog_tables::start(
        &h.ctx,
        1,
        vec![dog_tables::carrying("web", vec![("jobs", jobs.clone())])],
    )
    .await;

    let reply = on_launch_link(&h, "jobs", "web", reads("web", "jobs")).await;

    assert_eq!(
        reply.result,
        Ok(Response::SheepDogTable { table: Some(jobs) })
    );
    let subscribe = Request::Subscribe {
        topics: vec!["config.sheep.jobs".to_string()],
    };
    let outcome = super::dispatch(
        envelope(2, subscribe),
        ConnId::next(),
        Some(Link::Launch {
            dog: "jobs",
            sheep: "web",
        }),
        &h.ctx,
    )
    .await;
    assert!(matches!(outcome, Outcome::Subscribe { .. }));
}

#[tokio::test(start_paused = true)]
async fn a_launch_link_is_refused_another_sheeps_table_and_every_other_request() {
    let h = harness(vec![ProcScript::never_exits(); 2]);
    let table = |n: u64| dog_tables::table(serde_json::json!({ "n": n }));
    dog_tables::start(
        &h.ctx,
        1,
        vec![
            dog_tables::carrying("web", vec![("jobs", table(1)), ("deploy", table(2))]),
            dog_tables::carrying("api", vec![("jobs", table(3))]),
        ],
    )
    .await;
    let refused = [
        ("sheep_dog_settings_resolved", reads("api", "jobs")),
        ("sheep_dog_settings_resolved", reads("web", "deploy")),
        ("sheep_dog_settings_resolved", reads("nobody", "jobs")),
        (
            "subscribe",
            Request::Subscribe {
                topics: vec!["config.sheep.*".to_string()],
            },
        ),
        (
            "subscribe",
            Request::Subscribe {
                topics: vec!["config.sheep.deploy".to_string()],
            },
        ),
        (
            "subscribe",
            Request::Subscribe {
                topics: vec!["config.sheep.jobs".to_string(), "*".to_string()],
            },
        ),
        ("ping", Request::Ping),
        ("list_flock", Request::ListFlock),
        (
            "dog_sheep_settings",
            Request::DogSheepSettings {
                dog: "jobs".to_string(),
            },
        ),
        (
            "sheep_config",
            Request::SheepConfig {
                name: "web".to_string(),
            },
        ),
        ("kill_daemon", Request::KillDaemon),
    ];

    for (kind, request) in refused {
        let reply = on_launch_link(&h, "jobs", "web", request).await;
        let err = reply.result.expect_err(kind);
        assert_eq!(err.code, RpcErrorCode::Forbidden, "{kind}");
        assert!(
            err.message.contains(&format!("`{kind}`")),
            "{kind}: {err:?}"
        );
    }
    assert_eq!(list_flock(&h.ctx, 9).await.len(), 2, "nothing was touched");
}

mod apply_config;
mod disable_enable;
mod disable_roll;
mod disabled_reload;
mod disabled_restarts;
mod disabled_wildcards;
mod dog_at_rest;
mod dog_delete;
mod dog_disable;
mod dog_enable_grants;
mod dog_entry_drift;
mod dog_env;
mod dog_events;
mod dog_fields;
mod dog_lifecycle;
mod dog_secrets;
mod dog_secrets_rotation;
mod dog_spawn;
mod dog_table_events;
mod dog_tables;
mod env_copy;
mod lambs_staleness;
mod log_tail;
mod misc_verbs;
mod pty;
mod push;
mod reset_field;
mod sheep_dog_names;
mod sheep_dog_secrets;
mod signal_lamb;
mod start_add;
mod stats;
mod trigger_caller;
mod trigger_signal;
mod vcs;
mod walk;