use std::fs;
use super::super::LOG_BUFFER;
use super::super::log_file::open_append;
use super::*;
#[tokio::test]
async fn a_rotation_lands_every_buffered_line_exactly_once() {
let mut pump = PumpHarness::start();
let mut before = String::new();
for n in 0..40 {
let line = format!("before-{n}");
pump.feed(false, &line).await;
before.push_str(&line);
before.push('\n');
}
assert!(
before.len() < LOG_BUFFER,
"the case needs lines small enough that the buffer may still hold them"
);
let rotated = pump.dir.path().join("out.log.1");
fs::rename(&pump.out_path, &rotated).unwrap();
pump.reopen().await;
assert_eq!(log_text(&rotated), before);
assert_eq!(log_text(&pump.out_path), "");
let mut after = String::new();
for n in 0..40 {
let line = format!("after-{n}");
pump.feed(false, &line).await;
after.push_str(&line);
after.push('\n');
}
pump.flush().await;
assert_eq!(log_text(&pump.out_path), after);
assert_eq!(
log_text(&rotated),
before,
"the archive must have stopped growing at the swap"
);
}
#[tokio::test]
async fn a_reopen_moves_both_streams_onto_the_recreated_paths() {
let mut pump = PumpHarness::start();
pump.feed(false, "before-out").await;
pump.feed(true, "before-err").await;
let rotated_out = pump.dir.path().join("out.log.1");
let rotated_err = pump.dir.path().join("err.log.1");
fs::rename(&pump.out_path, &rotated_out).unwrap();
fs::rename(&pump.err_path, &rotated_err).unwrap();
assert!(
!pump.out_path.exists(),
"sanity: the rename really moved it"
);
pump.reopen().await;
assert_eq!(log_text(&rotated_out), "before-out\n");
assert_eq!(log_text(&rotated_err), "before-err\n");
assert_eq!(log_text(&pump.out_path), "");
assert_eq!(log_text(&pump.err_path), "");
pump.feed(false, "after-out").await;
pump.feed(true, "after-err").await;
pump.reopen().await;
assert_eq!(log_text(&pump.out_path), "after-out\n");
assert_eq!(log_text(&pump.err_path), "after-err\n");
assert_eq!(log_text(&rotated_out), "before-out\n");
assert_eq!(log_text(&rotated_err), "before-err\n");
}
#[tokio::test]
async fn a_reopened_handle_still_appends_so_a_truncation_leaves_no_hole() {
let mut pump = PumpHarness::start();
pump.feed(false, "first").await;
fs::rename(&pump.out_path, pump.dir.path().join("out.log.1")).unwrap();
pump.reopen().await;
pump.feed(false, "second").await;
assert_file_settles(&pump.out_path, "second\n").await;
fs::File::create(&pump.out_path).unwrap();
assert_eq!(fs::metadata(&pump.out_path).unwrap().len(), 0);
pump.feed(false, "third").await;
assert_file_settles(&pump.out_path, "third\n").await;
}
#[cfg(unix)]
#[tokio::test]
async fn opening_a_symlinked_log_path_is_refused_rather_than_followed() {
let dir = tempfile::tempdir().unwrap();
let target = dir.path().join("someone-elses.conf");
let link = dir.path().join("web-out.log");
fs::write(&target, b"original").unwrap();
std::os::unix::fs::symlink(&target, &link).unwrap();
let error = open_append(&link)
.await
.expect_err("a symlinked log path must not be opened for appending");
assert_eq!(
fs::read(&target).unwrap(),
b"original",
"a refused open must not have reached the symlink's target at all"
);
assert_eq!(
error.to_string(),
crate::runner::SYMLINK_REFUSED,
"the operator who reads this needs the word symlink, not ELOOP's \
own wording about levels of them"
);
}
#[cfg(target_os = "linux")]
#[tokio::test]
async fn a_log_path_below_a_symlinked_parent_the_daemon_owns_opens_on_linux() {
let dir = tempfile::tempdir().unwrap();
let real = dir.path().join("real");
let link = dir.path().join("logs");
fs::create_dir(&real).unwrap();
std::os::unix::fs::symlink(&real, &link).unwrap();
open_append(&link.join("web-out.log"))
.await
.expect("a link the daemon's own user owns is trusted");
assert!(real.join("web-out.log").exists());
}
#[cfg(target_os = "linux")]
#[tokio::test]
async fn a_plain_log_path_still_appends_after_the_resolving_open() {
use tokio::io::AsyncWriteExt as _;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("web-out.log");
let mut handle = open_append(&path).await.unwrap();
handle.write_all(b"first line\n").await.unwrap();
handle.flush().await.unwrap();
fs::File::create(&path).unwrap();
handle.write_all(b"second\n").await.unwrap();
handle.flush().await.unwrap();
assert_eq!(fs::read(&path).unwrap(), b"second\n");
}
#[tokio::test]
async fn a_reopen_is_answered_while_both_streams_are_idle() {
let pump = PumpHarness::start();
pump.reopen().await;
fs::remove_file(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
pump.reopen().await;
assert!(pump.out_path.exists(), "stdout's path must be back");
assert!(pump.err_path.exists(), "stderr's path must be back");
}
#[tokio::test]
async fn a_stream_that_has_ended_is_still_reopened() {
let mut pump = PumpHarness::start();
pump.feed(false, "before-out").await;
pump.feed(true, "before-err").await;
pump.out_writer
.shutdown()
.await
.expect("a duplex stream's write end must close");
let_the_duplex_pump_settle().await;
fs::remove_file(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
let outcome = pump.reopen_for_answer().await;
assert_eq!(
outcome,
Ok(()),
"an ended stream's path is as openable as a live one's"
);
assert!(
pump.out_path.exists(),
"the ended stream's path must come back too"
);
assert!(
pump.err_path.exists(),
"the live stream's path must be back"
);
}
#[tokio::test]
async fn a_reopen_that_cannot_open_a_path_again_answers_with_the_failure() {
let pump = PumpHarness::start();
pump.reopen().await;
fs::rename(&pump.out_path, pump.dir.path().join("out.log.1")).unwrap();
fs::create_dir(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
let error = pump
.reopen_for_answer()
.await
.expect_err("a reopen that could not open stdout's path must say so");
assert!(
error.message.contains(pump.out_path.to_str().unwrap()),
"the failure must name the path it could not open: {error}"
);
assert!(
!error.message.contains(pump.err_path.to_str().unwrap()),
"stderr's path opened fine and must not be reported: {error}"
);
assert!(
pump.err_path.exists(),
"stderr must be reopened even though stdout's open failed"
);
}