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 17DEFAULT_DIR="$(pwd)" 18if [[ -n "${TEST_SRCDIR}" ]]; then 19 DEFAULT_DIR="${TEST_SRCDIR}" 20fi 21readonly DEFAULT_DIR 22readonly CLI="${DEFAULT_DIR}/${1:-"github_release_util.sh"}" 23readonly TEST_UTILS="${DEFAULT_DIR}/${2:-test_utils.sh}" 24 25# Load the test library. 26source "${TEST_UTILS}" 27 28test_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} 32git ls-remote ssh://git@github.com/tink-crypto/some-repo 33git clone ssh://git@github.com/tink-crypto/some-repo 34git branch 1.6 35git push origin 1.6 36EOF 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 486c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD 496c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main 50EOF 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 67test_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} 71git ls-remote ssh://git@github.com/tink-crypto/some-repo 72git clone ssh://git@github.com/tink-crypto/some-repo 73git branch 1.6 6c68b48c884e0aeb983b8864f35187d9584d0d74 74git push origin 1.6 75EOF 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 866c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD 876c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main 88EOF 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 107test_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} 111git ls-remote ssh://git@github.com/tink-crypto/some-repo 112git clone ssh://git@github.com/tink-crypto/some-repo 113git checkout 1.6 114git tag -a v1.6.2 -m some-repo version 1.6.2 115git push origin v1.6.2 116EOF 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 1276c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD 1286c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main 1299940095f3081a116fa7a1337ad5ba27a3ccc59fe refs/heads/1.6 130EOF 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 149test_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} 153git ls-remote ssh://git@github.com/tink-crypto/some-repo 154EOF 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 177test_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} 181git ls-remote ssh://git@github.com/tink-crypto/some-repo 182git clone ssh://git@github.com/tink-crypto/some-repo 183EOF 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 1946c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD 1956c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main 196EOF 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 211test_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} 215git ls-remote ssh://git@github.com/tink-crypto/some-repo 216EOF 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 2276c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD 2286c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main 229EOF 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 244test_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} 248git ls-remote ssh://git@github.com/tink-crypto/some-repo 249EOF 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 2606c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD 2616c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main 262112a7d3a0453a1d926448519f94fe5a91c69be45 refs/heads/1.6 2638c266441044c4dfaf7560e21663a8037043b750b refs/tags/v1.6.2 264195ec3c1edeee8877ab5dc287f95c4402e3fb510 refs/tags/v1.6.1 265c6f48771296bca0bd22724b208abafeae7d7b764 refs/tags/v1.6.0 266EOF 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 281test_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 311test_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 322test_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} 328git ls-remote ${expected_url} 329git clone ${expected_url} 330git branch 1.6 331git push origin 1.6 332EOF 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 3436c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD 3446c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main 345EOF 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 364test_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} 370git ls-remote ${expected_url} 371git clone ${expected_url} 372git checkout 1.6 373git tag -a v1.6.2 -m some-repo version 1.6.2 374git push origin v1.6.2 375EOF 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 3866c68b48c884e0aeb983b8864f35187d9584d0d74 HEAD 3876c68b48c884e0aeb983b8864f35187d9584d0d74 refs/heads/main 388112a7d3a0453a1d926448519f94fe5a91c69be45 refs/heads/1.6 389EOF 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 408main() { 409 run_all_tests "$@" 410} 411 412main "$@" 413