fix(test): Use a mutex to prevent race conditions between tests relying on KUBECONFIG env var
All checks were successful
Run Check Script / check (pull_request) Successful in 1m0s

This commit is contained in:
2026-01-07 14:13:28 -05:00
parent 4e5a24b07a
commit ad5abe1748

View File

@@ -1222,9 +1222,11 @@ fn extract_base_domain(host: &str) -> Option<String> {
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};
static TEST_COUNTER: AtomicUsize = AtomicUsize::new(0);
static ENV_LOCK: Mutex<()> = Mutex::new(());
/// Sets environment variables with unique names to avoid concurrency issues between tests.
/// Returns the names of the (config_var, profile_var) used.
@@ -1289,6 +1291,7 @@ mod tests {
#[test]
fn test_remote_k8s_from_env_var_only_context() {
let _lock = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
run_in_isolated_env(|| {
unsafe {
std::env::remove_var("KUBECONFIG");
@@ -1305,6 +1308,7 @@ mod tests {
#[test]
fn test_remote_k8s_from_env_var_unknown_key_trim() {
let _lock = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
run_in_isolated_env(|| {
unsafe {
std::env::remove_var("KUBECONFIG");
@@ -1324,6 +1328,7 @@ mod tests {
#[test]
fn test_remote_k8s_from_env_var_empty_malformed() {
let _lock = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
run_in_isolated_env(|| {
unsafe {
std::env::remove_var("KUBECONFIG");
@@ -1341,6 +1346,7 @@ mod tests {
#[test]
fn test_remote_k8s_from_env_var_kubeconfig_fallback() {
let _lock = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
run_in_isolated_env(|| {
unsafe {
std::env::set_var("KUBECONFIG", "/fallback/path");
@@ -1357,6 +1363,7 @@ mod tests {
#[test]
fn test_remote_k8s_from_env_var_kubeconfig_no_fallback_if_provided() {
let _lock = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
run_in_isolated_env(|| {
unsafe {
std::env::set_var("KUBECONFIG", "/fallback/path");