diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-03-28 09:33:46 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-03-28 09:35:59 +0300 |
commit | e11d3f00ce8d9201b91c3524af1ebc8ae5f6a020 (patch) | |
tree | e0bb561973cea26cd3a5fc60edb64f3bc953ef1b | |
parent | 0983a83714199f0fb59eba04459aaab44a89de57 (diff) |
ssh: Inject feature flags via the test context
When setting up a push for our tests, we also have the ability to inject
feature flags by setting the `featureFlags` field. This has a major
downside though: the feature flags injected via the context and the
feature flags executed at a later point when actually running the
command may easily drift apart. This inconsistency is something that may
break assumptions on our side though, where some parts may now evaluate
a flag to `true` while others evaluate it to `false`.
Fix this inconsistency by instead accepting feature flags via a context.
This both simplifies the code and fixes such an upcoming incompatibility
when we're about to default-enable Git v2.35.1.gl1.
-rw-r--r-- | internal/gitaly/service/ssh/receive_pack_test.go | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/internal/gitaly/service/ssh/receive_pack_test.go b/internal/gitaly/service/ssh/receive_pack_test.go index 266b99844..cc03f870e 100644 --- a/internal/gitaly/service/ssh/receive_pack_test.go +++ b/internal/gitaly/service/ssh/receive_pack_test.go @@ -2,6 +2,7 @@ package ssh import ( "bytes" + "context" "fmt" "io" "os" @@ -118,18 +119,16 @@ func TestReceivePackPushSuccess(t *testing.T) { // when deserializing the HooksPayload. By setting all flags to `true` explicitly, we both // verify that gitaly-ssh picks up feature flags correctly and fix the test to behave the // same with and without Praefect. - featureFlags := map[featureflag.FeatureFlag]bool{} for _, featureFlag := range featureflag.All { - featureFlags[featureFlag] = true + ctx = featureflag.ContextWithFeatureFlag(ctx, featureFlag, true) } - lHead, rHead, err := testCloneAndPush(t, cfg, cfg.SocketPath, repo, repoPath, pushParams{ + lHead, rHead, err := testCloneAndPush(ctx, t, cfg, cfg.SocketPath, repo, repoPath, pushParams{ storageName: cfg.Storages[0].Name, glID: "123", glUsername: "user", glRepository: glRepository, glProjectPath: glProjectPath, - featureFlags: featureFlags, }) require.NoError(t, err) require.Equal(t, lHead, rHead, "local and remote head not equal. push failed") @@ -187,7 +186,7 @@ func TestReceivePackPushSuccessWithGitProtocol(t *testing.T) { Seed: gittest.SeedGitLabTest, }) - lHead, rHead, err := testCloneAndPush(t, cfg, cfg.SocketPath, repo, repoPath, pushParams{ + lHead, rHead, err := testCloneAndPush(ctx, t, cfg, cfg.SocketPath, repo, repoPath, pushParams{ storageName: testhelper.DefaultStorageName, glRepository: "project-123", glID: "1", @@ -204,14 +203,16 @@ func TestReceivePackPushSuccessWithGitProtocol(t *testing.T) { func TestReceivePackPushFailure(t *testing.T) { t.Parallel() + ctx := testhelper.Context(t) + cfg, repo, repoPath := testcfg.BuildWithRepo(t) serverSocketPath := runSSHServer(t, cfg) - _, _, err := testCloneAndPush(t, cfg, serverSocketPath, repo, repoPath, pushParams{storageName: "foobar", glID: "1"}) + _, _, err := testCloneAndPush(ctx, t, cfg, serverSocketPath, repo, repoPath, pushParams{storageName: "foobar", glID: "1"}) require.Error(t, err, "local and remote head equal. push did not fail") - _, _, err = testCloneAndPush(t, cfg, serverSocketPath, repo, repoPath, pushParams{storageName: cfg.Storages[0].Name, glID: ""}) + _, _, err = testCloneAndPush(ctx, t, cfg, serverSocketPath, repo, repoPath, pushParams{storageName: cfg.Storages[0].Name, glID: ""}) require.Error(t, err, "local and remote head equal. push did not fail") } @@ -233,7 +234,7 @@ func TestReceivePackPushHookFailure(t *testing.T) { hookContent := []byte("#!/bin/sh\nexit 1") require.NoError(t, os.WriteFile(filepath.Join(gitCmdFactory.HooksPath(ctx), "pre-receive"), hookContent, 0o755)) - _, _, err := testCloneAndPush(t, cfg, cfg.SocketPath, repo, repoPath, pushParams{storageName: cfg.Storages[0].Name, glID: "1"}) + _, _, err := testCloneAndPush(ctx, t, cfg, cfg.SocketPath, repo, repoPath, pushParams{storageName: cfg.Storages[0].Name, glID: "1"}) require.Error(t, err) require.Contains(t, err.Error(), "(pre-receive hook declined)") } @@ -549,7 +550,7 @@ func TestSSHReceivePackToHooks(t *testing.T) { gittest.WriteCheckNewObjectExistsHook(t, cloneDetails.RemoteRepoPath) - lHead, rHead, err := sshPush(t, cfg, cloneDetails, cfg.SocketPath, pushParams{ + lHead, rHead, err := sshPush(ctx, t, cfg, cloneDetails, cfg.SocketPath, pushParams{ storageName: cfg.Storages[0].Name, glID: glID, glRepository: glRepository, @@ -577,8 +578,8 @@ func setupSSHClone(t *testing.T, cfg config.Cfg, remoteRepo *gitalypb.Repository newHead := gittest.WriteCommit(t, cfg, localRepoPath, gittest.WithMessage(fmt.Sprintf("Testing ReceivePack RPC around %d", time.Now().Unix())), gittest.WithTreeEntries(gittest.TreeEntry{ - Path: "foo.txt", - Mode: "100644", + Path: "foo.txt", + Mode: "100644", Content: "foo bar", }), gittest.WithBranch("master"), @@ -597,7 +598,7 @@ func setupSSHClone(t *testing.T, cfg config.Cfg, remoteRepo *gitalypb.Repository } } -func sshPush(t *testing.T, cfg config.Cfg, cloneDetails SSHCloneDetails, serverSocketPath string, params pushParams) (string, string, error) { +func sshPush(ctx context.Context, t *testing.T, cfg config.Cfg, cloneDetails SSHCloneDetails, serverSocketPath string, params pushParams) (string, string, error) { pbTempRepo := &gitalypb.Repository{ StorageName: params.storageName, RelativePath: cloneDetails.TempRepo, @@ -614,16 +615,11 @@ func sshPush(t *testing.T, cfg config.Cfg, cloneDetails SSHCloneDetails, serverS }) require.NoError(t, err) - featureFlags := []string{} - for flag, value := range params.featureFlags { - featureFlags = append(featureFlags, fmt.Sprintf("%s:%v", flag.Name, value)) - } - cmd := gittest.NewCommand(t, cfg, "-C", cloneDetails.LocalRepoPath, "push", "-v", "git@localhost:test/test.git", "master") cmd.Env = []string{ fmt.Sprintf("GITALY_PAYLOAD=%s", payload), fmt.Sprintf("GITALY_ADDRESS=%s", serverSocketPath), - fmt.Sprintf("GITALY_FEATUREFLAGS=%s", strings.Join(featureFlags, ",")), + fmt.Sprintf("GITALY_FEATUREFLAGS=%s", strings.Join(featureflag.AllFlags(ctx), ",")), fmt.Sprintf("GIT_SSH_COMMAND=%s receive-pack", filepath.Join(cfg.BinDir, "gitaly-ssh")), } @@ -642,11 +638,11 @@ func sshPush(t *testing.T, cfg config.Cfg, cloneDetails SSHCloneDetails, serverS return string(localHead), string(remoteHead), nil } -func testCloneAndPush(t *testing.T, cfg config.Cfg, serverSocketPath string, remoteRepo *gitalypb.Repository, remoteRepoPath string, params pushParams) (string, string, error) { +func testCloneAndPush(ctx context.Context, t *testing.T, cfg config.Cfg, serverSocketPath string, remoteRepo *gitalypb.Repository, remoteRepoPath string, params pushParams) (string, string, error) { cloneDetails, cleanup := setupSSHClone(t, cfg, remoteRepo, remoteRepoPath) defer cleanup() - return sshPush(t, cfg, cloneDetails, serverSocketPath, params) + return sshPush(ctx, t, cfg, cloneDetails, serverSocketPath, params) } func drainPostReceivePackResponse(stream gitalypb.SSHService_SSHReceivePackClient) error { @@ -665,5 +661,4 @@ type pushParams struct { glProjectPath string gitConfigOptions []string gitProtocol string - featureFlags map[featureflag.FeatureFlag]bool } |