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 |
---|
23 | FCM_SVN_HOOK_ADMIN_EMAIL='your.admin.team' |
---|
24 | . $TEST_SOURCE_DIR/test_header_more |
---|
25 | |
---|
26 | test_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 | #------------------------------------------------------------------------------- |
---|
44 | tests 50 |
---|
45 | #------------------------------------------------------------------------------- |
---|
46 | cp -p "$FCM_HOME/etc/svn-hooks/pre-commit" "$REPOS_PATH/hooks/" |
---|
47 | sed -i "/set -eu/a\ |
---|
48 | echo \$2 >$PWD/txn" "$REPOS_PATH/hooks/pre-commit" |
---|
49 | #------------------------------------------------------------------------------- |
---|
50 | TEST_KEY="$TEST_KEY_BASE-svnperm-1" # Blocked by svnperms.py |
---|
51 | # Install fake svnperms.py |
---|
52 | test_tidy |
---|
53 | cat >bin/svnperms.py <<__BASH__ |
---|
54 | #!/bin/bash |
---|
55 | echo "\$@" >$PWD/svnperms.py.out |
---|
56 | echo "Access denied!" >&2 |
---|
57 | false |
---|
58 | __BASH__ |
---|
59 | chmod +x "bin/svnperms.py" |
---|
60 | echo '[foo]' >"$REPOS_PATH/hooks/svnperms.conf" |
---|
61 | # Try commit |
---|
62 | touch file1 |
---|
63 | run_fail "$TEST_KEY" \ |
---|
64 | svn import --no-auth-cache -q -m'test' file1 "$REPOS_URL/file1" |
---|
65 | TXN=$(<txn) |
---|
66 | # Tests |
---|
67 | file_grep "$TEST_KEY.err" 'Access denied!' "$TEST_KEY.err" |
---|
68 | date2datefmt "$REPOS_PATH/log/pre-commit.log" \ |
---|
69 | >"$TEST_KEY.pre-commit.log.expected" |
---|
70 | file_cmp "$TEST_KEY.pre-commit.log" "$TEST_KEY.pre-commit.log.expected" <<__LOG__ |
---|
71 | YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER |
---|
72 | A file1 |
---|
73 | Access denied! |
---|
74 | __LOG__ |
---|
75 | file_cmp "$TEST_KEY.svnperms.py.out" svnperms.py.out <<__OUT__ |
---|
76 | -r $REPOS_PATH -t $TXN -f $REPOS_PATH/hooks/svnperms.conf |
---|
77 | __OUT__ |
---|
78 | date2datefmt mail.out >"$TEST_KEY.mail.out.expected" |
---|
79 | file_cmp "$TEST_KEY.mail.out" "$TEST_KEY.mail.out.expected" <<__LOG__ |
---|
80 | -s [pre-commit] $REPOS_PATH@$TXN your.admin.team |
---|
81 | YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER |
---|
82 | A file1 |
---|
83 | Access denied! |
---|
84 | __LOG__ |
---|
85 | #------------------------------------------------------------------------------- |
---|
86 | TEST_KEY="$TEST_KEY_BASE-svnperm-2" # svnperms.conf is bad symlink |
---|
87 | # Install fake svnperms.py |
---|
88 | test_tidy |
---|
89 | ln -f -s "no-such-file" "$REPOS_PATH/hooks/svnperms.conf" |
---|
90 | # Try commit |
---|
91 | touch file1 |
---|
92 | run_fail "$TEST_KEY" \ |
---|
93 | svn import --no-auth-cache -q -m'test' file1 "$REPOS_URL/file1" |
---|
94 | TXN=$(<txn) |
---|
95 | # Tests |
---|
96 | file_grep "$TEST_KEY.err" 'foo: permission configuration file not found.' \ |
---|
97 | "$TEST_KEY.err" |
---|
98 | file_grep "$TEST_KEY.err-2" 'your.admin.team has been notified.' "$TEST_KEY.err" |
---|
99 | date2datefmt "$REPOS_PATH/log/pre-commit.log" \ |
---|
100 | >"$TEST_KEY.pre-commit.log.expected" |
---|
101 | file_cmp "$TEST_KEY.pre-commit.log" "$TEST_KEY.pre-commit.log.expected" <<__LOG__ |
---|
102 | YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER |
---|
103 | A file1 |
---|
104 | foo: permission configuration file not found. |
---|
105 | your.admin.team has been notified. |
---|
106 | __LOG__ |
---|
107 | run_fail "$TEST_KEY.svnperms.py.out" test -e svnperms.py.out |
---|
108 | date2datefmt mail.out >"$TEST_KEY.mail.out.expected" |
---|
109 | file_cmp "$TEST_KEY.mail.out" "$TEST_KEY.mail.out.expected" <<__LOG__ |
---|
110 | -s [pre-commit] $REPOS_PATH@$TXN your.admin.team |
---|
111 | YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER |
---|
112 | A file1 |
---|
113 | foo: permission configuration file not found. |
---|
114 | your.admin.team has been notified. |
---|
115 | __LOG__ |
---|
116 | #------------------------------------------------------------------------------- |
---|
117 | TEST_KEY="$TEST_KEY_BASE-svnperm-3" # Good svnperms.conf |
---|
118 | test_tidy |
---|
119 | cat >bin/svnperms.py <<__BASH__ |
---|
120 | #!/bin/bash |
---|
121 | echo "\$@" >$PWD/svnperms.py.out |
---|
122 | __BASH__ |
---|
123 | chmod +x bin/svnperms.py |
---|
124 | echo '[foo]' >"$REPOS_PATH/hooks/svnperms.conf" |
---|
125 | # Try commit |
---|
126 | touch file1 |
---|
127 | run_pass "$TEST_KEY" \ |
---|
128 | svn import --no-auth-cache -q -m'test' file1 "$REPOS_URL/file1" |
---|
129 | TXN=$(<txn) |
---|
130 | # Tests |
---|
131 | run_fail "$TEST_KEY.pre-commit.log" test -s "$REPOS_PATH/log/pre-commit.log" |
---|
132 | file_cmp "$TEST_KEY.svnperms.py.out" "svnperms.py.out" <<__OUT__ |
---|
133 | -r $REPOS_PATH -t $TXN -f $REPOS_PATH/hooks/svnperms.conf |
---|
134 | __OUT__ |
---|
135 | run_fail "$TEST_KEY.mail.out" test -e mail.out |
---|
136 | #------------------------------------------------------------------------------- |
---|
137 | TEST_KEY="$TEST_KEY_BASE-svnperm-4" # No svnperms.conf |
---|
138 | test_tidy |
---|
139 | # Try commit |
---|
140 | touch file2 |
---|
141 | run_pass "$TEST_KEY" \ |
---|
142 | svn import --no-auth-cache -q -m'test' file2 "$REPOS_URL/file2" |
---|
143 | # Tests |
---|
144 | run_fail "$TEST_KEY.pre-commit.log" test -s "$REPOS_PATH/log/pre-commit.log" |
---|
145 | run_fail "$TEST_KEY.svnperms.py.out" test -e svnperms.py.out |
---|
146 | run_fail "$TEST_KEY.mail.out" test -e mail.out |
---|
147 | #------------------------------------------------------------------------------- |
---|
148 | TEST_KEY="$TEST_KEY_BASE-size-1" # bigger than default |
---|
149 | test_tidy |
---|
150 | perl -e 'map {print(rand())} 1..2097152' >file3 # a large file |
---|
151 | run_fail "$TEST_KEY" \ |
---|
152 | svn import --no-auth-cache -q -m'test' file3 "$REPOS_URL/file3" |
---|
153 | TXN=$(<txn) |
---|
154 | file_grep "$TEST_KEY.err" "foo@$TXN: changeset size ..MB exceeds 10MB." \ |
---|
155 | "$TEST_KEY.err" |
---|
156 | file_grep "$TEST_KEY.err-2" \ |
---|
157 | 'Email your.admin.team if you need to bypass this restriction.' "$TEST_KEY.err" |
---|
158 | date2datefmt "$REPOS_PATH/log/pre-commit.log" \ |
---|
159 | | sed 's/\(size \).*\(MB exceeds\)/\1??\2/' \ |
---|
160 | >"$TEST_KEY.pre-commit.log.expected" |
---|
161 | file_cmp "$TEST_KEY.pre-commit.log" "$TEST_KEY.pre-commit.log.expected" <<__LOG__ |
---|
162 | YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER |
---|
163 | A file3 |
---|
164 | foo@$TXN: changeset size ??MB exceeds 10MB. |
---|
165 | Email your.admin.team if you need to bypass this restriction. |
---|
166 | __LOG__ |
---|
167 | date2datefmt mail.out | sed 's/\(size \).*\(MB exceeds\)/\1??\2/' \ |
---|
168 | >"$TEST_KEY.mail.out.expected" |
---|
169 | file_cmp "$TEST_KEY.mail.out.expected" "$TEST_KEY.mail.out.expected" <<__OUT__ |
---|
170 | -s [pre-commit] $REPOS_PATH@$TXN your.admin.team |
---|
171 | YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER |
---|
172 | A file3 |
---|
173 | foo@$TXN: changeset size ??MB exceeds 10MB. |
---|
174 | Email your.admin.team if you need to bypass this restriction. |
---|
175 | __OUT__ |
---|
176 | #------------------------------------------------------------------------------- |
---|
177 | TEST_KEY="$TEST_KEY_BASE-size-2" # bigger than default, threshold increased |
---|
178 | test_tidy |
---|
179 | echo '20' >"$REPOS_PATH/hooks/pre-commit-size-threshold.conf" |
---|
180 | perl -e 'map {print(rand())} 1..2097152' >file3 # a large file |
---|
181 | run_pass "$TEST_KEY" \ |
---|
182 | svn import --no-auth-cache -q -m'test' file3 "$REPOS_URL/file3" |
---|
183 | TXN="$(<'txn')" |
---|
184 | # Tests |
---|
185 | date2datefmt "$REPOS_PATH/log/pre-commit.log" \ |
---|
186 | | sed 's/\(size \).*\(MB exceeds\)/\1??\2/' \ |
---|
187 | >"$TEST_KEY.pre-commit.log.expected" |
---|
188 | file_cmp "${TEST_KEY}.pre-commit.log" "${TEST_KEY}.pre-commit.log.expected" <<__LOG__ |
---|
189 | YYYY-mm-ddTHH:MM:SSZ+ ${TXN} by ${USER} |
---|
190 | A file3 |
---|
191 | foo@${TXN}: changeset size ??MB exceeds 1MB. |
---|
192 | __LOG__ |
---|
193 | run_fail "$TEST_KEY.mail.out" test -e 'mail.out' |
---|
194 | #------------------------------------------------------------------------------- |
---|
195 | TEST_KEY="$TEST_KEY_BASE-custom-1" # block by custom script |
---|
196 | test_tidy |
---|
197 | cat >"$REPOS_PATH/hooks/pre-commit-custom" <<__BASH__ |
---|
198 | #!/bin/bash |
---|
199 | echo "\$@" >$PWD/pre-commit-custom.out |
---|
200 | echo 'I am a blocker.' >&2 |
---|
201 | false |
---|
202 | __BASH__ |
---|
203 | chmod +x "$REPOS_PATH/hooks/pre-commit-custom" |
---|
204 | touch file4 |
---|
205 | run_fail "$TEST_KEY" \ |
---|
206 | svn import --no-auth-cache -q -m'test' file4 "$REPOS_URL/file4" |
---|
207 | TXN=$(<txn) |
---|
208 | # Tests |
---|
209 | file_grep "$TEST_KEY.err" 'I am a blocker.' "$TEST_KEY.err" |
---|
210 | file_cmp "$TEST_KEY-custom.out" pre-commit-custom.out <<__OUT__ |
---|
211 | $REPOS_PATH $TXN |
---|
212 | __OUT__ |
---|
213 | date2datefmt "$REPOS_PATH/log/pre-commit.log" \ |
---|
214 | | sed 's/\(size \).*\(MB exceeds\)/\1??\2/' \ |
---|
215 | >"$TEST_KEY.pre-commit.log" |
---|
216 | file_cmp "$TEST_KEY.pre-commit.log" "$TEST_KEY.pre-commit.log" <<__OUT__ |
---|
217 | YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER |
---|
218 | A file4 |
---|
219 | I am a blocker. |
---|
220 | __OUT__ |
---|
221 | date2datefmt mail.out | sed 's/\(size \).*\(MB exceeds\)/\1??\2/' \ |
---|
222 | >"$TEST_KEY.mail.out.expected" |
---|
223 | file_cmp "$TEST_KEY.mail.out.expected" "$TEST_KEY.mail.out.expected" <<__OUT__ |
---|
224 | -s [pre-commit] $REPOS_PATH@$TXN your.admin.team |
---|
225 | YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER |
---|
226 | A file4 |
---|
227 | I am a blocker. |
---|
228 | __OUT__ |
---|
229 | #------------------------------------------------------------------------------- |
---|
230 | TEST_KEY="$TEST_KEY_BASE-custom-2" # custom script OK |
---|
231 | test_tidy |
---|
232 | cat >"$REPOS_PATH/hooks/pre-commit-custom" <<__BASH__ |
---|
233 | #!/bin/bash |
---|
234 | echo "\$@" >$PWD/pre-commit-custom.out |
---|
235 | __BASH__ |
---|
236 | chmod +x "$REPOS_PATH/hooks/pre-commit-custom" |
---|
237 | touch file4 |
---|
238 | run_pass "$TEST_KEY" \ |
---|
239 | svn import --no-auth-cache -q -m'test' file4 "$REPOS_URL/file4" |
---|
240 | TXN=$(<txn) |
---|
241 | # Tests |
---|
242 | file_cmp "$TEST_KEY-custom.out" pre-commit-custom.out <<__OUT__ |
---|
243 | $REPOS_PATH $TXN |
---|
244 | __OUT__ |
---|
245 | run_fail "$TEST_KEY.pre-commit.log" test -s "$REPOS_PATH/log/pre-commit.log" |
---|
246 | run_fail "$TEST_KEY.mail.out" test -e mail.out |
---|
247 | #------------------------------------------------------------------------------- |
---|
248 | # Branch create owner verify, goods |
---|
249 | echo 'Hello World' >README |
---|
250 | svn import -m "hello: new project" README "$REPOS_URL/hello/trunk/README" |
---|
251 | rm README |
---|
252 | for 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" |
---|
259 | done |
---|
260 | #------------------------------------------------------------------------------- |
---|
261 | # Branch create owner verify, bad |
---|
262 | test_tidy |
---|
263 | TEST_KEY="$TEST_KEY_BASE-branch-owner-bad" |
---|
264 | echo 'verify-branch-owner' >"$REPOS_PATH/hooks/commit.conf" |
---|
265 | run_fail "$TEST_KEY" svn cp --parents -m "$TEST_KEY" \ |
---|
266 | "$REPOS_URL/hello/trunk" "$REPOS_URL/hello/branches/dev/nosuchuser/whatever" |
---|
267 | TXN=$(<txn) |
---|
268 | file_grep "$TEST_KEY.err" \ |
---|
269 | '\[INVALID BRANCH OWNER\] A hello/branches/dev/nosuchuser/whatever/' \ |
---|
270 | "$TEST_KEY.err" |
---|
271 | date2datefmt "$REPOS_PATH/log/pre-commit.log" \ |
---|
272 | >"$TEST_KEY.pre-commit.log.expected" |
---|
273 | file_cmp "$TEST_KEY.pre-commit.log" \ |
---|
274 | "$TEST_KEY.pre-commit.log.expected" <<__LOG__ |
---|
275 | YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER |
---|
276 | A hello/branches/dev/nosuchuser/ |
---|
277 | A hello/branches/dev/nosuchuser/whatever/ |
---|
278 | [INVALID BRANCH OWNER] A hello/branches/dev/nosuchuser/whatever/ |
---|
279 | __LOG__ |
---|
280 | date2datefmt mail.out >"$TEST_KEY.mail.out.expected" |
---|
281 | file_cmp "$TEST_KEY.mail.out" "$TEST_KEY.mail.out.expected" <<__LOG__ |
---|
282 | -s [pre-commit] $REPOS_PATH@$TXN your.admin.team |
---|
283 | YYYY-mm-ddTHH:MM:SSZ+ $TXN by $USER |
---|
284 | A hello/branches/dev/nosuchuser/ |
---|
285 | A 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 |
---|
290 | test_tidy |
---|
291 | TEST_KEY="$TEST_KEY_BASE-branch-owner-no-verify-bad" |
---|
292 | run_pass "$TEST_KEY" svn cp --parents -m "$TEST_KEY" \ |
---|
293 | "$REPOS_URL/hello/trunk" "$REPOS_URL/hello/branches/dev/nosuchuser/whatever" |
---|
294 | run_fail "$TEST_KEY.pre-commit.log" test -s "$REPOS_PATH/log/pre-commit.log" |
---|
295 | #------------------------------------------------------------------------------- |
---|
296 | exit |
---|