Skip to content

Commit

Permalink
Merge pull request #102 from fermyon/redis
Browse files Browse the repository at this point in the history
Conformance tests for Redis
  • Loading branch information
rylev authored Jun 27, 2024
2 parents 9fcd834 + 1a1f1a6 commit e4172ca
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion conformance-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn run_test(test: conformance_tests::Test) -> Result<(), anyhow::Error> {
conformance_tests::config::Precondition::TcpEcho => {}
conformance_tests::config::Precondition::KeyValueStore(_) => {}
conformance_tests::config::Precondition::Sqlite => {}
conformance_tests::config::Precondition::Redis => {}
}
}
for invocation in test.config.invocations {
Expand All @@ -47,8 +48,8 @@ fn run_test(test: conformance_tests::Test) -> Result<(), anyhow::Error> {
/// When encountering a magic key-value pair, substitute the value with a different value.
fn substitution(key: &str, value: &str) -> Option<String> {
match (key, value) {
("port", "7") => Some(7.to_string()),
("port", "80") => Some(HTTP_PORT.to_string()),
("port", port) => Some(port.to_owned()),
_ => panic!("Unexpected substitution: {} = {}", key, value),
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ fn apply_request_transformations(
let headers_to_add = calculate_default_headers(&request, base, route_match)
.context("could not calculate default headers for request")?
.into_iter()
.flat_map(|(k, v)| {
k.into_iter()
.map(move |s| (s.to_string(), v.clone().into_bytes()))
})
.flat_map(|(k, v)| k.into_iter().map(move |s| (s, v.clone().into_bytes())))
.chain(request.headers().entries());
let headers = Headers::new();
for (key, value) in headers_to_add {
Expand Down
46 changes: 42 additions & 4 deletions crates/spin-test-virt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,49 @@ impl redis::GuestConnection for RedisConnection {
fn execute(
&self,
command: String,
arguments: Vec<redis::RedisParameter>,
mut arguments: Vec<redis::RedisParameter>,
) -> Result<Vec<redis::RedisResult>, redis::Error> {
let _ = (command, arguments);
// TODO: implement this by getting input from user
Err(redis::Error::Other("not yet implemented".into()))
let mut get_binary = || match arguments.pop().ok_or(redis::Error::TypeError)? {
redis::RedisParameter::Int64(_) => Err(redis::Error::TypeError),
redis::RedisParameter::Binary(b) => Ok(b),
};
match command.as_str() {
"incr" => {
let key = get_binary()?;
let key = String::from_utf8(key).map_err(|_| redis::Error::TypeError)?;
self.incr(key).map(|i| vec![redis::RedisResult::Int64(i)])
}
"set" => {
let value = get_binary()?;
let key = get_binary()?;
let key = String::from_utf8(key).map_err(|_| redis::Error::TypeError)?;
self.set(key, value);
Ok(vec![])
}
"append" => {
let value = get_binary()?;
let key = get_binary()?;
let key = String::from_utf8(key).map_err(|_| redis::Error::TypeError)?;
let mut current = self.get(key.clone())?.unwrap_or_default();
current.extend(value);
self.set(key, current);
Ok(vec![])
}
"get" => {
let key = get_binary()?;
let key = String::from_utf8(key).map_err(|_| redis::Error::TypeError)?;
let value = self.get(key)?;
Ok(value
.map(|v| vec![redis::RedisResult::Binary(v)])
.unwrap_or_default())
}
_ => {
// TODO: implement this by getting input from user
Err(redis::Error::Other(format!(
"not able to execute '{command}' command"
)))
}
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions crates/spin-test-virt/src/wasi/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl exports::types::GuestFields for Fields {
) -> Result<exports::types::Fields, exports::types::HeaderError> {
let mut fields: HashMap<String, Vec<Vec<u8>>> = HashMap::new();
for (k, v) in entries {
fields.entry(k).or_default().push(v);
fields.entry(k.to_lowercase()).or_default().push(v);
}
let fields = Fields {
fields: RefCell::new(fields),
Expand All @@ -346,7 +346,8 @@ impl exports::types::GuestFields for Fields {
fn get(&self, name: exports::types::FieldKey) -> Vec<exports::types::FieldValue> {
self.fields
.borrow()
.get(&name)
// Downcase the key to make it case-insensitive
.get(&name.to_lowercase())
.map(Clone::clone)
.unwrap_or_default()
}
Expand Down Expand Up @@ -375,7 +376,7 @@ impl exports::types::GuestFields for Fields {
// TODO: check for mutability rules
self.fields
.borrow_mut()
.entry(name)
.entry(name.to_lowercase())
.or_default()
.push(value);
Ok(())
Expand Down

0 comments on commit e4172ca

Please sign in to comment.