1
use std::path::{Path, PathBuf};
2
use std::time::{Duration, SystemTime};
3

            
4
use super::{GRACE, hold, is_cluster_name, is_stale, sweep_stale_clusters};
5

            
6
7
fn cluster(base: &Path, name: &str) -> anyhow::Result<PathBuf> {
7
7
    let root = base.join(name);
8
7
    std::fs::create_dir_all(root.join("pgdata"))?;
9
7
    Ok(root)
10
7
}
11

            
12
5
fn age(root: &Path) -> anyhow::Result<()> {
13
5
    std::fs::File::open(root)?.set_modified(SystemTime::now() - GRACE * 2)?;
14
5
    Ok(())
15
5
}
16

            
17
#[test]
18
1
fn only_the_names_xtask_creates_match() {
19
1
    assert!(is_cluster_name("nomisync-ci-pgAb3dE9"));
20
1
    assert!(!is_cluster_name("nomisync-ci-pg"));
21
1
    assert!(!is_cluster_name("nomisync-ci-pgAb3dE9x"));
22
1
    assert!(!is_cluster_name("nomisync-ci-pg-Ab3dE"));
23
1
    assert!(!is_cluster_name("other-ci-pgAb3dE9"));
24
1
    assert!(!is_cluster_name("my-nomisync-ci-pgAb3dE9"));
25
1
}
26

            
27
#[test]
28
1
fn a_cluster_of_another_user_is_never_stale() {
29
1
    assert!(!is_stale(1, 1000, GRACE * 2, || true));
30
1
}
31

            
32
#[test]
33
1
fn a_young_or_locked_cluster_is_not_stale() {
34
1
    assert!(!is_stale(1000, 1000, Duration::ZERO, || true));
35
1
    assert!(!is_stale(1000, 1000, GRACE * 2, || false));
36
1
    assert!(is_stale(1000, 1000, GRACE, || true));
37
1
}
38

            
39
#[test]
40
1
fn a_held_lock_keeps_an_old_cluster_even_with_a_dead_postmaster_pid() -> anyhow::Result<()> {
41
1
    let base = tempfile::tempdir()?;
42
1
    let root = cluster(base.path(), "nomisync-ci-pgHeld01")?;
43
1
    std::fs::write(root.join("pgdata/postmaster.pid"), "2147483646\n/data\n")?;
44
1
    let _lock = hold(&root)?;
45
1
    age(&root)?;
46
1
    assert!(sweep_stale_clusters(base.path()).is_empty());
47
1
    assert!(root.exists());
48
1
    Ok(())
49
1
}
50

            
51
#[test]
52
1
fn a_released_lock_and_an_old_mtime_remove_the_cluster() -> anyhow::Result<()> {
53
1
    let base = tempfile::tempdir()?;
54
1
    let root = cluster(base.path(), "nomisync-ci-pgGone01")?;
55
1
    drop(hold(&root)?);
56
1
    age(&root)?;
57
1
    assert_eq!(sweep_stale_clusters(base.path()), vec![root.clone()]);
58
1
    assert!(!root.exists());
59
1
    Ok(())
60
1
}
61

            
62
#[test]
63
1
fn a_fresh_cluster_is_kept_even_unlocked() -> anyhow::Result<()> {
64
1
    let base = tempfile::tempdir()?;
65
1
    let root = cluster(base.path(), "nomisync-ci-pgFresh1")?;
66
1
    drop(hold(&root)?);
67
1
    assert!(sweep_stale_clusters(base.path()).is_empty());
68
1
    assert!(root.exists());
69
1
    Ok(())
70
1
}
71

            
72
#[test]
73
1
fn without_a_lock_file_only_an_old_cluster_goes() -> anyhow::Result<()> {
74
1
    let base = tempfile::tempdir()?;
75
1
    let fresh = cluster(base.path(), "nomisync-ci-pgFresh2")?;
76
1
    let old = cluster(base.path(), "nomisync-ci-pgOld001")?;
77
1
    age(&old)?;
78
1
    assert_eq!(sweep_stale_clusters(base.path()), vec![old.clone()]);
79
1
    assert!(fresh.exists());
80
1
    assert!(!old.exists());
81
1
    Ok(())
82
1
}
83

            
84
#[test]
85
1
fn a_foreign_name_is_kept_even_when_abandoned() -> anyhow::Result<()> {
86
1
    let base = tempfile::tempdir()?;
87
1
    let foreign = cluster(base.path(), "someone-else")?;
88
1
    let longer = cluster(base.path(), "nomisync-ci-pgDead012")?;
89
1
    age(&foreign)?;
90
1
    age(&longer)?;
91
1
    assert!(sweep_stale_clusters(base.path()).is_empty());
92
1
    assert!(foreign.exists());
93
1
    assert!(longer.exists());
94
1
    Ok(())
95
1
}
96

            
97
#[test]
98
1
fn a_missing_base_sweeps_nothing() {
99
1
    assert!(sweep_stale_clusters(Path::new("/nonexistent/nomisync-sweep")).is_empty());
100
1
}