gitea源码

mirror_pull_test.go 4.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package mirror
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func Test_parseRemoteUpdateOutput(t *testing.T) {
  9. output := `
  10. * [new tag] v0.1.8 -> v0.1.8
  11. * [new branch] master -> origin/master
  12. - [deleted] (none) -> origin/test1
  13. - [deleted] (none) -> tag1
  14. + f895a1e...957a993 test2 -> origin/test2 (forced update)
  15. 957a993..a87ba5f test3 -> origin/test3
  16. * [new ref] refs/pull/26595/head -> refs/pull/26595/head
  17. * [new ref] refs/pull/26595/merge -> refs/pull/26595/merge
  18. e0639e38fb..6db2410489 refs/pull/25873/head -> refs/pull/25873/head
  19. + 1c97ebc746...976d27d52f refs/pull/25873/merge -> refs/pull/25873/merge (forced update)
  20. `
  21. results := parseRemoteUpdateOutput(output, "origin")
  22. assert.Len(t, results, 10)
  23. assert.Equal(t, "refs/tags/v0.1.8", results[0].refName.String())
  24. assert.Equal(t, gitShortEmptySha, results[0].oldCommitID)
  25. assert.Empty(t, results[0].newCommitID)
  26. assert.Equal(t, "refs/heads/master", results[1].refName.String())
  27. assert.Equal(t, gitShortEmptySha, results[1].oldCommitID)
  28. assert.Empty(t, results[1].newCommitID)
  29. assert.Equal(t, "refs/heads/test1", results[2].refName.String())
  30. assert.Empty(t, results[2].oldCommitID)
  31. assert.Equal(t, gitShortEmptySha, results[2].newCommitID)
  32. assert.Equal(t, "refs/tags/tag1", results[3].refName.String())
  33. assert.Empty(t, results[3].oldCommitID)
  34. assert.Equal(t, gitShortEmptySha, results[3].newCommitID)
  35. assert.Equal(t, "refs/heads/test2", results[4].refName.String())
  36. assert.Equal(t, "f895a1e", results[4].oldCommitID)
  37. assert.Equal(t, "957a993", results[4].newCommitID)
  38. assert.Equal(t, "refs/heads/test3", results[5].refName.String())
  39. assert.Equal(t, "957a993", results[5].oldCommitID)
  40. assert.Equal(t, "a87ba5f", results[5].newCommitID)
  41. assert.Equal(t, "refs/pull/26595/head", results[6].refName.String())
  42. assert.Equal(t, gitShortEmptySha, results[6].oldCommitID)
  43. assert.Empty(t, results[6].newCommitID)
  44. assert.Equal(t, "refs/pull/26595/merge", results[7].refName.String())
  45. assert.Equal(t, gitShortEmptySha, results[7].oldCommitID)
  46. assert.Empty(t, results[7].newCommitID)
  47. assert.Equal(t, "refs/pull/25873/head", results[8].refName.String())
  48. assert.Equal(t, "e0639e38fb", results[8].oldCommitID)
  49. assert.Equal(t, "6db2410489", results[8].newCommitID)
  50. assert.Equal(t, "refs/pull/25873/merge", results[9].refName.String())
  51. assert.Equal(t, "1c97ebc746", results[9].oldCommitID)
  52. assert.Equal(t, "976d27d52f", results[9].newCommitID)
  53. }
  54. func Test_checkRecoverableSyncError(t *testing.T) {
  55. cases := []struct {
  56. recoverable bool
  57. message string
  58. }{
  59. // A race condition in http git-fetch where certain refs were listed on the remote and are no longer there, would exit status 128
  60. {true, "fatal: remote error: upload-pack: not our ref 988881adc9fc3655077dc2d4d757d480b5ea0e11"},
  61. // A race condition where a local gc/prune removes a named ref during a git-fetch would exit status 1
  62. {true, "cannot lock ref 'refs/pull/123456/merge': unable to resolve reference 'refs/pull/134153/merge'"},
  63. // A race condition in http git-fetch where named refs were listed on the remote and are no longer there
  64. {true, "error: cannot lock ref 'refs/remotes/origin/foo': unable to resolve reference 'refs/remotes/origin/foo': reference broken"},
  65. // A race condition in http git-fetch where named refs were force-pushed during the update, would exit status 128
  66. {true, "error: cannot lock ref 'refs/pull/123456/merge': is at 988881adc9fc3655077dc2d4d757d480b5ea0e11 but expected 7f894307ffc9553edbd0b671cab829786866f7b2"},
  67. // A race condition with other local git operations, such as git-maintenance, would exit status 128 (well, "Unable" the "U" is uppercase)
  68. {true, "fatal: Unable to create '/data/gitea-repositories/foo-org/bar-repo.git/./objects/info/commit-graphs/commit-graph-chain.lock': File exists."},
  69. // Missing or unauthorized credentials, would exit status 128
  70. {false, "fatal: Authentication failed for 'https://example.com/foo-does-not-exist/bar.git/'"},
  71. // A non-existent remote repository, would exit status 128
  72. {false, "fatal: Could not read from remote repository."},
  73. // A non-functioning proxy, would exit status 128
  74. {false, "fatal: unable to access 'https://example.com/foo-does-not-exist/bar.git/': Failed to connect to configured-https-proxy port 1080 after 0 ms: Couldn't connect to server"},
  75. }
  76. for _, c := range cases {
  77. assert.Equal(t, c.recoverable, checkRecoverableSyncError(c.message), "test case: %s", c.message)
  78. }
  79. }