code.gitea.io/gitea@v1.21.7/services/repository/archiver/archiver_test.go (about)

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