Skip to content

Commit 86faaf9

Browse files
committed
Merge branch 'cb/git-daemon-tests'
* cb/git-daemon-tests: git-daemon tests: wait until daemon is ready git-daemon: produce output when ready git-daemon: add tests
2 parents 6e1c9bb + 561b133 commit 86faaf9

File tree

3 files changed

+220
-3
lines changed

3 files changed

+220
-3
lines changed

daemon.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,8 @@ static int serve(struct string_list *listen_addr, int listen_port,
10861086

10871087
drop_privileges(cred);
10881088

1089+
loginfo("Ready to rumble");
1090+
10891091
return service_loop(&socklist);
10901092
}
10911093

@@ -1270,10 +1272,8 @@ int main(int argc, char **argv)
12701272
if (inetd_mode || serve_mode)
12711273
return execute();
12721274

1273-
if (detach) {
1275+
if (detach)
12741276
daemonize();
1275-
loginfo("Ready to rumble");
1276-
}
12771277
else
12781278
sanitize_stdfds();
12791279

t/lib-git-daemon.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/sh
2+
3+
if test -z "$GIT_TEST_GIT_DAEMON"
4+
then
5+
skip_all="git-daemon testing disabled (define GIT_TEST_GIT_DAEMON to enable)"
6+
test_done
7+
fi
8+
9+
LIB_GIT_DAEMON_PORT=${LIB_GIT_DAEMON_PORT-'8121'}
10+
11+
GIT_DAEMON_PID=
12+
GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
13+
GIT_DAEMON_URL=git://127.0.0.1:$LIB_GIT_DAEMON_PORT
14+
15+
start_git_daemon() {
16+
if test -n "$GIT_DAEMON_PID"
17+
then
18+
error "start_git_daemon already called"
19+
fi
20+
21+
mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH"
22+
23+
trap 'code=$?; stop_git_daemon; (exit $code); die' EXIT
24+
25+
say >&3 "Starting git daemon ..."
26+
mkfifo git_daemon_output
27+
git daemon --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
28+
--reuseaddr --verbose \
29+
--base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
30+
"$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
31+
>&3 2>git_daemon_output &
32+
GIT_DAEMON_PID=$!
33+
{
34+
read line
35+
echo >&4 "$line"
36+
cat >&4 &
37+
38+
# Check expected output
39+
if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"
40+
then
41+
kill "$GIT_DAEMON_PID"
42+
wait "$GIT_DAEMON_PID"
43+
trap 'die' EXIT
44+
error "git daemon failed to start"
45+
fi
46+
} <git_daemon_output
47+
}
48+
49+
stop_git_daemon() {
50+
if test -z "$GIT_DAEMON_PID"
51+
then
52+
return
53+
fi
54+
55+
trap 'die' EXIT
56+
57+
# kill git-daemon child of git
58+
say >&3 "Stopping git daemon ..."
59+
kill "$GIT_DAEMON_PID"
60+
wait "$GIT_DAEMON_PID" >&3 2>&4
61+
ret=$?
62+
# expect exit with status 143 = 128+15 for signal TERM=15
63+
if test $ret -ne 143
64+
then
65+
error "git daemon exited with status: $ret"
66+
fi
67+
GIT_DAEMON_PID=
68+
rm -f git_daemon_output
69+
}

