diff options
author | Quang-Minh Nguyen <qmnguyen@gitlab.com> | 2023-12-06 13:05:46 +0300 |
---|---|---|
committer | Quang-Minh Nguyen <qmnguyen@gitlab.com> | 2023-12-22 06:19:28 +0300 |
commit | 081014df5deb8522ff7fa452eeb898d79ef5397d (patch) | |
tree | 4c46437847fa746c87b6823f1aea96a205227853 | |
parent | e872757e33f17a375135a093e738d4fe01f388f8 (diff) |
Add support for custom setup to transaction manager test
At the moment, all the sub-tests of the transaction manager share the
same test setup. In some later commits, we'll need to implement a new
set of tests with totally different setup. This commit lays the
foundation for custom setup.
-rw-r--r-- | internal/gitaly/storage/storagemgr/testhelper_test.go | 1 | ||||
-rw-r--r-- | internal/gitaly/storage/storagemgr/transaction_manager_test.go | 161 |
2 files changed, 84 insertions, 78 deletions
diff --git a/internal/gitaly/storage/storagemgr/testhelper_test.go b/internal/gitaly/storage/storagemgr/testhelper_test.go index 28e8aee9d..2f8cb9234 100644 --- a/internal/gitaly/storage/storagemgr/testhelper_test.go +++ b/internal/gitaly/storage/storagemgr/testhelper_test.go @@ -399,6 +399,7 @@ type steps []any type transactionTestCase struct { desc string steps steps + customSetup func(*testing.T, context.Context, partitionID, string) testTransactionSetup expectedState StateAssertion } diff --git a/internal/gitaly/storage/storagemgr/transaction_manager_test.go b/internal/gitaly/storage/storagemgr/transaction_manager_test.go index a4b613497..2a2b4e41e 100644 --- a/internal/gitaly/storage/storagemgr/transaction_manager_test.go +++ b/internal/gitaly/storage/storagemgr/transaction_manager_test.go @@ -153,99 +153,99 @@ func reverseIndexFileDirectoryEntry(cfg config.Cfg) testhelper.DirectoryEntry { } } -func TestTransactionManager(t *testing.T) { - t.Parallel() +func setupTest(t *testing.T, ctx context.Context, testPartitionID partitionID, relativePath string) testTransactionSetup { + t.Helper() + + cfg := testcfg.Build(t) + + repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{ + SkipCreationViaService: true, + RelativePath: relativePath, + }) + + firstCommitOID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents()) + secondCommitOID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(firstCommitOID)) + thirdCommitOID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(secondCommitOID)) + divergingCommitOID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(firstCommitOID), gittest.WithMessage("diverging commit")) + + cmdFactory := gittest.NewCommandFactory(t, cfg) + catfileCache := catfile.NewCache(cfg) + t.Cleanup(catfileCache.Stop) + + logger := testhelper.NewLogger(t) + locator := config.NewLocator(cfg) + localRepo := localrepo.New( + logger, + locator, + cmdFactory, + catfileCache, + repo, + ) - ctx := testhelper.Context(t) + objectHash, err := localRepo.ObjectHash(ctx) + require.NoError(t, err) - // testPartitionID is the partition ID used in the tests for the TransactionManager. - const testPartitionID partitionID = 1 + hasher := objectHash.Hash() + _, err = hasher.Write([]byte("content does not matter")) + require.NoError(t, err) + nonExistentOID, err := objectHash.FromHex(hex.EncodeToString(hasher.Sum(nil))) + require.NoError(t, err) - setupTest := func(t *testing.T, relativePath string) testTransactionSetup { + packCommit := func(oid git.ObjectID) []byte { t.Helper() - cfg := testcfg.Build(t) - - repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{ - SkipCreationViaService: true, - RelativePath: relativePath, - }) - - firstCommitOID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents()) - secondCommitOID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(firstCommitOID)) - thirdCommitOID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(secondCommitOID)) - divergingCommitOID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(firstCommitOID), gittest.WithMessage("diverging commit")) - - cmdFactory := gittest.NewCommandFactory(t, cfg) - catfileCache := catfile.NewCache(cfg) - t.Cleanup(catfileCache.Stop) - - logger := testhelper.NewLogger(t) - locator := config.NewLocator(cfg) - localRepo := localrepo.New( - logger, - locator, - cmdFactory, - catfileCache, - repo, + var pack bytes.Buffer + require.NoError(t, + localRepo.PackObjects(ctx, strings.NewReader(oid.String()), &pack), ) - objectHash, err := localRepo.ObjectHash(ctx) - require.NoError(t, err) + return pack.Bytes() + } - hasher := objectHash.Hash() - _, err = hasher.Write([]byte("content does not matter")) - require.NoError(t, err) - nonExistentOID, err := objectHash.FromHex(hex.EncodeToString(hasher.Sum(nil))) - require.NoError(t, err) + return testTransactionSetup{ + PartitionID: testPartitionID, + RelativePath: relativePath, + RepositoryPath: repoPath, + Repo: localRepo, + Config: cfg, + ObjectHash: objectHash, + CommandFactory: cmdFactory, + RepositoryFactory: localrepo.NewFactory(logger, locator, cmdFactory, catfileCache), + NonExistentOID: nonExistentOID, + Commits: testTransactionCommits{ + First: testTransactionCommit{ + OID: firstCommitOID, + Pack: packCommit(firstCommitOID), + }, + Second: testTransactionCommit{ + OID: secondCommitOID, + Pack: packCommit(secondCommitOID), + }, + Third: testTransactionCommit{ + OID: thirdCommitOID, + Pack: packCommit(thirdCommitOID), + }, + Diverging: testTransactionCommit{ + OID: divergingCommitOID, + Pack: packCommit(divergingCommitOID), + }, + }, + } +} - packCommit := func(oid git.ObjectID) []byte { - t.Helper() +func TestTransactionManager(t *testing.T) { + t.Parallel() - var pack bytes.Buffer - require.NoError(t, - localRepo.PackObjects(ctx, strings.NewReader(oid.String()), &pack), - ) + ctx := testhelper.Context(t) - return pack.Bytes() - } - - return testTransactionSetup{ - PartitionID: testPartitionID, - RelativePath: relativePath, - RepositoryPath: repoPath, - Repo: localRepo, - Config: cfg, - ObjectHash: objectHash, - CommandFactory: cmdFactory, - RepositoryFactory: localrepo.NewFactory(logger, locator, cmdFactory, catfileCache), - NonExistentOID: nonExistentOID, - Commits: testTransactionCommits{ - First: testTransactionCommit{ - OID: firstCommitOID, - Pack: packCommit(firstCommitOID), - }, - Second: testTransactionCommit{ - OID: secondCommitOID, - Pack: packCommit(secondCommitOID), - }, - Third: testTransactionCommit{ - OID: thirdCommitOID, - Pack: packCommit(thirdCommitOID), - }, - Diverging: testTransactionCommit{ - OID: divergingCommitOID, - Pack: packCommit(divergingCommitOID), - }, - }, - } - } + // testPartitionID is the partition ID used in the tests for the TransactionManager. + const testPartitionID partitionID = 1 // A clean repository is setup for each test. We build a setup ahead of the tests here once to // get deterministic commit IDs, relative path and object hash we can use to build the declarative // test cases. relativePath := gittest.NewRepositoryName(t) - setup := setupTest(t, relativePath) + setup := setupTest(t, ctx, testPartitionID, relativePath) var testCases []transactionTestCase subTests := [][]transactionTestCase{ @@ -268,7 +268,12 @@ func TestTransactionManager(t *testing.T) { t.Parallel() // Setup the repository with the exact same state as what was used to build the test cases. - setup := setupTest(t, relativePath) + var setup testTransactionSetup + if tc.customSetup != nil { + setup = tc.customSetup(t, ctx, testPartitionID, relativePath) + } else { + setup = setupTest(t, ctx, testPartitionID, relativePath) + } runTransactionTest(t, ctx, tc, setup) }) } |