1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package git_test
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"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/helper/text"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper/testcfg"
)
func TestGitCommandProxy(t *testing.T) {
cfg := testcfg.Build(t)
requestReceived := false
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestReceived = true
}))
defer ts.Close()
oldHTTPProxy := os.Getenv("http_proxy")
defer require.NoError(t, os.Setenv("http_proxy", oldHTTPProxy))
require.NoError(t, os.Setenv("http_proxy", ts.URL))
ctx, cancel := testhelper.Context()
defer cancel()
dir := testhelper.TempDir(t)
gitCmdFactory := git.NewExecCommandFactory(cfg)
cmd, err := gitCmdFactory.NewWithoutRepo(ctx, git.SubCmd{
Name: "clone",
Args: []string{"http://gitlab.com/bogus-repo", dir},
}, git.WithDisabledHooks())
require.NoError(t, err)
err = cmd.Wait()
require.NoError(t, err)
require.True(t, requestReceived)
}
func TestExecCommandFactory_NewWithDir(t *testing.T) {
cfg := testcfg.Build(t)
gitCmdFactory := git.NewExecCommandFactory(cfg)
t.Run("no dir specified", func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
_, err := gitCmdFactory.NewWithDir(ctx, "", nil, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "no 'dir' provided")
})
t.Run("runs in dir", func(t *testing.T) {
repoPath := testhelper.TempDir(t)
gittest.Exec(t, cfg, "init", repoPath)
gittest.Exec(t, cfg, "-C", repoPath, "commit", "--allow-empty", "-m", "initial commit")
ctx, cancel := testhelper.Context()
defer cancel()
var stderr bytes.Buffer
cmd, err := gitCmdFactory.NewWithDir(ctx, repoPath, git.SubCmd{
Name: "rev-parse",
Args: []string{"master"},
}, git.WithStderr(&stderr))
require.NoError(t, err)
revData, err := io.ReadAll(cmd)
require.NoError(t, err)
require.NoError(t, cmd.Wait(), stderr.String())
require.Equal(t, "99ed180822d96f70810847eba6d0d168c582258d", text.ChompBytes(revData))
})
t.Run("doesn't runs in non existing dir", func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
var stderr bytes.Buffer
_, err := gitCmdFactory.NewWithDir(ctx, "non-existing-dir", git.SubCmd{
Name: "rev-parse",
Args: []string{"master"},
}, git.WithStderr(&stderr))
require.Error(t, err)
require.Contains(t, err.Error(), "no such file or directory")
})
}
|