e2e/maint_test.go

419f6dfdc5489a0c6374e36dd1ebbfca68040056
gitbay/e2e/maint_test.go history · blame · raw

68 lines · 2489 bytes

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