pub(crate) use crate::fake_https as https;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::{Duration, UNIX_EPOCH};
use crate::clock::FixedClock;
use crate::net::FakePorts;
use crate::process::fake::{Call, FakeProcesses, Reply};
use crate::project::platform::HostOs;
use crate::{HostPorts, ProcessRunner, RunnerConfig};
pub(crate) const TEST_TIME: u64 = 1_792_065_600;
pub(crate) fn modelled<E: Default + Send + 'static>(answer: fn(&Mutex<E>, &Call) -> Reply) -> (Arc<Mutex<E>>, Arc<FakeProcesses>) {
let engine = Arc::new(Mutex::new(E::default()));
let fake = Arc::new(FakeProcesses::model({
let engine = Arc::clone(&engine);
move |call| {
let reply = answer(&engine, call);
let silent = reply.code == 0 && !reply.missing && reply.stdout.is_empty();
if silent && call.program == "docker" && call.args.first().map(String::as_str) == Some("info") {
return Reply::ok("29.0.0");
}
reply
}
}));
(engine, fake)
}
pub(crate) fn processes(fake: &Arc<FakeProcesses>) -> Arc<dyn ProcessRunner> {
Arc::<FakeProcesses>::clone(fake)
}
pub(crate) fn runner_config(home: &Path, processes: Arc<dyn ProcessRunner>, ports: Arc<dyn HostPorts>, host: HostOs) -> RunnerConfig {
RunnerConfig {
home: home.to_path_buf(),
processes,
ports,
clock: Arc::new(FixedClock(UNIX_EPOCH + Duration::from_secs(TEST_TIME))),
host,
tf_plugin_cache: None,
host_env: crate::host_env::HostEnv::default(),
}
}
pub(crate) fn no_ports() -> Arc<dyn HostPorts> {
Arc::new(FakePorts::new([]))
}
pub(crate) async fn traced<T>(f: impl Future<Output = T>) -> (T, String) {
use std::cell::RefCell;
use std::sync::OnceLock;
thread_local! {
static CAPTURE: RefCell<Option<Vec<u8>>> = const { RefCell::new(None) };
}
struct ThisThread;
impl std::io::Write for ThisThread {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
CAPTURE.with(|c| {
if let Some(buf) = c.borrow_mut().as_mut() {
buf.extend_from_slice(bytes);
}
});
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
static INSTALLED: OnceLock<()> = OnceLock::new();
INSTALLED.get_or_init(|| {
let subscriber = tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.with_writer(|| ThisThread)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("the test subscriber is set once");
});
CAPTURE.with(|c| *c.borrow_mut() = Some(Vec::new()));
let given = f.await;
let bytes = CAPTURE.with(|c| c.borrow_mut().take()).unwrap_or_default();
(given, String::from_utf8_lossy(&bytes).into_owned())
}
pub(crate) fn every_variant<T: schemars::JsonSchema + serde::de::DeserializeOwned>() -> Vec<T> {
crate::every_name::<T>().iter().map(|n| crate::from_wire_name(n).unwrap()).collect()
}
pub(crate) fn assert_named_as_serde<T>(name: fn(T) -> &'static str)
where
T: Copy + PartialEq + std::fmt::Debug + serde::Serialize + serde::de::DeserializeOwned + schemars::JsonSchema,
{
let all: Vec<T> = every_variant();
assert!(!all.is_empty(), "no variant found in the schema");
for &value in &all {
let json = serde_json::to_value(value).unwrap();
assert_eq!(json, serde_json::json!(name(value)), "the name disagrees with serde for {value:?}");
let back: T = serde_json::from_value(json).unwrap();
assert_eq!(back, value, "{value:?} didn't read back as itself");
}
let mut names: Vec<&str> = all.iter().map(|&v| name(v)).collect();
names.sort_unstable();
names.dedup();
assert_eq!(names.len(), all.len(), "two variants share a name");
}
#[test]
fn every_variant_is_read_from_the_schema_in_order() {
use crate::{ErrorCode, Park};
assert_eq!(every_variant::<Park>(), [Park::Pause, Park::Shutdown]);
let codes = every_variant::<ErrorCode>();
assert!(codes.contains(&ErrorCode::ProjectInvalid), "{codes:?}");
}
#[tokio::test]
async fn a_capture_holds_its_own_threads_events_and_none_of_another_threads() {
let elsewhere = || std::thread::spawn(|| tracing::warn!(what = "elsewhere", "a trace")).join().unwrap();
elsewhere();
let ((), here) = traced(async {
tracing::warn!(what = "here", "a trace");
elsewhere();
})
.await;
assert!(here.contains("what=\"here\"") || here.contains("what=here"), "{here}");
assert!(!here.contains("elsewhere"), "another thread's trace isn't this capture's: {here}");
}
pub(crate) fn child_test_saying(exact: &str, var: &str, home: &Path, said: &str) -> (std::process::Child, bool) {
use std::io::BufRead as _;
let mut child = std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", exact, "--nocapture", "--test-threads=1"])
.env(var, home)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::null())
.spawn()
.unwrap();
let mut lines = std::io::BufReader::new(child.stdout.take().unwrap()).lines();
let saying = lines.by_ref().map_while(Result::ok).any(|line| line.contains(said));
(child, saying)
}
pub(crate) fn owned_map(pairs: &[(&str, &str)]) -> std::collections::BTreeMap<String, String> {
pairs.iter().map(|(k, v)| ((*k).to_owned(), (*v).to_owned())).collect()
}