Skip to content

Commit de5605c

Browse files
fix: template-redis-enterprise feature gate and example API updates
- Add template-redis-enterprise to feature gate cfg in src/lib.rs and src/template.rs - Update testing_basics.rs to use current ExecCommand and LogsCommand API signatures - Add dynamic port mapping and readiness probe for Postgres in test_fixtures.rs - Add test_sentinel example entry to Cargo.toml Co-authored-by: David Horner <[email protected]>
1 parent d1d09ca commit de5605c

5 files changed

Lines changed: 75 additions & 20 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,8 @@ required-features = ["template-redis-cluster"]
110110
name = "redis_enterprise_template"
111111
path = "examples/redis_enterprise_template.rs"
112112
required-features = ["template-redis-enterprise"]
113+
114+
[[example]]
115+
name = "test_sentinel"
116+
path = "examples/test_sentinel.rs"
117+
required-features = ["template-redis"]

examples/test_fixtures.rs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,71 @@ impl PostgresFixture {
144144
) -> Result<Self, Box<dyn std::error::Error>> {
145145
let container_name = unique_name("postgres");
146146

147-
RunCommand::new("postgres:15-alpine")
147+
// Use dynamic host port mapping to avoid collisions on common ports (e.g., 5432)
148+
let cid = RunCommand::new("postgres:15-alpine")
148149
.name(&container_name)
149150
.env("POSTGRES_DB", database)
150151
.env("POSTGRES_USER", username)
151152
.env("POSTGRES_PASSWORD", password)
152-
.port(port, 5432)
153+
.port_dyn(5432)
153154
.detach()
154155
.remove()
155156
.execute()
156157
.await?;
157158

158-
// Wait for PostgreSQL to be ready
159-
wait_for_port("localhost", port, Duration::from_secs(10)).await?;
159+
// Determine mapped host port so we can probe from the host
160+
let mapped_ports = cid.port_mappings().await?;
161+
let host_port = mapped_ports.first().map(|m| m.host_port).unwrap_or(port);
162+
163+
// Wait for PostgreSQL to be ready on the host-mapped port
164+
wait_for_port("localhost", host_port, Duration::from_secs(10)).await?;
160165

161166
// Additional wait for PostgreSQL initialization
162167
tokio::time::sleep(Duration::from_secs(2)).await;
163168

169+
// Probe PostgreSQL with psql until it accepts connections
170+
let mut ready = false;
171+
for _ in 0..30 {
172+
let probe = ExecCommand::new(
173+
&container_name,
174+
vec![
175+
"psql".to_string(),
176+
"-U".to_string(),
177+
username.to_string(),
178+
"-d".to_string(),
179+
database.to_string(),
180+
"-c".to_string(),
181+
"SELECT 1".to_string(),
182+
],
183+
)
184+
.env("PGPASSWORD", password)
185+
.execute()
186+
.await;
187+
188+
if let Ok(out) = probe {
189+
if out.exit_code == 0 {
190+
ready = true;
191+
break;
192+
}
193+
}
194+
195+
tokio::time::sleep(Duration::from_secs(1)).await;
196+
}
197+
198+
if !ready {
199+
return Err(format!(
200+
"PostgreSQL in container {} did not become ready in time",
201+
container_name
202+
)
203+
.into());
204+
}
205+
164206
Ok(Self {
165207
container_name,
166208
database: database.to_string(),
167209
username: username.to_string(),
168210
password: password.to_string(),
169-
port,
211+
port: host_port,
170212
})
171213
}
172214

examples/testing_basics.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ async fn test_with_auto_cleanup() {
3535
tokio::time::sleep(Duration::from_secs(1)).await;
3636

3737
// Execute test commands
38-
let result = ExecCommand::new(&container_name)
39-
.cmd(vec!["redis-cli", "PING"])
40-
.execute()
41-
.await
42-
.expect("Failed to execute command");
38+
let result = ExecCommand::new(
39+
&container_name,
40+
vec!["redis-cli".to_string(), "PING".to_string()],
41+
)
42+
.execute()
43+
.await
44+
.expect("Failed to execute command");
4345

4446
assert_eq!(result.stdout.trim(), "PONG");
4547

@@ -112,8 +114,8 @@ async fn test_with_error_handling() {
112114
eprintln!("Failed to start container: {}", e);
113115

114116
// Try to get logs for debugging
115-
if let Ok(logs) = LogsCommand::new(&container_name).tail(50).execute().await {
116-
eprintln!("Container logs:\n{}", logs);
117+
if let Ok(logs) = LogsCommand::new(&container_name).tail("50").execute().await {
118+
eprintln!("Container logs:\n{}", logs.stdout);
117119
}
118120

119121
panic!("Test failed due to container startup error");
@@ -194,9 +196,9 @@ async fn test_with_log_inspection() {
194196
RunCommand::new("alpine")
195197
.name(&container_name)
196198
.cmd(vec![
197-
"sh",
198-
"-c",
199-
"echo 'Starting app...'; sleep 1; echo 'App ready!'",
199+
"sh".to_string(),
200+
"-c".to_string(),
201+
"echo 'Starting app...'; sleep 1; echo 'App ready!'".to_string(),
200202
])
201203
.detach()
202204
.execute()
@@ -212,10 +214,10 @@ async fn test_with_log_inspection() {
212214
.await
213215
.expect("Failed to fetch logs");
214216

215-
println!("Container logs:\n{}", logs);
217+
println!("Container logs:\n{}", logs.stdout);
216218

217-
assert!(logs.contains("Starting app..."));
218-
assert!(logs.contains("App ready!"));
219+
assert!(logs.stdout.contains("Starting app..."));
220+
assert!(logs.stdout.contains("App ready!"));
219221

220222
// Cleanup
221223
let _ = RmCommand::new(&container_name).force().execute().await;
@@ -250,7 +252,7 @@ mod tests {
250252
// Start a simple container
251253
let output = RunCommand::new("alpine")
252254
.name(&container_name)
253-
.cmd(vec!["echo", "Hello from test!"])
255+
.cmd(vec!["echo".to_string(), "Hello from test!".to_string()])
254256
.execute()
255257
.await
256258
.expect("Failed to run container");

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ pub mod stream;
406406
feature = "templates",
407407
feature = "template-redis",
408408
feature = "template-redis-cluster",
409+
feature = "template-redis-enterprise",
409410
feature = "template-postgres",
410411
feature = "template-mysql",
411412
feature = "template-mongodb",
@@ -456,6 +457,7 @@ pub mod stream;
456457
feature = "templates",
457458
feature = "template-redis",
458459
feature = "template-redis-cluster",
460+
feature = "template-redis-enterprise",
459461
feature = "template-postgres",
460462
feature = "template-mysql",
461463
feature = "template-mongodb",

src/template.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ use std::collections::HashMap;
1616
use tracing::{debug, error, info, trace, warn};
1717

1818
// Redis templates
19-
#[cfg(any(feature = "template-redis", feature = "template-redis-cluster"))]
19+
#[cfg(any(
20+
feature = "template-redis",
21+
feature = "template-redis-cluster",
22+
feature = "template-redis-enterprise"
23+
))]
2024
pub mod redis;
2125

2226
// Database templates

0 commit comments

Comments
 (0)