From 85279f06aae1796d1551609041c5e0cf691bb297 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 5 Feb 2024 11:48:19 +0000 Subject: [PATCH] ci: update push options tests Keep the push options tests more constrained to our CI environment; writing files within the test sandbox instead of outside of it. Provide the path to the output file in the test data. In addition, add the repository to the test resources instead of recreating the hooks every time. --- ci/hooks/pre-receive | 2 - ci/test.sh | 60 +++++-------------- tests/libgit2/online/push.c | 34 +++++++---- tests/resources/pushoptions.git/HEAD | 1 + .../pushoptions.git/branches/.gitignore | 0 tests/resources/pushoptions.git/config | 8 +++ tests/resources/pushoptions.git/description | 1 + .../pushoptions.git/hooks/pre-receive | 3 + tests/resources/pushoptions.git/info/exclude | 6 ++ .../pushoptions.git/objects/info/.gitignore | 0 .../pushoptions.git/objects/pack/.gitignore | 0 .../pushoptions.git/refs/heads/.gitignore | 0 .../pushoptions.git/refs/tags/.gitignore | 0 13 files changed, 57 insertions(+), 58 deletions(-) delete mode 100755 ci/hooks/pre-receive create mode 100644 tests/resources/pushoptions.git/HEAD create mode 100644 tests/resources/pushoptions.git/branches/.gitignore create mode 100644 tests/resources/pushoptions.git/config create mode 100644 tests/resources/pushoptions.git/description create mode 100755 tests/resources/pushoptions.git/hooks/pre-receive create mode 100644 tests/resources/pushoptions.git/info/exclude create mode 100644 tests/resources/pushoptions.git/objects/info/.gitignore create mode 100644 tests/resources/pushoptions.git/objects/pack/.gitignore create mode 100644 tests/resources/pushoptions.git/refs/heads/.gitignore create mode 100644 tests/resources/pushoptions.git/refs/tags/.gitignore diff --git a/ci/hooks/pre-receive b/ci/hooks/pre-receive deleted file mode 100755 index 92be65ce0..000000000 --- a/ci/hooks/pre-receive +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -printf "$GIT_PUSH_OPTION_0$GIT_PUSH_OPTION_1$GIT_PUSH_OPTION_2" > %file% diff --git a/ci/test.sh b/ci/test.sh index 8543169ce..421756822 100755 --- a/ci/test.sh +++ b/ci/test.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -set -ex +set -e if [ -n "$SKIP_TESTS" ]; then if [ -z "$SKIP_OFFLINE_TESTS" ]; then SKIP_OFFLINE_TESTS=1; fi @@ -162,14 +162,7 @@ echo "" if should_run "GITDAEMON_TESTS"; then echo "Starting git daemon (standard)..." GIT_STANDARD_DIR=`mktemp -d ${TMPDIR}/git_standard.XXXXXXXX` - git init --bare "${GIT_STANDARD_DIR}/test.git" >/dev/null - git config --file "${GIT_STANDARD_DIR}/test.git/config" receive.advertisePushOptions true - for f in $(ls ${SOURCE_DIR}/ci/hooks) - do - sed "s=%file%=${TMPDIR}/push-option-result-git-daemon=g" "${SOURCE_DIR}/ci/hooks/$f" > "${GIT_STANDARD_DIR}/test.git/hooks/${f}" - chmod +x "$GIT_STANDARD_DIR/test.git/hooks/${f}" - done - + cp -R "${SOURCE_DIR}/tests/resources/pushoptions.git" "${GIT_STANDARD_DIR}/test.git" git daemon --listen=localhost --export-all --enable=receive-pack --base-path="${GIT_STANDARD_DIR}" "${GIT_STANDARD_DIR}" 2>/dev/null & GIT_STANDARD_PID=$! @@ -204,14 +197,7 @@ if should_run "NTLM_TESTS" || should_run "ONLINE_TESTS"; then echo "Starting HTTP server..." HTTP_DIR=`mktemp -d ${TMPDIR}/http.XXXXXXXX` - git init --bare "${HTTP_DIR}/test.git" - git config --file "${HTTP_DIR}/test.git/config" receive.advertisePushOptions true - - for f in $(ls ${SOURCE_DIR}/ci/hooks) - do - sed "s=%file%=${TMPDIR}/push-option-result-git-ntlm=g" "${SOURCE_DIR}/ci/hooks/$f" > "${HTTP_DIR}/test.git/hooks/${f}" - chmod +x "$HTTP_DIR/test.git/hooks/${f}" - done + cp -R "${SOURCE_DIR}/tests/resources/pushoptions.git" "${HTTP_DIR}/test.git" java -jar poxygit.jar --address 127.0.0.1 --port 9000 --credentials foo:baz --quiet "${HTTP_DIR}" & HTTP_PID=$! @@ -220,14 +206,8 @@ fi if should_run "SSH_TESTS"; then echo "Starting SSH server..." SSHD_DIR=`mktemp -d ${TMPDIR}/sshd.XXXXXXXX` - git init --bare "${SSHD_DIR}/test.git" >/dev/null - git config --file "${SSHD_DIR}/test.git/config" receive.advertisePushOptions true - - for f in $(ls ${SOURCE_DIR}/ci/hooks) - do - sed "s=%file%=${TMPDIR}/push-option-result-git-ssh=g" "${SOURCE_DIR}/ci/hooks/$f" > "${SSHD_DIR}/test.git/hooks/${f}" - chmod +x "$SSHD_DIR/test.git/hooks/${f}" - done + cp -R "${SOURCE_DIR}/tests/resources/pushoptions.git" "${SSHD_DIR}/test.git" + ls -FlasR "${SSHD_DIR}" cat >"${SSHD_DIR}/sshd_config" <<-EOF Port 2222 @@ -344,13 +324,11 @@ if should_run "GITDAEMON_TESTS"; then echo "Running gitdaemon (standard) tests" echo "" - if [[ "$RUN_PUSH_OPTONS_TESTS" = "true " ]]; then - export GITTEST_PUSH_OPTION_RESULT="${TMPDIR}/push-option-result-git-daemon" - fi export GITTEST_REMOTE_URL="git://localhost/test.git" + export GITTEST_PUSH_OPTIONS=true run_test gitdaemon - unset GITTEST_PUSH_OPTION_RESULT unset GITTEST_REMOTE_URL + unset GITTEST_PUSH_OPTIONS echo "" echo "Running gitdaemon (namespace) tests" @@ -402,33 +380,29 @@ if should_run "NTLM_TESTS"; then echo "Running NTLM tests (IIS emulation)" echo "" - if [[ "$RUN_PUSH_OPTONS_TESTS" = "true " ]]; then - export GITTEST_PUSH_OPTION_RESULT="${TMPDIR}/push-option-result-git-ntlm" - fi export GITTEST_REMOTE_URL="http://localhost:9000/ntlm/test.git" export GITTEST_REMOTE_USER="foo" export GITTEST_REMOTE_PASS="baz" + export GITTEST_PUSH_OPTIONS=true run_test auth_clone_and_push - unset GITTEST_PUSH_OPTION_RESULT unset GITTEST_REMOTE_URL unset GITTEST_REMOTE_USER unset GITTEST_REMOTE_PASS + unset GITTEST_PUSH_OPTIONS echo "" echo "Running NTLM tests (Apache emulation)" echo "" - if [[ "$RUN_PUSH_OPTONS_TESTS" == "true " ]]; then - export GITTEST_PUSH_OPTION_RESULT="${TMPDIR}/push-option-result-git-ntlm" - fi export GITTEST_REMOTE_URL="http://localhost:9000/broken-ntlm/test.git" export GITTEST_REMOTE_USER="foo" export GITTEST_REMOTE_PASS="baz" + export GITTEST_PUSH_OPTIONS=true run_test auth_clone_and_push - unset GITTEST_PUSH_OPTION_RESULT unset GITTEST_REMOTE_URL unset GITTEST_REMOTE_USER unset GITTEST_REMOTE_PASS + unset GITTEST_PUSH_OPTIONS fi if should_run "NEGOTIATE_TESTS" && -n "$GITTEST_NEGOTIATE_PASSWORD" ; then @@ -477,25 +451,21 @@ if should_run "SSH_TESTS"; then echo "Running ssh tests" echo "" - if [[ "$RUN_PUSH_OPTONS_TESTS" == "true " ]]; then - export GITTEST_PUSH_OPTION_RESULT="${TMPDIR}/push-option-result-ssh" - fi export GITTEST_REMOTE_URL="ssh://localhost:2222/$SSHD_DIR/test.git" + export GITTEST_PUSH_OPTIONS=true run_test ssh - unset GITTEST_PUSH_OPTION_RESULT unset GITTEST_REMOTE_URL + unset GITTEST_PUSH_OPTIONS echo "" echo "Running ssh tests (scp-style paths)" echo "" - if [[ "$RUN_PUSH_OPTONS_TESTS" == "true " ]]; then - export GITTEST_PUSH_OPTION_RESULT="${TMPDIR}/push-option-result-ssh" - fi export GITTEST_REMOTE_URL="[localhost:2222]:$SSHD_DIR/test.git" + export GITTEST_PUSH_OPTIONS=true run_test ssh - unset GITTEST_PUSH_OPTION_RESULT unset GITTEST_REMOTE_URL + unset GITTEST_PUSH_OPTIONS unset GITTEST_SSH_CMD diff --git a/tests/libgit2/online/push.c b/tests/libgit2/online/push.c index cfd43209d..e5693bf34 100644 --- a/tests/libgit2/online/push.c +++ b/tests/libgit2/online/push.c @@ -21,11 +21,11 @@ static char *_remote_ssh_passphrase = NULL; static char *_remote_default = NULL; static char *_remote_expectcontinue = NULL; +static char *_remote_push_options = NULL; + static char *_orig_ssh_cmd = NULL; static char *_ssh_cmd = NULL; -static char *_remote_push_options_result = NULL; - static int cred_acquire_cb(git_credential **, const char *, const char *, unsigned int, void *); static git_remote *_remote; @@ -373,7 +373,7 @@ void test_online_push__initialize(void) _remote_ssh_passphrase = cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE"); _remote_default = cl_getenv("GITTEST_REMOTE_DEFAULT"); _remote_expectcontinue = cl_getenv("GITTEST_REMOTE_EXPECTCONTINUE"); - _remote_push_options_result = cl_getenv("GITTEST_PUSH_OPTION_RESULT"); + _remote_push_options = cl_getenv("GITTEST_PUSH_OPTIONS"); _remote = NULL; _orig_ssh_cmd = cl_getenv("GIT_SSH"); @@ -437,7 +437,7 @@ void test_online_push__cleanup(void) git__free(_remote_ssh_passphrase); git__free(_remote_default); git__free(_remote_expectcontinue); - git__free(_remote_push_options_result); + git__free(_remote_push_options); git__free(_orig_ssh_cmd); git__free(_ssh_cmd); @@ -449,6 +449,7 @@ void test_online_push__cleanup(void) record_callbacks_data_clear(&_record_cbs_data); + cl_fixture_cleanup("push-options-result"); cl_fixture_cleanup("testrepo.git"); cl_git_sandbox_cleanup(); } @@ -827,30 +828,41 @@ void test_online_push__force(void) NULL); } -static void push_option_test(git_strarray push_options, const char *expected_option) +static void push_option_test(git_strarray given_options, const char *expected_option) { const char *specs[] = { "refs/heads/b1:refs/heads/b1" }; push_status exp_stats[] = { { "refs/heads/b1", 1 } }; expected_ref exp_refs[] = { { "refs/heads/b1", &_oid_b1 } }; + git_str push_options_path = GIT_STR_INIT; git_str push_options_result = GIT_STR_INIT; + char *options[16]; + git_strarray push_options = { options, given_options.count + 1 }; + size_t i; /* Skip the test if we're missing the push options result file */ - if (!_remote_push_options_result) + if (!_remote_push_options) cl_skip(); + cl_assert(given_options.count < 16); + + cl_git_pass(git_str_joinpath(&push_options_path, clar_sandbox_path(), "push-options-result")); + + options[0] = push_options_path.ptr; + for (i = 0; i < given_options.count; i++) + options[i + 1] = given_options.strings[i]; + do_push(specs, ARRAY_SIZE(specs), exp_stats, ARRAY_SIZE(exp_stats), exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1, &push_options); - if (git_futils_readbuffer(&push_options_result, _remote_push_options_result) < 0) - cl_fail("Failed to read push options result file"); - - cl_assert_equal_strn(expected_option, git_str_cstr(&push_options_result), - strlen(expected_option)); + cl_assert(git_fs_path_exists(push_options_path.ptr)); + cl_git_pass(git_futils_readbuffer(&push_options_result, push_options_path.ptr)); + cl_assert_equal_s(expected_option, git_str_cstr(&push_options_result)); git_str_dispose(&push_options_result); + git_str_dispose(&push_options_path); } void test_online_push__options(void) diff --git a/tests/resources/pushoptions.git/HEAD b/tests/resources/pushoptions.git/HEAD new file mode 100644 index 000000000..b870d8262 --- /dev/null +++ b/tests/resources/pushoptions.git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/main diff --git a/tests/resources/pushoptions.git/branches/.gitignore b/tests/resources/pushoptions.git/branches/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/tests/resources/pushoptions.git/config b/tests/resources/pushoptions.git/config new file mode 100644 index 000000000..23d39788f --- /dev/null +++ b/tests/resources/pushoptions.git/config @@ -0,0 +1,8 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = true + ignorecase = true + precomposeunicode = true +[receive] + advertisePushOptions = true diff --git a/tests/resources/pushoptions.git/description b/tests/resources/pushoptions.git/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/tests/resources/pushoptions.git/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/tests/resources/pushoptions.git/hooks/pre-receive b/tests/resources/pushoptions.git/hooks/pre-receive new file mode 100755 index 000000000..24f48d34c --- /dev/null +++ b/tests/resources/pushoptions.git/hooks/pre-receive @@ -0,0 +1,3 @@ +#!/bin/sh +printf "${GIT_PUSH_OPTION_1}${GIT_PUSH_OPTION_2}${GIT_PUSH_OPTION_3}" > "${GIT_PUSH_OPTION_0}" +exit 0 diff --git a/tests/resources/pushoptions.git/info/exclude b/tests/resources/pushoptions.git/info/exclude new file mode 100644 index 000000000..a5196d1be --- /dev/null +++ b/tests/resources/pushoptions.git/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/tests/resources/pushoptions.git/objects/info/.gitignore b/tests/resources/pushoptions.git/objects/info/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/tests/resources/pushoptions.git/objects/pack/.gitignore b/tests/resources/pushoptions.git/objects/pack/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/tests/resources/pushoptions.git/refs/heads/.gitignore b/tests/resources/pushoptions.git/refs/heads/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/tests/resources/pushoptions.git/refs/tags/.gitignore b/tests/resources/pushoptions.git/refs/tags/.gitignore new file mode 100644 index 000000000..e69de29bb