Repository navigation
Expand file tree
/
Copy pathuv_git_sources_recipe_test.go
More file actions
390 lines (351 loc) · 17.4 KB
/
Copy pathuv_git_sources_recipe_test.go
File metadata and controls
390 lines (351 loc) · 17.4 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/codefly-dev/core/agents/services"
builderv0 "github.com/codefly-dev/core/generated/go/codefly/services/builder/v0"
"github.com/stretchr/testify/require"
)
const (
commitA = "8b58bb4786656900153af99dd758fc496b9c3c96"
commitB = "90db9b742baa9c07532cc08a90d58e790b881312"
)
// gitSourcesLock is a lock in the form uv writes it: two packages built from
// subdirectories of one repository at one commit, the same repository at a
// second commit, an ssh source with a port, sources a recipe cannot declare
// (http, file, a credential, percent-encoding, "@" in the path), and a
// registry package.
var gitSourcesLock = fmt.Sprintf(`version = 1
requires-python = ">=3.12"
[[package]]
name = "liba"
version = "0.1.0"
source = { git = "https://git.example.invalid/acme/mono?subdirectory=liba&rev=%[1]s#%[1]s" }
[[package]]
name = "libb"
version = "0.1.0"
source = { git = "https://git.example.invalid/acme/mono?subdirectory=libb&rev=%[1]s#%[1]s" }
[[package]]
name = "libc"
version = "0.2.0"
source = { git = "https://git.example.invalid/acme/mono?subdirectory=libc&rev=%[2]s#%[2]s" }
[[package]]
name = "libd"
version = "0.1.0"
source = { git = "ssh://git@git.example.invalid:2222/acme/tools.git?rev=main#%[2]s" }
[[package]]
name = "libe"
version = "0.1.0"
source = { git = "http://token@git.example.invalid/acme/plain?rev=main#%[1]s" }
[[package]]
name = "libf"
version = "0.1.0"
source = { git = "file:///srv/local?rev=main#%[1]s" }
[[package]]
name = "libg"
version = "0.1.0"
source = { git = "https://TOKEN@git.example.invalid/acme/secret?rev=main#%[1]s" }
[[package]]
name = "libh"
version = "0.1.0"
source = { git = "https://git.example.invalid/Team%%20Project/repo?rev=main#%[1]s" }
[[package]]
name = "libi"
version = "0.1.0"
source = { git = "https://git.example.invalid/acme/lib@v1?rev=main#%[1]s" }
[[package]]
name = "six"
version = "1.17.0"
source = { registry = "https://pypi.org/simple" }
`, commitA, commitB)
var gitSourcesDeclared = []*builderv0.GitSourceDownload{
{Repository: "https://git.example.invalid/acme/mono", Commit: commitA, MirrorContext: "uvsources"},
{Repository: "https://git.example.invalid/acme/mono", Commit: commitB, MirrorContext: "uvsources"},
{Repository: "ssh://git@git.example.invalid:2222/acme/tools.git", Commit: commitB, MirrorContext: "uvsources"},
}
func requireDeclares(t *testing.T, want, got []*builderv0.GitSourceDownload) {
t.Helper()
require.Len(t, got, len(want))
for i := range want {
require.Equal(t, want[i].String(), got[i].String())
}
}
// TestLockedGitSourcesDeclaresEachLockedCommitOnce reads the URL uv hands git
// (the recorded one up to its query) and the locked commit, sorted, with a
// repository at one commit declared once however many packages it builds. The
// http and file sources are left out, named by package for the warning, and
// left to the build to fetch as before.
func TestLockedGitSourcesDeclaresEachLockedCommitOnce(t *testing.T) {
lock := filepath.Join(t.TempDir(), "uv.lock")
writeFile(t, lock, gitSourcesLock)
declared, unmirrored, err := lockedGitSources(lock)
require.NoError(t, err)
requireDeclares(t, gitSourcesDeclared, declared)
require.Equal(t, []unmirroredSource{
{Package: "libe", Reason: "repository must start with https:// or ssh://"},
{Package: "libf", Reason: "repository must start with https:// or ssh://"},
{Package: "libg", Reason: "repository must carry no user information over https"},
{Package: "libh", Reason: "repository must carry no percent-encoding"},
{Package: "libi", Reason: "repository must carry no @ in the path"},
}, unmirrored)
mirrors := uvGitMirrors(declared)
require.Len(t, mirrors, 2, "one mirror per repository")
require.Equal(t, services.GitSourceMirrorPath("https://git.example.invalid/acme/mono"), mirrors[0].Path)
}
// TestLockedGitSourcesRefusesAnUnpinnedSource: uv always records the full
// commit id, so a git source without one is a malformed lock. The error names
// the package, never the URL, which may carry a credential.
func TestLockedGitSourcesRefusesAnUnpinnedSource(t *testing.T) {
for _, source := range []string{"https://token@git.example.invalid/acme/mono?rev=main", "https://token@git.example.invalid/acme/mono#8b58bb4"} {
lock := filepath.Join(t.TempDir(), "uv.lock")
writeFile(t, lock, fmt.Sprintf("version = 1\n\n[[package]]\nname = \"liba\"\nversion = \"0.1.0\"\nsource = { git = %q }\n", source))
_, _, err := lockedGitSources(lock)
require.ErrorContains(t, err, `package "liba"`)
require.NotContains(t, err.Error(), "token")
}
}
// TestBuildDeclaresTheLockedGitSources holds the recipe: exactly the lock's git
// sources, the v6 contract, and the mirror mapping in the Dockerfile; without
// git sources, no declaration, the contract and Dockerfile of before.
func TestBuildDeclaresTheLockedGitSources(t *testing.T) {
builder, _ := createdBuilder(t)
outputDir := t.TempDir()
plan := emitRecipe(t, builder, outputDir)
require.Empty(t, plan.GetRecipes()[0].GetGitSourceDownloads())
require.Equal(t, services.DockerBuildRecipeContractVersion, plan.GetContractVersion())
requireGolden(t, "no-git-sources", dockerfileOf(t, outputDir, plan))
writeFile(t, filepath.Join(builder.SourceLocation, "uv.lock"), gitSourcesLock)
outputDir = t.TempDir()
plan = emitRecipe(t, builder, outputDir)
require.NoError(t, services.VerifyDockerBuildPlan(outputDir, plan))
requireDeclares(t, gitSourcesDeclared, plan.GetRecipes()[0].GetGitSourceDownloads())
require.Equal(t, services.DockerBuildRecipeGitSourcesContractVersion, plan.GetContractVersion())
requireGolden(t, "git-sources", dockerfileOf(t, outputDir, plan))
// What a recipe cannot declare reaches neither the plan nor the Dockerfile.
for _, rendered := range []string{plan.String(), dockerfileOf(t, outputDir, plan)} {
for _, refused := range []string{"TOKEN", "Team", "lib@v1", "acme/plain", "/srv/local"} {
require.NotContains(t, rendered, refused)
}
}
}
// TestWorkspaceMemberDeclaresTheRootLocksGitSources reads the lock uv syncs a
// workspace member from: the workspace root's.
func TestWorkspaceMemberDeclaresTheRootLocksGitSources(t *testing.T) {
builder, _ := createdBuilder(t)
uvWorkspaceService(t, builder.Location, workspaceRequiresPython)
writeFile(t, filepath.Join(builder.Location, "uv.lock"), gitSourcesLock)
plan := emitRecipe(t, builder, t.TempDir())
requireDeclares(t, gitSourcesDeclared, plan.GetRecipes()[0].GetGitSourceDownloads())
}
// requireGolden compares a rendered Dockerfile, its pinned images replaced, to
// testdata/dockerfile/<name>.golden. The no-git-sources golden is the
// Dockerfile rendered before git sources were declared, byte for byte.
// UPDATE_GOLDEN=1 rewrites them after an intended template change.
func requireGolden(t *testing.T, name, dockerfile string) {
t.Helper()
dockerfile = strings.ReplaceAll(dockerfile, runtimeImage.FullName(), "BUILDER_IMAGE")
dockerfile = strings.ReplaceAll(dockerfile, uvImage, "UV_IMAGE")
path := filepath.Join("testdata", "dockerfile", name+".golden")
if os.Getenv("UPDATE_GOLDEN") != "" {
require.NoError(t, os.WriteFile(path, []byte(dockerfile), 0o644))
}
golden, err := os.ReadFile(path)
require.NoError(t, err)
require.Equal(t, string(golden), dockerfile)
}
// run runs a command in dir and fails the test on error.
func run(t *testing.T, dir string, env []string, name string, args ...string) string {
t.Helper()
command := exec.CommandContext(t.Context(), name, args...)
command.Dir = dir
command.Env = append(os.Environ(), env...)
out, err := command.CombinedOutput()
require.NoError(t, err, "%s %v: %s", name, args, out)
return strings.TrimSpace(string(out))
}
// TestLockedGitSourceBuildsFromTheSuppliedMirror is the claim end to end. The
// project depends on a package in a subdirectory of a git repository whose URL
// the build cannot reach, standing in for one it has no credential for; the
// host's uv locks it through a host-side rewrite, as a host credential would
// let it. Built with the mirror context the caller supplies, the image carries
// the package; built without it, the build fails on the unreachable URL.
func TestLockedGitSourceBuildsFromTheSuppliedMirror(t *testing.T) {
for _, tool := range []string{"docker", "uv", "git"} {
if _, err := exec.LookPath(tool); err != nil {
t.Skipf("%s is not installed: the git source image cannot be built", tool)
}
}
const repository = "https://git.example.invalid/example/libraries"
work := t.TempDir()
library := filepath.Join(work, "libraries")
writeFile(t, filepath.Join(library, "lib", "pyproject.toml"), `[project]
name = "example-git-lib"
version = "0.3.0"
requires-python = ">=3.10"
[build-system]
requires = ["uv_build>=0.8,<0.13"]
build-backend = "uv_build"
`)
writeFile(t, filepath.Join(library, "lib", "src", "example_git_lib", "__init__.py"), "")
identity := []string{"GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com", "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com"}
run(t, library, nil, "git", "init", "-q")
run(t, library, nil, "git", "add", "-A")
run(t, library, identity, "git", "commit", "-qm", "library")
commit := run(t, library, nil, "git", "rev-parse", "HEAD")
served := filepath.Join(work, "served.git")
run(t, work, nil, "git", "clone", "-q", "--bare", library, served)
builder, _ := createdBuilder(t)
writeFile(t, filepath.Join(builder.SourceLocation, "pyproject.toml"), fmt.Sprintf(`[project]
name = "example-service"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = ["example-git-lib"]
[tool.uv]
package = false
[tool.uv.sources]
example-git-lib = { git = %q, subdirectory = "lib", rev = %q }
`, repository, commit))
rewrite := []string{"GIT_CONFIG_COUNT=1", "GIT_CONFIG_KEY_0=url.file://" + served + ".insteadOf", "GIT_CONFIG_VALUE_0=" + repository, "UV_CACHE_DIR=" + filepath.Join(work, "uv-cache")}
run(t, builder.SourceLocation, rewrite, "uv", "lock", "-q")
lock, err := os.ReadFile(filepath.Join(builder.SourceLocation, "uv.lock"))
require.NoError(t, err)
require.Contains(t, string(lock), fmt.Sprintf("%s?subdirectory=lib&rev=%s#%s", repository, commit, commit))
outputDir := t.TempDir()
plan := emitRecipe(t, builder, outputDir)
recipe := plan.GetRecipes()[0]
requireDeclares(t, []*builderv0.GitSourceDownload{{Repository: repository, Commit: commit, MirrorContext: "uvsources"}}, recipe.GetGitSourceDownloads())
// The mirror context as the caller fills it.
mirrors := filepath.Join(work, "mirrors")
mirror := filepath.Join(mirrors, services.GitSourceMirrorPath(repository))
run(t, work, nil, "git", "init", "-q", "--bare", mirror)
run(t, mirror, nil, "git", "fetch", "-q", served, "+"+commit+":"+services.GitSourceMirrorRef(commit))
tag := fmt.Sprintf("codefly-fastapi-uv-git-source-test:%d", time.Now().UnixMilli())
build := func(args ...string) ([]byte, error) {
args = append([]string{"build", "-f", filepath.Join(outputDir, recipe.GetDockerfile())}, args...)
return exec.CommandContext(t.Context(), "docker", append(args, builder.Location)...).CombinedOutput()
}
out, err := build("-t", tag, "--build-context", "uvsources="+mirrors)
require.NoError(t, err, "the mirrored git source did not build:\n%s", out)
t.Cleanup(func() { _ = exec.Command("docker", "rmi", "-f", tag).Run() })
installed, err := exec.CommandContext(t.Context(), "docker", "run", "--rm", tag,
"python", "-c", "import importlib.metadata as m; print(m.version('example-git-lib'))").CombinedOutput()
require.NoError(t, err, "%s", installed)
require.Contains(t, string(installed), "0.3.0", "the image does not carry the git source")
out, err = build()
require.Error(t, err, "without the mirror the unreachable URL must fail the build:\n%s", out)
require.Contains(t, string(out), "git.example.invalid")
}
// lockWith is a uv-form lock with one package per git source, all at commitA.
func lockWith(sources ...string) string {
lock := "version = 1\n"
for i, source := range sources {
lock += fmt.Sprintf("\n[[package]]\nname = \"lib%d\"\nversion = \"0.1.0\"\nsource = { git = \"%s?rev=main#%s\" }\n", i, source, commitA)
}
return lock
}
// TestLockedGitSourcesLeavesOutAPrefixOfAnUndeclaredSource: git's insteadOf
// rewrites every URL that begins with the declared one, so a declared URL
// that begins another locked source's undeclared URL would send that source to
// the wrong mirror. It is left out too, until no such capture remains.
func TestLockedGitSourcesLeavesOutAPrefixOfAnUndeclaredSource(t *testing.T) {
lock := filepath.Join(t.TempDir(), "uv.lock")
writeFile(t, lock, lockWith("https://h.example/acme/mono", "https://h.example/acme/mono@v1",
"https://h.example/acme/x", "https://h.example/acme/xy", "https://h.example/acme/xyz@v1", "https://h.example/acme/kept"))
declared, unmirrored, err := lockedGitSources(lock)
require.NoError(t, err)
requireDeclares(t, []*builderv0.GitSourceDownload{{Repository: "https://h.example/acme/kept", Commit: commitA, MirrorContext: "uvsources"}}, declared)
var names []string
for _, source := range unmirrored {
names = append(names, source.Package)
}
require.ElementsMatch(t, []string{"lib0", "lib1", "lib2", "lib3", "lib4"}, names)
require.Contains(t, unmirrored, unmirroredSource{Package: "lib0", Reason: prefixCaptureReason})
require.Contains(t, unmirrored, unmirroredSource{Package: "lib2", Reason: prefixCaptureReason})
}
// TestLockedGitSourcesKeepsALongerDeclaredSource: git applies the longest
// matching insteadOf, so two declared URLs, one beginning the other, each
// reach their own mirror.
func TestLockedGitSourcesKeepsALongerDeclaredSource(t *testing.T) {
lock := filepath.Join(t.TempDir(), "uv.lock")
writeFile(t, lock, lockWith("https://h.example/acme/mono", "https://h.example/acme/mono.git"))
declared, unmirrored, err := lockedGitSources(lock)
require.NoError(t, err)
require.Empty(t, unmirrored)
require.Len(t, declared, 2)
}
// runMapping renders the recipe for lock and runs its mirror mapping, then
// script, in the builder base image with env set, as a base image or an
// earlier ENV may set it.
func runMapping(t *testing.T, lock string, env []string, script string) string {
t.Helper()
builder, _ := createdBuilder(t)
writeFile(t, filepath.Join(builder.SourceLocation, "uv.lock"), lock)
outputDir := t.TempDir()
dockerfile := dockerfileOf(t, outputDir, emitRecipe(t, builder, outputDir))
run := "RUN --mount=type=bind,from=uvsources,target=/uvsources \\\n"
start := strings.Index(dockerfile, run)
end := strings.Index(dockerfile, "mkdir -p /opt/uv/python && uv sync")
require.True(t, start >= 0 && end > start, "the rendered sync step has no mirror mapping")
mapping := strings.ReplaceAll(dockerfile[start+len(run):end], "\\\n", "")
mirrors := t.TempDir()
writeFile(t, filepath.Join(mirrors, "present"), "")
args := []string{"run", "--rm", "--network=none", "-w", "/tmp", "-v", mirrors + ":/uvsources:ro"}
for _, variable := range env {
args = append(args, "-e", variable)
}
out, err := exec.CommandContext(t.Context(), "docker", append(args, runtimeImage.FullName(), "sh", "-c", mapping+script)...).CombinedOutput()
require.NoError(t, err, "%s", out)
return string(out)
}
// TestGitSourceMappingInTheBuilderImage runs the rendered mapping where it
// runs: appended after the git configuration the environment carries when its
// count is plain, leaving that configuration alone otherwise, and never
// rewriting a source left out of the declaration.
func TestGitSourceMappingInTheBuilderImage(t *testing.T) {
if _, err := exec.LookPath("docker"); err != nil {
t.Skip("docker is not installed: the mapping cannot be run")
}
const mono = "https://git.example.invalid/acme/mono"
mirror := "file:///uvsources/" + services.GitSourceMirrorPath(mono)
inherited := []string{"GIT_CONFIG_KEY_0=user.name", "GIT_CONFIG_VALUE_0=inherited"}
for _, count := range []string{"", "1", "01", "+1"} {
t.Run("count "+count, func(t *testing.T) {
env := append([]string{"GIT_CONFIG_COUNT=" + count}, inherited...)
if count == "" {
env = nil
}
out := runMapping(t, gitSourcesLock, env, "git ls-remote --get-url "+mono+" && { git config --get user.name || true; } && echo count=$GIT_CONFIG_COUNT")
want := mirror + "\ninherited\ncount=3\n"
if count == "" {
want = mirror + "\ncount=2\n"
}
require.Equal(t, want, out)
})
}
for _, count := range []string{" 1", "-0", "99999", "abc"} {
t.Run("count "+count, func(t *testing.T) {
env := append([]string{"GIT_CONFIG_COUNT=" + count}, inherited...)
out := runMapping(t, gitSourcesLock, env, `echo "count=[$GIT_CONFIG_COUNT] keys=$(env | grep -c '^GIT_CONFIG_KEY_') prompt=[$GIT_TERMINAL_PROMPT]"`)
require.Equal(t, "inherited GIT_CONFIG_COUNT is not a plain count; git sources are fetched without the mirror\ncount=["+count+"] keys=1 prompt=[]\n", out)
})
}
// The limitation, as it is today without a mirror: an image whose own git
// configuration rewrites a declared URL keeps its rewrite, which git
// prefers, so the source is fetched where the image says, not broken.
t.Run("inherited rewrite of a declared URL", func(t *testing.T) {
out := runMapping(t, gitSourcesLock, []string{"GIT_CONFIG_COUNT=1",
"GIT_CONFIG_KEY_0=url.https://proxy.invalid/.insteadOf", "GIT_CONFIG_VALUE_0=" + mono},
"git ls-remote --get-url "+mono+" && echo count=$GIT_CONFIG_COUNT")
require.Equal(t, "https://proxy.invalid/\ncount=3\n", out)
})
t.Run("an undeclared source the declared URL begins", func(t *testing.T) {
out := runMapping(t, lockWith(mono, mono+"@v1", "https://git.example.invalid/acme/tools"), nil,
"git ls-remote --get-url "+mono+"@v1 && git ls-remote --get-url "+mono)
require.Equal(t, mono+"@v1\n"+mono+"\n", out)
})
}