Debian Codegraph @

kokoro/testutils/github_release_util_test.sh

raw
1 #!/bin/bash
2 # Copyright 2022 Google LLC
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 ################################################################################
16
17 DEFAULT_DIR="$(pwd)"
18 if [[ -n "${TEST_SRCDIR}" ]]; then
19 DEFAULT_DIR="${TEST_SRCDIR}"
20 fi
21 readonly DEFAULT_DIR
22 readonly CLI="${DEFAULT_DIR}/${1:-"github_release_util.sh"}"
23 readonly TEST_UTILS="${DEFAULT_DIR}/${2:-test_utils.sh}"
24
25 # Load the test library.
26 source "${TEST_UTILS}"
27
28 test_GitHubReleaseUtil_CreateBranchMinorSucceeds() {
29 cd "${TEST_CASE_TMPDIR}"
30 local -r expected_git_cmds_file="${TEST_CASE_TMPDIR}/expected_cmds.txt"
31 cat << EOF > ${expected_git_cmds_file}
32 git ls-remote ssh://[email protected]/tink-crypto/some-repo
33 git clone ssh://[email protected]/tink-crypto/some-repo
34 git branch 1.6
35 git push origin 1.6
36 EOF
37
38 local -r actual_git_cmds_file="${TEST_CASE_TMPDIR}/actual_git_cmds_file.txt"
39 # Mock git command.
40 git() {
41 local -r command="$1"
42 shift 1
43 cmd_and_args="git ${command} $@"
44 echo "${cmd_and_args}" >> "${actual_git_cmds_file}"
45 case "${command}" in
46 "ls-remote")
47 cat << EOF
48 6c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD
49 6c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main
50 EOF
51 ;;
52 "clone")
53 local -r repo_name="${1##*/}"
54 mkdir "${repo_name}"
55 ;;
56 *) ;; # Do nothing
57 esac
58 }
59 # Run this in the caller's environment.
60 (
61 source "${CLI}" -r create_branch 1.6.0 some-repo &> /dev/null
62 )
63 ASSERT_CMD_SUCCEEDED
64 ASSERT_FILE_EQUALS "${actual_git_cmds_file}" "${expected_git_cmds_file}"
65 }
66
67 test_GitHubReleaseUtil_CreateBranchMinorWithCommitSucceeds() {
68 cd "${TEST_CASE_TMPDIR}"
69 local -r expected_git_cmds_file="${TEST_CASE_TMPDIR}/expected_cmds.txt"
70 cat << EOF > ${expected_git_cmds_file}
71 git ls-remote ssh://[email protected]/tink-crypto/some-repo
72 git clone ssh://[email protected]/tink-crypto/some-repo
73 git branch 1.6 6c68b48c884e0aeb983b8864f35187d9584d0d74
74 git push origin 1.6
75 EOF
76 local -r actual_git_cmds_file="${TEST_CASE_TMPDIR}/actual_git_cmds_file.txt"
77 # Mock git command.
78 git() {
79 local -r command="$1"
80 shift 1
81 cmd_and_args="git ${command} $@"
82 echo "${cmd_and_args}" >> "${actual_git_cmds_file}"
83 case "${command}" in
84 "ls-remote")
85 cat << EOF
86 6c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD
87 6c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main
88 EOF
89 ;;
90 "clone")
91 local -r repo_name="${1##*/}"
92 mkdir "${repo_name}"
93 ;;
94 *) ;; # Do nothing
95 esac
96 }
97
98 # Run this in the caller's environment.
99 (
100 source "${CLI}" -r -c 6c68b48c884e0aeb983b8864f35187d9584d0d74 \
101 create_branch 1.6.0 some-repo &> /dev/null
102 )
103 ASSERT_CMD_SUCCEEDED
104 ASSERT_FILE_EQUALS "${actual_git_cmds_file}" "${expected_git_cmds_file}"
105 }
106
107 test_GitHubReleaseUtil_CreateTagPatchSucceeds() {
108 cd "${TEST_CASE_TMPDIR}"
109 local -r expected_git_cmds_file="${TEST_CASE_TMPDIR}/expected_cmds.txt"
110 cat << EOF > ${expected_git_cmds_file}
111 git ls-remote ssh://[email protected]/tink-crypto/some-repo
112 git clone ssh://[email protected]/tink-crypto/some-repo
113 git checkout 1.6
114 git tag -a v1.6.2 -m some-repo version 1.6.2
115 git push origin v1.6.2
116 EOF
117 local -r actual_git_cmds_file="${TEST_CASE_TMPDIR}/actual_git_cmds_file.txt"
118 # Mock git command.
119 git() {
120 local -r command="$1"
121 shift 1
122 cmd_and_args="git ${command} $@"
123 echo "${cmd_and_args}" >> "${actual_git_cmds_file}"
124 case "${command}" in
125 "ls-remote")
126 cat << EOF
127 6c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD
128 6c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main
129 9940095f3081a116fa7a1337ad5ba27a3ccc59fe refs/heads/1.6
130 EOF
131 ;;
132 "clone")
133 local -r repo_name="${1##*/}"
134 mkdir "${repo_name}"
135 ;;
136 *) ;; # Do nothing.
137 esac
138 }
139
140 # Run this in the caller's environment.
141 (
142 source "${CLI}" -r -c 6c68b48c884e0aeb983b8864f35187d9584d0d74 \
143 create_tag 1.6.2 some-repo &> /dev/null
144 )
145 ASSERT_CMD_SUCCEEDED
146 ASSERT_FILE_EQUALS "${actual_git_cmds_file}" "${expected_git_cmds_file}"
147 }
148
149 test_GitHubReleaseUtil_CreateBranchFailsWhenLsRemoteFails() {
150 cd "${TEST_CASE_TMPDIR}"
151 local -r expected_git_cmds_file="${TEST_CASE_TMPDIR}/expected_cmds.txt"
152 cat << EOF > ${expected_git_cmds_file}
153 git ls-remote ssh://[email protected]/tink-crypto/some-repo
154 EOF
155 local actual_git_cmds_file="${TEST_CASE_TMPDIR}/actual_cmds.txt"
156 # Mock git command.
157 git() {
158 local -r command="$1"
159 shift 1
160 cmd_and_args="git ${command} $@"
161 echo "${cmd_and_args}" >> "${actual_git_cmds_file}"
162 case "${command}" in
163 "ls-remote") return 1 ;;
164 *) ;; # Do nothing.
165 esac
166 }
167
168 # Run this in a subshell to prevent exiting on failure.
169 (
170 source "${CLI}" -r create_branch 1.6.2 some-repo &> /dev/null
171 )
172 ASSERT_CMD_FAILED
173 ASSERT_FILE_EQUALS "${actual_git_cmds_file}" "${expected_git_cmds_file}"
174 echo "" > "${actual_git_cmds_file}"
175 }
176
177 test_GitHubReleaseUtil_CreateBranchFailsWhenCloneFails() {
178 cd "${TEST_CASE_TMPDIR}"
179 local -r expected_git_cmds_file="${TEST_CASE_TMPDIR}/expected_cmds.txt"
180 cat << EOF > ${expected_git_cmds_file}
181 git ls-remote ssh://[email protected]/tink-crypto/some-repo
182 git clone ssh://[email protected]/tink-crypto/some-repo
183 EOF
184 local actual_git_cmds_file="${TEST_CASE_TMPDIR}/actual_cmds.txt"
185 # Mock git command.
186 git() {
187 local -r command="$1"
188 shift 1
189 cmd_and_args="git ${command} $@"
190 echo "${cmd_and_args}" >> "${actual_git_cmds_file}"
191 case "${command}" in
192 "ls-remote")
193 cat << EOF
194 6c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD
195 6c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main
196 EOF
197 ;;
198 "clone") return 1 ;;
199 *) ;; # Do nothing.
200 esac
201 }
202
203 # Run this in a subshell to prevent exiting on failure.
204 (
205 source "${CLI}" -r create_branch 1.6.2 some-repo &> /dev/null
206 )
207 ASSERT_CMD_FAILED
208 ASSERT_FILE_EQUALS "${actual_git_cmds_file}" "${expected_git_cmds_file}"
209 }
210
211 test_GitHubReleaseUtil_CreateTagFailsIfBranchDoesNotExist() {
212 cd "${TEST_CASE_TMPDIR}"
213 local -r expected_git_cmds_file="${TEST_CASE_TMPDIR}/expected_cmds.txt"
214 cat << EOF > ${expected_git_cmds_file}
215 git ls-remote ssh://[email protected]/tink-crypto/some-repo
216 EOF
217 local actual_git_cmds_file="${TEST_CASE_TMPDIR}/actual_cmds.txt"
218 # Mock git command.
219 git() {
220 local -r command="$1"
221 shift 1
222 cmd_and_args="git ${command} $@"
223 echo "${cmd_and_args}" >> "${actual_git_cmds_file}"
224 case "${command}" in
225 "ls-remote")
226 cat << EOF
227 6c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD
228 6c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main
229 EOF
230 ;;
231 *) ;; # Do nothing.
232 esac
233 }
234
235 # Run this in a subshell to prevent exiting on failure.
236 (
237 source "${CLI}" -r create_tag 1.6.2 some-repo &> /dev/null
238 )
239
240 ASSERT_CMD_FAILED
241 ASSERT_FILE_EQUALS "${actual_git_cmds_file}" "${expected_git_cmds_file}"
242 }
243
244 test_GitHubReleaseUtil_CreateTagFailsIfReleaseTagAlreadyExists() {
245 cd "${TEST_CASE_TMPDIR}"
246 local -r expected_git_cmds_file="${TEST_CASE_TMPDIR}/expected_cmds.txt"
247 cat << EOF > ${expected_git_cmds_file}
248 git ls-remote ssh://[email protected]/tink-crypto/some-repo
249 EOF
250 local actual_git_cmds_file="${TEST_CASE_TMPDIR}/actual_cmds.txt"
251 # Mock git command.
252 git() {
253 local -r command="$1"
254 shift 1
255 cmd_and_args="git ${command} $@"
256 echo "${cmd_and_args}" >> "${actual_git_cmds_file}"
257 case "${command}" in
258 "ls-remote")
259 cat << EOF
260 6c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD
261 6c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main
262 112a7d3a0453a1d926448519f94fe5a91c69be45 refs/heads/1.6
263 8c266441044c4dfaf7560e21663a8037043b750b refs/tags/v1.6.2
264 195ec3c1edeee8877ab5dc287f95c4402e3fb510 refs/tags/v1.6.1
265 c6f48771296bca0bd22724b208abafeae7d7b764 refs/tags/v1.6.0
266 EOF
267 ;;
268 *) ;; # Do nothing.
269 esac
270 }
271
272 # Run this in a subshell to prevent exiting on failure.
273 (
274 source "${CLI}" -r create_tag 1.6.2 some-repo &> /dev/null
275 )
276
277 ASSERT_CMD_FAILED
278 ASSERT_FILE_EQUALS "${actual_git_cmds_file}" "${expected_git_cmds_file}"
279 }
280
281 test_GitHubReleaseUtil_FailsWhenInvalidVersion() {
282 cd "${TEST_CASE_TMPDIR}"
283 for action in create_branch create_tag; do
284 (
285 source "${CLI}" -r "${action}" 1 some-repo &> /dev/null
286 )
287 ASSERT_CMD_FAILED
288 (
289 source "${CLI}" -r "${action}" 1.2 some-repo &> /dev/null
290 )
291 ASSERT_CMD_FAILED
292 (
293 source "${CLI}" -r "${action}" 1.2.a some-repo &> /dev/null
294 )
295 ASSERT_CMD_FAILED
296 (
297 source "${CLI}" -r "${action}" a.b.c some-repo &> /dev/null
298 )
299 ASSERT_CMD_FAILED
300 (
301 source "${CLI}" -r "${action}" 1.2.3.4 some-repo &> /dev/null
302 )
303 ASSERT_CMD_FAILED
304 (
305 source "${CLI}" -r "${action}" invalid some-repo &> /dev/null
306 )
307 ASSERT_CMD_FAILED
308 done
309 }
310
311 test_GitHubReleaseUtil_FailsWhenNoRepoNameIsGiven() {
312 cd "${TEST_CASE_TMPDIR}"
313 for action in create_branch create_tag; do
314 # Run this in a subshell to prevent exiting on failure.
315 (
316 source "${CLI}" -r "${action}" 1.6.0 &> /dev/null
317 )
318 ASSERT_CMD_FAILED
319 done
320 }
321
322 test_GitHubReleaseUtil_CreateBranchUsesCorrectGithubToken() {
323 cd "${TEST_CASE_TMPDIR}"
324 local -r access_token="a227da63673c236090a067c3f96b62e74dbd5857"
325 local -r expected_url="https://ise-crypto:${access_token}@github.com/tink-crypto/some-repo"
326 local -r expected_git_cmds_file="${TEST_CASE_TMPDIR}/expected_cmds.txt"
327 cat << EOF > ${expected_git_cmds_file}
328 git ls-remote ${expected_url}
329 git clone ${expected_url}
330 git branch 1.6
331 git push origin 1.6
332 EOF
333 local -r actual_git_cmds_file="${TEST_CASE_TMPDIR}/actual_git_cmds_file.txt"
334 # Mock git command.
335 git() {
336 local -r command="$1"
337 shift 1
338 cmd_and_args="git ${command} $@"
339 echo "${cmd_and_args}" >> "${actual_git_cmds_file}"
340 case "${command}" in
341 "ls-remote")
342 cat << EOF
343 6c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD
344 6c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main
345 EOF
346 ;;
347 "clone")
348 local -r repo_name="${1##*/}"
349 mkdir "${repo_name}"
350 ;;
351 *) ;; # Do nothing
352 esac
353 }
354
355 # Run this in the caller's environment.
356 (
357 source "${CLI}" -r -t "${access_token}" create_branch 1.6.0 some-repo \
358 &> /dev/null
359 )
360 ASSERT_CMD_SUCCEEDED
361 ASSERT_FILE_EQUALS "${actual_git_cmds_file}" "${expected_git_cmds_file}"
362 }
363
364 test_GitHubReleaseUtil_CreateTagUsesCorrectGithubToken() {
365 cd "${TEST_CASE_TMPDIR}"
366 local -r access_token="a227da63673c236090a067c3f96b62e74dbd5857"
367 local -r expected_url="https://ise-crypto:${access_token}@github.com/tink-crypto/some-repo"
368 local -r expected_git_cmds_file="${TEST_CASE_TMPDIR}/expected_cmds.txt"
369 cat << EOF > ${expected_git_cmds_file}
370 git ls-remote ${expected_url}
371 git clone ${expected_url}
372 git checkout 1.6
373 git tag -a v1.6.2 -m some-repo version 1.6.2
374 git push origin v1.6.2
375 EOF
376 local -r actual_git_cmds_file="${TEST_CASE_TMPDIR}/actual_git_cmds_file.txt"
377 # Mock git command.
378 git() {
379 local -r command="$1"
380 shift 1
381 cmd_and_args="git ${command} $@"
382 echo "${cmd_and_args}" >> "${actual_git_cmds_file}"
383 case "${command}" in
384 "ls-remote")
385 cat << EOF
386 6c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD
387 6c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main
388 112a7d3a0453a1d926448519f94fe5a91c69be45 refs/heads/1.6
389 EOF
390 ;;
391 "clone")
392 local -r repo_name="${1##*/}"
393 mkdir "${repo_name}"
394 ;;
395 *) ;; # Do nothing
396 esac
397 }
398
399 # Run this in the caller's environment.
400 (
401 source "${CLI}" -r -t "${access_token}" create_tag 1.6.2 some-repo \
402 &> /dev/null
403 )
404 ASSERT_CMD_SUCCEEDED
405 ASSERT_FILE_EQUALS "${actual_git_cmds_file}" "${expected_git_cmds_file}"
406 }
407
408 main() {
409 run_all_tests "$@"
410 }
411
412 main "$@"