Lutra/crates/deploy/tests/remote.rs
danny a8ad74755c D2: remote browser operations, compare and sync in lutra-deploy
Remote trait gains remove_file, remove_dir and rename (SFTP, FTP,
folder; test servers too). browse: list, read as text (binary and size
caps), write, rename, mkdir, recursive delete that never takes the root.
job: compare (local only, server only, different by size then
contents) and sync (uploads plus explicitly listed deletions).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:49:59 +01:00

170 lines
5.7 KiB
Rust

//! The remote browser, compare and sync (D2) against the local SFTP and
//! FTP test servers (127.0.0.1 only): list, read (text, binary, missing),
//! write, rename, new folder, recursive delete (never the root), compare a
//! folder, and sync with uploads and confirmed deletions.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::path::Path;
use lutra_deploy::browse::{self, RemoteText, Target};
use lutra_deploy::known_hosts::{self, HostKey};
use lutra_deploy::testserver::{FtpServer, SftpServer};
use lutra_deploy::{AuthKind, Cancel, DiffStatus, Job, Monitor, Secret, Server, ServerKind, job};
const PASSWORD: &str = "Otter-Browse-Pa55!";
fn server(kind: ServerKind, port: u16) -> Server {
Server {
id: "t".into(),
name: "Test".into(),
kind,
host: "127.0.0.1".into(),
port: Some(port),
user: "lutra".into(),
root: "/www".into(),
auth: AuthKind::Password,
key_path: None,
mappings: vec![],
excluded: vec!["*.log".into()],
upload_on_save: false,
}
}
fn job(server: Server, project: &Path, known: &Path) -> Job {
Job {
server,
project_root: project.to_path_buf(),
secret: Some(Secret::new(PASSWORD.into()).unwrap()),
known_hosts: known.to_path_buf(),
cancel: Cancel::new(),
monitor: Monitor::new(),
}
}
/// Runs the whole browser, compare and sync scenario against one server.
fn scenario(j: &Job, remote: &Path) {
let project = &j.project_root;
std::fs::create_dir_all(project.join("src")).unwrap();
std::fs::write(project.join("index.php"), "<?php // same").unwrap();
std::fs::write(project.join("src/a.php"), "<?php // local").unwrap();
std::fs::write(project.join("src/new.php"), "<?php // new").unwrap();
std::fs::write(project.join("src/x.log"), "ignored").unwrap();
let www = remote.join("www");
std::fs::create_dir_all(www.join("src")).unwrap();
std::fs::write(www.join("index.php"), "<?php // same").unwrap();
std::fs::write(www.join("src/a.php"), "<?php // SERVER").unwrap();
std::fs::write(www.join("src/old.php"), "<?php // old").unwrap();
std::fs::write(www.join("blob.bin"), [0u8, 159, 146, 150]).unwrap();
let names: Vec<String> = browse::list(j, "")
.unwrap()
.into_iter()
.map(|e| e.name)
.collect();
assert_eq!(names, ["src", "blob.bin", "index.php"]);
let a = browse::read(j, &Target::Mapped("src/a.php".into())).unwrap();
assert_eq!(a.text.as_deref(), Some("<?php // SERVER"));
assert!(
browse::read(j, &Target::Browser("blob.bin".into()))
.unwrap()
.binary
);
assert_eq!(
browse::read(j, &Target::Browser("nope.php".into())).unwrap(),
RemoteText::default()
);
assert!(browse::read(j, &Target::Mapped("src/x.log".into())).is_err());
browse::write(j, "edited/e.php", b"<?php // edited").unwrap();
assert_eq!(
std::fs::read_to_string(www.join("edited/e.php")).unwrap(),
"<?php // edited"
);
browse::rename(j, "edited/e.php", "edited/f.php").unwrap();
assert!(www.join("edited/f.php").is_file());
browse::mkdir(j, "edited/deep/er").unwrap();
assert!(www.join("edited/deep/er").is_dir());
assert_eq!(browse::delete(j, "edited").unwrap(), 4);
assert!(!www.join("edited").exists());
assert!(browse::delete(j, "").is_err());
assert!(browse::list(j, "../..").is_err());
let diffs = job::compare(j, &[String::new()]).unwrap();
let seen: Vec<(String, DiffStatus)> =
diffs.iter().map(|d| (d.path.clone(), d.status)).collect();
assert_eq!(
seen,
[
("blob.bin".to_string(), DiffStatus::RemoteOnly),
("src/a.php".to_string(), DiffStatus::Different),
("src/new.php".to_string(), DiffStatus::LocalOnly),
("src/old.php".to_string(), DiffStatus::RemoteOnly),
]
);
let summary = job::sync(
j,
&["src/a.php".into(), "src/new.php".into()],
&["src/old.php".into()],
)
.unwrap();
assert_eq!((summary.files, summary.deleted, summary.failed), (2, 1, 0));
assert!(!www.join("src/old.php").exists());
assert!(
www.join("blob.bin").exists(),
"only confirmed files are deleted"
);
let after = job::compare(j, &["src".into()]).unwrap();
assert!(after.is_empty(), "{after:?}");
let text = j
.monitor
.take_lines()
.into_iter()
.map(|l| l.message)
.collect::<Vec<_>>()
.join("\n");
assert!(text.contains("deleted /www/src/old.php"));
assert!(!text.contains(PASSWORD));
}
#[test]
fn sftp_browse_compare_and_sync() {
let remote = tempfile::tempdir().unwrap();
let sftp = SftpServer::start(remote.path(), "lutra", Some(PASSWORD), None, 21).unwrap();
let project = tempfile::tempdir().unwrap();
let state = tempfile::tempdir().unwrap();
let known = state.path().join("known_hosts");
let s = server(ServerKind::Sftp, sftp.addr.port());
known_hosts::trust(
&known,
&HostKey {
host: s.address(),
algorithm: sftp.algorithm.clone(),
fingerprint: sftp.fingerprint.clone(),
},
)
.unwrap();
scenario(&job(s, project.path(), &known), remote.path());
}
#[test]
fn ftp_browse_compare_and_sync() {
let remote = tempfile::tempdir().unwrap();
let ftp = FtpServer::start(remote.path(), "lutra", PASSWORD).unwrap();
let project = tempfile::tempdir().unwrap();
let known = project.path().join("unused");
scenario(
&job(
server(ServerKind::Ftp, ftp.addr.port()),
project.path(),
&known,
),
remote.path(),
);
}