use arcbox_constants::paths::{K3S_CNI_BIN_DIR, K3S_CNI_CONF_DIR};
const OVERLAY_MOUNT_OPTIONS: &str = r#"["index=on", "nfs_export=on"]"#;
const CONTAINERD_CONFIG_TEMPLATE: &str = include_str!("containerd-config.toml");
pub fn render() -> String {
CONTAINERD_CONFIG_TEMPLATE
.replace("@CNI_BIN_DIR@", K3S_CNI_BIN_DIR)
.replace("@CNI_CONF_DIR@", K3S_CNI_CONF_DIR)
.replace("@OVERLAY_MOUNT_OPTIONS@", OVERLAY_MOUNT_OPTIONS)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cni_paths_come_from_the_shared_constants() {
let config = render();
assert!(config.contains(&format!("bin_dir = \"{K3S_CNI_BIN_DIR}\"")));
assert!(config.contains(&format!("conf_dir = \"{K3S_CNI_CONF_DIR}\"")));
assert!(config.contains("max_conf_num = 1"));
}
#[test]
fn no_placeholder_survives_rendering_outside_comments() {
let config = render();
let leftovers: Vec<&str> = config
.lines()
.filter(|line| !line.trim_start().starts_with('#'))
.filter(|line| line.contains('@'))
.collect();
assert!(
leftovers.is_empty(),
"unsubstituted placeholder left in the rendered config: {leftovers:?}"
);
}
#[test]
fn the_rendered_config_is_valid_toml() {
let parsed: toml::Value = render().parse().expect("config must be valid TOML");
assert_eq!(parsed["version"].as_integer(), Some(2));
}
#[test]
fn overlay_mount_options_keep_nfs_export_and_index_together() {
let parsed: toml::Value = render().parse().unwrap();
let options = parsed["plugins"]["io.containerd.snapshotter.v1.overlayfs"]["mount_options"]
.as_array()
.expect("overlayfs snapshotter must declare mount_options")
.iter()
.map(|value| value.as_str().unwrap().to_owned())
.collect::<Vec<_>>();
assert!(options.contains(&"nfs_export=on".to_owned()));
assert!(
options.contains(&"index=on".to_owned()),
"nfs_export=on without index=on is rejected by the kernel: {options:?}"
);
assert!(
!options.iter().any(|option| option == "index=off"),
"index=off would conflict with nfs_export=on: {options:?}"
);
}
}