Skip to content

Move environment parsing tests out of the unit test suite #1438

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

Merged
merged 1 commit into from
Sep 18, 2024
Merged
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
68 changes: 1 addition & 67 deletions server/svix-server/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ pub fn load() -> Result<Arc<ConfigurationInner>> {

#[cfg(test)]
mod tests {
use std::{sync::Arc, time::Duration};
use std::sync::Arc;

use figment::{
providers::{Format as _, Toml},
Expand All @@ -462,74 +462,8 @@ mod tests {
use super::{load, CacheBackend, CacheType, QueueBackend, QueueType};
use crate::core::security::{JWTAlgorithm, JwtSigningConfig};

#[test]
fn test_retry_schedule_parsing() {
figment::Jail::expect_with(|jail| {
jail.set_env("SVIX_JWT_SECRET", "x");

// Multi item
jail.set_env("SVIX_RETRY_SCHEDULE", "[1,2]");

let cfg = load().unwrap();
assert_eq!(
cfg.retry_schedule,
vec![Duration::new(1, 0), Duration::new(2, 0)]
);

// Single item
jail.set_env("SVIX_RETRY_SCHEDULE", "[1]");

let cfg = load().unwrap();
assert_eq!(cfg.retry_schedule, vec![Duration::new(1, 0)]);

// Empty
jail.set_env("SVIX_RETRY_SCHEDULE", "[]");

let cfg = load().unwrap();
assert!(cfg.retry_schedule.is_empty());

Ok(())
});
}

#[test]
fn test_retry_schedule_parsing_legacy() {
figment::Jail::expect_with(|jail| {
jail.set_env("SVIX_JWT_SECRET", "x");

// Multi item
jail.set_env("SVIX_RETRY_SCHEDULE", "1,2");

let cfg = load().unwrap();
assert_eq!(
cfg.retry_schedule,
vec![Duration::new(1, 0), Duration::new(2, 0)]
);

// Single item and empty were failing before so not testing them

Ok(())
});
}

#[test]
fn test_proxy_addr_from_env_parsing() {
figment::Jail::expect_with(|jail| {
jail.set_env("SVIX_QUEUE_TYPE", "memory");
jail.set_env("SVIX_JWT_SECRET", "x");
jail.set_env("SVIX_PROXY_ADDR", "socks5://127.0.0.1");

let cfg = load().unwrap();
assert!(cfg.proxy_config.is_some());

Ok(())
});
}

#[test]
fn test_cache_or_queue_dsn_priority() {
// NOTE: Does not use `figment::Jail` like the above because set env vars will leak into
// other tests overwriting real configurations
let mut cfg = load().unwrap();
let cfg = Arc::make_mut(&mut cfg);

Expand Down
77 changes: 77 additions & 0 deletions server/svix-server/tests/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::time::Duration;

use svix_server::cfg::load;

// Using a single test in its own integration test binary such that
// `figment::Jail` can't influence other tests.
//
// The only thing it does is save & restore things, it doesn't actually have any
// protections against `jail.set_env` values being seen by other tests -
// internally it calls `std::env::set_var`.
#[test]
fn test_environment_parsing() {
test_retry_schedule_parsing();
test_retry_schedule_parsing_legacy();
test_proxy_addr_from_env_parsing();
}

fn test_retry_schedule_parsing() {
figment::Jail::expect_with(|jail| {
jail.set_env("SVIX_JWT_SECRET", "x");

// Multi item
jail.set_env("SVIX_RETRY_SCHEDULE", "[1,2]");

let cfg = load().unwrap();
assert_eq!(
cfg.retry_schedule,
vec![Duration::new(1, 0), Duration::new(2, 0)]
);

// Single item
jail.set_env("SVIX_RETRY_SCHEDULE", "[1]");

let cfg = load().unwrap();
assert_eq!(cfg.retry_schedule, vec![Duration::new(1, 0)]);

// Empty
jail.set_env("SVIX_RETRY_SCHEDULE", "[]");

let cfg = load().unwrap();
assert!(cfg.retry_schedule.is_empty());

Ok(())
});
}

fn test_retry_schedule_parsing_legacy() {
figment::Jail::expect_with(|jail| {
jail.set_env("SVIX_JWT_SECRET", "x");

// Multi item
jail.set_env("SVIX_RETRY_SCHEDULE", "1,2");

let cfg = load().unwrap();
assert_eq!(
cfg.retry_schedule,
vec![Duration::new(1, 0), Duration::new(2, 0)]
);

// Single item and empty were failing before so not testing them

Ok(())
});
}

fn test_proxy_addr_from_env_parsing() {
figment::Jail::expect_with(|jail| {
jail.set_env("SVIX_QUEUE_TYPE", "memory");
jail.set_env("SVIX_JWT_SECRET", "x");
jail.set_env("SVIX_PROXY_ADDR", "socks5://127.0.0.1");

let cfg = load().unwrap();
assert!(cfg.proxy_config.is_some());

Ok(())
});
}