diff --git a/src/evict.rs b/src/evict.rs index 9ccec4f..fe66f96 100644 --- a/src/evict.rs +++ b/src/evict.rs @@ -473,7 +473,10 @@ mod tests { use tokio::sync::watch; - use crate::git::{GitCache, GitConfig}; + use crate::git::GitCache; + use crate::test_util::test_git_config; + #[cfg(unix)] + use crate::test_util::write_test_script; #[cfg(unix)] #[tokio::test] @@ -669,7 +672,7 @@ mod tests { let metrics = Arc::new(Metrics::new()); let idx = CacheIndex::new(root.to_path_buf(), 4096, metrics.clone()); - let cache = GitCache::new(dummy_cfg(), metrics.clone(), Some(idx.clone())); + let cache = GitCache::new(test_git_config(), metrics.clone(), Some(idx.clone())); idx.mark_changed("big.git"); // request path would do this after a clone maintain(&cache, &idx).await; // measures big.git (>cap) then evicts it @@ -697,7 +700,7 @@ mod tests { let metrics = Arc::new(Metrics::new()); let idx = CacheIndex::new(root.to_path_buf(), 6000, metrics.clone()); // over cap let cache = Arc::new(GitCache::new( - dummy_cfg(), + test_git_config(), metrics.clone(), Some(idx.clone()), )); @@ -750,7 +753,7 @@ mod tests { #[tokio::test] async fn evict_is_a_noop_when_the_mirror_is_already_gone() { let tmp = tempfile::tempdir().unwrap(); - let cache = GitCache::new(dummy_cfg(), Arc::new(Metrics::new()), None); + let cache = GitCache::new(test_git_config(), Arc::new(Metrics::new()), None); // No `HEAD` at this path, so `evict` returns early without touching disk. let dir = tmp.path().join("absent.git"); cache.evict("absent.git", &dir).await.unwrap(); @@ -797,7 +800,7 @@ mod tests { "only the blob is tracked, not the in-flight file" ); - let cache = GitCache::new(dummy_cfg(), metrics.clone(), Some(idx.clone())); + let cache = GitCache::new(test_git_config(), metrics.clone(), Some(idx.clone())); maintain(&cache, &idx).await; // over cap -> the blob file is unlinked assert!(!blob.exists(), "over-cap LFS blob should be evicted"); assert_eq!(idx.totals(), (0, 0)); @@ -868,20 +871,11 @@ mod tests { make_mirror(&root.join("a.git"), 10, None); make_mirror(&root.join("b.git"), 10, None); let binary = root.join("git-wrapper"); - // Avoid writable descriptors inherited by parallel tests' forked children. - assert!( - std::process::Command::new("sh") - .args(["-c", "printf '%s' \"$2\" > \"$1\"", "sh"]) - .arg(&binary) - .arg(format!("#!/bin/sh\n{script}")) - .status() - .unwrap() - .success() - ); + write_test_script(&binary, &format!("#!/bin/sh\n{script}")); std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o755)).unwrap(); let metrics = Arc::new(Metrics::new()); let idx = CacheIndex::new(root.to_path_buf(), cap, metrics.clone()); - let mut cfg = dummy_cfg(); + let mut cfg = test_git_config(); cfg.git_binary = binary.to_str().unwrap().into(); cfg.refresh = Some(crate::git::RefreshConfig { interval: Duration::from_secs(3600), @@ -893,21 +887,6 @@ mod tests { (cache, idx, repo) } - fn dummy_cfg() -> GitConfig { - // Eviction never shells out to git, so the binary is irrelevant. - GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(10), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, - } - } - /// A fake bare mirror: `HEAD` plus a data file summing to at least `data_bytes`. /// When `mtime` is set, both files are stamped with it so the mirror's last-used /// signal is deterministic. diff --git a/src/git.rs b/src/git.rs index 9654048..4657a7d 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1652,11 +1652,15 @@ mod tests { use super::*; use std::sync::Mutex as StdMutex; + use crate::test_util::test_git_config; + #[cfg(unix)] + use crate::test_util::write_test_script; + #[tokio::test(start_paused = true)] async fn refresh_usage_tracks_success_and_ttl_hits_only() { let root = tempfile::tempdir().unwrap(); let repo = refresh_test_mirror(root.path(), "repo.git"); - let mut cfg = test_config(); + let mut cfg = test_git_config(); cfg.git_binary = "true".into(); let mut cache = GitCache::new(cfg, Arc::new(Metrics::new()), None); enable_refresh(&mut cache, 3600, 604800, 1); @@ -1933,7 +1937,7 @@ mod tests { #[tokio::test] async fn refresh_without_config_has_no_candidates_and_skips_the_fetch() { - let cache = GitCache::new(test_config(), Arc::new(Metrics::new()), None); + let cache = GitCache::new(test_git_config(), Arc::new(Metrics::new()), None); let root = tempfile::tempdir().unwrap(); let repo = crate::repo::resolve("repo.git", "https://example.com", root.path()).unwrap(); cache.slot(&repo.name).await.refresh.lock().await.used = @@ -1948,7 +1952,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn refresh_candidates_use_client_recency_and_window_boundary() { - let mut cache = GitCache::new(test_config(), Arc::new(Metrics::new()), None); + let mut cache = GitCache::new(test_git_config(), Arc::new(Metrics::new()), None); enable_refresh(&mut cache, 3600, 20, 1); let root = tempfile::tempdir().unwrap(); for name in ["old.git", "recent.git", "newest.git"] { @@ -2366,7 +2370,7 @@ mod tests { async fn refresh_disabled_creates_no_task_or_request_state_access() { let root = tempfile::tempdir().unwrap(); let repo = refresh_test_mirror(root.path(), "repo.git"); - let mut cfg = test_config(); + let mut cfg = test_git_config(); cfg.git_binary = "true".into(); let cache = GitCache::new(cfg, Arc::new(Metrics::new()), None); let slot = cache.slot(&repo.name).await; @@ -2386,7 +2390,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn refresh_counts_candidates_the_round_deadline_never_started() { - let mut cache = GitCache::new(test_config(), Arc::new(Metrics::new()), None); + let mut cache = GitCache::new(test_git_config(), Arc::new(Metrics::new()), None); let root = tempfile::tempdir().unwrap(); let older = crate::repo::resolve("older.git", "https://example.com", root.path()).unwrap(); let newer = crate::repo::resolve("newer.git", "https://example.com", root.path()).unwrap(); @@ -2415,7 +2419,7 @@ mod tests { #[tokio::test] async fn refresh_unusable_settings_count_as_disabled() { for (interval, concurrency) in [(3600, 0), (0, 1)] { - let mut cfg = test_config(); + let mut cfg = test_git_config(); cfg.refresh = Some(RefreshConfig { interval: Duration::from_secs(interval), active_window: Duration::from_secs(1), @@ -2496,7 +2500,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn refresh_lock_deadline_and_shutdown_leave_failure_state_unchanged() { - let cache = GitCache::new(test_config(), Arc::new(Metrics::new()), None); + let cache = GitCache::new(test_git_config(), Arc::new(Metrics::new()), None); let root = tempfile::tempdir().unwrap(); let repo = crate::repo::resolve("repo.git", "https://example.com", root.path()).unwrap(); let slot = cache.slot(&repo.name).await; @@ -2539,7 +2543,7 @@ mod tests { let repo = crate::repo::resolve("repo.git", "https://example.com", root.path()).unwrap(); std::fs::create_dir_all(&repo.cache_dir).unwrap(); std::fs::write(repo.cache_dir.join("HEAD"), "ref: refs/heads/main").unwrap(); - let mut cfg = test_config(); + let mut cfg = test_git_config(); cfg.git_binary = "true".into(); cfg.fetch_ttl = Duration::from_secs(60); let mut cache = GitCache::new(cfg, Arc::new(Metrics::new()), None); @@ -2568,7 +2572,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn refresh_skips_inactive_missing_auth_blocked_and_backoff_repos() { - let cache = GitCache::new(test_config(), Arc::new(Metrics::new()), None); + let cache = GitCache::new(test_git_config(), Arc::new(Metrics::new()), None); let root = tempfile::tempdir().unwrap(); let repo = crate::repo::resolve("repo.git", "https://example.com", root.path()).unwrap(); let slot = cache.slot(&repo.name).await; @@ -2627,7 +2631,7 @@ mod tests { #[tokio::test] async fn refresh_auth_mark_requires_server_credential_success() { - let mut cfg = test_config(); + let mut cfg = test_git_config(); cfg.upstream_auth_header = Some("Authorization: server".into()); let mut cache = GitCache::new(cfg, Arc::new(Metrics::new()), None); enable_refresh(&mut cache, 3600, 604800, 1); @@ -2667,21 +2671,7 @@ mod tests { .status() .unwrap(); assert!(status.success()); - let cache = GitCache::new( - GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(60), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, - }, - Arc::new(Metrics::new()), - None, - ); + let cache = GitCache::new(test_git_config(), Arc::new(Metrics::new()), None); let repo = RepoRef { name: "test/repo.git".into(), upstream_url: String::new(), @@ -2763,7 +2753,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn shutdown_cancels_repository_waiters_and_rejects_admission() { - let cache = GitCache::new(test_config(), Arc::new(Metrics::new()), None); + let cache = GitCache::new(test_git_config(), Arc::new(Metrics::new()), None); let root = tempfile::tempdir().unwrap(); let repo = crate::repo::resolve("repo.git", "https://example.com", root.path()).unwrap(); let slot = cache.slot(&repo.name).await; @@ -2805,8 +2795,9 @@ mod tests { ), ); std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o755)).unwrap(); - let mut config = test_config(); + let mut config = test_git_config(); config.git_binary = binary.to_str().unwrap().into(); + config.max_upload_packs = 1; let cache = GitCache::new(config, Arc::new(Metrics::new()), None); let repo = crate::repo::resolve("repo.git", "https://example.com", root.path()).unwrap(); let stream = cache @@ -4229,22 +4220,6 @@ mod tests { cmd.spawn().unwrap() } - /// Write `script` through a short-lived `sh`, so no writable descriptor for it - /// lives in this process: a child forked by a parallel test would inherit one - /// and the later `exec` would fail with "Text file busy". - #[cfg(unix)] - fn write_test_script(path: &Path, script: &str) { - assert!( - std::process::Command::new("sh") - .args(["-c", "printf '%s' \"$2\" > \"$1\"", "sh"]) - .arg(path) - .arg(script) - .status() - .unwrap() - .success() - ); - } - #[cfg(unix)] fn write_failing_git(dir: &Path, body: &str) -> std::path::PathBuf { use std::os::unix::fs::PermissionsExt; @@ -4320,19 +4295,6 @@ mod tests { eof: false, } } - fn test_config() -> GitConfig { - GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(60), - max_wants: 0, - max_upload_packs: 1, - pack_threads: None, - pack_window_memory: None, - refresh: None, - } - } /// `ps` state of `pid`: empty once reaped, `Z` while a zombie awaits reaping. /// `kill -0` reports zombies as alive, and /proc is Linux-only. @@ -4361,14 +4323,8 @@ mod tests { GitCache::new( GitConfig { git_binary: binary.into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(60), - max_wants: 100, max_upload_packs: 1, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..test_git_config() }, Arc::new(Metrics::new()), None, @@ -4437,7 +4393,7 @@ mod tests { } async fn refresh_spawn_failure_fixture(root: &Path) -> (GitCache, RepoRef) { - let mut cfg = test_config(); + let mut cfg = test_git_config(); cfg.git_binary = root.join("missing-git").to_str().unwrap().into(); let mut cache = GitCache::new(cfg, Arc::new(Metrics::new()), None); let repo = crate::repo::resolve("repo.git", "https://example.com", root).unwrap(); @@ -4454,7 +4410,7 @@ mod tests { let binary = root.join("git-wrapper"); write_test_script(&binary, &format!("#!/bin/sh\n{script}")); std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o755)).unwrap(); - let mut cfg = test_config(); + let mut cfg = test_git_config(); cfg.git_binary = binary.to_str().unwrap().into(); cfg.upstream_auth_header = Some("Authorization: server".into()); cfg.fetch_ttl = Duration::from_secs(60); diff --git a/src/lib.rs b/src/lib.rs index 88f3944..5cd70fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,3 +13,5 @@ pub mod lfs; pub mod metrics; pub mod repo; pub mod server; +#[cfg(test)] +mod test_util; diff --git a/src/test_util.rs b/src/test_util.rs new file mode 100644 index 0000000..41c5011 --- /dev/null +++ b/src/test_util.rs @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: Apache-2.0 +//! Helpers shared by the unit tests of several modules. + +use std::time::Duration; + +use crate::git::GitConfig; + +/// A `GitConfig` for unit tests: the real `git`, no upstream credential, no +/// upload-pack cap and a 60 second fetch TTL. Override what a test needs with +/// struct update syntax, so a new field is added here and not at every site. +pub(crate) fn test_git_config() -> GitConfig { + GitConfig { + git_binary: "git".into(), + upstream_auth_header: None, + big_file_threshold: "8m".into(), + fetch_ttl: Duration::from_secs(60), + max_wants: 100, + max_upload_packs: 0, + pack_threads: None, + pack_window_memory: None, + refresh: None, + } +} + +#[cfg(unix)] +use std::path::Path; + +/// Write `script` through a short-lived `sh`, so no writable descriptor for it +/// lives in this process: a child forked by a parallel test would inherit one +/// and the later `exec` would fail with "Text file busy". +#[cfg(unix)] +pub(crate) fn write_test_script(path: &Path, script: &str) { + assert!( + std::process::Command::new("sh") + .args(["-c", "printf '%s' \"$2\" > \"$1\"", "sh"]) + .arg(path) + .arg(script) + .status() + .unwrap() + .success() + ); +} diff --git a/tests/auth.rs b/tests/auth.rs index c63cc99..1e0d4b3 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -341,15 +341,8 @@ fn state(auth: Arc) -> AppState { let cache_root = std::env::temp_dir().join("git-cache-proxy-auth-tests-unused"); let metrics = Arc::new(Metrics::new()); let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), fetch_ttl: Duration::from_secs(10), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; let lfs = Arc::new(Lfs::new( LfsConfig { diff --git a/tests/e2e.rs b/tests/e2e.rs index 3984dd4..0639bf6 100644 --- a/tests/e2e.rs +++ b/tests/e2e.rs @@ -59,21 +59,7 @@ async fn reused_mirror_fetches_configured_upstream_for_refs_and_missing_sha() { let cache = tempfile::tempdir().unwrap(); let metrics = Arc::new(Metrics::new()); - let gitcache = GitCache::new( - GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::ZERO, - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, - }, - metrics.clone(), - None, - ); + let gitcache = GitCache::new(helpers::test_git_config(), metrics.clone(), None); let mut repo = git_cache_proxy::repo::RepoRef { name: "repo.git".into(), upstream_url: format!("file://{}", old.path().display()), @@ -159,17 +145,7 @@ async fn upload_pack_decodes_gzip_encoded_request() { // Proxy over the file:// upstream (in-process, driven via `oneshot`). let cache = tempfile::tempdir().unwrap(); let metrics = Arc::new(Metrics::new()); - let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(0), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, - }; + let cfg = helpers::test_git_config(); let state = AppState { cache: Arc::new(GitCache::new(cfg, metrics.clone(), None)), lfs: Arc::new(Lfs::new( @@ -289,17 +265,7 @@ async fn serves_want_by_sha_for_unadvertised_pull_ref() { // --- Proxy over the file:// upstream. --- let cache = tempfile::tempdir().unwrap(); let metrics = Arc::new(Metrics::new()); - let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(0), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, - }; + let cfg = helpers::test_git_config(); let state = AppState { cache: Arc::new(GitCache::new(cfg, metrics.clone(), None)), lfs: Arc::new(Lfs::new( @@ -427,17 +393,7 @@ async fn want_by_sha_fetch_failure_is_recorded_and_degrades_gracefully() { let cache = tempfile::tempdir().unwrap(); let metrics = Arc::new(Metrics::new()); - let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(0), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, - }; + let cfg = helpers::test_git_config(); let state = AppState { cache: Arc::new(GitCache::new(cfg, metrics.clone(), None)), lfs: Arc::new(Lfs::new( @@ -562,15 +518,8 @@ async fn want_by_sha_pins_are_capped_per_mirror() { let cache = tempfile::tempdir().unwrap(); let metrics = Arc::new(Metrics::new()); let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(0), max_wants: 1, // keep at most one pin per mirror - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; let state = AppState { cache: Arc::new(GitCache::new(cfg, metrics.clone(), None)), @@ -708,15 +657,8 @@ async fn caps_want_by_sha_fetch_within_a_single_request() { let cache = tempfile::tempdir().unwrap(); let metrics = Arc::new(Metrics::new()); let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(0), max_wants: 1, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; let gitcache = GitCache::new(cfg, metrics, None); let upstream_base = format!("file://{}", up.path().display()); @@ -804,15 +746,8 @@ async fn want_by_sha_pins_are_unlimited_when_max_wants_is_zero() { let cache = tempfile::tempdir().unwrap(); let metrics = Arc::new(Metrics::new()); let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(0), max_wants: 0, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; let gitcache = GitCache::new(cfg, metrics, None); let upstream_base = format!("file://{}", up.path().display()); @@ -888,14 +823,7 @@ async fn want_by_sha_tolerates_object_check_failure() { let metrics = Arc::new(Metrics::new()); let cfg = GitConfig { git_binary: shim.to_str().unwrap().into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(0), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; let gitcache = GitCache::new(cfg, metrics, None); let upstream_base = format!("file://{}", up.path().display()); @@ -951,17 +879,7 @@ async fn clones_through_proxy_serves_all_refs_and_rejects_push() { // --- Bring up the proxy pointed at the file:// upstream. --- let cache = tempfile::tempdir().unwrap(); let metrics = Arc::new(Metrics::new()); - let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(0), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, - }; + let cfg = helpers::test_git_config(); // Eviction enabled with an effectively unbounded cap: no mirror is ever // evicted, but the on-request index bookkeeping (touch on serve, mark-changed // on clone/fetch) runs, which the assertion below checks. @@ -1535,14 +1453,8 @@ async fn upload_pack_cap_holds_a_slot_until_the_process_exits() { let cache = GitCache::new( GitConfig { git_binary: fake_git.to_str().unwrap().into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(0), - max_wants: 100, max_upload_packs: 1, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }, Arc::new(Metrics::new()), None, @@ -1607,14 +1519,9 @@ async fn upload_pack_runs_with_pack_options() { let cache = GitCache::new( GitConfig { git_binary: fake_git.to_str().unwrap().into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), - fetch_ttl: Duration::from_secs(0), - max_wants: 100, - max_upload_packs: 0, pack_threads: Some(2), pack_window_memory: Some("64m".into()), - refresh: None, + ..helpers::test_git_config() }, Arc::new(Metrics::new()), None, @@ -1731,14 +1638,8 @@ async fn start_proxy_with_auth( let metrics = Arc::new(Metrics::new()); let cfg = GitConfig { git_binary: git_binary.into(), - upstream_auth_header: None, big_file_threshold: big_file_threshold.into(), - fetch_ttl: Duration::from_secs(0), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; let state = AppState { cache: Arc::new(GitCache::new(cfg, metrics.clone(), None)), diff --git a/tests/helpers.rs b/tests/helpers.rs index 4d451ac..9b68f27 100644 --- a/tests/helpers.rs +++ b/tests/helpers.rs @@ -19,6 +19,7 @@ use base64::Engine; use base64::engine::general_purpose::STANDARD; use git_cache_proxy::auth::AuthConfig; use git_cache_proxy::config::AuthProvider; +use git_cache_proxy::git::GitConfig; use tokio::sync::{Mutex, Notify}; /// The signing key the test replicas share, long enough for HMAC-SHA256. @@ -150,6 +151,23 @@ pub async fn spawn_validator_with(reply: Reply) -> (String, Arc>> (format!("http://{addr}"), hits, arrived) } +/// A `GitConfig` for tests: the real `git`, no upstream credential, no upload-pack +/// cap and a zero fetch TTL (every request fetches). Override what a test needs +/// with struct update syntax, so a new field is added here and not at every site. +pub fn test_git_config() -> GitConfig { + GitConfig { + git_binary: "git".into(), + upstream_auth_header: None, + big_file_threshold: "8m".into(), + fetch_ttl: Duration::ZERO, + max_wants: 100, + max_upload_packs: 0, + pack_threads: None, + pack_window_memory: None, + refresh: None, + } +} + /// A `github` provider config probing the mock at `base` as its upstream, with /// its verdict reused for `ttl_secs` (0: revalidated on every request). pub fn github_auth_config(base: &str, ttl_secs: u64) -> AuthConfig { diff --git a/tests/http.rs b/tests/http.rs index eceab51..0c238e4 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -18,6 +18,8 @@ use git_cache_proxy::metrics::Metrics; use git_cache_proxy::server::{AppState, router}; use tower::ServiceExt; // for `oneshot` +mod helpers; + #[tokio::test] async fn health_endpoints_return_ok() { assert_eq!( @@ -185,15 +187,8 @@ async fn upstream_failure_returns_bad_gateway_and_records_error() { let cache = tempfile::tempdir().unwrap(); let metrics = Arc::new(Metrics::new()); let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), fetch_ttl: Duration::from_secs(10), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; let lfs = Arc::new(Lfs::new( LfsConfig { @@ -245,15 +240,8 @@ async fn upload_pack_upstream_failure_returns_bad_gateway() { let metrics = Arc::new(Metrics::new()); let upstream = "file:///nonexistent/git-cache-proxy-upstream"; let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), fetch_ttl: Duration::from_secs(10), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; let lfs = Arc::new(Lfs::new( LfsConfig { @@ -348,15 +336,8 @@ fn state(auth: Arc) -> AppState { let cache_root = std::env::temp_dir().join("git-cache-proxy-tests-unused"); let metrics = Arc::new(Metrics::new()); let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), fetch_ttl: Duration::from_secs(10), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; let lfs = Arc::new(Lfs::new( LfsConfig { diff --git a/tests/lfs.rs b/tests/lfs.rs index b097760..f84d5c4 100644 --- a/tests/lfs.rs +++ b/tests/lfs.rs @@ -414,15 +414,8 @@ fn proxy_state(addr: SocketAddr, cache: &std::path::Path, metrics: Arc) let upstream = format!("http://{addr}"); let idx = CacheIndex::new(cache.to_path_buf(), u64::MAX, metrics.clone()); let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), fetch_ttl: Duration::from_secs(10), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; AppState { cache: Arc::new(GitCache::new(cfg, metrics.clone(), Some(idx.clone()))), diff --git a/tests/refresh.rs b/tests/refresh.rs index 3f86cd7..407b4bc 100644 --- a/tests/refresh.rs +++ b/tests/refresh.rs @@ -19,6 +19,8 @@ use git_cache_proxy::repo::resolve; use git_cache_proxy::server::{AppState, router}; use tower::ServiceExt; +mod helpers; + #[tokio::test(start_paused = true)] async fn refresh_default_disabled_does_not_start_a_task() { let cfg = Config::try_parse_from(["proxy", "--upstream", "https://example.com"]).unwrap(); @@ -29,14 +31,9 @@ async fn refresh_default_disabled_does_not_start_a_task() { let cache = GitCache::new( GitConfig { git_binary: "missing-git-must-not-run".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), fetch_ttl: Duration::from_secs(10), - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, refresh: cfg.refresh_config(), + ..helpers::test_git_config() }, Arc::new(Metrics::new()), None, diff --git a/tests/retry.rs b/tests/retry.rs index 23c14c5..ee40a56 100644 --- a/tests/retry.rs +++ b/tests/retry.rs @@ -14,6 +14,8 @@ use git_cache_proxy::git::{CacheOutcome, GitCache, GitConfig}; use git_cache_proxy::metrics::Metrics; use git_cache_proxy::repo::{RepoRef, resolve}; +mod helpers; + #[tokio::test] async fn clone_retries_tls_eof_and_removes_partial_staging() { let f = Fixture::new(); @@ -299,14 +301,9 @@ exec git "$@" let cache = GitCache::new( GitConfig { git_binary: binary.to_str().unwrap().into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), fetch_ttl, max_wants: 0, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }, Arc::new(Metrics::new()), None, diff --git a/tests/upstream_credential.rs b/tests/upstream_credential.rs index 5fb3793..692cbb5 100644 --- a/tests/upstream_credential.rs +++ b/tests/upstream_credential.rs @@ -265,15 +265,8 @@ async fn client_state(base: &str) -> AppState { let cache_root = cache.keep(); let metrics = Arc::new(Metrics::new()); let cfg = GitConfig { - git_binary: "git".into(), - upstream_auth_header: None, - big_file_threshold: "8m".into(), fetch_ttl: Duration::from_secs(0), // always fetch, so a revoked token is seen - max_wants: 100, - max_upload_packs: 0, - pack_threads: None, - pack_window_memory: None, - refresh: None, + ..helpers::test_git_config() }; let auth = Arc::new(Auth::build(helpers::github_auth_config(base, 0), metrics.clone()).unwrap());