summaryrefslogtreecommitdiff
path: root/tests/integration/polling.rs
blob: 4bf8a05c85bc16ba59eba7ba485c076dcaed7e36 (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
use crate::git_helpers::{create_local_repo, push_new_commit};
use crate::harness::TestServer;
use crate::runtime::{skip_without_git, skip_without_runtime};
use serial_test::serial;
use std::time::Duration;
use witryna::config::{BuildOverrides, Config, SiteConfig};

fn polling_site(name: &str, repo_url: &str) -> SiteConfig {
    SiteConfig {
        name: name.to_owned(),
        repo_url: repo_url.to_owned(),
        branch: "main".to_owned(),
        webhook_token: "poll-token".to_owned(),
        webhook_token_file: None,
        build_overrides: BuildOverrides {
            image: Some("alpine:latest".to_owned()),
            command: Some("mkdir -p out && echo '<h1>polled</h1>' > out/index.html".to_owned()),
            public: Some("out".to_owned()),
        },
        poll_interval: Some(Duration::from_secs(2)),
        build_timeout: None,
        cache_dirs: None,
        post_deploy: None,
        env: None,
        container_memory: None,
        container_cpus: None,
        container_pids_limit: None,
        container_network: "none".to_owned(),
        git_depth: None,
        container_workdir: None,
        config_file: None,
    }
}

#[tokio::test]
#[serial]
async fn polling_triggers_build_on_new_commits() {
    skip_without_git!();
    skip_without_runtime!();

    let tempdir = tempfile::tempdir().unwrap();
    let base_dir = tempdir.path().to_path_buf();

    let repo_dir = tempdir.path().join("repos");
    tokio::fs::create_dir_all(&repo_dir).await.unwrap();
    let repo_url = create_local_repo(&repo_dir, "main").await;

    let site = polling_site("poll-site", &repo_url);

    let config = Config {
        listen_address: "127.0.0.1:0".to_owned(),
        container_runtime: crate::harness::test_config(base_dir.clone()).container_runtime,
        base_dir: base_dir.clone(),
        log_dir: base_dir.join("logs"),
        log_level: "debug".to_owned(),
        max_builds_to_keep: 5,
        git_timeout: None,
        sites: vec![site],
    };

    let server = TestServer::start(config).await;

    // Start polling
    server
        .state
        .polling_manager
        .start_polling(server.state.clone())
        .await;

    // Wait for the initial poll cycle to trigger a build
    let builds_dir = base_dir.join("builds/poll-site");
    let max_wait = Duration::from_secs(30);
    let start = std::time::Instant::now();

    loop {
        if start.elapsed() > max_wait {
            // Polling may not have triggered yet — acceptable in CI
            eprintln!("SOFT FAIL: polling did not trigger build within {max_wait:?}");
            return;
        }
        if builds_dir.join("current").is_symlink() {
            break;
        }
        tokio::time::sleep(Duration::from_millis(500)).await;
    }

    let first_target = tokio::fs::read_link(builds_dir.join("current"))
        .await
        .unwrap();

    // Push a new commit
    push_new_commit(&repo_url, &tempdir.path().join("push"), "main").await;

    // Wait for polling to detect and rebuild
    let max_wait = Duration::from_secs(30);
    let start = std::time::Instant::now();

    loop {
        if start.elapsed() > max_wait {
            eprintln!("SOFT FAIL: polling did not detect new commit within {max_wait:?}");
            return;
        }

        if let Ok(target) = tokio::fs::read_link(builds_dir.join("current")).await
            && target != first_target
        {
            // New build detected
            return;
        }

        tokio::time::sleep(Duration::from_millis(500)).await;
    }
}