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,
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<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> {
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<Child, DynError> {
"--rm",
"docker.mkaenner.de/snake:latest",
])
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.stdin(Stdio::inherit())
.spawn()?;
loop {
if let Some(status) = snake.try_wait()? {