| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | . "$(dirname "$0")/testlib.sh" |
| 4 | |
| 5 | setup_expected_concurrent_transfers |
| 6 | |
| 7 | begin_test "batch transfer" |
| 8 | ( |
| 9 | set -e |
| 10 | |
| 11 | # This initializes a new bare git repository in test/remote. |
| 12 | # These remote repositories are global to every test, so keep the names |
| 13 | # unique. |
| 14 | reponame1="$(basename "$0" ".sh")" |
| 15 | reponame2="CAPITALLETTERS" |
| 16 | reponame=$reponame1$reponame2 |
| 17 | setup_remote_repo "$reponame" |
| 18 | |
| 19 | # Clone the repository from the test Git server. This is empty, and will be |
| 20 | # used to test a "git pull" below. The repo is cloned to $TRASHDIR/clone |
| 21 | clone_repo "$reponame" clone |
| 22 | |
| 23 | # Clone the repository again to $TRASHDIR/repo. This will be used to commit |
| 24 | # and push objects. |
| 25 | clone_repo "$reponame" repo |
| 26 | |
| 27 | # This executes Git LFS from the local repo that was just cloned. |
| 28 | git lfs track "*.dat" 2>&1 | tee track.log |
| 29 | grep "Tracking \"\*.dat\"" track.log |
| 30 | |
| 31 | contents="a" |
| 32 | contents_oid=$(calc_oid "$contents") |
| 33 | |
| 34 | printf "%s" "$contents" > a.dat |
| 35 | git add a.dat |
| 36 | git add .gitattributes |
| 37 | git commit -m "add a.dat" 2>&1 | tee commit.log |
| 38 | grep "main (root-commit)" commit.log |
| 39 | grep "2 files changed" commit.log |
| 40 | grep "create mode 100644 a.dat" commit.log |
| 41 | grep "create mode 100644 .gitattributes" commit.log |
| 42 | |
| 43 | [ "a" = "$(cat a.dat)" ] |
| 44 | |
| 45 | # This is a small shell function that runs several git commands together. |
| 46 | assert_pointer "main" "a.dat" "$contents_oid" 1 |
| 47 | |
| 48 | refute_server_object "$reponame" "$contents_oid" |
| 49 | |
| 50 | # This pushes to the remote repository set up at the top of the test. |
| 51 | git push origin main 2>&1 | tee push.log |
| 52 | grep "Uploading LFS objects: 100% (1/1), 1 B" push.log |
| 53 | grep "main -> main" push.log |
| 54 | |
| 55 | assert_server_object "$reponame" "$contents_oid" |
| 56 | |
| 57 | # change to the clone's working directory |
| 58 | cd ../clone |
| 59 | |
| 60 | git pull origin main |
| 61 | |
| 62 | [ "a" = "$(cat a.dat)" ] |
| 63 | |
| 64 | assert_pointer "main" "a.dat" "$contents_oid" 1 |
| 65 | ) |
| 66 | end_test |
| 67 | |
| 68 | begin_test "batch transfers occur in reverse order by size" |
| 69 | ( |
| 70 | set -e |
| 71 | |
| 72 | reponame="batch-order-test" |
| 73 | setup_remote_repo "$reponame" |
| 74 | clone_repo "$reponame" "$reponame" |
| 75 | |
| 76 | git lfs track "*.dat" |
| 77 | git add .gitattributes |
| 78 | git commit -m "initial commit" |
| 79 | |
| 80 | small_contents="small" |
| 81 | small_oid="$(calc_oid "$small_contents")" |
| 82 | printf "%s" "$small_contents" > small.dat |
| 83 | |
| 84 | bigger_contents="bigger" |
| 85 | bigger_oid="$(calc_oid "$bigger_contents")" |
| 86 | printf "%s" "$bigger_contents" > bigger.dat |
| 87 | |
| 88 | git add *.dat |
| 89 | git commit -m "add small and large objects" |
| 90 | |
| 91 | GIT_CURL_VERBOSE=1 git push origin main 2>&1 | tee push.log |
| 92 | |
| 93 | batch="$(grep "{\"operation\":\"upload\"" push.log | head -1)" |
| 94 | |
| 95 | pos_small="$(substring_position "$batch" "$small_oid")" |
| 96 | pos_large="$(substring_position "$batch" "$bigger_oid")" |
| 97 | |
| 98 | # Assert that the larger object shows up earlier in the batch than the |
| 99 | # smaller object |
| 100 | [ "$pos_large" -lt "$pos_small" ] |
| 101 | ) |
| 102 | end_test |
| 103 | |
| 104 | begin_test "batch transfers succeed with an empty hash algorithm" |
| 105 | ( |
| 106 | set -e |
| 107 | |
| 108 | reponame="batch-test-empty-algo" |
| 109 | contents="batch-hash-algo-empty" |
| 110 | setup_remote_repo "$reponame" |
| 111 | clone_repo "$reponame" "$reponame" |
| 112 | |
| 113 | git lfs track "*.dat" |
| 114 | printf "hi" > good.dat |
| 115 | printf "%s" "$contents" > special.dat |
| 116 | git add .gitattributes good.dat special.dat |
| 117 | git commit -m "hi" |
| 118 | |
| 119 | git push origin main |
| 120 | assert_server_object "$reponame" "$(calc_oid "$contents")" |
| 121 | ) |
| 122 | end_test |
| 123 | |
| 124 | begin_test "batch transfers fail with an unknown hash algorithm" |
| 125 | ( |
| 126 | set -e |
| 127 | |
| 128 | reponame="batch-test-invalid-algo" |
| 129 | contents="batch-hash-algo-invalid" |
| 130 | setup_remote_repo "$reponame" |
| 131 | clone_repo "$reponame" "$reponame" |
| 132 | |
| 133 | git lfs track "*.dat" |
| 134 | printf "hi" > good.dat |
| 135 | printf "%s" "$contents" > special.dat |
| 136 | git add .gitattributes good.dat special.dat |
| 137 | git commit -m "hi" |
| 138 | |
| 139 | git push origin main 2>&1 | tee push.log |
| 140 | grep 'unsupported hash algorithm' push.log |
| 141 | refute_server_object "$reponame" "$(calc_oid "$contents")" |
| 142 | ) |
| 143 | end_test |
| 144 | |
| 145 | begin_test "batch transfers with ssh endpoint (git-lfs-authenticate)" |
| 146 | ( |
| 147 | set -e |
| 148 | |
| 149 | reponame="batch-ssh" |
| 150 | setup_remote_repo "$reponame" |
| 151 | clone_repo "$reponame" "$reponame" |
| 152 | |
| 153 | sshurl="${GITSERVER/http:\/\//ssh://git@}/$reponame" |
| 154 | git config lfs.url "$sshurl" |
| 155 | |
| 156 | contents="test" |
| 157 | git lfs track "*.dat" |
| 158 | printf "%s" "$contents" > test.dat |
| 159 | git add .gitattributes test.dat |
| 160 | git commit -m "initial commit" |
| 161 | |
| 162 | GIT_TRACE=1 git push origin main >push.log 2>&1 |
| 163 | [ "1" -eq "$(grep -c "exec: lfs-ssh-echo.*git-lfs-authenticate /$reponame upload" push.log)" ] |
| 164 | assert_server_object "$reponame" "$(calc_oid "$contents")" |
| 165 | ) |
| 166 | end_test |
| 167 | |
| 168 | assert_ssh_transfer_session_counts() { |
| 169 | local log="$1" |
| 170 | local msg="$2" |
| 171 | local min="$3" |
| 172 | local max="$4" |
| 173 | |
| 174 | local count="$(grep -c "$msg" "$log")" |
| 175 | |
| 176 | [ "$max" -ge "$count" ] |
| 177 | [ "$min" -le "$count" ] |
| 178 | } |
| 179 | |
| 180 | assert_ssh_transfer_sessions() { |
| 181 | local log="$1" |
| 182 | local direction="$2" |
| 183 | local num_objs="$3" |
| 184 | local max_concurrency="$4" |
| 185 | |
| 186 | local min_expected_start=1 |
| 187 | local max_expected_start=$(( num_objs > max_concurrency ? max_concurrency : num_objs )) |
| 188 | local min_expected_end=1 |
| 189 | local max_expected_end="$max_expected_start" |
| 190 | |
| 191 | local expected_ctrl=1 |
| 192 | |
| 193 | # Versions of Git prior to 2.11.0 invoke Git LFS via the "smudge" filter |
| 194 | # rather than the "process" filter, so a separate Git LFS process runs for |
| 195 | # each downloaded object and spawns its own control socket SSH connection. |
| 196 | smudge_count="$(grep -c "exec: .*git-lfs smudge --" "$log")" || true |
| 197 | if [ 0 -lt "$smudge_count" ]; then |
| 198 | gitversion="$(git version | cut -d" " -f3)" |
| 199 | set +e |
| 200 | compare_version "$gitversion" '2.11.0' |
| 201 | result=$? |
| 202 | set -e |
| 203 | if [ "$result" -eq "$VERSION_LOWER" ]; then |
| 204 | min_expected_start="$num_objs" |
| 205 | max_expected_start="$num_objs" |
| 206 | min_expected_end="$num_objs" |
| 207 | max_expected_end="$num_objs" |
| 208 | expected_ctrl="$num_objs" |
| 209 | fi |
| 210 | fi |
| 211 | |
| 212 | local max_expected_nonctrl=$(( max_expected_start - expected_ctrl )) |
| 213 | |
| 214 | local lines="$(grep "exec: lfs-ssh-echo.*git-lfs-transfer .*${reponame}.git $direction" "$log")" |
| 215 | local ctrl_count="$(printf '%s' "$lines" | grep -c -- '-oControlMaster=yes')" |
| 216 | local nonctrl_count="$(printf '%s' "$lines" | grep -c -- '-oControlMaster=no')" |
| 217 | |
| 218 | [ "$expected_ctrl" -eq "$ctrl_count" ] |
| 219 | [ "$max_expected_nonctrl" -ge "$nonctrl_count" ] |
| 220 | |
| 221 | assert_ssh_transfer_session_counts "$log" 'spawning pure SSH connection' \ |
| 222 | "$min_expected_start" "$max_expected_start" |
| 223 | assert_ssh_transfer_session_counts "$log" 'pure SSH connection successful' \ |
| 224 | "$min_expected_start" "$max_expected_start" |
| 225 | assert_ssh_transfer_session_counts "$log" 'terminating pure SSH connection' \ |
| 226 | "$min_expected_end" "$max_expected_end" |
| 227 | } |
| 228 | |
| 229 | begin_test "batch transfers with ssh endpoint (git-lfs-transfer)" |
| 230 | ( |
| 231 | set -e |
| 232 | |
| 233 | setup_pure_ssh |
| 234 | |
| 235 | reponame="batch-ssh-transfer" |
| 236 | setup_remote_repo "$reponame" |
| 237 | clone_repo "$reponame" "$reponame" |
| 238 | |
| 239 | sshurl=$(ssh_remote "$reponame") |
| 240 | git config lfs.url "$sshurl" |
| 241 | |
| 242 | contents="test" |
| 243 | git lfs track "*.dat" |
| 244 | printf "%s" "$contents" > test.dat |
| 245 | git add .gitattributes test.dat |
| 246 | git commit -m "initial commit" |
| 247 | |
| 248 | # On Windows we do not multiplex SSH connections by default, so we |
| 249 | # enforce their use in order to match other platforms' connection counts. |
| 250 | git config --global lfs.ssh.autoMultiplex true |
| 251 | |
| 252 | GIT_TRACE=1 git push origin main 2>&1 | tee push.log |
| 253 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 254 | |
| 255 | assert_ssh_transfer_sessions 'push.log' 'upload' 1 "$expectedConcurrentTransfers" |
| 256 | assert_remote_object "$reponame" "$(calc_oid "$contents")" "${#contents}" |
| 257 | |
| 258 | cd .. |
| 259 | GIT_TRACE=1 git clone "$sshurl" "$reponame-2" 2>&1 | tee clone.log |
| 260 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 261 | |
| 262 | assert_ssh_transfer_sessions 'clone.log' 'download' 1 "$expectedConcurrentTransfers" |
| 263 | |
| 264 | cd "$reponame-2" |
| 265 | git lfs fsck |
| 266 | ) |
| 267 | end_test |
| 268 | |
| 269 | begin_test "batch transfers with ssh endpoint and multiple objects (git-lfs-transfer)" |
| 270 | ( |
| 271 | set -e |
| 272 | |
| 273 | setup_pure_ssh |
| 274 | |
| 275 | reponame="batch-ssh-transfer-multiple" |
| 276 | setup_remote_repo "$reponame" |
| 277 | clone_repo "$reponame" "$reponame" |
| 278 | |
| 279 | contents1="test1" |
| 280 | contents2="test2" |
| 281 | contents3="test3" |
| 282 | git lfs track "*.dat" |
| 283 | printf "%s" "$contents1" >test1.dat |
| 284 | printf "%s" "$contents2" >test2.dat |
| 285 | printf "%s" "$contents3" >test3.dat |
| 286 | git add .gitattributes test*.dat |
| 287 | git commit -m "initial commit" |
| 288 | |
| 289 | sshurl=$(ssh_remote "$reponame") |
| 290 | git config lfs.url "$sshurl" |
| 291 | |
| 292 | # On Windows we do not multiplex SSH connections by default, so we |
| 293 | # enforce their use in order to match other platforms' connection counts. |
| 294 | git config --global lfs.ssh.autoMultiplex true |
| 295 | |
| 296 | GIT_TRACE=1 git push origin main 2>&1 | tee push.log |
| 297 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 298 | |
| 299 | assert_ssh_transfer_sessions 'push.log' 'upload' 3 "$expectedConcurrentTransfers" |
| 300 | assert_remote_object "$reponame" "$(calc_oid "$contents1")" "${#contents1}" |
| 301 | assert_remote_object "$reponame" "$(calc_oid "$contents2")" "${#contents2}" |
| 302 | assert_remote_object "$reponame" "$(calc_oid "$contents3")" "${#contents3}" |
| 303 | |
| 304 | cd .. |
| 305 | GIT_TRACE=1 git clone "$sshurl" "$reponame-2" 2>&1 | tee clone.log |
| 306 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 307 | |
| 308 | assert_ssh_transfer_sessions 'clone.log' 'download' 3 "$expectedConcurrentTransfers" |
| 309 | |
| 310 | cd "$reponame-2" |
| 311 | git lfs fsck |
| 312 | ) |
| 313 | end_test |
| 314 | |
| 315 | begin_test "batch transfers with ssh endpoint and multiple objects exceeding workers (git-lfs-transfer)" |
| 316 | ( |
| 317 | set -e |
| 318 | |
| 319 | setup_pure_ssh |
| 320 | |
| 321 | reponame="batch-ssh-transfer-multiple-exceeding-workers" |
| 322 | setup_remote_repo "$reponame" |
| 323 | clone_repo "$reponame" "$reponame" |
| 324 | |
| 325 | contents1="test1" |
| 326 | contents2="test2" |
| 327 | contents3="test3" |
| 328 | git lfs track "*.dat" |
| 329 | printf "%s" "$contents1" >test1.dat |
| 330 | printf "%s" "$contents2" >test2.dat |
| 331 | printf "%s" "$contents3" >test3.dat |
| 332 | git add .gitattributes test*.dat |
| 333 | git commit -m "initial commit" |
| 334 | |
| 335 | sshurl=$(ssh_remote "$reponame") |
| 336 | git config lfs.url "$sshurl" |
| 337 | |
| 338 | # On Windows we do not multiplex SSH connections by default, so we |
| 339 | # enforce their use in order to match other platforms' connection counts. |
| 340 | git config --global lfs.ssh.autoMultiplex true |
| 341 | |
| 342 | # Allow no more than two concurrent workers to transfer objects at once. |
| 343 | git config --global lfs.concurrentTransfers 2 |
| 344 | |
| 345 | GIT_TRACE=1 git push origin main 2>&1 | tee push.log |
| 346 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 347 | |
| 348 | assert_ssh_transfer_sessions 'push.log' 'upload' 3 2 |
| 349 | assert_remote_object "$reponame" "$(calc_oid "$contents1")" "${#contents1}" |
| 350 | assert_remote_object "$reponame" "$(calc_oid "$contents2")" "${#contents2}" |
| 351 | assert_remote_object "$reponame" "$(calc_oid "$contents3")" "${#contents3}" |
| 352 | |
| 353 | cd .. |
| 354 | GIT_TRACE=1 git clone "$sshurl" "$reponame-2" 2>&1 | tee clone.log |
| 355 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 356 | |
| 357 | assert_ssh_transfer_sessions 'clone.log' 'download' 3 2 |
| 358 | |
| 359 | cd "$reponame-2" |
| 360 | git lfs fsck |
| 361 | ) |
| 362 | end_test |
| 363 | |
| 364 | begin_test "batch transfers with ssh endpoint and multiple branches (git-lfs-transfer)" |
| 365 | ( |
| 366 | set -e |
| 367 | |
| 368 | setup_pure_ssh |
| 369 | |
| 370 | reponame="batch-ssh-transfer-multiple-branches" |
| 371 | setup_remote_repo "$reponame" |
| 372 | clone_repo "$reponame" "$reponame" |
| 373 | |
| 374 | git lfs track "*.dat" |
| 375 | |
| 376 | contents1="test1" |
| 377 | printf "%s" "$contents1" >test1.dat |
| 378 | git add .gitattributes test1.dat |
| 379 | git commit -m "initial commit" |
| 380 | |
| 381 | git checkout -b second main |
| 382 | |
| 383 | contents2="test2" |
| 384 | printf "%s" "$contents2" >test2.dat |
| 385 | git add test2.dat |
| 386 | git commit -m "second" |
| 387 | |
| 388 | sshurl=$(ssh_remote "$reponame") |
| 389 | git config lfs.url "$sshurl" |
| 390 | |
| 391 | # On Windows we do not multiplex SSH connections by default, so we |
| 392 | # enforce their use in order to match other platforms' connection counts. |
| 393 | git config --global lfs.ssh.autoMultiplex true |
| 394 | |
| 395 | GIT_TRACE=1 git push --all origin 2>&1 | tee push.log |
| 396 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 397 | |
| 398 | assert_ssh_transfer_sessions 'push.log' 'upload' 2 "$expectedConcurrentTransfers" |
| 399 | assert_remote_object "$reponame" "$(calc_oid "$contents1")" "${#contents1}" |
| 400 | assert_remote_object "$reponame" "$(calc_oid "$contents2")" "${#contents2}" |
| 401 | |
| 402 | rm -rf .git/lfs/objects |
| 403 | |
| 404 | GIT_TRACE=1 git lfs fetch --all 2>&1 | tee fetch.log |
| 405 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 406 | |
| 407 | assert_ssh_transfer_sessions 'fetch.log' 'download' 2 "$expectedConcurrentTransfers" |
| 408 | |
| 409 | git lfs fsck |
| 410 | ) |
| 411 | end_test |