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
17 changes: 7 additions & 10 deletions tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,11 +877,10 @@ async fn want_by_sha_tolerates_object_check_failure() {
// A git shim that passes everything through except `cat-file`, which fails.
// It drains stdin first: exiting unread races the query write into EPIPE.
let shim = work.path().join("git-shim");
std::fs::write(
helpers::write_test_script(
&shim,
"#!/bin/sh\nif [ \"$1\" = cat-file ]; then cat >/dev/null; echo 'fatal: simulated cat-file failure' >&2; exit 1; fi\nexec git \"$@\"\n",
)
.unwrap();
);
std::fs::set_permissions(&shim, std::fs::Permissions::from_mode(0o755)).unwrap();

let cache = tempfile::tempdir().unwrap();
Expand Down Expand Up @@ -1467,7 +1466,7 @@ async fn check_upload_pack_disconnect(reset_connection: bool) {
let bin = tempfile::tempdir().unwrap();
let pid_file = bin.path().join("grandchild.pid");
let fake_git = bin.path().join("fake-git");
std::fs::write(
helpers::write_test_script(
&fake_git,
format!(
"#!/bin/sh\n\
Expand All @@ -1478,8 +1477,7 @@ async fn check_upload_pack_disconnect(reset_connection: bool) {
exec git \"$@\"\n",
pid_file.display()
),
)
.unwrap();
);
std::fs::set_permissions(&fake_git, std::fs::Permissions::from_mode(0o755)).unwrap();
let proxy = start_proxy_with_git(up.path(), "8m", fake_git.to_str().unwrap()).await;

Expand Down Expand Up @@ -1530,7 +1528,7 @@ async fn upload_pack_cap_holds_a_slot_until_the_process_exits() {

let dir = tempfile::tempdir().unwrap();
let fake_git = dir.path().join("fake-git");
std::fs::write(&fake_git, "#!/bin/sh\ncat >/dev/null\nexec sleep 30\n").unwrap();
helpers::write_test_script(&fake_git, "#!/bin/sh\ncat >/dev/null\nexec sleep 30\n");
std::fs::set_permissions(&fake_git, std::fs::Permissions::from_mode(0o755)).unwrap();
let cache = GitCache::new(
GitConfig {
Expand Down Expand Up @@ -1595,14 +1593,13 @@ async fn upload_pack_runs_with_pack_options() {
let dir = tempfile::tempdir().unwrap();
let argv_log = dir.path().join("argv.log");
let fake_git = dir.path().join("fake-git");
std::fs::write(
helpers::write_test_script(
&fake_git,
format!(
"#!/bin/sh\nprintf '%s\\n' \"$*\" >> '{}'\nexec git \"$@\"\n",
argv_log.display()
),
)
.unwrap();
);
std::fs::set_permissions(&fake_git, std::fs::Permissions::from_mode(0o755)).unwrap();
let cache = GitCache::new(
GitConfig {
Expand Down
17 changes: 17 additions & 0 deletions tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// rest is the sharing, not a real gap.
#![allow(dead_code)]

use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

Expand Down Expand Up @@ -168,3 +169,19 @@ pub fn github_auth_config(base: &str, ttl_secs: u64) -> AuthConfig {
upstream_base: base.to_string(),
}
}

/// Write an executable-to-be script through a short-lived `sh`, so this process
/// never holds a writable descriptor for it. The tests run on parallel threads:
/// a child forked by another one while the descriptor is open inherits it, and
/// a later `exec` of the script then fails with "Text file busy".
pub fn write_test_script(path: &Path, script: impl AsRef<str>) {
assert!(
std::process::Command::new("sh")
.args(["-c", "printf '%s' \"$2\" > \"$1\"", "sh"])
.arg(path)
.arg(script.as_ref())
.status()
.unwrap()
.success()
);
}
7 changes: 4 additions & 3 deletions tests/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use rustix::process::{Pid, Signal, kill_process};
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;

mod helpers;

#[tokio::test]
async fn signals_cancel_upstream_before_http_drain() {
for signal in [Signal::TERM, Signal::INT] {
Expand All @@ -23,11 +25,10 @@ async fn signals_cancel_upstream_before_http_drain() {
.success()
);
let wrapper = root.path().join("git-wrapper");
std::fs::write(
helpers::write_test_script(
&wrapper,
"#!/bin/sh\nsleep 30 &\necho \"$$ $!\" > \"$READY_FIFO\"\nwait\n",
)
.unwrap();
);
std::fs::set_permissions(&wrapper, std::fs::Permissions::from_mode(0o755)).unwrap();
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
Expand Down
7 changes: 4 additions & 3 deletions tests/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -259,7 +261,7 @@ impl Fixture {
std::fs::create_dir(&upstream).unwrap();
git(&upstream, &["init", "--bare", "-q", "repo.git"]);
let binary = root.path().join("git-wrapper");
std::fs::write(
helpers::write_test_script(
&binary,
r#"#!/bin/sh
set -eu
Expand Down Expand Up @@ -287,8 +289,7 @@ if test "$count" -le "$limit"; then
fi
exec git "$@"
"#,
)
.unwrap();
);
std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o755)).unwrap();
let repo = resolve(
"repo.git",
Expand Down
Loading