t/t5570-git-daemon.sh

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/bin/sh
2+
3+
test_description='test fetching over git protocol'
4+
. ./test-lib.sh
5+
6+
LIB_GIT_DAEMON_PORT=${LIB_GIT_DAEMON_PORT-5570}
7+
. "$TEST_DIRECTORY"/lib-git-daemon.sh
8+
start_git_daemon
9+
10+
test_expect_success 'setup repository' '
11+
echo content >file &&
12+
git add file &&
13+
git commit -m one
14+
'
15+
16+
test_expect_success 'create git-accessible bare repository' '
17+
mkdir "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
18+
(cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
19+
git --bare init &&
20+
: >git-daemon-export-ok
21+
) &&
22+
git remote add public "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
23+
git push public master:master
24+
'
25+
26+
test_expect_success 'clone git repository' '
27+
git clone "$GIT_DAEMON_URL/repo.git" clone &&
28+
test_cmp file clone/file
29+
'
30+
31+
test_expect_success 'fetch changes via git protocol' '
32+
echo content >>file &&
33+
git commit -a -m two &&
34+
git push public &&
35+
(cd clone && git pull) &&
36+
test_cmp file clone/file
37+
'
38+
39+
test_expect_failure 'remote detects correct HEAD' '
40+
git push public master:other &&
41+
(cd clone &&
42+
git remote set-head -d origin &&
43+
git remote set-head -a origin &&
44+
git symbolic-ref refs/remotes/origin/HEAD > output &&
45+
echo refs/remotes/origin/master > expect &&
46+
test_cmp expect output
47+
)
48+
'
49+
50+
test_expect_success 'prepare pack objects' '
51+
cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
52+
(cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
53+
git --bare repack -a -d
54+
)
55+
'
56+
57+
test_expect_success 'fetch notices corrupt pack' '
58+
cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
59+
(cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
60+
p=`ls objects/pack/pack-*.pack` &&
61+
chmod u+w $p &&
62+
printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
63+
) &&
64+
mkdir repo_bad1.git &&
65+
(cd repo_bad1.git &&
66+
git --bare init &&
67+
test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad1.git" &&
68+
test 0 = `ls objects/pack/pack-*.pack | wc -l`
69+
)
70+
'
71+
72+
test_expect_success 'fetch notices corrupt idx' '
73+
cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
74+
(cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
75+
p=`ls objects/pack/pack-*.idx` &&
76+
chmod u+w $p &&
77+
printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
78+
) &&
79+
mkdir repo_bad2.git &&
80+
(cd repo_bad2.git &&
81+
git --bare init &&
82+
test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad2.git" &&
83+
test 0 = `ls objects/pack | wc -l`
84+
)
85+
'
86+
87+
test_remote_error()
88+
{
89+
do_export=YesPlease
90+
while test $# -gt 0
91+
do
92+
case $1 in
93+
-x)
94+
shift
95+
chmod -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
96+
;;
97+
-n)
98+
shift
99+
do_export=
100+
;;
101+
*)
102+
break
103+
esac
104+
done
105+
106+
if test $# -ne 3
107+
then
108+
error "invalid number of arguments"
109+
fi
110+
111+
cmd=$1
112+
repo=$2
113+
msg=$3
114+
115+
if test -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo"
116+
then
117+
if test -n "$do_export"
118+
then
119+
: >"$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
120+
else
121+
rm -f "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
122+
fi
123+
fi
124+
125+
test_must_fail git "$cmd" "$GIT_DAEMON_URL/$repo" 2>output &&
126+
echo "fatal: remote error: $msg: /$repo" >expect &&
127+
test_cmp expect output
128+
ret=$?
129+
chmod +x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
130+
(exit $ret)
131+
}
132+
133+
msg="access denied or repository not exported"
134+
test_expect_success 'clone non-existent' "test_remote_error clone nowhere.git '$msg'"
135+
test_expect_success 'push disabled' "test_remote_error push repo.git '$msg'"
136+
test_expect_success 'read access denied' "test_remote_error -x fetch repo.git '$msg'"
137+
test_expect_success 'not exported' "test_remote_error -n fetch repo.git '$msg'"
138+
139+
stop_git_daemon
140+
start_git_daemon --informative-errors
141+
142+
test_expect_success 'clone non-existent' "test_remote_error clone nowhere.git 'no such repository'"
143+
test_expect_success 'push disabled' "test_remote_error push repo.git 'service not enabled'"
144+
test_expect_success 'read access denied' "test_remote_error -x fetch repo.git 'no such repository'"
145+
test_expect_success 'not exported' "test_remote_error -n fetch repo.git 'repository not exported'"
146+
147+
stop_git_daemon
148+
test_done

0 commit comments

Comments
 (0)