From d8452daddf6e04c49366eb8da31f3cb328a9bd5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=A4nner?= Date: Sun, 6 Oct 2024 21:34:51 +0200 Subject: [PATCH] run local build in docker for regression test --- xtask/src/main.rs | 59 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 88517d1..af7681d 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -2,7 +2,7 @@ use std::{ env, net::TcpStream, path::{Path, PathBuf}, - process::{Child, Command}, + process::{Child, Command, Stdio}, thread::sleep, time::Duration, }; @@ -143,8 +143,8 @@ fn vs_production() -> Result<(), DynError> { } fn regression() -> Result<(), DynError> { - let mut snake = run_snake(8000, Some("error"))?; - let mut prod = match run_production(8001) { + let mut snake = run_local_docker(53511)?; + let mut prod = match run_production(64576) { Ok(prod) => prod, err => { snake.kill()?; @@ -165,7 +165,7 @@ fn regression() -> Result<(), DynError> { } fn try_regression() -> Result<(usize, usize), DynError> { - const GAMES: usize = 1000; + const GAMES: usize = 100; // limit the parallelism rayon::ThreadPoolBuilder::new() .num_threads(std::thread::available_parallelism()?.get() / 3) @@ -184,16 +184,14 @@ fn try_regression() -> Result<(usize, usize), DynError> { "11", "-H", "11", - "--timeout", - "50", "--name", "local", "--url", - "http://localhost:8000", + "http://localhost:53511", "--name", "production", "--url", - "http://localhost:8001", + "http://localhost:64576", "-g", "duel", ]) @@ -244,13 +242,51 @@ fn run_snake(port: u16, log: Option<&str>) -> Result { } } +fn run_local_docker(port: u16) -> Result { + if !Command::new("docker") + .current_dir(project_root()) + .args(["build", ".", "-t", "localhost/local_snake:latest"]) + .status()? + .success() + { + Err("Build of docker image failed")?; + } + let mut snake = Command::new("docker") + .args([ + "run", + "--name", + "battlesnake-regression-local", + "--replace", + "--env", + "RUST_LOG=error", + "--env", + format!("ROCKET_PORT={}", port).as_str(), + "--network=host", + "-i", + "--rm", + "local_snake", + ]) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .stdin(Stdio::inherit()) + .spawn()?; + loop { + if let Some(status) = snake.try_wait()? { + Err(format!("{status}"))?; + } + if TcpStream::connect(("127.0.0.1", port)).is_ok() { + break Ok(snake); + } + sleep(Duration::from_secs(1)); + } +} + fn run_production(port: u16) -> Result { let mut snake = Command::new("docker") .args([ "run", "--name", - "battlesnake-regression", - "--init", + "battlesnake-regression-production", "--replace", "--env", "RUST_LOG=error", @@ -261,6 +297,9 @@ fn run_production(port: u16) -> Result { "--rm", "docker.mkaenner.de/snake:latest", ]) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .stdin(Stdio::inherit()) .spawn()?; loop { if let Some(status) = snake.try_wait()? {