use std::path::PathBuf;
pub(super) const V0_1_18: &str = "shep-daemon-v0.1.18";
pub(super) const V0_1_20: &str = "shep-daemon-v0.1.20";
pub(super) const V0_1_21: &str = "shep-daemon-v0.1.21";
pub(super) const V0_1_22: &str = "shep-daemon-v0.1.22";
pub(super) const V0_1_23: &str = "shep-daemon-v0.1.23";
pub(super) const V0_1_30: &str = "shep-daemon-v0.1.30";
pub(super) const V0_5_0: &str = "shep-daemon-v0.5.0";
pub(super) const V0_6_2: &str = "shep-daemon-v0.6.2";
pub(super) const V0_8_0: &str = "shep-daemon-v0.8.0";
pub(super) const V0_11_0: &str = "shep-daemon-v0.11.0";
pub(super) const V0_12_0: &str = "shep-daemon-v0.12.0";
pub(super) const V0_13_0: &str = "shep-daemon-v0.13.0";
const BLOB_BEFORE_RELOADS: [&str; 7] = [
"listener_fd",
"next_action_stamp",
"next_deadline",
"next_id",
"pidfile_fd",
"sheep",
"version",
];
const BLOB_WITH_RELOADS: [&str; 8] = [
"listener_fd",
"next_action_stamp",
"next_deadline",
"next_id",
"pidfile_fd",
"reloads",
"sheep",
"version",
];
const SHEEP_BEFORE_MARKERS: [&str; 11] = [
"app",
"credentials",
"epoch",
"fds",
"id",
"instance",
"last_exit",
"name",
"pid",
"restarts",
"status",
];
const SHEEP_KEYS_FROM_V0_1_21: [&str; 4] = ["manual", "pending_delete", "ready_failed", "reload"];
const SHEEP_KEYS_FROM_V0_1_22: [&str; 5] = [
"manual",
"pending_delete",
"ready_failed",
"reload",
"restart_due",
];
const SHEEP_KEYS_FROM_V0_1_23: [&str; 6] = [
"manual",
"pending_delete",
"ready_failed",
"reload",
"restart_due",
"dog",
];
const SHEEP_KEYS_FROM_V0_1_30: [&str; 8] = [
"manual",
"pending_delete",
"ready_failed",
"reload",
"restart_due",
"dog",
"pending",
"pending_reidentifies",
];
const SHEEP_KEYS_FROM_V0_13_0: [&str; 9] = [
"manual",
"pending_delete",
"ready_failed",
"reload",
"restart_due",
"dog",
"pending",
"pending_reidentifies",
"disabled",
];
const FOUR_DESCRIPTORS: [&str; 4] = ["err_log", "err_pipe", "out_log", "out_pipe"];
const SIX_DESCRIPTORS: [&str; 6] = [
"channel", "err_log", "err_pipe", "out_log", "out_pipe", "stdin",
];
const APP_AT_V0_1_18: [&str; 40] = [
"action_timeout",
"args",
"autorestart",
"autostart",
"channel",
"cron_restart",
"cron_timezone",
"cwd",
"env",
"err_file",
"exp_backoff_restart_delay",
"fold",
"graceful_timeout",
"group",
"ignore_watch",
"increment_var",
"instances",
"interpreter",
"kill_signal",
"kill_timeout",
"listen_timeout",
"liveness_probe",
"max_memory",
"max_restarts",
"merge_logs",
"min_uptime",
"name",
"out_file",
"readiness_probe",
"restart_delay",
"reuse_port",
"script",
"shutdown_with_message",
"stdin",
"stop_exit_codes",
"user",
"wait_ready",
"watch",
"watch_delay",
"watch_options",
];
const APP_KEYS_FROM_V0_5_0: [&str; 1] = ["depends_on"];
const APP_KEYS_FROM_V0_6_2: [&str; 2] = ["depends_on", "environment"];
const APP_KEYS_FROM_V0_8_0: [&str; 3] = ["depends_on", "environment", "level_rules"];
const APP_KEYS_FROM_V0_11_0: [&str; 4] = ["depends_on", "environment", "level_rules", "dogs"];
const APP_KEYS_FROM_V0_12_0: [&str; 5] = [
"depends_on",
"environment",
"level_rules",
"dogs",
"log_timestamps",
];
const APP_KEYS_DROPPED_AT_V0_8_0: [&str; 1] = ["increment_var"];
pub(super) fn fixtures_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("fixtures/handover")
}
pub(super) fn fixture_path(stem: &str) -> PathBuf {
fixtures_dir().join(format!("{stem}.json"))
}
pub(super) fn fixture_value(stem: &str) -> serde_json::Value {
let path = fixture_path(stem);
let text = std::fs::read_to_string(&path)
.unwrap_or_else(|error| panic!("the fixture {} is not readable: {error}", path.display()));
serde_json::from_str(&text)
.unwrap_or_else(|error| panic!("the fixture {} is not JSON: {error}", path.display()))
}
fn keys(object: &serde_json::Value) -> Vec<String> {
let mut found: Vec<String> = object
.as_object()
.expect("a blob, a carried sheep, its descriptors and its config are all objects")
.keys()
.cloned()
.collect();
found.sort_unstable();
found
}
fn sorted<T: AsRef<str>>(names: &[T]) -> Vec<String> {
let mut found: Vec<String> = names.iter().map(|key| key.as_ref().to_owned()).collect();
found.sort_unstable();
found
}
fn sorted_plus<T: AsRef<str>>(base: &[T], added: &[&str]) -> Vec<String> {
let mut found = sorted(base);
found.extend(added.iter().map(|key| (*key).to_owned()));
found.sort_unstable();
found
}
fn app_shape(added: &[&str], dropped: &[&str]) -> Vec<String> {
let mut found = sorted(&APP_AT_V0_1_18);
found.extend(added.iter().map(|key| (*key).to_owned()));
found.retain(|key| !dropped.contains(&key.as_str()));
found.sort_unstable();
found
}
struct Shape {
blob: &'static [&'static str],
sheep: &'static [&'static str],
fds: &'static [&'static str],
app_added: &'static [&'static str],
app_dropped: &'static [&'static str],
}
impl Shape {
const fn at(
blob: &'static [&'static str],
sheep: &'static [&'static str],
fds: &'static [&'static str],
app_added: &'static [&'static str],
app_dropped: &'static [&'static str],
) -> Self {
Self {
blob,
sheep,
fds,
app_added,
app_dropped,
}
}
}
const SHAPES: [(&str, Shape); 12] = [
(
V0_1_18,
Shape::at(&BLOB_BEFORE_RELOADS, &[], &FOUR_DESCRIPTORS, &[], &[]),
),
(
V0_1_20,
Shape::at(&BLOB_BEFORE_RELOADS, &[], &SIX_DESCRIPTORS, &[], &[]),
),
(
V0_1_21,
Shape::at(
&BLOB_WITH_RELOADS,
&SHEEP_KEYS_FROM_V0_1_21,
&SIX_DESCRIPTORS,
&[],
&[],
),
),
(
V0_1_22,
Shape::at(
&BLOB_WITH_RELOADS,
&SHEEP_KEYS_FROM_V0_1_22,
&SIX_DESCRIPTORS,
&[],
&[],
),
),
(
V0_1_23,
Shape::at(
&BLOB_WITH_RELOADS,
&SHEEP_KEYS_FROM_V0_1_23,
&SIX_DESCRIPTORS,
&[],
&[],
),
),
(
V0_1_30,
Shape::at(
&BLOB_WITH_RELOADS,
&SHEEP_KEYS_FROM_V0_1_30,
&SIX_DESCRIPTORS,
&[],
&[],
),
),
(
V0_5_0,
Shape::at(
&BLOB_WITH_RELOADS,
&SHEEP_KEYS_FROM_V0_1_30,
&SIX_DESCRIPTORS,
&APP_KEYS_FROM_V0_5_0,
&[],
),
),
(
V0_6_2,
Shape::at(
&BLOB_WITH_RELOADS,
&SHEEP_KEYS_FROM_V0_1_30,
&SIX_DESCRIPTORS,
&APP_KEYS_FROM_V0_6_2,
&[],
),
),
(
V0_8_0,
Shape::at(
&BLOB_WITH_RELOADS,
&SHEEP_KEYS_FROM_V0_1_30,
&SIX_DESCRIPTORS,
&APP_KEYS_FROM_V0_8_0,
&APP_KEYS_DROPPED_AT_V0_8_0,
),
),
(
V0_11_0,
Shape::at(
&BLOB_WITH_RELOADS,
&SHEEP_KEYS_FROM_V0_1_30,
&SIX_DESCRIPTORS,
&APP_KEYS_FROM_V0_11_0,
&APP_KEYS_DROPPED_AT_V0_8_0,
),
),
(
V0_12_0,
Shape::at(
&BLOB_WITH_RELOADS,
&SHEEP_KEYS_FROM_V0_1_30,
&SIX_DESCRIPTORS,
&APP_KEYS_FROM_V0_12_0,
&APP_KEYS_DROPPED_AT_V0_8_0,
),
),
(
V0_13_0,
Shape::at(
&BLOB_WITH_RELOADS,
&SHEEP_KEYS_FROM_V0_13_0,
&SIX_DESCRIPTORS,
&APP_KEYS_FROM_V0_12_0,
&APP_KEYS_DROPPED_AT_V0_8_0,
),
),
];
#[track_caller]
pub(super) fn assert_the_shape(stem: &str, sheep_count: usize) {
let tag = stem.strip_suffix("-populated").unwrap_or(stem);
let (_, shape) = SHAPES
.iter()
.find(|(name, _)| *name == tag)
.unwrap_or_else(|| panic!("{stem} names no release in SHAPES"));
let raw = fixture_value(stem);
assert_eq!(
keys(&raw),
sorted(shape.blob),
"{stem}: the keys the blob holds at its own level"
);
let flock = raw["sheep"]
.as_array()
.expect("a blob's sheep are an array");
assert_eq!(
flock.len(),
sheep_count,
"{stem}: how many sheep the blob carries"
);
for (index, carried) in flock.iter().enumerate() {
assert_eq!(
keys(carried),
sorted_plus(&SHEEP_BEFORE_MARKERS, shape.sheep),
"{stem}: the keys the sheep at index {index} holds"
);
assert_eq!(
keys(&carried["fds"]),
sorted(shape.fds),
"{stem}: the descriptors the sheep at index {index} carries"
);
assert_eq!(
keys(&carried["app"]),
app_shape(shape.app_added, shape.app_dropped),
"{stem}: the config keys the sheep at index {index} carries"
);
}
}