New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
02-pre-commit.t in vendors/t/svn-hooks – NEMO

source: vendors/t/svn-hooks/02-pre-commit.t @ 10669

Last change on this file since 10669 was 10669, checked in by nicolasmartin, 5 years ago

Import latest FCM release from Github into the repository for testing

File size: 11.4 KB
Line 
1#!/bin/bash
2#-------------------------------------------------------------------------------
3# (C) British Crown Copyright 2006-17 Met Office.
4#
5# This file is part of FCM, tools for managing and building source code.
6#
7# FCM is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# FCM is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with FCM. If not, see <http://www.gnu.org/licenses/>.
19#-------------------------------------------------------------------------------
20# Basic tests for "pre-commit".
21#-------------------------------------------------------------------------------
22. $(dirname $0)/test_header
23FCM_SVN_HOOK_ADMIN_EMAIL='your.admin.team'
24. $TEST_SOURCE_DIR/test_header_more
25
26test_tidy() {
27    rm -f \
28        "$REPOS_PATH/hooks/pre-commit-custom" \
29        "$REPOS_PATH/hooks/pre-commit-size-threshold.conf" \
30        "$REPOS_PATH/hooks/commit.conf" \
31        "$REPOS_PATH/hooks/svnperms.conf" \
32        "$REPOS_PATH/log/pre-commit.log" \
33        README \
34        bin/svnperms.py \
35        file1 \
36        file2 \
37        file3 \
38        file4 \
39        mail.out \
40        pre-commit-custom.out \
41        svnperms.py.out
42}
43#-------------------------------------------------------------------------------
44tests 50
45#-------------------------------------------------------------------------------
46cp -p "$FCM_HOME/etc/svn-hooks/pre-commit" "$REPOS_PATH/hooks/"
47sed -i "/set -eu/a\
48echo \$2 >$PWD/txn" "$REPOS_PATH/hooks/pre-commit"
49#-------------------------------------------------------------------------------
50TEST_KEY="$TEST_KEY_BASE-svnperm-1" # Blocked by svnperms.py
51# Install fake svnperms.py
52test_tidy
53cat >bin/svnperms.py <<__BASH__
54#!/bin/bash
55echo "\$@" >$PWD/svnperms.py.out
56echo "Access denied!" >&2
57false
58__BASH__
59chmod +x "bin/svnperms.py"
60echo '[foo]' >"$REPOS_PATH/hooks/svnperms.conf"
61# Try commit
62touch file1
63run_fail "$TEST_KEY" \
64    svn import --no-auth-cache -q -m'test' file1 "$REPOS_URL/file1"
65TXN=$(<txn)
66# Tests
67file_grep "$TEST_KEY.err" 'Access denied!' "$TEST_KEY.err"
68date2datefmt "$REPOS_PATH/log/pre-commit.log" \
69    >"$TEST_KEY.pre-commit.log.expected"
70file_cmp "$TEST_KEY.pre-commit.log" "$TEST_KEY.pre-commit.log.expected" <<__LOG__
71YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER
72A   file1
73Access denied!
74__LOG__
75file_cmp "$TEST_KEY.svnperms.py.out" svnperms.py.out <<__OUT__
76-r $REPOS_PATH -t $TXN -f $REPOS_PATH/hooks/svnperms.conf
77__OUT__
78date2datefmt mail.out >"$TEST_KEY.mail.out.expected"
79file_cmp  "$TEST_KEY.mail.out" "$TEST_KEY.mail.out.expected" <<__LOG__
80-s [pre-commit] $REPOS_PATH@$TXN your.admin.team
81YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER
82A   file1
83Access denied!
84__LOG__
85#-------------------------------------------------------------------------------
86TEST_KEY="$TEST_KEY_BASE-svnperm-2" # svnperms.conf is bad symlink
87# Install fake svnperms.py
88test_tidy
89ln -f -s "no-such-file" "$REPOS_PATH/hooks/svnperms.conf"
90# Try commit
91touch file1
92run_fail "$TEST_KEY" \
93    svn import --no-auth-cache -q -m'test' file1 "$REPOS_URL/file1"
94TXN=$(<txn)
95# Tests
96file_grep "$TEST_KEY.err" 'foo: permission configuration file not found.' \
97    "$TEST_KEY.err"
98file_grep "$TEST_KEY.err-2" 'your.admin.team has been notified.' "$TEST_KEY.err"
99date2datefmt "$REPOS_PATH/log/pre-commit.log" \
100    >"$TEST_KEY.pre-commit.log.expected"
101file_cmp "$TEST_KEY.pre-commit.log" "$TEST_KEY.pre-commit.log.expected" <<__LOG__
102YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER
103A   file1
104foo: permission configuration file not found.
105your.admin.team has been notified.
106__LOG__
107run_fail "$TEST_KEY.svnperms.py.out" test -e svnperms.py.out
108date2datefmt mail.out >"$TEST_KEY.mail.out.expected"
109file_cmp "$TEST_KEY.mail.out" "$TEST_KEY.mail.out.expected" <<__LOG__
110-s [pre-commit] $REPOS_PATH@$TXN your.admin.team
111YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER
112A   file1
113foo: permission configuration file not found.
114your.admin.team has been notified.
115__LOG__
116#-------------------------------------------------------------------------------
117TEST_KEY="$TEST_KEY_BASE-svnperm-3" # Good svnperms.conf
118test_tidy
119cat >bin/svnperms.py <<__BASH__
120#!/bin/bash
121echo "\$@" >$PWD/svnperms.py.out
122__BASH__
123chmod +x bin/svnperms.py
124echo '[foo]' >"$REPOS_PATH/hooks/svnperms.conf"
125# Try commit
126touch file1
127run_pass "$TEST_KEY" \
128    svn import --no-auth-cache -q -m'test' file1 "$REPOS_URL/file1"
129TXN=$(<txn)
130# Tests
131run_fail "$TEST_KEY.pre-commit.log" test -s "$REPOS_PATH/log/pre-commit.log"
132file_cmp "$TEST_KEY.svnperms.py.out" "svnperms.py.out" <<__OUT__
133-r $REPOS_PATH -t $TXN -f $REPOS_PATH/hooks/svnperms.conf
134__OUT__
135run_fail "$TEST_KEY.mail.out" test -e mail.out
136#-------------------------------------------------------------------------------
137TEST_KEY="$TEST_KEY_BASE-svnperm-4" # No svnperms.conf
138test_tidy
139# Try commit
140touch file2
141run_pass "$TEST_KEY" \
142    svn import --no-auth-cache -q -m'test' file2 "$REPOS_URL/file2"
143# Tests
144run_fail "$TEST_KEY.pre-commit.log" test -s "$REPOS_PATH/log/pre-commit.log"
145run_fail "$TEST_KEY.svnperms.py.out" test -e svnperms.py.out
146run_fail "$TEST_KEY.mail.out" test -e mail.out
147#-------------------------------------------------------------------------------
148TEST_KEY="$TEST_KEY_BASE-size-1" # bigger than default
149test_tidy
150perl -e 'map {print(rand())} 1..2097152' >file3 # a large file
151run_fail "$TEST_KEY" \
152    svn import --no-auth-cache -q -m'test' file3 "$REPOS_URL/file3"
153TXN=$(<txn)
154file_grep "$TEST_KEY.err" "foo@$TXN: changeset size ..MB exceeds 10MB." \
155    "$TEST_KEY.err"
156file_grep "$TEST_KEY.err-2" \
157    'Email your.admin.team if you need to bypass this restriction.' "$TEST_KEY.err"
158date2datefmt "$REPOS_PATH/log/pre-commit.log" \
159    | sed 's/\(size \).*\(MB exceeds\)/\1??\2/' \
160    >"$TEST_KEY.pre-commit.log.expected"
161file_cmp "$TEST_KEY.pre-commit.log" "$TEST_KEY.pre-commit.log.expected" <<__LOG__
162YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER
163A   file3
164foo@$TXN: changeset size ??MB exceeds 10MB.
165Email your.admin.team if you need to bypass this restriction.
166__LOG__
167date2datefmt mail.out | sed 's/\(size \).*\(MB exceeds\)/\1??\2/' \
168     >"$TEST_KEY.mail.out.expected"
169file_cmp "$TEST_KEY.mail.out.expected" "$TEST_KEY.mail.out.expected" <<__OUT__
170-s [pre-commit] $REPOS_PATH@$TXN your.admin.team
171YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER
172A   file3
173foo@$TXN: changeset size ??MB exceeds 10MB.
174Email your.admin.team if you need to bypass this restriction.
175__OUT__
176#-------------------------------------------------------------------------------
177TEST_KEY="$TEST_KEY_BASE-size-2" # bigger than default, threshold increased
178test_tidy
179echo '20' >"$REPOS_PATH/hooks/pre-commit-size-threshold.conf"
180perl -e 'map {print(rand())} 1..2097152' >file3 # a large file
181run_pass "$TEST_KEY" \
182    svn import --no-auth-cache -q -m'test' file3 "$REPOS_URL/file3"
183TXN="$(<'txn')"
184# Tests
185date2datefmt "$REPOS_PATH/log/pre-commit.log" \
186    | sed 's/\(size \).*\(MB exceeds\)/\1??\2/' \
187    >"$TEST_KEY.pre-commit.log.expected"
188file_cmp "${TEST_KEY}.pre-commit.log" "${TEST_KEY}.pre-commit.log.expected" <<__LOG__
189YYYY-mm-ddTHH:MM:SSZ+ ${TXN} by ${USER}
190A   file3
191foo@${TXN}: changeset size ??MB exceeds 1MB.
192__LOG__
193run_fail "$TEST_KEY.mail.out" test -e 'mail.out'
194#-------------------------------------------------------------------------------
195TEST_KEY="$TEST_KEY_BASE-custom-1" # block by custom script
196test_tidy
197cat >"$REPOS_PATH/hooks/pre-commit-custom" <<__BASH__
198#!/bin/bash
199echo "\$@" >$PWD/pre-commit-custom.out
200echo 'I am a blocker.' >&2
201false
202__BASH__
203chmod +x "$REPOS_PATH/hooks/pre-commit-custom"
204touch file4
205run_fail "$TEST_KEY" \
206    svn import --no-auth-cache -q -m'test' file4 "$REPOS_URL/file4"
207TXN=$(<txn)
208# Tests
209file_grep "$TEST_KEY.err" 'I am a blocker.' "$TEST_KEY.err"
210file_cmp "$TEST_KEY-custom.out" pre-commit-custom.out <<__OUT__
211$REPOS_PATH $TXN
212__OUT__
213date2datefmt "$REPOS_PATH/log/pre-commit.log" \
214    | sed 's/\(size \).*\(MB exceeds\)/\1??\2/' \
215     >"$TEST_KEY.pre-commit.log"
216file_cmp "$TEST_KEY.pre-commit.log" "$TEST_KEY.pre-commit.log" <<__OUT__
217YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER
218A   file4
219I am a blocker.
220__OUT__
221date2datefmt mail.out | sed 's/\(size \).*\(MB exceeds\)/\1??\2/' \
222     >"$TEST_KEY.mail.out.expected"
223file_cmp "$TEST_KEY.mail.out.expected" "$TEST_KEY.mail.out.expected" <<__OUT__
224-s [pre-commit] $REPOS_PATH@$TXN your.admin.team
225YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER
226A   file4
227I am a blocker.
228__OUT__
229#-------------------------------------------------------------------------------
230TEST_KEY="$TEST_KEY_BASE-custom-2" # custom script OK
231test_tidy
232cat >"$REPOS_PATH/hooks/pre-commit-custom" <<__BASH__
233#!/bin/bash
234echo "\$@" >$PWD/pre-commit-custom.out
235__BASH__
236chmod +x "$REPOS_PATH/hooks/pre-commit-custom"
237touch file4
238run_pass "$TEST_KEY" \
239    svn import --no-auth-cache -q -m'test' file4 "$REPOS_URL/file4"
240TXN=$(<txn)
241# Tests
242file_cmp "$TEST_KEY-custom.out" pre-commit-custom.out <<__OUT__
243$REPOS_PATH $TXN
244__OUT__
245run_fail "$TEST_KEY.pre-commit.log" test -s "$REPOS_PATH/log/pre-commit.log"
246run_fail "$TEST_KEY.mail.out" test -e mail.out
247#-------------------------------------------------------------------------------
248# Branch create owner verify, goods
249echo 'Hello World' >README
250svn import -m "hello: new project" README "$REPOS_URL/hello/trunk/README"
251rm README
252for KEY in $USER Share Config Rel; do
253    test_tidy
254    TEST_KEY="$TEST_KEY_BASE-branch-owner-$KEY"
255    echo 'verify-branch-owner' >"$REPOS_PATH/hooks/commit.conf"
256    run_pass "$TEST_KEY" svn cp --parents -m "$TEST_KEY" \
257        "$REPOS_URL/hello/trunk" "$REPOS_URL/hello/branches/dev/$KEY/whatever"
258    run_fail "$TEST_KEY.pre-commit.log" test -s "$REPOS_PATH/log/pre-commit.log"
259done
260#-------------------------------------------------------------------------------
261# Branch create owner verify, bad
262test_tidy
263TEST_KEY="$TEST_KEY_BASE-branch-owner-bad"
264echo 'verify-branch-owner' >"$REPOS_PATH/hooks/commit.conf"
265run_fail "$TEST_KEY" svn cp --parents -m "$TEST_KEY" \
266    "$REPOS_URL/hello/trunk" "$REPOS_URL/hello/branches/dev/nosuchuser/whatever"
267TXN=$(<txn)
268file_grep "$TEST_KEY.err" \
269    '\[INVALID BRANCH OWNER\] A   hello/branches/dev/nosuchuser/whatever/' \
270    "$TEST_KEY.err"
271date2datefmt "$REPOS_PATH/log/pre-commit.log" \
272    >"$TEST_KEY.pre-commit.log.expected"
273file_cmp "$TEST_KEY.pre-commit.log" \
274    "$TEST_KEY.pre-commit.log.expected" <<__LOG__
275YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER
276A   hello/branches/dev/nosuchuser/
277A   hello/branches/dev/nosuchuser/whatever/
278[INVALID BRANCH OWNER] A   hello/branches/dev/nosuchuser/whatever/
279__LOG__
280date2datefmt mail.out >"$TEST_KEY.mail.out.expected"
281file_cmp  "$TEST_KEY.mail.out" "$TEST_KEY.mail.out.expected" <<__LOG__
282-s [pre-commit] $REPOS_PATH@$TXN your.admin.team
283YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER
284A   hello/branches/dev/nosuchuser/
285A   hello/branches/dev/nosuchuser/whatever/
286[INVALID BRANCH OWNER] A   hello/branches/dev/nosuchuser/whatever/
287__LOG__
288#-------------------------------------------------------------------------------
289# Branch create owner no verify, bad
290test_tidy
291TEST_KEY="$TEST_KEY_BASE-branch-owner-no-verify-bad"
292run_pass "$TEST_KEY" svn cp --parents -m "$TEST_KEY" \
293    "$REPOS_URL/hello/trunk" "$REPOS_URL/hello/branches/dev/nosuchuser/whatever"
294run_fail "$TEST_KEY.pre-commit.log" test -s "$REPOS_PATH/log/pre-commit.log"
295#-------------------------------------------------------------------------------
296exit
Note: See TracBrowser for help on using the repository browser.