|
1 | 1 | use std::process::Command; |
| 2 | +use std::time::{Duration, Instant}; |
2 | 3 |
|
3 | 4 | fn initium_bin() -> String { |
4 | 5 | env!("CARGO_BIN_EXE_initium").to_string() |
@@ -239,3 +240,122 @@ fn test_env_var_false_boolean_not_set() { |
239 | 240 | stderr |
240 | 241 | ); |
241 | 242 | } |
| 243 | + |
| 244 | +#[test] |
| 245 | +fn test_sidecar_flag_on_failure_exits_immediately() { |
| 246 | + // --sidecar should NOT keep the process alive when the subcommand fails |
| 247 | + let start = Instant::now(); |
| 248 | + let output = Command::new(initium_bin()) |
| 249 | + .args([ |
| 250 | + "--sidecar", |
| 251 | + "wait-for", |
| 252 | + "--target", |
| 253 | + "tcp://localhost:1", |
| 254 | + "--timeout", |
| 255 | + "1s", |
| 256 | + "--max-attempts", |
| 257 | + "1", |
| 258 | + ]) |
| 259 | + .output() |
| 260 | + .unwrap(); |
| 261 | + let elapsed = start.elapsed(); |
| 262 | + assert!( |
| 263 | + !output.status.success(), |
| 264 | + "expected failure exit code with --sidecar on error" |
| 265 | + ); |
| 266 | + // Should exit quickly (well under 10s), not sleep |
| 267 | + assert!( |
| 268 | + elapsed < Duration::from_secs(10), |
| 269 | + "sidecar should not sleep on failure, took {:?}", |
| 270 | + elapsed |
| 271 | + ); |
| 272 | +} |
| 273 | + |
| 274 | +#[test] |
| 275 | +fn test_sidecar_flag_on_success_sleeps() { |
| 276 | + // --sidecar on a successful command should keep the process alive. |
| 277 | + // We use `exec -- true` which succeeds immediately, then verify the |
| 278 | + // process is still running after a short delay. |
| 279 | + let mut child = Command::new(initium_bin()) |
| 280 | + .args(["--sidecar", "exec", "--", "true"]) |
| 281 | + .stderr(std::process::Stdio::piped()) |
| 282 | + .spawn() |
| 283 | + .unwrap(); |
| 284 | + |
| 285 | + // Wait briefly to give the process time to complete the subcommand |
| 286 | + std::thread::sleep(Duration::from_secs(2)); |
| 287 | + |
| 288 | + // The process should still be running (sidecar sleep) |
| 289 | + let status = child.try_wait().unwrap(); |
| 290 | + assert!( |
| 291 | + status.is_none(), |
| 292 | + "expected sidecar process to still be running, but it exited: {:?}", |
| 293 | + status |
| 294 | + ); |
| 295 | + |
| 296 | + // Clean up: kill the process |
| 297 | + child.kill().unwrap(); |
| 298 | + child.wait().unwrap(); |
| 299 | +} |
| 300 | + |
| 301 | +#[test] |
| 302 | +fn test_sidecar_env_var() { |
| 303 | + // INITIUM_SIDECAR=true should enable sidecar mode via env var |
| 304 | + let mut child = Command::new(initium_bin()) |
| 305 | + .args(["exec", "--", "true"]) |
| 306 | + .env("INITIUM_SIDECAR", "true") |
| 307 | + .stderr(std::process::Stdio::piped()) |
| 308 | + .spawn() |
| 309 | + .unwrap(); |
| 310 | + |
| 311 | + std::thread::sleep(Duration::from_secs(2)); |
| 312 | + |
| 313 | + let status = child.try_wait().unwrap(); |
| 314 | + assert!( |
| 315 | + status.is_none(), |
| 316 | + "expected sidecar process to still be running via env var, but it exited: {:?}", |
| 317 | + status |
| 318 | + ); |
| 319 | + |
| 320 | + child.kill().unwrap(); |
| 321 | + child.wait().unwrap(); |
| 322 | +} |
| 323 | + |
| 324 | +#[test] |
| 325 | +fn test_sidecar_logs_message_on_success() { |
| 326 | + // --sidecar should log "sidecar mode" message on success |
| 327 | + let mut child = Command::new(initium_bin()) |
| 328 | + .args(["--sidecar", "exec", "--", "true"]) |
| 329 | + .stderr(std::process::Stdio::piped()) |
| 330 | + .spawn() |
| 331 | + .unwrap(); |
| 332 | + |
| 333 | + std::thread::sleep(Duration::from_secs(2)); |
| 334 | + |
| 335 | + // Kill and collect stderr |
| 336 | + child.kill().unwrap(); |
| 337 | + let output = child.wait_with_output().unwrap(); |
| 338 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 339 | + assert!( |
| 340 | + stderr.contains("sidecar mode"), |
| 341 | + "expected sidecar mode log message, got: {}", |
| 342 | + stderr |
| 343 | + ); |
| 344 | +} |
| 345 | + |
| 346 | +#[test] |
| 347 | +fn test_without_sidecar_exits_on_success() { |
| 348 | + // Without --sidecar, a successful command should exit immediately |
| 349 | + let start = Instant::now(); |
| 350 | + let output = Command::new(initium_bin()) |
| 351 | + .args(["exec", "--", "true"]) |
| 352 | + .output() |
| 353 | + .unwrap(); |
| 354 | + let elapsed = start.elapsed(); |
| 355 | + assert!(output.status.success(), "exec true should succeed"); |
| 356 | + assert!( |
| 357 | + elapsed < Duration::from_secs(5), |
| 358 | + "without --sidecar, process should exit immediately, took {:?}", |
| 359 | + elapsed |
| 360 | + ); |
| 361 | +} |
0 commit comments