Welcome to mirror list, hosted at ThFree Co, Russian Federation.

execution_environment_test.go « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e3775c11316962723a75bc8505fb5ebb013aaecb (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package git_test

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v14/internal/git"
	"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config"
	"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)

func TestDistributedGitEnvironmentConstructor(t *testing.T) {
	constructor := git.DistributedGitEnvironmentConstructor{}

	testhelper.ModifyEnvironment(t, "GITALY_TESTING_GIT_BINARY", "")

	t.Run("empty configuration fails", func(t *testing.T) {
		_, err := constructor.Construct(config.Cfg{})
		require.Equal(t, git.ErrNotConfigured, err)
	})

	t.Run("configuration with Git binary path succeeds", func(t *testing.T) {
		execEnv, err := constructor.Construct(config.Cfg{
			Git: config.Git{
				BinPath: "/foo/bar",
			},
		})
		require.NoError(t, err)
		defer execEnv.Cleanup()

		require.Equal(t, "/foo/bar", execEnv.BinaryPath)
		require.Equal(t, []string(nil), execEnv.EnvironmentVariables)
	})

	t.Run("empty configuration with environment override", func(t *testing.T) {
		testhelper.ModifyEnvironment(t, "GITALY_TESTING_GIT_BINARY", "/foo/bar")

		execEnv, err := constructor.Construct(config.Cfg{})
		require.NoError(t, err)
		defer execEnv.Cleanup()

		require.Equal(t, "/foo/bar", execEnv.BinaryPath)
		require.Equal(t, []string(nil), execEnv.EnvironmentVariables)
	})

	t.Run("configuration overrides environment variable", func(t *testing.T) {
		testhelper.ModifyEnvironment(t, "GITALY_TESTING_GIT_BINARY", "envvar")

		execEnv, err := constructor.Construct(config.Cfg{
			Git: config.Git{
				BinPath: "config",
			},
		})
		require.NoError(t, err)
		defer execEnv.Cleanup()

		require.Equal(t, "config", execEnv.BinaryPath)
		require.Equal(t, []string(nil), execEnv.EnvironmentVariables)
	})
}

func TestBundledGitEnvironmentConstructor(t *testing.T) {
	testhelper.ModifyEnvironment(t, "GITALY_TESTING_BUNDLED_GIT_PATH", "")

	constructor := git.BundledGitEnvironmentConstructor{}

	seedDirWithExecutables := func(t *testing.T, executableNames ...string) string {
		dir := testhelper.TempDir(t)
		for _, executableName := range executableNames {
			require.NoError(t, os.WriteFile(filepath.Join(dir, executableName), nil, 0o777))
		}
		return dir
	}

	t.Run("disabled bundled Git fails", func(t *testing.T) {
		_, err := constructor.Construct(config.Cfg{})
		require.Equal(t, git.ErrNotConfigured, err)
	})

	t.Run("bundled Git without binary directory fails", func(t *testing.T) {
		_, err := constructor.Construct(config.Cfg{
			Git: config.Git{
				UseBundledBinaries: true,
			},
		})
		require.Equal(t, errors.New("cannot use bundled binaries without bin path being set"), err)
	})

	t.Run("incomplete binary directory succeeds", func(t *testing.T) {
		_, err := constructor.Construct(config.Cfg{
			BinDir: seedDirWithExecutables(t, "gitaly-git", "gitaly-git-remote-http"),
			Git: config.Git{
				UseBundledBinaries: true,
			},
		})
		require.Error(t, err)
		require.Equal(t, "checking bundled Git binary \"gitaly-git-http-backend\": no such file or directory", err.Error())
	})

	t.Run("complete binary directory succeeds", func(t *testing.T) {
		binDir := seedDirWithExecutables(t, "gitaly-git", "gitaly-git-remote-http", "gitaly-git-http-backend")

		execEnv, err := constructor.Construct(config.Cfg{
			BinDir: binDir,
			Git: config.Git{
				UseBundledBinaries: true,
			},
		})
		require.NoError(t, err)
		defer execEnv.Cleanup()

		// We create a temporary directory where the symlinks are created, and we cannot
		// predict its exact path.
		require.Equal(t, "git", filepath.Base(execEnv.BinaryPath))

		execPrefix := filepath.Dir(execEnv.BinaryPath)
		require.Equal(t, []string{
			"GIT_EXEC_PATH=" + execPrefix,
		}, execEnv.EnvironmentVariables)

		for _, binary := range []string{"git", "git-remote-http", "git-http-backend"} {
			target, err := filepath.EvalSymlinks(filepath.Join(execPrefix, binary))
			require.NoError(t, err)
			require.Equal(t, filepath.Join(binDir, "gitaly-"+binary), target)
		}
	})

	t.Run("cleanup removes temporary directory", func(t *testing.T) {
		execEnv, err := constructor.Construct(config.Cfg{
			BinDir: seedDirWithExecutables(t, "gitaly-git", "gitaly-git-remote-http", "gitaly-git-http-backend"),
			Git: config.Git{
				UseBundledBinaries: true,
			},
		})
		require.NoError(t, err)

		execPrefix := filepath.Dir(execEnv.BinaryPath)
		require.DirExists(t, execPrefix)

		execEnv.Cleanup()

		require.NoDirExists(t, execPrefix)
	})

	t.Run("bundled Git path without binary directory fails", func(t *testing.T) {
		testhelper.ModifyEnvironment(t, "GITALY_TESTING_BUNDLED_GIT_PATH", "/does/not/exist")
		_, err := constructor.Construct(config.Cfg{})
		require.Equal(t, errors.New("cannot use bundled binaries without bin path being set"), err)
	})

	t.Run("nonexistent bundled Git path via environment fails", func(t *testing.T) {
		testhelper.ModifyEnvironment(t, "GITALY_TESTING_BUNDLED_GIT_PATH", "/does/not/exist")
		_, err := constructor.Construct(config.Cfg{
			BinDir: testhelper.TempDir(t),
		})
		require.Error(t, err)
		require.Equal(t, err.Error(), "statting \"gitaly-git\": stat /does/not/exist/gitaly-git: no such file or directory")
	})

	t.Run("incomplete bundled Git environment fails", func(t *testing.T) {
		bundledGitPath := seedDirWithExecutables(t, "gitaly-git", "gitaly-git-remote-http")
		testhelper.ModifyEnvironment(t, "GITALY_TESTING_BUNDLED_GIT_PATH", bundledGitPath)

		_, err := constructor.Construct(config.Cfg{
			BinDir: testhelper.TempDir(t),
		})
		require.Error(t, err)
		require.Contains(t, err.Error(), "statting \"gitaly-git-http-backend\": ")
	})

	t.Run("complete bundled Git environment populates binary directory", func(t *testing.T) {
		bundledGitPath := seedDirWithExecutables(t, "gitaly-git", "gitaly-git-remote-http", "gitaly-git-http-backend")
		testhelper.ModifyEnvironment(t, "GITALY_TESTING_BUNDLED_GIT_PATH", bundledGitPath)

		execEnv, err := constructor.Construct(config.Cfg{
			BinDir: testhelper.TempDir(t),
		})
		require.NoError(t, err)
		defer execEnv.Cleanup()

		require.Equal(t, "git", filepath.Base(execEnv.BinaryPath))
		execPrefix := filepath.Dir(execEnv.BinaryPath)

		require.Equal(t, []string{
			"GIT_EXEC_PATH=" + execPrefix,
		}, execEnv.EnvironmentVariables)

		for _, binary := range []string{"git", "git-remote-http", "git-http-backend"} {
			target, err := filepath.EvalSymlinks(filepath.Join(execPrefix, binary))
			require.NoError(t, err)
			require.Equal(t, filepath.Join(bundledGitPath, "gitaly-"+binary), target)
		}
	})

	t.Run("with version suffix", func(t *testing.T) {
		constructor := git.BundledGitEnvironmentConstructor{
			Suffix: "-v2.35.1",
		}

		binDir := seedDirWithExecutables(t, "gitaly-git-v2.35.1", "gitaly-git-remote-http-v2.35.1", "gitaly-git-http-backend-v2.35.1")

		execEnv, err := constructor.Construct(config.Cfg{
			BinDir: binDir,
			Git: config.Git{
				UseBundledBinaries: true,
			},
		})
		require.NoError(t, err)
		defer execEnv.Cleanup()

		require.Equal(t, "git", filepath.Base(execEnv.BinaryPath))
		execPrefix := filepath.Dir(execEnv.BinaryPath)
		require.Equal(t, []string{
			"GIT_EXEC_PATH=" + execPrefix,
		}, execEnv.EnvironmentVariables)

		for _, binary := range []string{"git", "git-remote-http", "git-http-backend"} {
			target, err := filepath.EvalSymlinks(filepath.Join(execPrefix, binary))
			require.NoError(t, err)
			require.Equal(t, filepath.Join(binDir, "gitaly-"+binary+"-v2.35.1"), target)
		}
	})
}

func TestFallbackGitEnvironmentConstructor(t *testing.T) {
	constructor := git.FallbackGitEnvironmentConstructor{}

	t.Run("failing lookup of executable causes failure", func(t *testing.T) {
		testhelper.ModifyEnvironment(t, "PATH", "/does/not/exist")

		_, err := constructor.Construct(config.Cfg{})
		require.Equal(t, fmt.Errorf("%w: no git executable found in PATH", git.ErrNotConfigured), err)
	})

	t.Run("successfully resolved executable", func(t *testing.T) {
		tempDir := testhelper.TempDir(t)
		gitPath := filepath.Join(tempDir, "git")
		require.NoError(t, os.WriteFile(gitPath, nil, 0o755))

		testhelper.ModifyEnvironment(t, "PATH", "/does/not/exist:"+tempDir)

		execEnv, err := constructor.Construct(config.Cfg{})
		require.NoError(t, err)
		defer execEnv.Cleanup()

		require.Equal(t, gitPath, execEnv.BinaryPath)
		require.Equal(t, []string(nil), execEnv.EnvironmentVariables)
	})
}