e2e/maint_test.go

70dc0648f931f6f7112c6b71b0be485eeae4077f
gitbay/e2e/maint_test.go history · blame · raw

67 lines · 2475 bytes

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