//! 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"), " = 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(" = 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::>() .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(), ); }