Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

archiver_test.go 4.5KB

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