Skip to content

Commit

Permalink
tests: use exec instead of fixed entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofl committed Jan 6, 2025
1 parent 07c8cdd commit 941973a
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/servers/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
// });
}

Expand All @@ -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 {
Expand All @@ -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();
}
}

0 comments on commit 941973a

Please sign in to comment.