diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-09-21 13:29:18 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-10-06 12:53:40 +0300 |
commit | 5cb50ac08b342056fa37df7ab3d5249ed069ff53 (patch) | |
tree | b7ff856631f489a6236f6d40e34943750dba56ea | |
parent | cd94be7ff514fd2e3e84910c42a46995c444b70a (diff) |
repository: Fix test for failing fetch in ReplicateRepository
While the testcase for ReplicateRepository which exercises failing
fetches fails as expected, the reason for failure is not the failing
fetch but instead it is that we do not have the required set of binaries
installed. Fixing this surfaces another issue: because fetches nowadays
happen internally instead of calling the `FetchInternalRemote()` RPC,
the mock remote server which stubs out the RPC with an error isn't
invoked at all.
Fix the test setup by setting up a "real" gRPC server, where the fetch
failure is provoked by writing garbage into the source repo's HEAD.
-rw-r--r-- | internal/gitaly/service/repository/replicate_test.go | 66 |
1 files changed, 24 insertions, 42 deletions
diff --git a/internal/gitaly/service/repository/replicate_test.go b/internal/gitaly/service/repository/replicate_test.go index 9ec61609a..8cd7dec72 100644 --- a/internal/gitaly/service/repository/replicate_test.go +++ b/internal/gitaly/service/repository/replicate_test.go @@ -12,7 +12,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v14/internal/backchannel" "gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config" - "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v14/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v14/internal/metadata" "gitlab.com/gitlab-org/gitaly/v14/internal/metadata/featureflag" @@ -351,59 +350,42 @@ func TestReplicateRepository_BadRepository(t *testing.T) { func TestReplicateRepository_FailedFetchInternalRemote(t *testing.T) { t.Parallel() - cfgBuilder := testcfg.NewGitalyCfgBuilder(testcfg.WithStorages("default", "replica")) - cfg := cfgBuilder.Build(t) - - cfg.SocketPath = runServerWithBadFetchInternalRemote(t, cfg) - locator := config.NewLocator(cfg) + cfg := testcfg.Build(t, testcfg.WithStorages("default", "replica")) + testhelper.BuildGitalyHooks(t, cfg) + testhelper.BuildGitalySSH(t, cfg) - testRepo, _ := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + // Our test setup does not allow for Praefects with multiple storages. We thus have to + // disable Praefect here. + cfg.SocketPath = runRepositoryServerWithConfig(t, cfg, nil, testserver.WithDisablePraefect()) - repoClient := newRepositoryClient(t, cfg, cfg.SocketPath) + targetRepo, _ := gittest.InitRepo(t, cfg, cfg.Storages[1]) - targetRepo := proto.Clone(testRepo).(*gitalypb.Repository) - targetRepo.StorageName = cfg.Storages[1].Name - - targetRepoPath, err := locator.GetPath(targetRepo) + // The source repository must be at the same path as the target repository, and it must be a + // real repository. In order to still have the fetch fail, we corrupt the repository by + // writing garbage into HEAD. + sourceRepo := &gitalypb.Repository{ + StorageName: "default", + RelativePath: targetRepo.RelativePath, + } + sourceRepoPath, err := config.NewLocator(cfg).GetPath(sourceRepo) require.NoError(t, err) - - require.NoError(t, os.MkdirAll(targetRepoPath, 0o755)) - testhelper.MustRunCommand(t, nil, "touch", filepath.Join(targetRepoPath, "invalid_git_repo")) + require.NoError(t, os.MkdirAll(sourceRepoPath, 0o777)) + gittest.Exec(t, cfg, "init", "--bare", sourceRepoPath) + require.NoError(t, os.WriteFile(filepath.Join(sourceRepoPath, "HEAD"), []byte("garbage"), 0o666)) ctx, cancel := testhelper.Context() defer cancel() md := testhelper.GitalyServersMetadataFromCfg(t, cfg) - injectedCtx := grpc_metadata.NewOutgoingContext(ctx, md) + ctx = grpc_metadata.NewOutgoingContext(ctx, md) - _, err = repoClient.ReplicateRepository(injectedCtx, &gitalypb.ReplicateRepositoryRequest{ + repoClient := newRepositoryClient(t, cfg, cfg.SocketPath) + + _, err = repoClient.ReplicateRepository(ctx, &gitalypb.ReplicateRepositoryRequest{ Repository: targetRepo, - Source: testRepo, + Source: sourceRepo, }) require.Error(t, err) -} - -func runServerWithBadFetchInternalRemote(t *testing.T, cfg config.Cfg) string { - return testserver.RunGitalyServer(t, cfg, nil, func(srv *grpc.Server, deps *service.Dependencies) { - gitalypb.RegisterRepositoryServiceServer(srv, NewServer( - deps.GetCfg(), - deps.GetRubyServer(), - deps.GetLocator(), - deps.GetTxManager(), - deps.GetGitCmdFactory(), - deps.GetCatfileCache(), - )) - gitalypb.RegisterRemoteServiceServer(srv, &mockRemoteServer{}) - }) -} - -type mockRemoteServer struct { - gitalypb.UnimplementedRemoteServiceServer -} - -func (m *mockRemoteServer) FetchInternalRemote(ctx context.Context, req *gitalypb.FetchInternalRemoteRequest) (*gitalypb.FetchInternalRemoteResponse, error) { - return &gitalypb.FetchInternalRemoteResponse{ - Result: false, - }, nil + require.Contains(t, err.Error(), "fetch: exit status 128") } |