Skip to content

Commit aee538b

Browse files
committed
Log the server / cluster logfile on error.
This allows us to find errors that aren't visible in stdout/stderr.
1 parent f77974e commit aee538b

File tree

2 files changed

+39
-48
lines changed

2 files changed

+39
-48
lines changed

redis/tests/support/cluster.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ impl RedisCluster {
204204
let mut process = cmd.spawn().unwrap();
205205
sleep(Duration::from_millis(50));
206206

207+
let log_file_index = cmd.get_args().position(|arg|arg == "--logfile").unwrap() + 1;
208+
let log_file_path = cmd.get_args().nth(log_file_index).unwrap();
207209
match process.try_wait() {
208210
Ok(Some(status)) => {
209211
let stdout = process.stdout.map_or(String::new(), |mut out|{
@@ -216,8 +218,10 @@ impl RedisCluster {
216218
out.read_to_string(&mut str).unwrap();
217219
str
218220
});
221+
222+
let log_file_contents = std::fs::read_to_string(log_file_path).unwrap();
219223
let err =
220-
format!("redis server creation failed with status {status:?}.\nstdout: `{stdout}`.\nstderr: `{stderr}`");
224+
format!("redis server creation failed with status {status:?}.\nstdout: `{stdout}`.\nstderr: `{stderr}`\nlog file: {log_file_contents}");
221225
if cur_attempts == max_attempts {
222226
panic!("{err}");
223227
}
@@ -230,7 +234,8 @@ impl RedisCluster {
230234
let mut cur_attempts = 0;
231235
loop {
232236
if cur_attempts == max_attempts {
233-
panic!("redis server creation failed: Port {port} closed")
237+
let log_file_contents = std::fs::read_to_string(log_file_path).unwrap();
238+
panic!("redis server creation failed: Port {port} closed. {log_file_contents}")
234239
}
235240
if port_in_use(&addr) {
236241
return process;

redis/tests/support/mod.rs

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub enum Module {
142142
pub struct RedisServer {
143143
pub process: process::Child,
144144
tempdir: tempfile::TempDir,
145+
log_file: PathBuf,
145146
addr: redis::ConnectionAddr,
146147
pub(crate) tls_paths: Option<TlsFilePaths>,
147148
}
@@ -174,6 +175,10 @@ impl RedisServer {
174175
RedisServer::with_modules(&[], true)
175176
}
176177

178+
pub fn log_file_contents(&self) -> String {
179+
std::fs::read_to_string(self.log_file.clone()).unwrap()
180+
}
181+
177182
pub fn get_addr(port: u16) -> ConnectionAddr {
178183
let server_type = ServerType::get_intended();
179184
match server_type {
@@ -277,7 +282,8 @@ impl RedisServer {
277282
.prefix("redis")
278283
.tempdir()
279284
.expect("failed to create tempdir");
280-
redis_cmd.arg("--logfile").arg(Self::log_file(&tempdir));
285+
let log_file = Self::log_file(&tempdir);
286+
redis_cmd.arg("--logfile").arg(log_file.clone());
281287
match addr {
282288
redis::ConnectionAddr::Tcp(ref bind, server_port) => {
283289
redis_cmd
@@ -288,6 +294,7 @@ impl RedisServer {
288294

289295
RedisServer {
290296
process: spawner(&mut redis_cmd),
297+
log_file,
291298
tempdir,
292299
addr,
293300
tls_paths: None,
@@ -327,6 +334,7 @@ impl RedisServer {
327334

328335
RedisServer {
329336
process: spawner(&mut redis_cmd),
337+
log_file,
330338
tempdir,
331339
addr,
332340
tls_paths: Some(tls_paths),
@@ -340,6 +348,7 @@ impl RedisServer {
340348
.arg(path);
341349
RedisServer {
342350
process: spawner(&mut redis_cmd),
351+
log_file,
343352
tempdir,
344353
addr,
345354
tls_paths: None,
@@ -417,15 +426,27 @@ impl TestContext {
417426
}
418427

419428
pub fn with_tls(tls_files: TlsFilePaths, mtls_enabled: bool) -> TestContext {
429+
Self::with_modules_and_tls(&[], mtls_enabled, Some(tls_files))
430+
}
431+
432+
pub fn with_modules(modules: &[Module], mtls_enabled: bool) -> TestContext {
433+
Self::with_modules_and_tls(modules, mtls_enabled, None)
434+
}
435+
436+
fn with_modules_and_tls(
437+
modules: &[Module],
438+
mtls_enabled: bool,
439+
tls_files: Option<TlsFilePaths>,
440+
) -> Self {
420441
let redis_port = get_random_available_port();
421442
let addr = RedisServer::get_addr(redis_port);
422443

423444
let server = RedisServer::new_with_addr_tls_modules_and_spawner(
424445
addr,
425446
None,
426-
Some(tls_files),
447+
tls_files,
427448
mtls_enabled,
428-
&[],
449+
modules,
429450
|cmd| {
430451
cmd.spawn()
431452
.unwrap_or_else(|err| panic!("Failed to run {cmd:?}: {err}"))
@@ -449,51 +470,16 @@ impl TestContext {
449470
sleep(millisecond);
450471
retries += 1;
451472
if retries > 100000 {
452-
panic!("Tried to connect too many times, last error: {err}");
453-
}
454-
} else {
455-
panic!("Could not connect: {err}");
456-
}
457-
}
458-
Ok(x) => {
459-
con = x;
460-
break;
461-
}
462-
}
463-
}
464-
redis::cmd("FLUSHDB").execute(&mut con);
465-
466-
TestContext {
467-
server,
468-
client,
469-
protocol: use_protocol(),
470-
}
471-
}
472-
473-
pub fn with_modules(modules: &[Module], mtls_enabled: bool) -> TestContext {
474-
let server = RedisServer::with_modules(modules, mtls_enabled);
475-
476-
#[cfg(feature = "tls-rustls")]
477-
let client =
478-
build_single_client(server.connection_info(), &server.tls_paths, mtls_enabled).unwrap();
479-
#[cfg(not(feature = "tls-rustls"))]
480-
let client = redis::Client::open(server.connection_info()).unwrap();
481-
482-
let mut con;
483-
484-
let millisecond = Duration::from_millis(1);
485-
let mut retries = 0;
486-
loop {
487-
match client.get_connection() {
488-
Err(err) => {
489-
if err.is_connection_refusal() {
490-
sleep(millisecond);
491-
retries += 1;
492-
if retries > 100000 {
493-
panic!("Tried to connect too many times, last error: {err}");
473+
panic!(
474+
"Tried to connect too many times, last error: {err}, logfile: {}",
475+
server.log_file_contents()
476+
);
494477
}
495478
} else {
496-
panic!("Could not connect: {err}");
479+
panic!(
480+
"Could not connect: {err}, logfile: {}",
481+
server.log_file_contents()
482+
);
497483
}
498484
}
499485
Ok(x) => {

0 commit comments

Comments
 (0)