Skip to content

Commit

Permalink
Added a Default impl to Configuration to use in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Jul 20, 2022
1 parent cf8909a commit 2846ae9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
19 changes: 19 additions & 0 deletions limitador-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ fn storage_config_from_env() -> Result<StorageConfiguration, ()> {
}
}

#[cfg(test)]
impl Default for Configuration {
fn default() -> Self {
Configuration {
limits_file: "".to_string(),
storage: StorageConfiguration::InMemory,
rls_host: "".to_string(),
rls_port: 0,
http_host: "".to_string(),
http_port: 0,
limit_name_in_labels: false,
}
}
}

fn env_option_is_enabled(env_name: &str) -> bool {
match env::var(env_name) {
Ok(value) => value == "1",
Expand Down Expand Up @@ -190,6 +205,8 @@ mod tests {
#[test]
#[serial]
fn test_config_defaults() {
let mut vars = VarEnvCleaner::new();
vars.set_var("LIMITS_FILE", "limitador-server/examples/limit.yaml");
let config = Configuration::from_env().unwrap();
assert_eq!(&config.limits_file, "");
assert_eq!(config.storage, StorageConfiguration::InMemory);
Expand All @@ -203,6 +220,7 @@ mod tests {
fn test_config_redis_defaults() {
let mut vars = VarEnvCleaner::new();
let url = "redis://127.0.1.1:7654";
vars.set_var("LIMITS_FILE", "limitador-server/examples/limit.yaml");
vars.set_var("REDIS_URL", url);

let config = Configuration::from_env().unwrap();
Expand All @@ -222,6 +240,7 @@ mod tests {
#[serial]
fn test_config_infinispan_defaults() {
let mut vars = VarEnvCleaner::new();
vars.set_var("LIMITS_FILE", "limitador-server/examples/limit.yaml");

let url = "127.0.2.2:9876";
vars.set_var("INFINISPAN_URL", url);
Expand Down
8 changes: 2 additions & 6 deletions limitador-server/src/envoy_rls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ mod tests {
async fn test_returns_ok_when_no_limits_apply() {
// No limits saved
let rate_limiter = MyRateLimiter::new(Arc::new(
Limiter::new(Configuration::from_env().unwrap())
.await
.unwrap(),
Limiter::new(Configuration::default()).await.unwrap(),
));

let req = RateLimitRequest {
Expand Down Expand Up @@ -228,9 +226,7 @@ mod tests {
#[tokio::test]
async fn test_returns_unknown_when_domain_is_empty() {
let rate_limiter = MyRateLimiter::new(Arc::new(
Limiter::new(Configuration::from_env().unwrap())
.await
.unwrap(),
Limiter::new(Configuration::default()).await.unwrap(),
));

let req = RateLimitRequest {
Expand Down
19 changes: 5 additions & 14 deletions limitador-server/src/http_api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,8 @@ mod tests {

#[actix_rt::test]
async fn test_metrics() {
let rate_limiter: Arc<Limiter> = Arc::new(
Limiter::new(Configuration::from_env().unwrap())
.await
.unwrap(),
);
let rate_limiter: Arc<Limiter> =
Arc::new(Limiter::new(Configuration::default()).await.unwrap());
let data = web::Data::new(rate_limiter);
let app = test::init_service(
App::new()
Expand All @@ -249,9 +246,7 @@ mod tests {

#[actix_rt::test]
async fn test_limits_read() {
let limiter = Limiter::new(Configuration::from_env().unwrap())
.await
.unwrap();
let limiter = Limiter::new(Configuration::default()).await.unwrap();
let namespace = "test_namespace";

let limit = create_test_limit(&limiter, namespace, 10).await;
Expand All @@ -276,9 +271,7 @@ mod tests {

#[actix_rt::test]
async fn test_check_and_report() {
let limiter = Limiter::new(Configuration::from_env().unwrap())
.await
.unwrap();
let limiter = Limiter::new(Configuration::default()).await.unwrap();

// Create a limit with max == 1
let namespace = "test_namespace";
Expand Down Expand Up @@ -324,9 +317,7 @@ mod tests {
#[actix_rt::test]
async fn test_check_and_report_endpoints_separately() {
let namespace = "test_namespace";
let limiter = Limiter::new(Configuration::from_env().unwrap())
.await
.unwrap();
let limiter = Limiter::new(Configuration::default()).await.unwrap();
let _limit = create_test_limit(&limiter, namespace, 1).await;

let rate_limiter: Arc<Limiter> = Arc::new(limiter);
Expand Down

0 comments on commit 2846ae9

Please sign in to comment.