Skip to content

Commit

Permalink
chore: more tests for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ion-elgreco committed Jan 9, 2025
1 parent e3327e6 commit eccaf7e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/lakefs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ deltalake-test = { path = "../test" }
pretty_env_logger = "0.5.0"
rand = "0.8"
which = "7"

maplit = "1"

[features]
integration_test_lakefs = []
91 changes: 91 additions & 0 deletions crates/lakefs/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,94 @@ impl ObjectStoreFactory for LakeFSObjectStoreFactory {
Ok((store, prefix))
}
}

#[cfg(test)]
mod tests {
use super::*;

use maplit::hashmap;
use serial_test::serial;

struct ScopedEnv {
vars: HashMap<std::ffi::OsString, std::ffi::OsString>,
}

impl ScopedEnv {
pub fn new() -> Self {
let vars = std::env::vars_os().collect();
Self { vars }
}

pub fn run<T>(mut f: impl FnMut() -> T) -> T {
let _env_scope = Self::new();
f()
}
}

fn clear_env_of_lakefs_s3_keys() {
let keys_to_clear = std::env::vars().filter_map(|(k, _v)| {
if AmazonS3ConfigKey::from_str(&k.to_ascii_lowercase()).is_ok() {
Some(k)
} else {
None
}
});

for k in keys_to_clear {
unsafe {
std::env::remove_var(k);
}
}
}

#[test]
#[serial]
fn when_merging_with_env_unsupplied_options_are_added() {
ScopedEnv::run(|| {
clear_env_of_lakefs_s3_keys();
let raw_options = hashmap! {};
std::env::set_var("ACCESS_KEY_ID", "env_key");
std::env::set_var("ENDPOINT", "env_key");
std::env::set_var("SECRET_ACCESS_KEY", "env_key");
std::env::set_var("REGION", "env_key");
let combined_options =
LakeFSObjectStoreFactory {}.with_env_s3(&StorageOptions(raw_options));

// Four and then the conditional_put built-in
assert_eq!(combined_options.0.len(), 5);

for (key, v) in combined_options.0 {
if key != "conditional_put" {
assert_eq!(v, "env_key");
}
}
});
}

#[tokio::test]
#[serial]
async fn when_merging_with_env_supplied_options_take_precedence() {
ScopedEnv::run(|| {
clear_env_of_lakefs_s3_keys();
let raw_options = hashmap! {
"ACCESS_KEY_ID".to_string() => "options_key".to_string(),
"ENDPOINT_URL".to_string() => "options_key".to_string(),
"SECRET_ACCESS_KEY".to_string() => "options_key".to_string(),
"REGION".to_string() => "options_key".to_string()
};
std::env::set_var("aws_access_key_id", "env_key");
std::env::set_var("aws_endpoint", "env_key");
std::env::set_var("aws_secret_access_key", "env_key");
std::env::set_var("aws_region", "env_key");

let combined_options =
LakeFSObjectStoreFactory {}.with_env_s3(&StorageOptions(raw_options));

for (key, v) in combined_options.0 {
if key != "conditional_put" {
assert_eq!(v, "options_key");
}
}
});
}
}

0 comments on commit eccaf7e

Please sign in to comment.