diff options
Diffstat (limited to 'internal/gitaly/rubyserver/rubyserver_test.go')
-rw-r--r-- | internal/gitaly/rubyserver/rubyserver_test.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/internal/gitaly/rubyserver/rubyserver_test.go b/internal/gitaly/rubyserver/rubyserver_test.go index a98d0eeb4..9e24585b8 100644 --- a/internal/gitaly/rubyserver/rubyserver_test.go +++ b/internal/gitaly/rubyserver/rubyserver_test.go @@ -3,11 +3,14 @@ package rubyserver import ( "context" "fmt" + "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/v14/internal/git" + "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/config/auth" "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config/log" @@ -141,3 +144,82 @@ func TestSetupEnv(t *testing.T) { "GIT_CONFIG_COUNT=1", }) } + +func TestServer_gitconfig(t *testing.T) { + for _, tc := range []struct { + desc string + setup func(t *testing.T) (config.Cfg, string) + expectedGitconfig string + }{ + { + desc: "writes a default gitconfig", + setup: func(t *testing.T) (config.Cfg, string) { + cfg := testcfg.Build(t) + expectedPath := filepath.Join(cfg.RuntimeDir, "ruby-gitconfig", "gitconfig") + return cfg, expectedPath + }, + // If no search path is provided, we should create a placeholder file that + // contains all settings important to Rugged. + expectedGitconfig: "[core]\n\tfsyncObjectFiles = true\n", + }, + { + desc: "uses injected gitconfig", + setup: func(t *testing.T) (config.Cfg, string) { + gitconfigDir := testhelper.TempDir(t) + expectedPath := filepath.Join(gitconfigDir, "gitconfig") + require.NoError(t, os.WriteFile(expectedPath, []byte("garbage"), 0o666)) + + cfg := testcfg.Build(t, testcfg.WithBase(config.Cfg{ + Ruby: config.Ruby{ + RuggedGitConfigSearchPath: gitconfigDir, + }, + })) + + return cfg, expectedPath + }, + // The gitconfig shouldn't have been rewritten, so it should still contain + // garbage after the server starts. + expectedGitconfig: "garbage", + }, + } { + t.Run(tc.desc, func(t *testing.T) { + ctx := testhelper.Context(t) + cfg, expectedPath := tc.setup(t) + + gitCmdFactory := gittest.NewCommandFactory(t, cfg) + locator := config.NewLocator(cfg) + + rubyServer := New(cfg, gitCmdFactory) + require.NoError(t, rubyServer.Start()) + defer rubyServer.Stop() + + // Verify that the expected gitconfig exists and has expected contents. + gitconfigContents := testhelper.MustReadFile(t, expectedPath) + require.Equal(t, tc.expectedGitconfig, string(gitconfigContents)) + + // Write a gitconfig that is invalidly formatted. Like this we can assert + // whether the Ruby server tries to read it or not because it should in fact + // fail. + require.NoError(t, os.WriteFile(expectedPath, []byte( + `[`, + ), 0o666)) + + repo, _ := gittest.InitRepo(t, cfg, cfg.Storages[0]) + + // We now do any random RPC request that hits the Ruby server... + client, err := rubyServer.WikiServiceClient(ctx) + require.NoError(t, err) + + ctx, err = SetHeaders(ctx, locator, repo) + require.NoError(t, err) + + stream, err := client.WikiListPages(ctx, &gitalypb.WikiListPagesRequest{Repository: repo}) + require.NoError(t, err) + + // ... and expect it to fail with an error parsing the configuration. This + // demonstrates the config was injected successfully. + _, err = stream.Recv() + testhelper.RequireGrpcError(t, fmt.Errorf("Rugged::ConfigError: failed to parse config file: missing ']' in section header (in %s:1)", expectedPath), err) + }) + } +} |