oxdock-process 0.10.0-alpha

Process orchestration for OxDock environments.
Documentation
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::collections::{HashMap, VecDeque};
#[allow(clippy::disallowed_types)]
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

use anyhow::{Result, anyhow, bail};
use oxdock_sys_test_utils::exit_status_from_code;

use crate::{
    BackgroundHandle, CommandContext, CommandMode, CommandOptions, CommandResult, CommandStderr,
    CommandStdout, ProcessManager, SharedInput,
};

/// Which stderr configuration an invocation carried. A discriminant rather
/// than the raw `CommandStderr` so the captured calls stay `PartialEq`/`Eq`
/// (stream handles are not comparable).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MockStreamMode {
    Inherit,
    Stream,
}

fn stderr_mode(stderr: &CommandStderr) -> MockStreamMode {
    match stderr {
        CommandStderr::Inherit => MockStreamMode::Inherit,
        CommandStderr::Stream(_) => MockStreamMode::Stream,
    }
}

/// Captured invocation for a foreground run.
#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(clippy::disallowed_types)]
pub struct MockRunCall {
    pub script: String,
    pub cwd: PathBuf,
    pub envs: HashMap<String, String>,
    pub cargo_target_dir: PathBuf,
    pub stdin_provided: bool,
    pub stdin: Option<Vec<u8>>,
    pub stderr_mode: MockStreamMode,
}

/// Captured invocation for a background spawn.
#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(clippy::disallowed_types)]
pub struct MockSpawnCall {
    pub script: String,
    pub cwd: PathBuf,
    pub envs: HashMap<String, String>,
    pub cargo_target_dir: PathBuf,
    pub stdin_provided: bool,
    pub stdin: Option<Vec<u8>>,
    pub stderr_mode: MockStreamMode,
}

#[derive(Clone, Default)]
pub struct MockProcessManager {
    runs: Arc<Mutex<Vec<MockRunCall>>>,
    spawns: Arc<Mutex<Vec<MockSpawnCall>>>,
    killed: Arc<Mutex<Vec<String>>>,
    plans: Arc<Mutex<VecDeque<BgPlan>>>,
}

impl MockProcessManager {
    pub fn recorded_runs(&self) -> Vec<MockRunCall> {
        self.runs.lock().expect("mock state poisoned").clone()
    }

    pub fn spawn_log(&self) -> Vec<MockSpawnCall> {
        self.spawns.lock().expect("mock state poisoned").clone()
    }

    pub fn killed(&self) -> Vec<String> {
        self.killed.lock().expect("mock state poisoned").clone()
    }

    pub fn push_bg_plan(&self, ready_after: usize, status: std::process::ExitStatus) {
        self.plans
            .lock()
            .expect("mock state poisoned")
            .push_back(BgPlan {
                ready_after,
                status,
            });
    }
}

impl ProcessManager for MockProcessManager {
    type Handle = MockHandle;

    fn run_command(
        &mut self,
        ctx: &CommandContext,
        script: &str,
        options: CommandOptions,
    ) -> Result<CommandResult<Self::Handle>> {
        let CommandOptions {
            mode,
            stdin,
            stdout,
            stderr,
        } = options;
        let stdin_provided = stdin.is_some();
        let captured_stdin = capture_stdin(stdin)?;
        let recorded_stderr = stderr_mode(&stderr);

        match mode {
            CommandMode::Foreground => {
                self.runs
                    .lock()
                    .expect("mock state poisoned")
                    .push(MockRunCall {
                        script: script.to_string(),
                        cwd: ctx.cwd().to_path_buf(),
                        envs: (**ctx.envs()).clone(),
                        cargo_target_dir: ctx.cargo_target_dir().to_path_buf(),
                        stdin_provided,
                        stdin: captured_stdin.clone(),
                        stderr_mode: recorded_stderr,
                    });
                match stdout {
                    CommandStdout::Capture => Ok(CommandResult::Captured(Vec::new())),
                    CommandStdout::Stream(_) | CommandStdout::Inherit => {
                        Ok(CommandResult::Completed)
                    }
                }
            }
            CommandMode::Background => {
                if matches!(stdout, CommandStdout::Capture) {
                    bail!("cannot capture stdout for background command");
                }
                self.spawns
                    .lock()
                    .expect("mock state poisoned")
                    .push(MockSpawnCall {
                        script: script.to_string(),
                        cwd: ctx.cwd().to_path_buf(),
                        envs: (**ctx.envs()).clone(),
                        cargo_target_dir: ctx.cargo_target_dir().to_path_buf(),
                        stdin_provided,
                        stdin: captured_stdin.clone(),
                        stderr_mode: recorded_stderr,
                    });
                let plan = self
                    .plans
                    .lock()
                    .expect("mock state poisoned")
                    .pop_front()
                    .unwrap_or_else(BgPlan::success);
                Ok(CommandResult::Background(MockHandle {
                    script: script.to_string(),
                    remaining: plan.ready_after,
                    status: plan.status,
                    killed: Arc::clone(&self.killed),
                    reaped: false,
                }))
            }
        }
    }
}

