Skip to content
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
41 changes: 10 additions & 31 deletions src/evict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()),
));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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),
Expand All @@ -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.
Expand Down
86 changes: 21 additions & 65 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 =
Expand All @@ -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"] {
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ pub mod lfs;
pub mod metrics;
pub mod repo;
pub mod server;
#[cfg(test)]
mod test_util;
42 changes: 42 additions & 0 deletions src/test_util.rs
Original file line number Diff line number Diff line change
@@ -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()
);
}
9 changes: 1 addition & 8 deletions tests/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,8 @@ fn state(auth: Arc<Auth>) -> 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 {
Expand Down
Loading
Loading