summaryrefslogtreecommitdiff
path: root/tests/integration/cli_cleanup.rs
blob: 822c7bcbd5453acac0ee05043bf99bd049257d0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use std::fmt::Write as _;
use std::process::Stdio;
use tempfile::TempDir;
use tokio::process::Command;

fn witryna_bin() -> std::path::PathBuf {
    let mut path = std::path::PathBuf::from(env!("CARGO_BIN_EXE_witryna"));
    if !path.exists() {
        path = std::path::PathBuf::from("target/debug/witryna");
    }
    path
}

async fn write_cleanup_config(
    dir: &std::path::Path,
    sites: &[&str],
    max_builds: u32,
) -> (std::path::PathBuf, std::path::PathBuf, std::path::PathBuf) {
    let base_dir = dir.join("data");
    let log_dir = dir.join("logs");
    tokio::fs::create_dir_all(&base_dir).await.unwrap();
    tokio::fs::create_dir_all(&log_dir).await.unwrap();

    let mut sites_toml = String::new();
    for name in sites {
        write!(
            sites_toml,
            r#"
[[sites]]
name = "{name}"
repo_url = "https://example.com/{name}.git"
branch = "main"
"#
        )
        .unwrap();
    }

    let config_path = dir.join("witryna.toml");
    let config = format!(
        r#"listen_address = "127.0.0.1:0"
container_runtime = "podman"
base_dir = "{base_dir}"
log_dir = "{log_dir}"
log_level = "info"
max_builds_to_keep = {max_builds}
{sites_toml}"#,
        base_dir = base_dir.display(),
        log_dir = log_dir.display(),
    );
    tokio::fs::write(&config_path, config).await.unwrap();
    (config_path, base_dir, log_dir)
}

async fn create_fake_builds(
    base_dir: &std::path::Path,
    log_dir: &std::path::Path,
    site: &str,
    timestamps: &[&str],
) {
    let builds_dir = base_dir.join("builds").join(site);
    let site_log_dir = log_dir.join(site);
    tokio::fs::create_dir_all(&builds_dir).await.unwrap();
    tokio::fs::create_dir_all(&site_log_dir).await.unwrap();

    for ts in timestamps {
        tokio::fs::create_dir_all(builds_dir.join(ts))
            .await
            .unwrap();
        tokio::fs::write(site_log_dir.join(format!("{ts}.log")), "build log")
            .await
            .unwrap();
    }
}

// ---------------------------------------------------------------------------
// Tier 1: no container runtime / git needed
// ---------------------------------------------------------------------------

