gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package archiver
  4. import (
  5. "testing"
  6. "time"
  7. "code.gitea.io/gitea/models/unittest"
  8. "code.gitea.io/gitea/services/contexttest"
  9. _ "code.gitea.io/gitea/models/actions"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestMain(m *testing.M) {
  13. unittest.MainTest(m)
  14. }
  15. func TestArchive_Basic(t *testing.T) {
  16. assert.NoError(t, unittest.PrepareTestDatabase())
  17. ctx, _ := contexttest.MockContext(t, "user27/repo49")
  18. firstCommit, secondCommit := "51f84af23134", "aacbdfe9e1c4"
  19. contexttest.LoadRepo(t, ctx, 49)
  20. contexttest.LoadGitRepo(t, ctx)
  21. defer ctx.Repo.GitRepo.Close()
  22. bogusReq, err := NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, firstCommit+".zip")
  23. assert.NoError(t, err)
  24. assert.NotNil(t, bogusReq)
  25. assert.Equal(t, firstCommit+".zip", bogusReq.GetArchiveName())
  26. // Check a series of bogus requests.
  27. // Step 1, valid commit with a bad extension.
  28. bogusReq, err = NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, firstCommit+".unknown")
  29. assert.Error(t, err)
  30. assert.Nil(t, bogusReq)
  31. // Step 2, missing commit.
  32. bogusReq, err = NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, "dbffff.zip")
  33. assert.Error(t, err)
  34. assert.Nil(t, bogusReq)
  35. // Step 3, doesn't look like branch/tag/commit.
  36. bogusReq, err = NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, "db.zip")
  37. assert.Error(t, err)
  38. assert.Nil(t, bogusReq)
  39. bogusReq, err = NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, "master.zip")
  40. assert.NoError(t, err)
  41. assert.NotNil(t, bogusReq)
  42. assert.Equal(t, "master.zip", bogusReq.GetArchiveName())
  43. bogusReq, err = NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, "test/archive.zip")
  44. assert.NoError(t, err)
  45. assert.NotNil(t, bogusReq)
  46. assert.Equal(t, "test-archive.zip", bogusReq.GetArchiveName())
  47. // Now two valid requests, firstCommit with valid extensions.
  48. zipReq, err := NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, firstCommit+".zip")
  49. assert.NoError(t, err)
  50. assert.NotNil(t, zipReq)
  51. tgzReq, err := NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, firstCommit+".tar.gz")
  52. assert.NoError(t, err)
  53. assert.NotNil(t, tgzReq)
  54. secondReq, err := NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, secondCommit+".bundle")
  55. assert.NoError(t, err)
  56. assert.NotNil(t, secondReq)
  57. inFlight := make([]*ArchiveRequest, 3)
  58. inFlight[0] = zipReq
  59. inFlight[1] = tgzReq
  60. inFlight[2] = secondReq
  61. doArchive(t.Context(), zipReq)
  62. doArchive(t.Context(), tgzReq)
  63. doArchive(t.Context(), secondReq)
  64. // Make sure sending an unprocessed request through doesn't affect the queue
  65. // count.
  66. doArchive(t.Context(), zipReq)
  67. // Sleep two seconds to make sure the queue doesn't change.
  68. time.Sleep(2 * time.Second)
  69. zipReq2, err := NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, firstCommit+".zip")
  70. assert.NoError(t, err)
  71. // This zipReq should match what's sitting in the queue, as we haven't
  72. // let it release yet. From the consumer's point of view, this looks like
  73. // a long-running archive task.
  74. assert.Equal(t, zipReq, zipReq2)
  75. // We still have the other three stalled at completion, waiting to remove
  76. // from archiveInProgress. Try to submit this new one before its
  77. // predecessor has cleared out of the queue.
  78. doArchive(t.Context(), zipReq2)
  79. // Now we'll submit a request and TimedWaitForCompletion twice, before and
  80. // after we release it. We should trigger both the timeout and non-timeout
  81. // cases.
  82. timedReq, err := NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, secondCommit+".tar.gz")
  83. assert.NoError(t, err)
  84. assert.NotNil(t, timedReq)
  85. doArchive(t.Context(), timedReq)
  86. zipReq2, err = NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, firstCommit+".zip")
  87. assert.NoError(t, err)
  88. // Now, we're guaranteed to have released the original zipReq from the queue.
  89. // Ensure that we don't get handed back the released entry somehow, but they
  90. // should remain functionally equivalent in all fields. The exception here
  91. // is zipReq.cchan, which will be non-nil because it's a completed request.
  92. // It's fine to go ahead and set it to nil now.
  93. assert.Equal(t, zipReq, zipReq2)
  94. assert.NotSame(t, zipReq, zipReq2)
  95. // Same commit, different compression formats should have different names.
  96. // Ideally, the extension would match what we originally requested.
  97. assert.NotEqual(t, zipReq.GetArchiveName(), tgzReq.GetArchiveName())
  98. assert.NotEqual(t, zipReq.GetArchiveName(), secondReq.GetArchiveName())
  99. }
  100. func TestErrUnknownArchiveFormat(t *testing.T) {
  101. err := ErrUnknownArchiveFormat{RequestNameType: "xxx"}
  102. assert.ErrorIs(t, err, ErrUnknownArchiveFormat{})
  103. }