use krab_runtime::{ContainerInfo, ContainerSpec, LogOpts, Runtime};
use std::collections::{BTreeMap, HashMap};
use std::io;
use std::sync::Mutex;
use tokio::sync::mpsc;
#[derive(Default)]
pub struct State {
pub containers: BTreeMap<String, ContainerInfo>,
pub specs: HashMap<String, ContainerSpec>,
pub images: Vec<String>,
pub pulls: Vec<String>,
pub calls: Vec<String>,
pub exec_code: HashMap<String, i64>,
pub networks: Vec<String>,
pub aliases: HashMap<String, Vec<String>>,
pub next: u64,
}
#[derive(Default)]
pub struct Fake {
pub st: Mutex<State>,
}
impl Fake {
pub fn with_images(images: &[&str]) -> Self {
let f = Fake::default();
f.st.lock().unwrap().images = images.iter().map(|s| s.to_string()).collect();
f
}
pub fn by_name(&self, name: &str) -> Option<ContainerInfo> {
self.st
.lock()
.unwrap()
.containers
.values()
.find(|c| c.name == name)
.cloned()
}
pub fn running(&self) -> Vec<ContainerInfo> {
self.st
.lock()
.unwrap()
.containers
.values()
.filter(|c| c.state == "running")
.cloned()
.collect()
}
pub fn set_state(&self, name: &str, state: &str, code: i64) {
let mut st = self.st.lock().unwrap();
if let Some(c) = st.containers.values_mut().find(|c| c.name == name) {
c.state = state.into();
c.exit_code = code;
c.finished_at = "2026-11-30T00:00:01Z".into();
}
}
pub fn set_exec(&self, name: &str, code: i64) {
self.st.lock().unwrap().exec_code.insert(name.into(), code);
}
pub fn count(&self, call: &str) -> usize {
self.st
.lock()
.unwrap()
.calls
.iter()
.filter(|c| c.starts_with(call))
.count()
}
}
fn nf() -> io::Error {
io::Error::new(io::ErrorKind::NotFound, "no such container")
}
impl Runtime for Fake {
fn name(&self) -> &str {
"fake"
}
async fn list(&self) -> io::Result<Vec<ContainerInfo>> {
Ok(self
.st
.lock()
.unwrap()
.containers
.values()
.cloned()
.collect())
}
async fn inspect(&self, info: &mut ContainerInfo) -> io::Result<()> {
let st = self.st.lock().unwrap();
let c = st
.containers
.get(&info.id)
.or_else(|| st.containers.values().find(|c| c.name == info.id))
.ok_or_else(nf)?;
*info = c.clone();
Ok(())
}
async fn logs(
&self,
_info: &ContainerInfo,
_opts: &LogOpts,
) -> io::Result<mpsc::Receiver<bytes::Bytes>> {
Err(nf())
}
async fn pull_image(&self, image: &str) -> io::Result<()> {
let mut st = self.st.lock().unwrap();
st.pulls.push(image.into());
if image.contains("missing") {
return Err(io::Error::other("manifest unknown"));
}
st.images.push(image.into());
Ok(())
}
async fn create_container(&self, spec: &ContainerSpec) -> io::Result<String> {
let mut st = self.st.lock().unwrap();
if !st.images.contains(&spec.image) {
return Err(io::Error::new(io::ErrorKind::NotFound, "No such image"));
}
if st.containers.values().any(|c| c.name == spec.name) {
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "conflict"));
}
st.next += 1;
let id = format!("c{:04}", st.next);
st.calls.push(format!("create {}", spec.name));
let info = ContainerInfo {
id: id.clone(),
name: spec.name.clone(),
image: spec.image.clone(),
image_id: "sha256:abc".into(),
state: "created".into(),
labels: spec.labels.clone(),
..Default::default()
};
st.containers.insert(id.clone(), info);
st.specs.insert(id.clone(), spec.clone());
Ok(id)
}
async fn start(&self, id: &str) -> io::Result<()> {
let mut st = self.st.lock().unwrap();
st.calls.push(format!("start {id}"));
let ip = format!("127.0.0.{}", 1);
let c = st.containers.get_mut(id).ok_or_else(nf)?;
c.state = "running".into();
c.ip = ip;
c.started_at = "2026-11-30T00:00:00Z".into();
Ok(())
}
async fn stop(&self, id: &str, _t: u64) -> io::Result<()> {
let mut st = self.st.lock().unwrap();
st.calls.push(format!("stop {id}"));
let c = st.containers.get_mut(id).ok_or_else(nf)?;
c.state = "exited".into();
c.exit_code = 137;
Ok(())
}
async fn remove(&self, id: &str, _force: bool) -> io::Result<()> {
let mut st = self.st.lock().unwrap();
st.calls.push(format!("remove {id}"));
st.containers.remove(id).map(|_| ()).ok_or_else(nf)
}
async fn ensure_network(&self, name: &str) -> io::Result<()> {
let mut st = self.st.lock().unwrap();
if !st.networks.iter().any(|n| n == name) {
st.networks.push(name.into());
}
Ok(())
}
async fn remove_network(&self, name: &str) -> io::Result<()> {
self.st.lock().unwrap().networks.retain(|n| n != name);
Ok(())
}
async fn connect_network(&self, c: &str, _n: &str, aliases: &[String]) -> io::Result<()> {
self.st
.lock()
.unwrap()
.aliases
.insert(c.into(), aliases.to_vec());
Ok(())
}
async fn disconnect_network(&self, c: &str, _n: &str) -> io::Result<()> {
self.st.lock().unwrap().aliases.remove(c);
Ok(())
}
async fn remove_volume(&self, _name: &str) -> io::Result<()> {
Ok(())
}
async fn exec_run(&self, id: &str, _cmd: &[String]) -> io::Result<(i64, Vec<u8>)> {
let st = self.st.lock().unwrap();
let name = st
.containers
.get(id)
.map(|c| c.name.clone())
.unwrap_or_default();
Ok((st.exec_code.get(&name).copied().unwrap_or(0), Vec::new()))
}
}