| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use oxy_core::repo::Repo; |
| 7 |
use oxygenhub::backups::{self, Destination, Directory, Setting}; |
| 8 |
use oxygenhub::config::Config; |
| 9 |
use oxygenhub::repos::RepoName; |
| 10 |
use oxygenhub::{db, Shared}; |
| 11 |
use std::path::Path; |
| 12 |
use std::sync::Arc; |
| 13 |
|
| 14 |
fn hermetic() { |
| 15 |
static ONCE: std::sync::Once = std::sync::Once::new(); |
| 16 |
static DIR: std::sync::OnceLock<tempfile::TempDir> = std::sync::OnceLock::new(); |
| 17 |
ONCE.call_once(|| { |
| 18 |
let d = tempfile::tempdir().unwrap(); |
| 19 |
std::env::set_var("OXY_HOME", d.path()); |
| 20 |
DIR.set(d).ok(); |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
struct Hub { |
| 25 |
app: Shared, |
| 26 |
base: String, |
| 27 |
_dir: tempfile::TempDir, |
| 28 |
} |
| 29 |
|
| 30 |
async fn hub(backups: &Path) -> Hub { |
| 31 |
hermetic(); |
| 32 |
let dir = tempfile::tempdir().unwrap(); |
| 33 |
let mut config = Config::load(dir.path()).unwrap(); |
| 34 |
config.auth.push_tokens = vec!["site".into()]; |
| 35 |
config.backups.directories = vec![backups.to_path_buf()]; |
| 36 |
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 37 |
let port = listener.local_addr().unwrap().port(); |
| 38 |
config.server.base_url = format!("http://127.0.0.1:{port}"); |
| 39 |
let app = oxygenhub::build(config).await.unwrap(); |
| 40 |
let router = oxygenhub::router(app.clone()); |
| 41 |
tokio::spawn(async move { axum::serve(listener, router).await.unwrap() }); |
| 42 |
Hub { app, base: format!("http://127.0.0.1:{port}"), _dir: dir } |
| 43 |
} |
| 44 |
|
| 45 |
fn write(root: &Path, rel: &str, content: &str) { |
| 46 |
let p = root.join(rel); |
| 47 |
std::fs::create_dir_all(p.parent().unwrap()).unwrap(); |
| 48 |
std::fs::write(p, content).unwrap(); |
| 49 |
} |
| 50 |
|
| 51 |
async fn create(app: &Shared, owner: &str, name: &str, visibility: hub_api::Visibility) { |
| 52 |
app.repos.create(&RepoName::parse(&format!("{owner}/{name}")).unwrap(), None).unwrap(); |
| 53 |
db::repos::create(&app.db, owner, name, "a demo", visibility).await.unwrap(); |
| 54 |
} |
| 55 |
|
| 56 |
fn push(url: &str, work: &Path, files: &[(&str, &str)]) { |
| 57 |
let mut c = Repo::init(work).unwrap(); |
| 58 |
c.config.set("remote.origin.url", url).unwrap(); |
| 59 |
c.config.set("remote.origin.token", "site").unwrap(); |
| 60 |
c.save_config().unwrap(); |
| 61 |
for (f, t) in files { |
| 62 |
write(work, f, t); |
| 63 |
} |
| 64 |
c.snapshot("t").unwrap(); |
| 65 |
c.commit("Some work", &[]).unwrap(); |
| 66 |
assert!(c.push("origin", false).unwrap().iter().all(|r| r.ok)); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
fn files(dir: &Path) -> Vec<(String, Vec<u8>)> { |
| 71 |
let mut out = Vec::new(); |
| 72 |
let mut stack = vec![dir.to_path_buf()]; |
| 73 |
while let Some(d) = stack.pop() { |
| 74 |
for e in std::fs::read_dir(&d).unwrap().flatten() { |
| 75 |
let p = e.path(); |
| 76 |
if p.is_dir() { |
| 77 |
stack.push(p); |
| 78 |
} else { |
| 79 |
out.push((p.strip_prefix(dir).unwrap().to_string_lossy().into_owned(), std::fs::read(&p).unwrap())); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
out.sort(); |
| 84 |
out |
| 85 |
} |
| 86 |
|
| 87 |
#[tokio::test(flavor = "multi_thread")] |
| 88 |
async fn a_backup_restores_the_same_repository_and_sends_only_what_changed() { |
| 89 |
let store = tempfile::tempdir().unwrap(); |
| 90 |
let h = hub(store.path()).await; |
| 91 |
db::accounts::create_account(&h.app.db, "andy", "[email protected]", "x", "salt", false).await.unwrap(); |
| 92 |
db::accounts::create_account(&h.app.db, "bob", "[email protected]", "x", "salt", false).await.unwrap(); |
| 93 |
create(&h.app, "andy", "demo", hub_api::Visibility::Private).await; |
| 94 |
create(&h.app, "bob", "other", hub_api::Visibility::Public).await; |
| 95 |
let work = tempfile::tempdir().unwrap(); |
| 96 |
push(&format!("{}/andy/demo", h.base), work.path(), &[("README.md", "hello\n"), ("src/main.rs", "fn main() {}\n")]); |
| 97 |
let other = tempfile::tempdir().unwrap(); |
| 98 |
push(&format!("{}/bob/other", h.base), other.path(), &[("x.txt", "bob's\n")]); |
| 99 |
|
| 100 |
|
| 101 |
let target = store.path().display().to_string(); |
| 102 |
let mut s = Setting::off("andy", "user"); |
| 103 |
s.every = "hourly".into(); |
| 104 |
s.target = target.clone(); |
| 105 |
backups::save(&h.app.db, &s).await.unwrap(); |
| 106 |
let first = backups::run(&h.app, "andy", "now").await.unwrap(); |
| 107 |
assert_eq!((first.status.as_str(), first.repos), ("ok", 1), "{}", first.detail); |
| 108 |
assert!(first.sent > 0 && first.bytes > 0); |
| 109 |
let dest = Directory { root: store.path().join("andy") }; |
| 110 |
assert!(dest.exists(&format!("runs/{}/DONE", first.at)).unwrap()); |
| 111 |
let records: serde_json::Value = serde_json::from_slice(&dest.get(&format!("runs/{}/records/demo.json", first.at)).unwrap()).unwrap(); |
| 112 |
assert_eq!(records["repositories"][0]["visibility"], "private"); |
| 113 |
let text = String::from_utf8_lossy(&dest.get(&format!("runs/{}/records/demo.json", first.at)).unwrap()).into_owned(); |
| 114 |
assert!(!text.contains("bob/other") && !text.contains("\"other\""), "nothing of another owner"); |
| 115 |
assert!(!store.path().join("bob").exists(), "bob set up no backup"); |
| 116 |
|
| 117 |
|
| 118 |
tokio::time::sleep(std::time::Duration::from_millis(1100)).await; |
| 119 |
let second = backups::run(&h.app, "andy", "now").await.unwrap(); |
| 120 |
assert_eq!((second.status.as_str(), second.sent), ("ok", 0), "{}", second.detail); |
| 121 |
|
| 122 |
|
| 123 |
let empty = tempfile::tempdir().unwrap(); |
| 124 |
let fresh = hub(empty.path()).await; |
| 125 |
let dest: Arc<dyn Destination> = Arc::new(Directory { root: store.path().join("andy") }); |
| 126 |
let done = backups::restore_into_hub(&fresh.app, dest, None).await.unwrap(); |
| 127 |
assert_eq!(done, vec!["andy/demo (repos)"]); |
| 128 |
let name = RepoName::parse("andy/demo").unwrap(); |
| 129 |
assert_eq!(files(&fresh.app.repos.dir(&name)), files(&h.app.repos.dir(&name)), "byte for byte"); |
| 130 |
let row = db::repos::get(&fresh.app.db, "andy", "demo").await.unwrap().expect("listed on the hub"); |
| 131 |
assert_eq!((row.visibility.as_str(), row.description.as_str()), ("private", "a demo")); |
| 132 |
let (a, b) = (h.app.repos.open(&name).unwrap(), fresh.app.repos.open(&name).unwrap()); |
| 133 |
assert_eq!( |
| 134 |
a.bookmarks().unwrap().iter().map(|b| b.heads.clone()).collect::<Vec<_>>(), |
| 135 |
b.bookmarks().unwrap().iter().map(|b| b.heads.clone()).collect::<Vec<_>>() |
| 136 |
); |
| 137 |
|
| 138 |
|
| 139 |
let mut bad = Setting::off("andy", "user"); |
| 140 |
bad.every = "hourly".into(); |
| 141 |
bad.target = "/tmp/somewhere-else".into(); |
| 142 |
backups::save(&h.app.db, &bad).await.unwrap(); |
| 143 |
let refused = backups::run(&h.app, "andy", "now").await.unwrap(); |
| 144 |
assert_eq!(refused.status, "failed"); |
| 145 |
assert!(refused.detail.contains("not a backup directory this hub allows"), "{}", refused.detail); |
| 146 |
} |
| 147 |
|
| 148 |
#[tokio::test(flavor = "multi_thread")] |
| 149 |
async fn backups_fall_due_on_their_schedule() { |
| 150 |
let store = tempfile::tempdir().unwrap(); |
| 151 |
let h = hub(store.path()).await; |
| 152 |
let mut s = Setting::off("andy", "user"); |
| 153 |
s.target = store.path().display().to_string(); |
| 154 |
backups::save(&h.app.db, &s).await.unwrap(); |
| 155 |
assert!(backups::due(&h.app.db).await.unwrap().is_empty(), "off is never due"); |
| 156 |
s.every = "hourly".into(); |
| 157 |
backups::save(&h.app.db, &s).await.unwrap(); |
| 158 |
assert_eq!(backups::due(&h.app.db).await.unwrap().len(), 1, "never run: due"); |
| 159 |
sqlx::query("INSERT INTO backup_runs (owner, started_ms, status) VALUES ('andy', ?, 'ok')") |
| 160 |
.bind(db::now_ms()) |
| 161 |
.execute(&h.app.db) |
| 162 |
.await |
| 163 |
.unwrap(); |
| 164 |
assert!(backups::due(&h.app.db).await.unwrap().is_empty(), "ran just now"); |
| 165 |
} |