run local build in docker for regression test

This commit is contained in:
Max Känner 2024-10-06 21:34:51 +02:00
parent e00eefb990
commit d8452daddf

View File

@ -2,7 +2,7 @@ use std::{
env, env,
net::TcpStream, net::TcpStream,
path::{Path, PathBuf}, path::{Path, PathBuf},
process::{Child, Command}, process::{Child, Command, Stdio},
thread::sleep, thread::sleep,
time::Duration, time::Duration,
}; };
@ -143,8 +143,8 @@ fn vs_production() -> Result<(), DynError> {
} }
fn regression() -> Result<(), DynError> { fn regression() -> Result<(), DynError> {
let mut snake = run_snake(8000, Some("error"))?; let mut snake = run_local_docker(53511)?;
let mut prod = match run_production(8001) { let mut prod = match run_production(64576) {
Ok(prod) => prod, Ok(prod) => prod,
err => { err => {
snake.kill()?; snake.kill()?;
@ -165,7 +165,7 @@ fn regression() -> Result<(), DynError> {
} }
fn try_regression() -> Result<(usize, usize), DynError> { fn try_regression() -> Result<(usize, usize), DynError> {
const GAMES: usize = 1000; const GAMES: usize = 100;
// limit the parallelism // limit the parallelism
rayon::ThreadPoolBuilder::new() rayon::ThreadPoolBuilder::new()
.num_threads(std::thread::available_parallelism()?.get() / 3) .num_threads(std::thread::available_parallelism()?.get() / 3)
@ -184,16 +184,14 @@ fn try_regression() -> Result<(usize, usize), DynError> {
"11", "11",
"-H", "-H",
"11", "11",
"--timeout",
"50",
"--name", "--name",
"local", "local",
"--url", "--url",
"http://localhost:8000", "http://localhost:53511",
"--name", "--name",
"production", "production",
"--url", "--url",
"http://localhost:8001", "http://localhost:64576",
"-g", "-g",
"duel", "duel",
]) ])
@ -244,13 +242,51 @@ fn run_snake(port: u16, log: Option<&str>) -> Result<Child, DynError> {
} }
} }
fn run_local_docker(port: u16) -> Result<Child, DynError> {
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<Child, DynError> { fn run_production(port: u16) -> Result<Child, DynError> {
let mut snake = Command::new("docker") let mut snake = Command::new("docker")
.args([ .args([
"run", "run",
"--name", "--name",
"battlesnake-regression", "battlesnake-regression-production",
"--init",
"--replace", "--replace",
"--env", "--env",
"RUST_LOG=error", "RUST_LOG=error",
@ -261,6 +297,9 @@ fn run_production(port: u16) -> Result<Child, DynError> {
"--rm", "--rm",
"docker.mkaenner.de/snake:latest", "docker.mkaenner.de/snake:latest",
]) ])
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.stdin(Stdio::inherit())
.spawn()?; .spawn()?;
loop { loop {
if let Some(status) = snake.try_wait()? { if let Some(status) = snake.try_wait()? {