struct BgPlan {
    ready_after: usize,
    status: std::process::ExitStatus,
}

impl BgPlan {
    fn success() -> Self {
        Self {
            ready_after: 0,
            status: exit_status_from_code(0),
        }
    }
}

#[derive(Clone)]
pub struct MockHandle {
    script: String,
    remaining: usize,
    status: std::process::ExitStatus,
    killed: Arc<Mutex<Vec<String>>>,
    reaped: bool,
}

impl BackgroundHandle for MockHandle {
    fn try_wait(&mut self) -> Result<Option<std::process::ExitStatus>> {
        if self.remaining == 0 {
            self.reaped = true;
            Ok(Some(self.status))
        } else {
            self.remaining -= 1;
            Ok(None)
        }
    }

    fn kill(&mut self) -> Result<()> {
        self.reaped = true;
        self.killed
            .lock()
            .expect("mock state poisoned")
            .push(self.script.clone());
        Ok(())
    }

    fn wait(&mut self) -> Result<std::process::ExitStatus> {
        self.reaped = true;
        Ok(self.status)
    }
}

impl Drop for MockHandle {
    fn drop(&mut self) {
        // Only naturally-completed-without-teardown handles reach here
        // un-reaped; log them as killed so `pm.killed()` assertions observe
        // Drop-driven teardown exactly like explicit kills.
        if !self.reaped {
            self.killed
                .lock()
                .expect("mock state poisoned")
                .push(self.script.clone());
        }
    }
}

