From eabe9274eeb446533e31e8ce8c46091bea94e234 Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Wed, 20 Jul 2022 10:47:04 +0100 Subject: [PATCH] Added a Default impl to Configuration to use in tests --- limitador-server/src/config.rs | 25 +++++++++++++++++++++--- limitador-server/src/envoy_rls/server.rs | 8 ++------ limitador-server/src/http_api/server.rs | 19 +++++------------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/limitador-server/src/config.rs b/limitador-server/src/config.rs index af319b2d..dec72f85 100644 --- a/limitador-server/src/config.rs +++ b/limitador-server/src/config.rs @@ -123,6 +123,21 @@ fn storage_config_from_env() -> Result { } } +#[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", @@ -190,8 +205,10 @@ 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.limits_file, "limitador-server/examples/limit.yaml"); assert_eq!(config.storage, StorageConfiguration::InMemory); assert_eq!(config.http_address(), "0.0.0.0:8080".to_string()); assert_eq!(config.rlp_address(), "0.0.0.0:8081".to_string()); @@ -203,10 +220,11 @@ 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(); - assert_eq!(&config.limits_file, ""); + assert_eq!(&config.limits_file, "limitador-server/examples/limit.yaml"); if let StorageConfiguration::Redis(ref redis_config) = config.storage { assert_eq!(redis_config.url, url); assert_eq!(redis_config.cache, None); @@ -222,11 +240,12 @@ 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); let config = Configuration::from_env().unwrap(); - assert_eq!(&config.limits_file, ""); + assert_eq!(&config.limits_file, "limitador-server/examples/limit.yaml"); if let StorageConfiguration::Infinispan(ref infinispan_config) = config.storage { assert_eq!(infinispan_config.url, url); assert_eq!(infinispan_config.cache, None); diff --git a/limitador-server/src/envoy_rls/server.rs b/limitador-server/src/envoy_rls/server.rs index e51352e4..8a6aa8de 100644 --- a/limitador-server/src/envoy_rls/server.rs +++ b/limitador-server/src/envoy_rls/server.rs @@ -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 { @@ -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 { diff --git a/limitador-server/src/http_api/server.rs b/limitador-server/src/http_api/server.rs index 88345f10..848bc6ad 100644 --- a/limitador-server/src/http_api/server.rs +++ b/limitador-server/src/http_api/server.rs @@ -225,11 +225,8 @@ mod tests { #[actix_rt::test] async fn test_metrics() { - let rate_limiter: Arc = Arc::new( - Limiter::new(Configuration::from_env().unwrap()) - .await - .unwrap(), - ); + let rate_limiter: Arc = + Arc::new(Limiter::new(Configuration::default()).await.unwrap()); let data = web::Data::new(rate_limiter); let app = test::init_service( App::new() @@ -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; @@ -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"; @@ -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 = Arc::new(limiter);