e2e/maint_test.go
69 lines · 2508 bytes
1package e2e
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "regexp"
8 "strings"
9 "testing"
10)
11
12func TestAdminGCAndStats(t *testing.T) {
13 t.Parallel()
14 inst := startInstance(t)
15 aliceKey := inst.newKey(t, "alice")
16 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
17
18 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
19 t.Fatalf("repo create: %s", errOut)
20 }
21 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/other"); code != 0 {
22 t.Fatal("second repo create failed")
23 }
24 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", "'x'"); code != 0 {
25 t.Fatal("issue create failed")
26 }
27
28 // Push several commits so the repo has loose objects worth packing.
29 work := t.TempDir()
30 env := inst.gitEnv(aliceKey)
31 mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
32 dir := filepath.Join(work, "w")
33 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
34 for i := 0; i < 10; i++ {
35 os.WriteFile(filepath.Join(dir, "f.txt"), []byte(strings.Repeat(fmt.Sprint(i), 2000)+"\n"), 0o644)
36 mustGit(t, dir, env, "add", ".")
37 mustGit(t, dir, env, "commit", "-q", "-m", fmt.Sprintf("c%d", i))
38 }
39 mustGit(t, dir, env, "push", "-q", "origin", "main")
40
41 // Stats: counts and per-repo disk usage.
42 out := inst.admin(t, "admin", "stats")
43 if !regexp.MustCompile(`repos\s+2`).MatchString(out) || !strings.Contains(out, "1 (1 open)") ||
44 !strings.Contains(out, "alice/app") || !strings.Contains(out, "alice/other") {
45 t.Fatalf("stats output: %s", out)
46 }
47 if out = inst.admin(t, "admin", "stats", "--json"); !strings.Contains(out, `"repos":2`) ||
48 !strings.Contains(out, `"path":"alice/app"`) {
49 t.Fatalf("stats json: %s", out)
50 }
51
52 // GC one repo, then all; the repo must survive (clone still works).
53 out = inst.admin(t, "admin", "gc", "--repo", "alice/app")
54 if !strings.Contains(out, "alice/app") || !strings.Contains(out, "total") ||
55 strings.Contains(out, "alice/other") {
56 t.Fatalf("scoped gc: %s", out)
57 }
58 if out = inst.admin(t, "admin", "gc"); !strings.Contains(out, "alice/other") {
59 t.Fatalf("full gc: %s", out)
60 }
61 if out = inst.forgedAdminErr(t, "admin", "gc", "--repo", "alice/nope"); !strings.Contains(out, "no repository") {
62 t.Fatalf("bogus repo: %s", out)
63 }
64 work2 := t.TempDir()
65 mustGit(t, work2, env, "clone", "-q", inst.sshURL("alice/app"), "r")
66 if data, err := os.ReadFile(filepath.Join(work2, "r", "f.txt")); err != nil || len(data) == 0 {
67 t.Fatal("repo damaged by gc")
68 }
69}