|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/cgi" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/git-pkgs/proxy/internal/gitcache" |
| 15 | +) |
| 16 | + |
| 17 | +// gitMirrorUpstream serves team/repo, with one commit on main, from git |
| 18 | +// http-backend, and returns the server and the commit. |
| 19 | +func gitMirrorUpstream(t *testing.T) (*httptest.Server, string) { |
| 20 | + t.Helper() |
| 21 | + git := func(dir string, args ...string) string { |
| 22 | + t.Helper() |
| 23 | + cmd := exec.Command("git", args...) |
| 24 | + cmd.Dir = dir |
| 25 | + cmd.Env = []string{ |
| 26 | + "PATH=" + os.Getenv("PATH"), "HOME=" + t.TempDir(), "GIT_CONFIG_NOSYSTEM=1", |
| 27 | + "GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com", |
| 28 | + "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com", |
| 29 | + } |
| 30 | + out, err := cmd.CombinedOutput() |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 33 | + } |
| 34 | + return strings.TrimSpace(string(out)) |
| 35 | + } |
| 36 | + backend := "" |
| 37 | + for _, name := range []string{"git-http-backend", "git-http-backend.exe"} { |
| 38 | + if path := filepath.Join(git("", "--exec-path"), name); fileExistsForTest(path) { |
| 39 | + backend = path |
| 40 | + } |
| 41 | + } |
| 42 | + if backend == "" { |
| 43 | + t.Skip("git http-backend is not installed") |
| 44 | + } |
| 45 | + root := t.TempDir() |
| 46 | + bare := filepath.Join(root, "team", "repo.git") |
| 47 | + git("", "init", "--quiet", "--bare", "--initial-branch=main", bare) |
| 48 | + work := t.TempDir() |
| 49 | + git("", "clone", "--quiet", bare, work) |
| 50 | + git(work, "commit", "--quiet", "--allow-empty", "-m", "first") |
| 51 | + git(work, "push", "--quiet", "origin", "HEAD:main") |
| 52 | + head := git(work, "rev-parse", "HEAD") |
| 53 | + |
| 54 | + server := httptest.NewServer(&cgi.Handler{ |
| 55 | + Path: backend, |
| 56 | + Env: []string{"PATH=" + os.Getenv("PATH"), "GIT_PROJECT_ROOT=" + root, "GIT_HTTP_EXPORT_ALL=1"}, |
| 57 | + }) |
| 58 | + t.Cleanup(server.Close) |
| 59 | + return server, head |
| 60 | +} |
| 61 | + |
| 62 | +func fileExistsForTest(path string) bool { |
| 63 | + _, err := os.Stat(path) |
| 64 | + return err == nil |
| 65 | +} |
| 66 | + |
| 67 | +// writeGitMirrorConfig writes a config with upstream.git "up" at |
| 68 | +// upstreamURL and the git cache, storage and database under dir. |
| 69 | +func writeGitMirrorConfig(t *testing.T, dir, upstreamURL string) string { |
| 70 | + t.Helper() |
| 71 | + cfg := "storage:\n" + |
| 72 | + " url: file://" + filepath.Join(dir, "artifacts") + "\n" + |
| 73 | + "database:\n" + |
| 74 | + " path: " + filepath.Join(dir, "proxy.db") + "\n" + |
| 75 | + "upstream:\n" + |
| 76 | + " allow_loopback: true\n" + |
| 77 | + " git:\n" + |
| 78 | + " up: " + upstreamURL + "\n" + |
| 79 | + "git:\n" + |
| 80 | + " cache_dir: " + filepath.Join(dir, "git") + "\n" |
| 81 | + path := filepath.Join(dir, "config.yaml") |
| 82 | + if err := os.WriteFile(path, []byte(cfg), 0o600); err != nil { |
| 83 | + t.Fatalf("writing config: %v", err) |
| 84 | + } |
| 85 | + return path |
| 86 | +} |
| 87 | + |
| 88 | +func TestMirrorCommandCachesGitCommit(t *testing.T) { |
| 89 | + upstream, head := gitMirrorUpstream(t) |
| 90 | + dir := t.TempDir() |
| 91 | + configPath := writeGitMirrorConfig(t, dir, upstream.URL) |
| 92 | + purl := "pkg:git/" + strings.TrimPrefix(upstream.URL, "http://") + "/team/repo@" + head |
| 93 | + |
| 94 | + output := runMirrorProcess(t, 0, "-config", configPath, purl) |
| 95 | + if !strings.Contains(output, "1 downloaded") { |
| 96 | + t.Errorf("output:\n%s", output) |
| 97 | + } |
| 98 | + cached, _ := filepath.Glob(filepath.Join(dir, "git", "up-*", "team", "repo.git")) |
| 99 | + if len(cached) != 1 { |
| 100 | + t.Errorf("cached repositories %v, want team/repo.git", cached) |
| 101 | + } |
| 102 | + |
| 103 | + output = runMirrorProcess(t, 0, "-config", configPath, purl) |
| 104 | + if !strings.Contains(output, "1 skipped") { |
| 105 | + t.Errorf("second run output:\n%s", output) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// TestMirrorCommandGitCacheInUse checks pkg:git items fail, pointing at the |
| 110 | +// mirror API, while the running server holds git.cache_dir. |
| 111 | +func TestMirrorCommandGitCacheInUse(t *testing.T) { |
| 112 | + upstream, head := gitMirrorUpstream(t) |
| 113 | + dir := t.TempDir() |
| 114 | + configPath := writeGitMirrorConfig(t, dir, upstream.URL) |
| 115 | + cache, err := gitcache.New(gitcache.Options{ |
| 116 | + CacheDir: filepath.Join(dir, "git"), |
| 117 | + RefreshWait: time.Second, |
| 118 | + Client: &http.Client{}, |
| 119 | + }) |
| 120 | + if err != nil { |
| 121 | + t.Fatal(err) |
| 122 | + } |
| 123 | + defer func() { _ = cache.Close() }() |
| 124 | + purl := "pkg:git/" + strings.TrimPrefix(upstream.URL, "http://") + "/team/repo@" + head |
| 125 | + |
| 126 | + output := runMirrorProcess(t, 1, "-config", configPath, purl) |
| 127 | + for _, want := range []string{"in use by another process", "mirror API", "1 failed"} { |
| 128 | + if !strings.Contains(output, want) { |
| 129 | + t.Errorf("output missing %q:\n%s", want, output) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments