Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: version_from_info from info cmd && change test start_redis_server_with_module add module args #368

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,15 +700,35 @@ impl Context {

pub fn version_from_info(info: RedisValue) -> Result<Version, RedisError> {
if let RedisValue::SimpleString(info_str) = info {
if let Some(ver) = utils::get_regexp_captures(
info_str.as_str(),
r"(?m)\bredis_version:([0-9]+)\.([0-9]+)\.([0-9]+)\b",
) {
return Ok(Version {
major: ver[0].parse::<c_int>().unwrap(),
minor: ver[1].parse::<c_int>().unwrap(),
patch: ver[2].parse::<c_int>().unwrap(),
});
let regex = regex::Regex::new(
r"(?m)\bredis_version:(?<major>[0-9]+)\.(?<minor>[0-9]+)\.(?<patch>[0-9]+)\b",
);

if regex.is_ok() {
let regex = regex.unwrap();
let ss = info_str.replace("\r\n", " ");
let ver_str: Vec<&str> = ss.split(' ').collect();
let mut s = "";
for item in ver_str.iter() {
if item.contains("redis_version:") {
s = item;
break;
}
}
//let s = ver_str.as_slice()[2];
let mut it = regex.captures_iter(s);
match it.next() {
None => {
return Err(RedisError::Str("Error getting redis_version"));
}
Some(caps) => {
return Ok(Version {
major: caps["major"].parse::<c_int>().unwrap(),
minor: caps["minor"].parse::<c_int>().unwrap(),
patch: caps["patch"].parse::<c_int>().unwrap(),
});
}
}
}
}
Err(RedisError::Str("Error getting redis_version"))
Expand Down
66 changes: 38 additions & 28 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod utils;
#[test]
fn test_hello() -> Result<()> {
let port: u16 = 6479;
let _guards = vec![start_redis_server_with_module("hello", port)
let _guards = vec![start_redis_server_with_module("hello", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -35,7 +35,7 @@ fn test_hello() -> Result<()> {
#[test]
fn test_keys_pos() -> Result<()> {
let port: u16 = 6480;
let _guards = vec![start_redis_server_with_module("keys_pos", port)
let _guards = vec![start_redis_server_with_module("keys_pos", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -58,7 +58,7 @@ fn test_keys_pos() -> Result<()> {
#[test]
fn test_helper_version() -> Result<()> {
let port: u16 = 6481;
let _guards = vec![start_redis_server_with_module("test_helper", port)
let _guards = vec![start_redis_server_with_module("test_helper", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -82,7 +82,7 @@ fn test_command_name() -> Result<()> {
use redis_module::RedisValue;

let port: u16 = 6482;
let _guards = vec![start_redis_server_with_module("test_helper", port)
let _guards = vec![start_redis_server_with_module("test_helper", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand Down Expand Up @@ -127,7 +127,7 @@ fn test_helper_info() -> Result<()> {
.into_iter()
.try_for_each(|(module, has_dictionary)| {
let port: u16 = 6483;
let _guards = vec![start_redis_server_with_module(module, port)
let _guards = vec![start_redis_server_with_module(module, port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -152,7 +152,7 @@ fn test_info_handler_multiple_sections() -> Result<()> {

MODULES.into_iter().try_for_each(|module| {
let port: u16 = 6500;
let _guards = vec![start_redis_server_with_module(module, port)
let _guards = vec![start_redis_server_with_module(module, port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -173,7 +173,7 @@ fn test_info_handler_multiple_sections() -> Result<()> {
#[test]
fn test_test_helper_err() -> Result<()> {
let port: u16 = 6484;
let _guards = vec![start_redis_server_with_module("hello", port)
let _guards = vec![start_redis_server_with_module("hello", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -193,7 +193,7 @@ fn test_test_helper_err() -> Result<()> {
#[test]
fn test_string() -> Result<()> {
let port: u16 = 6485;
let _guards = vec![start_redis_server_with_module("string", port)
let _guards = vec![start_redis_server_with_module("string", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -213,7 +213,7 @@ fn test_string() -> Result<()> {
#[test]
fn test_scan() -> Result<()> {
let port: u16 = 6486;
let _guards = vec![start_redis_server_with_module("scan_keys", port)
let _guards = vec![start_redis_server_with_module("scan_keys", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -239,7 +239,7 @@ fn test_scan() -> Result<()> {
#[test]
fn test_stream_reader() -> Result<()> {
let port: u16 = 6487;
let _guards = vec![start_redis_server_with_module("stream", port)
let _guards = vec![start_redis_server_with_module("stream", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand Down Expand Up @@ -279,7 +279,7 @@ fn test_stream_reader() -> Result<()> {
#[test]
fn test_call() -> Result<()> {
let port: u16 = 6488;
let _guards = vec![start_redis_server_with_module("call", port)
let _guards = vec![start_redis_server_with_module("call", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -296,7 +296,7 @@ fn test_call() -> Result<()> {
#[test]
fn test_ctx_flags() -> Result<()> {
let port: u16 = 6489;
let _guards = vec![start_redis_server_with_module("ctx_flags", port)
let _guards = vec![start_redis_server_with_module("ctx_flags", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -311,7 +311,7 @@ fn test_ctx_flags() -> Result<()> {
#[test]
fn test_get_current_user() -> Result<()> {
let port: u16 = 6490;
let _guards = vec![start_redis_server_with_module("acl", port)
let _guards = vec![start_redis_server_with_module("acl", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -326,7 +326,7 @@ fn test_get_current_user() -> Result<()> {
#[test]
fn test_verify_acl_on_user() -> Result<()> {
let port: u16 = 6491;
let _guards = vec![start_redis_server_with_module("acl", port)
let _guards = vec![start_redis_server_with_module("acl", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand Down Expand Up @@ -367,7 +367,7 @@ fn test_verify_acl_on_user() -> Result<()> {
#[test]
fn test_key_space_notifications() -> Result<()> {
let port: u16 = 6492;
let _guards = vec![start_redis_server_with_module("events", port)
let _guards = vec![start_redis_server_with_module("events", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -391,7 +391,7 @@ fn test_key_space_notifications() -> Result<()> {
#[test]
fn test_context_mutex() -> Result<()> {
let port: u16 = 6493;
let _guards = vec![start_redis_server_with_module("threads", port)
let _guards = vec![start_redis_server_with_module("threads", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -413,8 +413,10 @@ fn test_context_mutex() -> Result<()> {
#[test]
fn test_server_event() -> Result<()> {
let port: u16 = 6494;
let _guards = vec![start_redis_server_with_module("server_events", port)
.with_context(|| "failed to start redis server")?];
let _guards = vec![
start_redis_server_with_module("server_events", port, vec![])
.with_context(|| "failed to start redis server")?,
];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;

Expand Down Expand Up @@ -462,8 +464,10 @@ fn test_server_event() -> Result<()> {
#[test]
fn test_configuration() -> Result<()> {
let port: u16 = 6495;
let _guards = vec![start_redis_server_with_module("configuration", port)
.with_context(|| "failed to start redis server")?];
let _guards = vec![
start_redis_server_with_module("configuration", port, vec![])
.with_context(|| "failed to start redis server")?,
];

let config_get = |config: &str| -> Result<String> {
let mut con =
Expand Down Expand Up @@ -535,7 +539,7 @@ fn test_configuration() -> Result<()> {
#[test]
fn test_response() -> Result<()> {
let port: u16 = 6496;
let _guards = vec![start_redis_server_with_module("response", port)
let _guards = vec![start_redis_server_with_module("response", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand Down Expand Up @@ -567,8 +571,10 @@ fn test_response() -> Result<()> {
#[test]
fn test_command_proc_macro() -> Result<()> {
let port: u16 = 6497;
let _guards = vec![start_redis_server_with_module("proc_macro_commands", port)
.with_context(|| "failed to start redis server")?];
let _guards = vec![
start_redis_server_with_module("proc_macro_commands", port, vec![])
.with_context(|| "failed to start redis server")?,
];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;

Expand Down Expand Up @@ -606,8 +612,10 @@ fn test_command_proc_macro() -> Result<()> {
#[test]
fn test_redis_value_derive() -> Result<()> {
let port: u16 = 6498;
let _guards = vec![start_redis_server_with_module("proc_macro_commands", port)
.with_context(|| "failed to start redis server")?];
let _guards = vec![
start_redis_server_with_module("proc_macro_commands", port, vec![])
.with_context(|| "failed to start redis server")?,
];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;

Expand All @@ -630,7 +638,7 @@ fn test_redis_value_derive() -> Result<()> {
#[test]
fn test_call_blocking() -> Result<()> {
let port: u16 = 6499;
let _guards = vec![start_redis_server_with_module("call", port)
let _guards = vec![start_redis_server_with_module("call", port, vec![])
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
Expand All @@ -653,8 +661,10 @@ fn test_call_blocking() -> Result<()> {
#[test]
fn test_open_key_with_flags() -> Result<()> {
let port: u16 = 6501;
let _guards = vec![start_redis_server_with_module("open_key_with_flags", port)
.with_context(|| "failed to start redis server")?];
let _guards = vec![
start_redis_server_with_module("open_key_with_flags", port, vec![])
.with_context(|| "failed to start redis server")?,
];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;

Expand Down
12 changes: 9 additions & 3 deletions tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ impl Drop for ChildGuard {
}
}

pub fn start_redis_server_with_module(module_name: &str, port: u16) -> Result<ChildGuard> {
pub fn start_redis_server_with_module(
module_name: &str,
port: u16,
other_args: Vec<&str>,
) -> Result<ChildGuard> {
let extension = if cfg!(target_os = "macos") {
"dylib"
} else {
Expand Down Expand Up @@ -51,14 +55,16 @@ pub fn start_redis_server_with_module(module_name: &str, port: u16) -> Result<Ch

let module_path = format!("{}", module_path.display());

let args = &[
let port_str = port.to_string();
let mut args = vec![
"--port",
&port.to_string(),
&port_str,
"--loadmodule",
module_path.as_str(),
"--enable-debug-command",
"yes",
];
args.extend(other_args);

let redis_server = Command::new("redis-server")
.args(args)
Expand Down