use serde::{Deserialize, Serialize};
pub const RESCAN_CANDIDATE_PREVIEW_CAPABILITY: &str = "rescan-candidate-preview/v1";
pub const RESCAN_CANDIDATE_MAX_BYTES: usize = 1024 * 1024;
pub const CONFIG_CHANGED_SINCE_PREVIEW: &str = "config_changed_since_preview";
pub const RESCAN_CANDIDATE_TOO_LARGE: &str = "candidate_too_large";
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RescanExpect {
pub config_sha256: String,
pub staged_generation: StagedGeneration,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct StagedGeneration {
pub incarnation: String,
pub counter: u64,
}
open_string_enum! {
RescanModuleActionKind {
Restart => "restart",
Reload => "reload",
Start => "start",
Stop => "stop",
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RescanModuleAction {
pub module_id: String,
pub action: RescanModuleActionKind,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub changed: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub drain_timeout_ms: Option<u64>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub busy_gauges: Vec<BusyGaugeReading>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct BusyGaugeReading {
pub gauge: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub value: Option<serde_json::Value>,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{ClientControlRequest, SupervisorRescanResult};
#[test]
fn an_action_a_newer_daemon_sends_round_trips_as_unknown() {
let wire = r#"{"module_id":"m","action":"swap"}"#;
let action: RescanModuleAction = serde_json::from_str(wire).unwrap();
assert_eq!(
action.action,
RescanModuleActionKind::Unknown("swap".to_string())
);
assert_eq!(serde_json::to_string(&action).unwrap(), wire);
}
#[test]
fn a_plain_rescan_reply_decodes_without_the_preview_fields() {
let wire = r#"{"added":[],"removed":[],"changed_pending_reload":[],"unchanged":1}"#;
let result: SupervisorRescanResult = serde_json::from_str(wire).unwrap();
assert!(result.module_actions.is_empty());
assert!(result.earlier_pending.is_empty());
assert_eq!(result.expect, None);
assert_eq!(serde_json::to_string(&result).unwrap(), wire);
}
#[test]
fn a_checked_rescan_without_preview_does_not_decode() {
let wire = r#"{"op":"supervisor.rescan_checked","candidate":"{}"}"#;
assert!(serde_json::from_str::<ClientControlRequest>(wire).is_err());
}
}