#[tokio::test]
async fn cli_cleanup_unknown_site() {
    let tempdir = TempDir::new().unwrap();
    let (config_path, _, _) = write_cleanup_config(tempdir.path(), &["real-site"], 5).await;

    let output = Command::new(witryna_bin())
        .args([
            "cleanup",
            "--config",
            config_path.to_str().unwrap(),
            "nonexistent",
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .await
        .unwrap();

    assert!(!output.status.success(), "should exit non-zero");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("not found"),
        "should mention 'not found', got: {stderr}"
    );
}

#[tokio::test]
async fn cli_cleanup_keep_zero_refused() {
    let tempdir = TempDir::new().unwrap();
    let (config_path, _, _) = write_cleanup_config(tempdir.path(), &["my-site"], 5).await;

    let output = Command::new(witryna_bin())
        .args([
            "cleanup",
            "--config",
            config_path.to_str().unwrap(),
            "--keep",
            "0",
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .await
        .unwrap();

    assert!(!output.status.success(), "should exit non-zero");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("--keep 0 would delete all builds"),
        "should refuse --keep 0, got: {stderr}"
    );
}

#[tokio::test]
async fn cli_cleanup_disabled_when_max_zero() {
    let tempdir = TempDir::new().unwrap();
    let (config_path, _, _) = write_cleanup_config(tempdir.path(), &["my-site"], 0).await;

    let output = Command::new(witryna_bin())
        .args(["cleanup", "--config", config_path.to_str().unwrap()])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .await
        .unwrap();

    assert!(
        output.status.success(),
        "should exit 0, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cleanup disabled"),
        "should say 'cleanup disabled', got: {stderr}"
    );
}

#[tokio::test]
async fn cli_cleanup_removes_old_builds() {
    let tempdir = TempDir::new().unwrap();
    let (config_path, base_dir, log_dir) =
        write_cleanup_config(tempdir.path(), &["site-a", "site-b"], 5).await;

    let timestamps = &[
        "20260126-100000-000001",
        "20260126-100000-000002",
        "20260126-100000-000003",
        "20260126-100000-000004",
    ];

    create_fake_builds(&base_dir, &log_dir, "site-a", timestamps).await;
    create_fake_builds(&base_dir, &log_dir, "site-b", timestamps).await;

    let output = Command::new(witryna_bin())
        .args([
            "cleanup",
            "--config",
            config_path.to_str().unwrap(),
            "--keep",
            "2",
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .await
        .unwrap();

    assert!(
        output.status.success(),
        "should exit 0, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("site-a: removed"),
        "should report site-a removals, got: {stderr}"
    );
    assert!(
        stderr.contains("site-b: removed"),
        "should report site-b removals, got: {stderr}"
    );
    assert!(
        stderr.contains("total:"),
        "should print total summary for multi-site, got: {stderr}"
    );

    // Verify filesystem: oldest 2 gone, newest 2 remain for each site
    for site in &["site-a", "site-b"] {
        let builds = base_dir.join("builds").join(site);
        assert!(!builds.join("20260126-100000-000001").exists());
        assert!(!builds.join("20260126-100000-000002").exists());
        assert!(builds.join("20260126-100000-000003").exists());
        assert!(builds.join("20260126-100000-000004").exists());

        let logs = log_dir.join(site);
        assert!(!logs.join("20260126-100000-000001.log").exists());
        assert!(!logs.join("20260126-100000-000002.log").exists());
        assert!(logs.join("20260126-100000-000003.log").exists());
        assert!(logs.join("20260126-100000-000004.log").exists());
    }
}

#[tokio::test]
async fn cli_cleanup_single_site_filter() {
    let tempdir = TempDir::new().unwrap();
    let (config_path, base_dir, log_dir) =
        write_cleanup_config(tempdir.path(), &["site-a", "site-b"], 5).await;

    let timestamps = &[
        "20260126-100000-000001",
        "20260126-100000-000002",
        "20260126-100000-000003",
        "20260126-100000-000004",
    ];

    create_fake_builds(&base_dir, &log_dir, "site-a", timestamps).await;
    create_fake_builds(&base_dir, &log_dir, "site-b", timestamps).await;

    let output = Command::new(witryna_bin())
        .args([
            "cleanup",
            "--config",
            config_path.to_str().unwrap(),
            "--keep",
            "2",
            "site-a",
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .await
        .unwrap();

    assert!(
        output.status.success(),
        "should exit 0, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stderr = String::from_utf8_lossy(&output.stderr);

    // site-a should be cleaned
    assert!(
        stderr.contains("site-a: removed"),
        "should report site-a removals, got: {stderr}"
    );

    // site-b should be untouched — not mentioned in output
    assert!(
        !stderr.contains("site-b"),
        "site-b should not appear in output, got: {stderr}"
    );

    // No total line for single-site cleanup
    assert!(
        !stderr.contains("total:"),
        "should not print total for single site, got: {stderr}"
    );

    // Verify site-b filesystem is untouched
    let site_b_builds = base_dir.join("builds").join("site-b");
    assert!(site_b_builds.join("20260126-100000-000001").exists());
    assert!(site_b_builds.join("20260126-100000-000004").exists());
}

#[tokio::test]
async fn cli_cleanup_keep_overrides_config() {
    let tempdir = TempDir::new().unwrap();
    // Config says max_builds_to_keep = 1
    let (config_path, base_dir, log_dir) =
        write_cleanup_config(tempdir.path(), &["my-site"], 1).await;

    let timestamps = &[
        "20260126-100000-000001",
        "20260126-100000-000002",
        "20260126-100000-000003",
        "20260126-100000-000004",
    ];

    create_fake_builds(&base_dir, &log_dir, "my-site", timestamps).await;

    // --keep 3 should override config's max_builds_to_keep=1
    let output = Command::new(witryna_bin())
        .args([
            "cleanup",
            "--config",
            config_path.to_str().unwrap(),
            "--keep",
            "3",
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .await
        .unwrap();

    assert!(
        output.status.success(),
        "should exit 0, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // With --keep 3 and 4 builds: only 1 should be removed
    let builds = base_dir.join("builds").join("my-site");
    assert!(
        !builds.join("20260126-100000-000001").exists(),
        "oldest should be removed"
    );
    assert!(
        builds.join("20260126-100000-000002").exists(),
        "second should remain"
    );
    assert!(
        builds.join("20260126-100000-000003").exists(),
        "third should remain"
    );
    assert!(
        builds.join("20260126-100000-000004").exists(),
        "newest should remain"
    );
}