diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2022-02-02 13:32:07 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2022-02-02 13:32:07 +0300 |
commit | b664d34ba4a0f359150614dc7a85ad0bf2554d6d (patch) | |
tree | e403c703b94da7511ec6d94ed7c65e1c5a17879d | |
parent | b5e30d67a0713d15ac09d916a9680a499cb3781e (diff) | |
parent | 7489199fe409eec4a39251e54abb13a0f6580c90 (diff) |
Merge branch 'smh-create-repo-conflicts' into 'master'
Create repositories via API in conflicts service tests
See merge request gitlab-org/gitaly!4276
3 files changed, 62 insertions, 49 deletions
diff --git a/internal/gitaly/service/conflicts/list_conflict_files_test.go b/internal/gitaly/service/conflicts/list_conflict_files_test.go index 0c2fb7b75..bfe08d35f 100644 --- a/internal/gitaly/service/conflicts/list_conflict_files_test.go +++ b/internal/gitaly/service/conflicts/list_conflict_files_test.go @@ -26,7 +26,7 @@ type conflictFile struct { func TestSuccessfulListConflictFilesRequest(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := SetupConflictsService(t, false, nil) + _, repo, _, client := SetupConflictsService(ctx, t, false, nil) ourCommitOid := "1a35b5a77cf6af7edf6703f88e82f6aff613666f" theirCommitOid := "8309e68585b28d61eb85b7e2834849dda6bf1733" @@ -92,7 +92,7 @@ end func TestSuccessfulListConflictFilesRequestWithAncestor(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := SetupConflictsService(t, true, nil) + _, repo, _, client := SetupConflictsService(ctx, t, true, nil) ourCommitOid := "824be604a34828eb682305f0d963056cfac87b2d" theirCommitOid := "1450cd639e0bc6721eb02800169e464f212cde06" @@ -138,7 +138,7 @@ func TestSuccessfulListConflictFilesRequestWithAncestor(t *testing.T) { func TestListConflictFilesHugeDiff(t *testing.T) { ctx := testhelper.Context(t) - cfg, repo, repoPath, client := SetupConflictsService(t, false, nil) + cfg, repo, repoPath, client := SetupConflictsService(ctx, t, false, nil) our := buildCommit(t, ctx, cfg, repo, repoPath, map[string][]byte{ "a": bytes.Repeat([]byte("a\n"), 128*1024), @@ -198,7 +198,7 @@ func buildCommit(t *testing.T, ctx context.Context, cfg config.Cfg, repo *gitaly func TestListConflictFilesFailedPrecondition(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := SetupConflictsService(t, true, nil) + _, repo, _, client := SetupConflictsService(ctx, t, true, nil) testCases := []struct { desc string @@ -255,7 +255,7 @@ func TestListConflictFilesFailedPrecondition(t *testing.T) { func TestListConflictFilesAllowTreeConflicts(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := SetupConflictsService(t, true, nil) + _, repo, _, client := SetupConflictsService(ctx, t, true, nil) ourCommitOid := "eb227b3e214624708c474bdab7bde7afc17cefcc" theirCommitOid := "824be604a34828eb682305f0d963056cfac87b2d" @@ -347,7 +347,7 @@ end func TestFailedListConflictFilesRequestDueToValidation(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := SetupConflictsService(t, true, nil) + _, repo, _, client := SetupConflictsService(ctx, t, true, nil) ourCommitOid := "0b4bc9a49b562e85de7cc9e834518ea6828729b9" theirCommitOid := "bb5206fee213d983da88c47f9cf4cc6caf9c66dc" diff --git a/internal/gitaly/service/conflicts/resolve_conflicts_test.go b/internal/gitaly/service/conflicts/resolve_conflicts_test.go index 5ae4d6534..04a409aa0 100644 --- a/internal/gitaly/service/conflicts/resolve_conflicts_test.go +++ b/internal/gitaly/service/conflicts/resolve_conflicts_test.go @@ -56,9 +56,20 @@ var ( ) func TestSuccessfulResolveConflictsRequestHelper(t *testing.T) { - cfg, repoProto, repoPath := SetupConfigAndRepo(t, true) - repo := localrepo.NewTestRepo(t, cfg, repoProto) + var verifyFunc func(t testing.TB, pushOptions []string, stdin io.Reader) + verifyFuncProxy := func(t *testing.T, ctx context.Context, repo *gitalypb.Repository, pushOptions, env []string, stdin io.Reader, stdout, stderr io.Writer) error { + // We use a proxy func here as we need to provide the hookManager dependency while creating the service but we only + // know the commit IDs after the service is created. The proxy allows us to modify the verifyFunc after the service + // is already built. + verifyFunc(t, pushOptions, stdin) + return nil + } + ctx := testhelper.Context(t) + hookManager := hook.NewMockManager(t, verifyFuncProxy, verifyFuncProxy, hook.NopUpdate, hook.NopReferenceTransaction) + cfg, repoProto, repoPath, client := SetupConflictsService(ctx, t, true, hookManager) + + repo := localrepo.NewTestRepo(t, cfg, repoProto) missingAncestorPath := "files/missing_ancestor.txt" files := []map[string]interface{}{ @@ -126,19 +137,15 @@ func TestSuccessfulResolveConflictsRequestHelper(t *testing.T) { theirCommitOID = commitConflict(theirCommitOID, targetBranch, "content-2") hookCount := 0 - verifyFunc := func(t *testing.T, ctx context.Context, repo *gitalypb.Repository, pushOptions, env []string, stdin io.Reader, stdout, stderr io.Writer) error { + verifyFunc = func(t testing.TB, pushOptions []string, stdin io.Reader) { changes, err := io.ReadAll(stdin) require.NoError(t, err) pattern := fmt.Sprintf("%s .* refs/heads/%s\n", ourCommitOID, sourceBranch) require.Regexp(t, regexp.MustCompile(pattern), string(changes)) require.Empty(t, pushOptions) hookCount++ - return nil } - hookManager := hook.NewMockManager(t, verifyFunc, verifyFunc, hook.NopUpdate, hook.NopReferenceTransaction) - client := SetupConflictsServiceWithConfig(t, &cfg, hookManager) - mdGS := testcfg.GitalyServersMetadataFromCfg(t, cfg) mdFF, _ := metadata.FromOutgoingContext(ctx) ctx = metadata.NewOutgoingContext(ctx, metadata.Join(mdGS, mdFF)) @@ -190,14 +197,13 @@ func TestSuccessfulResolveConflictsRequestHelper(t *testing.T) { } func TestResolveConflictsWithRemoteRepo(t *testing.T) { - hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, _, _, client := SetupConflictsService(t, true, hookManager) ctx := testhelper.Context(t) + hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) + cfg, sourceRepo, sourceRepoPath, client := SetupConflictsService(ctx, t, true, hookManager) testcfg.BuildGitalySSH(t, cfg) testcfg.BuildGitalyHooks(t, cfg) - sourceRepo, sourceRepoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) sourceBlobOID := gittest.WriteBlob(t, cfg, sourceRepoPath, []byte("contents-1\n")) sourceCommitOID := gittest.WriteCommit(t, cfg, sourceRepoPath, gittest.WithTreeEntries(gittest.TreeEntry{ @@ -206,7 +212,9 @@ func TestResolveConflictsWithRemoteRepo(t *testing.T) { ) gittest.Exec(t, cfg, "-C", sourceRepoPath, "update-ref", "refs/heads/source", sourceCommitOID.String()) - targetRepo, targetRepoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + targetRepo, targetRepoPath := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) targetBlobOID := gittest.WriteBlob(t, cfg, targetRepoPath, []byte("contents-2\n")) targetCommitOID := gittest.WriteCommit(t, cfg, targetRepoPath, gittest.WithTreeEntries(gittest.TreeEntry{ @@ -259,9 +267,10 @@ func TestResolveConflictsWithRemoteRepo(t *testing.T) { } func TestResolveConflictsLineEndings(t *testing.T) { - hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repo, repoPath, client := SetupConflictsService(t, true, hookManager) ctx := testhelper.Context(t) + hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) + cfg, repo, repoPath, client := SetupConflictsService(ctx, t, true, hookManager) + ctx = testhelper.MergeOutgoingMetadata(ctx, testcfg.GitalyServersMetadataFromCfg(t, cfg)) for _, tc := range []struct { @@ -378,9 +387,10 @@ func TestResolveConflictsLineEndings(t *testing.T) { } func TestResolveConflictsNonOIDRequests(t *testing.T) { - hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repoProto, _, client := SetupConflictsService(t, true, hookManager) ctx := testhelper.Context(t) + hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) + cfg, repoProto, _, client := SetupConflictsService(ctx, t, true, hookManager) + ctx = testhelper.MergeOutgoingMetadata(ctx, testcfg.GitalyServersMetadataFromCfg(t, cfg)) stream, err := client.ResolveConflicts(ctx) @@ -414,11 +424,12 @@ func TestResolveConflictsNonOIDRequests(t *testing.T) { } func TestResolveConflictsIdenticalContent(t *testing.T) { + ctx := testhelper.Context(t) + hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repoProto, repoPath, client := SetupConflictsService(t, true, hookManager) + cfg, repoProto, repoPath, client := SetupConflictsService(ctx, t, true, hookManager) repo := localrepo.NewTestRepo(t, cfg, repoProto) - ctx := testhelper.Context(t) sourceBranch := "conflict-resolvable" sourceOID, err := repo.ResolveRevision(ctx, git.Revision(sourceBranch)) @@ -510,11 +521,13 @@ func TestResolveConflictsIdenticalContent(t *testing.T) { } func TestResolveConflictsStableID(t *testing.T) { + ctx := testhelper.Context(t) + hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repoProto, _, client := SetupConflictsService(t, true, hookManager) + cfg, repoProto, _, client := SetupConflictsService(ctx, t, true, hookManager) repo := localrepo.NewTestRepo(t, cfg, repoProto) - ctx := testhelper.Context(t) + md := testcfg.GitalyServersMetadataFromCfg(t, cfg) ctx = testhelper.MergeOutgoingMetadata(ctx, md) @@ -577,9 +590,10 @@ func TestResolveConflictsStableID(t *testing.T) { } func TestFailedResolveConflictsRequestDueToResolutionError(t *testing.T) { - hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repo, _, client := SetupConflictsService(t, true, hookManager) ctx := testhelper.Context(t) + hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) + cfg, repo, _, client := SetupConflictsService(ctx, t, true, hookManager) + mdGS := testcfg.GitalyServersMetadataFromCfg(t, cfg) mdFF, _ := metadata.FromOutgoingContext(ctx) ctx = metadata.NewOutgoingContext(ctx, metadata.Join(mdGS, mdFF)) @@ -632,9 +646,9 @@ func TestFailedResolveConflictsRequestDueToResolutionError(t *testing.T) { } func TestFailedResolveConflictsRequestDueToValidation(t *testing.T) { - hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repo, _, client := SetupConflictsService(t, true, hookManager) ctx := testhelper.Context(t) + hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) + cfg, repo, _, client := SetupConflictsService(ctx, t, true, hookManager) mdGS := testcfg.GitalyServersMetadataFromCfg(t, cfg) ourCommitOid := "1450cd639e0bc6721eb02800169e464f212cde06" @@ -797,13 +811,12 @@ func TestFailedResolveConflictsRequestDueToValidation(t *testing.T) { } func TestResolveConflictsQuarantine(t *testing.T) { - cfg, _, _, client := SetupConflictsService(t, true, nil) ctx := testhelper.Context(t) + cfg, sourceRepoProto, sourceRepoPath, client := SetupConflictsService(ctx, t, true, nil) testcfg.BuildGitalySSH(t, cfg) testcfg.BuildGitalyHooks(t, cfg) - sourceRepoProto, sourceRepoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) sourceBlobOID := gittest.WriteBlob(t, cfg, sourceRepoPath, []byte("contents-1\n")) sourceCommitOID := gittest.WriteCommit(t, cfg, sourceRepoPath, gittest.WithTreeEntries(gittest.TreeEntry{ @@ -823,7 +836,9 @@ func TestResolveConflictsQuarantine(t *testing.T) { exit 1 `)) - targetRepoProto, targetRepoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + targetRepoProto, targetRepoPath := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) targetBlobOID := gittest.WriteBlob(t, cfg, targetRepoPath, []byte("contents-2\n")) targetCommitOID := gittest.WriteCommit(t, cfg, targetRepoPath, gittest.WithTreeEntries(gittest.TreeEntry{ diff --git a/internal/gitaly/service/conflicts/testhelper_test.go b/internal/gitaly/service/conflicts/testhelper_test.go index 8280cfde3..62cba45c3 100644 --- a/internal/gitaly/service/conflicts/testhelper_test.go +++ b/internal/gitaly/service/conflicts/testhelper_test.go @@ -1,6 +1,8 @@ package conflicts import ( + "context" + "path/filepath" "testing" "gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest" @@ -22,32 +24,28 @@ func TestMain(m *testing.M) { testhelper.Run(m) } -func SetupConfigAndRepo(t testing.TB, bare bool) (config.Cfg, *gitalypb.Repository, string) { +func SetupConflictsService(ctx context.Context, t testing.TB, bare bool, hookManager hook.Manager) (config.Cfg, *gitalypb.Repository, string, gitalypb.ConflictsServiceClient) { cfg := testcfg.Build(t) testcfg.BuildGitalyGit2Go(t, cfg) - repo, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0], gittest.CloneRepoOpts{ - WithWorktree: !bare, - }) - - return cfg, repo, repoPath -} - -func SetupConflictsServiceWithConfig(t testing.TB, cfg *config.Cfg, hookManager hook.Manager) gitalypb.ConflictsServiceClient { - serverSocketPath := runConflictsServer(t, *cfg, hookManager) + serverSocketPath := runConflictsServer(t, cfg, hookManager) cfg.SocketPath = serverSocketPath client, conn := NewConflictsClient(t, serverSocketPath) t.Cleanup(func() { conn.Close() }) - return client -} - -func SetupConflictsService(t testing.TB, bare bool, hookManager hook.Manager) (config.Cfg, *gitalypb.Repository, string, gitalypb.ConflictsServiceClient) { - cfg, repo, repoPath := SetupConfigAndRepo(t, bare) + repo, repoPath := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) - client := SetupConflictsServiceWithConfig(t, &cfg, hookManager) + if !bare { + gittest.AddWorktree(t, cfg, repoPath, "worktree") + repoPath = filepath.Join(repoPath, "worktree") + // AddWorktree creates a detached worktree. Checkout master here so the + // branch pointer moves as we later commit. + gittest.Exec(t, cfg, "-C", repoPath, "checkout", "master") + } return cfg, repo, repoPath, client } @@ -85,7 +83,7 @@ func runConflictsServer(t testing.TB, cfg config.Cfg, hookManager hook.Manager) deps.GetLinguist(), deps.GetCatfileCache(), )) - }, testserver.WithHookManager(hookManager)) + }, testserver.WithHookManager(hookManager), testserver.WithDisableMetadataForceCreation()) } func NewConflictsClient(t testing.TB, serverSocketPath string) (gitalypb.ConflictsServiceClient, *grpc.ClientConn) { |