Skip to content

Commit 8e66b2b

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 c010455 commit 8e66b2b

File tree

2 files changed

+37
-47
lines changed

2 files changed

+37
-47
lines changed

redis/tests/support/cluster.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,12 @@ impl RedisCluster {
216216
out.read_to_string(&mut str).unwrap();
217217
str
218218
});
219+
220+
let log_file_index = cmd.get_args().position(|arg|arg == "--logfile").unwrap() + 1;
221+
let log_file_path = cmd.get_args().nth(log_file_index).unwrap();
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
}

redis/tests/support/mod.rs

+32-46
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 {
@@ -270,7 +275,8 @@ impl RedisServer {
270275
.prefix("redis")
271276
.tempdir()
272277
.expect("failed to create tempdir");
273-
redis_cmd.arg("--logfile").arg(Self::log_file(&tempdir));
278+
let log_file = Self::log_file(&tempdir);
279+
redis_cmd.arg("--logfile").arg(log_file.clone());
274280
match addr {
275281
redis::ConnectionAddr::Tcp(ref bind, server_port) => {
276282
redis_cmd
@@ -281,6 +287,7 @@ impl RedisServer {
281287

282288
RedisServer {
283289
process: spawner(&mut redis_cmd),
290+
log_file,
284291
tempdir,
285292
addr,
286293
tls_paths: None,
@@ -320,6 +327,7 @@ impl RedisServer {
320327

321328
RedisServer {
322329
process: spawner(&mut redis_cmd),
330+
log_file,
323331
tempdir,
324332
addr,
325333
tls_paths: Some(tls_paths),
@@ -333,6 +341,7 @@ impl RedisServer {
333341
.arg(path);
334342
RedisServer {
335343
process: spawner(&mut redis_cmd),
344+
log_file,
336345
tempdir,
337346
addr,
338347
tls_paths: None,
@@ -410,15 +419,27 @@ impl TestContext {
410419
}
411420

412421
pub fn with_tls(tls_files: TlsFilePaths, mtls_enabled: bool) -> TestContext {
422+
Self::with_modules_and_tls(&[], mtls_enabled, Some(tls_files))
423+
}
424+
425+
pub fn with_modules(modules: &[Module], mtls_enabled: bool) -> TestContext {
426+
Self::with_modules_and_tls(modules, mtls_enabled, None)
427+
}
428+
429+
fn with_modules_and_tls(
430+
modules: &[Module],
431+
mtls_enabled: bool,
432+
tls_files: Option<TlsFilePaths>,
433+
) -> Self {
413434
let redis_port = get_random_available_port();
414435
let addr = RedisServer::get_addr(redis_port);
415436

416437
let server = RedisServer::new_with_addr_tls_modules_and_spawner(
417438
addr,
418439
None,
419-
Some(tls_files),
440+
tls_files,
420441
mtls_enabled,
421-
&[],
442+
modules,
422443
|cmd| {
423444
cmd.spawn()
424445
.unwrap_or_else(|err| panic!("Failed to run {cmd:?}: {err}"))
@@ -442,51 +463,16 @@ impl TestContext {
442463
sleep(millisecond);
443464
retries += 1;
444465
if retries > 100000 {
445-
panic!("Tried to connect too many times, last error: {err}");
446-
}
447-
} else {
448-
panic!("Could not connect: {err}");
449-
}
450-
}
451-
Ok(x) => {
452-
con = x;
453-
break;
454-
}
455-
}
456-
}
457-
redis::cmd("FLUSHDB").execute(&mut con);
458-
459-
TestContext {
460-
server,
461-
client,
462-
protocol: use_protocol(),
463-
}
464-
}
465-
466-
pub fn with_modules(modules: &[Module], mtls_enabled: bool) -> TestContext {
467-
let server = RedisServer::with_modules(modules, mtls_enabled);
468-
469-
#[cfg(feature = "tls-rustls")]
470-
let client =
471-
build_single_client(server.connection_info(), &server.tls_paths, mtls_enabled).unwrap();
472-
#[cfg(not(feature = "tls-rustls"))]
473-
let client = redis::Client::open(server.connection_info()).unwrap();
474-
475-
let mut con;
476-
477-
let millisecond = Duration::from_millis(1);
478-
let mut retries = 0;
479-
loop {
480-
match client.get_connection() {
481-
Err(err) => {
482-
if err.is_connection_refusal() {
483-
sleep(millisecond);
484-
retries += 1;
485-
if retries > 100000 {
486-
panic!("Tried to connect too many times, last error: {err}");
466+
panic!(
467+
"Tried to connect too many times, last error: {err}, logfile: {}",
468+
server.log_file_contents()
469+
);
487470
}
488471
} else {
489-
panic!("Could not connect: {err}");
472+
panic!(
473+
"Could not connect: {err}, logfile: {}",
474+
server.log_file_contents()
475+
);
490476
}
491477
}
492478
Ok(x) => {

0 commit comments

Comments
 (0)