summaryrefslogtreecommitdiff
path: root/tests/integration/harness.rs
blob: c015fa8417c9568278db34dc2dd1bd6e29f9064b (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use governor::{Quota, RateLimiter};
use std::collections::HashMap;
use std::num::NonZeroU32;
use std::path::PathBuf;
use std::sync::Arc;
use tempfile::TempDir;
use tokio::net::TcpListener;
use tokio::sync::{RwLock, oneshot};
use witryna::build_guard::BuildScheduler;
use witryna::config::{BuildOverrides, Config, SiteConfig};
use witryna::polling::PollingManager;
use witryna::server::AppState;

/// A running test server with its own temp directory and shutdown handle.
pub struct TestServer {
    pub base_url: String,
    pub state: AppState,
    /// Kept alive for RAII cleanup of the config file written during startup.
    #[allow(dead_code)]
    pub tempdir: TempDir,
    shutdown_tx: Option<oneshot::Sender<()>>,
}

impl TestServer {
    /// Start a new test server with the given config.
    /// Binds to `127.0.0.1:0` (OS-assigned port).
    pub async fn start(config: Config) -> Self {
        Self::start_with_rate_limit(config, 1000).await
    }

    /// Start a new test server with a specific rate limit.
    pub async fn start_with_rate_limit(mut config: Config, rate_limit: u32) -> Self {
        let tempdir = TempDir::new().expect("failed to create temp dir");
        let config_path = tempdir.path().join("witryna.toml");

        // Write a minimal config file so SIGHUP reload has something to read
        let config_toml = build_config_toml(&config);
        tokio::fs::write(&config_path, &config_toml)
            .await
            .expect("failed to write test config");

        config
            .resolve_secrets()
            .await
            .expect("failed to resolve secrets");

        let quota = Quota::per_minute(NonZeroU32::new(rate_limit).expect("rate limit must be > 0"));

        let state = AppState {
            config: Arc::new(RwLock::new(config)),
            config_path: Arc::new(config_path),
            build_scheduler: Arc::new(BuildScheduler::new()),
            rate_limiter: Arc::new(RateLimiter::dashmap(quota)),
            polling_manager: Arc::new(PollingManager::new()),
        };

        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("failed to bind to random port");
        let port = listener.local_addr().unwrap().port();
        let base_url = format!("http://127.0.0.1:{port}");

        let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

        let server_state = state.clone();
        tokio::spawn(async move {
            witryna::test_support::run_server(server_state, listener, async {
                let _ = shutdown_rx.await;
            })
            .await
            .expect("server failed");
        });

        Self {
            base_url,
            state,
            tempdir,
            shutdown_tx: Some(shutdown_tx),
        }
    }

    /// Get an async reqwest client.
    pub fn client() -> reqwest::Client {
        reqwest::Client::new()
    }

    /// Build a URL for the given path.
    pub fn url(&self, path: &str) -> String {
        format!("{}/{}", self.base_url, path.trim_start_matches('/'))
    }

    /// Shut down the server gracefully.
    pub fn shutdown(&mut self) {
        if let Some(tx) = self.shutdown_tx.take() {
            let _ = tx.send(());
        }
    }
}

impl Drop for TestServer {
    fn drop(&mut self) {
        self.shutdown();
    }
}

/// Build a default test config pointing to the given base dir.
pub fn test_config(base_dir: PathBuf) -> Config {
    let log_dir = base_dir.join("logs");
    Config {
        listen_address: "127.0.0.1:0".to_owned(),
        container_runtime: "podman".to_owned(),
        base_dir,
        log_dir,
        log_level: "debug".to_owned(),
        rate_limit_per_minute: 10,
        max_builds_to_keep: 5,
        git_timeout: None,
        sites: vec![],
    }
}

/// Build a test config with a single site.
pub fn test_config_with_site(base_dir: PathBuf, site: SiteConfig) -> Config {
    let log_dir = base_dir.join("logs");
    Config {
        listen_address: "127.0.0.1:0".to_owned(),
        container_runtime: detect_container_runtime(),
        base_dir,
        log_dir,
        log_level: "debug".to_owned(),
        rate_limit_per_minute: 10,
        max_builds_to_keep: 5,
        git_timeout: None,
        sites: vec![site],
    }
}

/// Build a test config with multiple sites.
pub fn test_config_with_sites(base_dir: PathBuf, sites: Vec<SiteConfig>) -> Config {
    let log_dir = base_dir.join("logs");
    Config {
        listen_address: "127.0.0.1:0".to_owned(),
        container_runtime: detect_container_runtime(),
        base_dir,
        log_dir,
        log_level: "debug".to_owned(),
        rate_limit_per_minute: 10,
        max_builds_to_keep: 5,
        git_timeout: None,
        sites,
    }
}

/// Builder for test `SiteConfig` instances.
///
/// Replaces `simple_site`, `site_with_overrides`, `site_with_hook`, and
/// `site_with_cache` with a single fluent API.
pub struct SiteBuilder {
    name: String,
    repo_url: String,
    token: String,
    webhook_token_file: Option<PathBuf>,
    image: Option<String>,
    command: Option<String>,
    public: Option<String>,
    cache_dirs: Option<Vec<String>>,
    post_deploy: Option<Vec<String>>,
    env: Option<HashMap<String, String>>,
    container_workdir: Option<String>,
}

impl SiteBuilder {
    pub fn new(name: &str, repo_url: &str, token: &str) -> Self {
        Self {
            name: name.to_owned(),
            repo_url: repo_url.to_owned(),
            token: token.to_owned(),
            webhook_token_file: None,
            image: None,
            command: None,
            public: None,
            cache_dirs: None,
            post_deploy: None,
            env: None,
            container_workdir: None,
        }
    }

    /// Set complete build overrides (image, command, public dir).
    pub fn overrides(mut self, image: &str, command: &str, public: &str) -> Self {
        self.image = Some(image.to_owned());
        self.command = Some(command.to_owned());
        self.public = Some(public.to_owned());
        self
    }

    pub fn webhook_token_file(mut self, path: PathBuf) -> Self {
        self.webhook_token_file = Some(path);
        self
    }

    pub fn post_deploy(mut self, hook: Vec<String>) -> Self {
        self.post_deploy = Some(hook);
        self
    }

    pub fn env(mut self, env_vars: HashMap<String, String>) -> Self {
        self.env = Some(env_vars);
        self
    }

    pub fn cache_dirs(mut self, dirs: Vec<String>) -> Self {
        self.cache_dirs = Some(dirs);
        self
    }

    #[allow(dead_code)]
    pub fn container_workdir(mut self, path: &str) -> Self {
        self.container_workdir = Some(path.to_owned());
        self
    }

    pub fn build(self) -> SiteConfig {
        SiteConfig {
            name: self.name,
            repo_url: self.repo_url,
            branch: "main".to_owned(),
            webhook_token: self.token,
            webhook_token_file: self.webhook_token_file,
            build_overrides: BuildOverrides {
                image: self.image,
                command: self.command,
                public: self.public,
            },
            poll_interval: None,
            build_timeout: None,
            cache_dirs: self.cache_dirs,
            post_deploy: self.post_deploy,
            env: self.env,
            container_memory: None,
            container_cpus: None,
            container_pids_limit: None,
            container_network: "none".to_owned(),
            git_depth: None,
            container_workdir: self.container_workdir,
            config_file: None,
        }
    }
}

/// Start a server with a single pre-configured site for simple tests.
///
/// Uses `my-site` with token `secret-token` — suitable for auth, 404, and basic endpoint tests.
pub async fn server_with_site() -> TestServer {
    let dir = tempfile::tempdir().unwrap().keep();
    let site = SiteBuilder::new("my-site", "https://example.com/repo.git", "secret-token").build();
    TestServer::start(test_config_with_site(dir, site)).await
}

/// Detect the first available container runtime.
fn detect_container_runtime() -> String {
    for runtime in &["podman", "docker"] {
        if std::process::Command::new(runtime)
            .args(["info"])
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
        {
            return (*runtime).to_owned();
        }
    }
    // Fallback — tests that need a runtime will skip themselves
    "podman".to_owned()
}

/// Serialize a Config into a minimal TOML string for writing to disk.
fn build_config_toml(config: &Config) -> String {
    use std::fmt::Write as _;

    let runtime_line = format!("container_runtime = \"{}\"\n", config.container_runtime);

    let mut toml = format!(
        r#"listen_address = "{}"
{}base_dir = "{}"
log_dir = "{}"
log_level = "{}"
rate_limit_per_minute = {}
max_builds_to_keep = {}
"#,
        config.listen_address,
        runtime_line,
        config.base_dir.display(),
        config.log_dir.display(),
        config.log_level,
        config.rate_limit_per_minute,
        config.max_builds_to_keep,
    );

    if let Some(timeout) = config.git_timeout {
        let _ = writeln!(toml, "git_timeout = \"{}s\"", timeout.as_secs());
    }

    for site in &config.sites {
        let _ = writeln!(toml, "\n[[sites]]");
        let _ = writeln!(toml, "name = \"{}\"", site.name);
        let _ = writeln!(toml, "repo_url = \"{}\"", site.repo_url);
        let _ = writeln!(toml, "branch = \"{}\"", site.branch);
        if !site.webhook_token.is_empty() {
            let _ = writeln!(toml, "webhook_token = \"{}\"", site.webhook_token);
        }
        if let Some(path) = &site.webhook_token_file {
            let _ = writeln!(toml, "webhook_token_file = \"{}\"", path.display());
        }

        if let Some(image) = &site.build_overrides.image {
            let _ = writeln!(toml, "image = \"{image}\"");
        }
        if let Some(command) = &site.build_overrides.command {
            let _ = writeln!(toml, "command = \"{command}\"");
        }
        if let Some(public) = &site.build_overrides.public {
            let _ = writeln!(toml, "public = \"{public}\"");
        }
        if let Some(interval) = site.poll_interval {
            let _ = writeln!(toml, "poll_interval = \"{}s\"", interval.as_secs());
        }
        if let Some(timeout) = site.build_timeout {
            let _ = writeln!(toml, "build_timeout = \"{}s\"", timeout.as_secs());
        }
        if let Some(depth) = site.git_depth {
            let _ = writeln!(toml, "git_depth = {depth}");
        }
        if let Some(workdir) = &site.container_workdir {
            let _ = writeln!(toml, "container_workdir = \"{workdir}\"");
        }
        if let Some(dirs) = &site.cache_dirs {
            let quoted: Vec<_> = dirs.iter().map(|d| format!("\"{d}\"")).collect();
            let _ = writeln!(toml, "cache_dirs = [{}]", quoted.join(", "));
        }
        if let Some(hook) = &site.post_deploy {
            let quoted: Vec<_> = hook.iter().map(|a| format!("\"{a}\"")).collect();
            let _ = writeln!(toml, "post_deploy = [{}]", quoted.join(", "));
        }
        if let Some(env_vars) = &site.env {
            let _ = writeln!(toml, "\n[sites.env]");
            for (key, value) in env_vars {
                let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
                let _ = writeln!(toml, "{key} = \"{escaped}\"");
            }
        }
    }

    toml
}