diff --git a/src/servers/dhcp.rs b/src/servers/dhcp.rs index 0c0c401..3461f58 100644 --- a/src/servers/dhcp.rs +++ b/src/servers/dhcp.rs @@ -72,6 +72,8 @@ impl DHCPRunner for Server { #[cfg(test)] mod tests { + use testcontainers::core::{CmdWaitFor, ExecCommand}; + use testcontainers::Image; use testcontainers::{runners::SyncRunner, GenericImage}; use std::env; use std::process::Command; @@ -91,7 +93,7 @@ mod tests { .output() .expect(&format!("Failed to execute command. Check directory {}", cwd)); - println!("{}", String::from_utf8_lossy(&_out.stdout)); + // println!("{}", String::from_utf8_lossy(&_out.stdout)); // }); } @@ -100,16 +102,27 @@ mod tests { build_images(); let client_thread = std::thread::spawn(move || { - let custom_image = GenericImage::new("client_image", "latest"); + let custom_image = GenericImage::new("test_image", "latest"); let container = custom_image.start().unwrap(); - let _ = container.stop(); - let out = String::from_utf8(container.stderr_to_vec().unwrap()).unwrap(); + // exit code, it waits for result + let mut res = container + .exec( + ExecCommand::new(["dhclient", "-4", "-d", "-v", "-p", "6768"]) + .with_cmd_ready_condition(CmdWaitFor::message_on_stderr("bound to")) + .with_cmd_ready_condition(CmdWaitFor::seconds(10)) + ) + .unwrap_or_else(|e| { + panic!("*** Failed to run dhclient. Error:\n{}", e.to_string()); + }); + + // It gets stuck here until the command finishes + let out = String::from_utf8(res.stderr_to_vec().unwrap()).unwrap(); let expected_lines = [ "binding to user-specified port", "DHCPDISCOVER on", - "bound to 172.12.1.101", + "bound to", ]; for expected in &expected_lines { @@ -119,26 +132,34 @@ mod tests { }); - let server_thread = std::thread::spawn(move || { - let custom_image = GenericImage::new("server_image", "latest"); - let container = custom_image.start().unwrap(); - let _ = container.stop(); + // Run the DHCP server on the main thread + let custom_image = GenericImage::new("test_image", "latest"); + let container = custom_image.start().unwrap(); - let out = String::from_utf8(container.stdout_to_vec().unwrap()).unwrap(); + // exit code, it waits for result + let mut res = container + .exec( + ExecCommand::new(["quick-serve", "--dhcp=6767", "-v", "--bind-ip=172.12.1.4"]) + .with_cmd_ready_condition(CmdWaitFor::message_on_stdout("dhcp_server: offered")) + .with_cmd_ready_condition(CmdWaitFor::seconds(10)) + ) + .unwrap_or_else(|e| { + panic!("*** Failed to run quick-serve. Error:\n{}", e.to_string()); + }); - let expected_lines = [ - "DHCP server started", - "dhcp_server: Request received", - "dhcp_server: offered 172.12.1.101", - ]; + let out = String::from_utf8(res.stdout_to_vec().unwrap()).unwrap(); - for expected in &expected_lines { - assert!(out.contains(expected), - "Expected line not found: {}\nCheck on the complete logs:\n{}", expected, out); - } - }); + let expected_lines = [ + "DHCP server started", + "dhcp_server: Request received", + "dhcp_server: offered", + ]; + + for expected in &expected_lines { + assert!(out.contains(expected), + "Expected line not found: {}\nCheck on the complete logs:\n{}", expected, out); + } client_thread.join().unwrap(); - server_thread.join().unwrap(); } }