fn capture_stdin(stdin: Option<SharedInput>) -> Result<Option<Vec<u8>>> {
    if let Some(reader) = stdin {
        let mut guard = reader.lock().map_err(|_| anyhow!("failed to lock stdin"))?;
        let mut buf = Vec::new();
        std::io::copy(&mut *guard, &mut buf)?;
        Ok(Some(buf))
    } else {
        Ok(None)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use oxdock_fs::{GuardedPath, PolicyPath};
    use std::collections::HashMap;

    fn test_ctx() -> (GuardedPath, CommandContext) {
        let temp = GuardedPath::tempdir().expect("tempdir");
        let guard = temp.as_guarded_path().clone();
        let cwd: PolicyPath = guard.clone().into();
        let ctx = CommandContext::from_map(&cwd, &HashMap::new(), &guard, &guard, &guard);
        (guard, ctx)
    }

    fn ok_status() -> std::process::ExitStatus {
        exit_status_from_code(0)
    }

    fn failing_status() -> std::process::ExitStatus {
        exit_status_from_code(3)
    }

    #[test]
    fn foreground_run_records_invocation_and_stderr_mode() {
        let (_root, ctx) = test_ctx();
        let mut pm = MockProcessManager::default();
        let input: SharedInput =
            std::sync::Arc::new(std::sync::Mutex::new(std::io::Cursor::new(b"abc".to_vec())));
        let stderr_sink: crate::SharedOutput =
            std::sync::Arc::new(std::sync::Mutex::new(Vec::<u8>::new()));
        let options = CommandOptions {
            stdin: Some(input),
            stderr: CommandStderr::Stream(stderr_sink),
            ..Default::default()
        };

        pm.run_command(&ctx, "build-all", options).expect("run");

        let runs = pm.recorded_runs();
        assert_eq!(runs.len(), 1);
        let call = &runs[0];
        assert_eq!(call.script, "build-all");
        assert_eq!(call.cwd, ctx.cwd().to_path_buf());
        assert!(call.stdin_provided);
        assert_eq!(call.stdin.as_deref(), Some(b"abc".as_slice()));
        assert_eq!(call.stderr_mode, MockStreamMode::Stream);
    }

    #[test]
    fn background_spawn_records_stderr_inherit_mode_by_default() {
        let (_root, ctx) = test_ctx();
        let mut pm = MockProcessManager::default();
        let options = CommandOptions {
            mode: CommandMode::Background,
            ..Default::default()
        };

        match pm.run_command(&ctx, "serve", options).expect("spawn") {
            CommandResult::Background(_) => {}
            _ => panic!("expected background handle"),
        }

        let spawns = pm.spawn_log();
        assert_eq!(spawns.len(), 1);
        assert_eq!(spawns[0].script, "serve");
        assert!(!spawns[0].stdin_provided);
        assert_eq!(spawns[0].stderr_mode, MockStreamMode::Inherit);
    }

    #[test]
    fn background_capture_stdout_bails() {
        let (_root, ctx) = test_ctx();
        let mut pm = MockProcessManager::default();
        let options = CommandOptions {
            mode: CommandMode::Background,
            stdout: CommandStdout::Capture,
            ..Default::default()
        };
        match pm.run_command(&ctx, "x", options) {
            Err(err) => assert!(err.to_string().contains("cannot capture stdout")),
            _ => panic!("expected bail for background capture"),
        }
    }

    #[test]
    fn try_wait_counts_down_until_planned_readiness() {
        let (_root, ctx) = test_ctx();
        let mut pm = MockProcessManager::default();
        pm.push_bg_plan(2, failing_status());
        let options = CommandOptions {
            mode: CommandMode::Background,
            ..Default::default()
        };
        let mut handle = match pm.run_command(&ctx, "job", options).expect("spawn") {
            CommandResult::Background(handle) => handle,
            _ => panic!("expected background handle"),
        };

        assert!(handle.try_wait().expect("poll 1").is_none());
        assert!(handle.try_wait().expect("poll 2").is_none());
        assert_eq!(handle.try_wait().expect("poll 3"), Some(failing_status()));
    }

    #[test]
    fn wait_returns_planned_status_even_before_countdown_finishes() {
        let (_root, ctx) = test_ctx();
        let mut pm = MockProcessManager::default();
        // usize::MAX models "never becomes ready via try_wait" while `wait`
        // still reports the planned status (executor teardown semantics).
        pm.push_bg_plan(usize::MAX, failing_status());
        let options = CommandOptions {
            mode: CommandMode::Background,
            ..Default::default()
        };
        let mut handle = match pm.run_command(&ctx, "stuck", options).expect("spawn") {
            CommandResult::Background(handle) => handle,
            _ => panic!("expected background handle"),
        };
        assert!(handle.try_wait().expect("poll").is_none());
        assert_eq!(handle.wait().expect("wait"), failing_status());
    }

    #[test]
    fn missing_plan_defaults_to_immediate_success() {
        let (_root, ctx) = test_ctx();
        let mut pm = MockProcessManager::default();
        let options = CommandOptions {
            mode: CommandMode::Background,
            ..Default::default()
        };
        let mut handle = match pm.run_command(&ctx, "quick", options).expect("spawn") {
            CommandResult::Background(handle) => handle,
            _ => panic!("expected background handle"),
        };
        assert_eq!(handle.try_wait().expect("poll"), Some(ok_status()));
    }

    #[test]
    fn kill_logs_scripts_in_kill_order() {
        let (_root, ctx) = test_ctx();
        let mut pm = MockProcessManager::default();
        let options = CommandOptions {
            mode: CommandMode::Background,
            ..Default::default()
        };
        let mut a = match pm.run_command(&ctx, "a", options.clone()).expect("spawn") {
            CommandResult::Background(handle) => handle,
            _ => panic!("expected background handle"),
        };
        let mut b = match pm.run_command(&ctx, "b", options).expect("spawn") {
            CommandResult::Background(handle) => handle,
            _ => panic!("expected background handle"),
        };

        b.kill().expect("kill b");
        a.kill().expect("kill a");

        assert_eq!(pm.killed(), vec!["b".to_string(), "a".to_string()]);
    }

    #[test]
    fn capture_stdin_reads_stream_or_reports_none() {
        let none = capture_stdin(None).expect("none case");
        assert_eq!(none, None);

        let input: SharedInput =
            std::sync::Arc::new(std::sync::Mutex::new(std::io::Cursor::new(b"xyz".to_vec())));
        let some = capture_stdin(Some(input)).expect("some case");
        assert_eq!(some, Some(b"xyz".to_vec()));
    }
}