internal/control/download_test.go
71 lines · 2038 bytes
1package control
2
3import (
4 "bytes"
5 "strings"
6 "testing"
7 "time"
8
9 "gitbay.org/gitbay/internal/packlimit"
10 "gitbay.org/gitbay/internal/protocol"
11 "gitbay.org/gitbay/internal/store"
12)
13
14// repo download takes a pack slot: busy while the limiter is full, and
15// the slot is free again once git archive has exited.
16func TestRepoDownloadTakesAPackSlot(t *testing.T) {
17 st, repo, root, _ := prunedRepo(t)
18 alice, err := st.UserByUsername("alice")
19 if err != nil {
20 t.Fatal(err)
21 }
22 packs := packlimit.New(1, 0, 0, time.Second)
23 hold, err := packs.Acquire(nil, "ip:elsewhere")
24 if err != nil {
25 t.Fatal(err)
26 }
27 c, errOut := pruneCtx(st, root, alice)
28 c.Packs = packs
29 if code := Dispatch(c, []string{"repo", "download", repo.Path()}); code != protocol.ExitFailure ||
30 !strings.Contains(errOut.String(), "limit of concurrent clones") {
31 t.Fatalf("busy: exit %d: %q", code, errOut.String())
32 }
33 hold()
34
35 c, errOut = pruneCtx(st, root, alice)
36 c.Packs = packs
37 if code := Dispatch(c, []string{"repo", "download", repo.Path()}); code != protocol.ExitOK {
38 t.Fatalf("free: exit %d: %s", code, errOut.String())
39 }
40 if c.Stdout.(*bytes.Buffer).Len() == 0 {
41 t.Fatal("no archive written")
42 }
43 hold, err = packs.Acquire(nil, "ip:elsewhere")
44 if err != nil {
45 t.Fatalf("slot not released after the download: %v", err)
46 }
47 hold()
48}
49
50// A download the caller may not make is refused before the limiter.
51func TestRepoDownloadRefusalStaysOffLimiter(t *testing.T) {
52 st, repo, root, _ := prunedRepo(t)
53 if err := st.SetRepoVisibility(repo.ID, "private"); err != nil {
54 t.Fatal(err)
55 }
56 bobID, err := st.CreateUser("bob", false)
57 if err != nil {
58 t.Fatal(err)
59 }
60 packs := packlimit.New(1, 0, 0, time.Second)
61 hold, err := packs.Acquire(nil, "ip:elsewhere")
62 if err != nil {
63 t.Fatal(err)
64 }
65 defer hold()
66 c, errOut := pruneCtx(st, root, store.User{ID: bobID, Username: "bob"})
67 c.Packs = packs
68 if code := Dispatch(c, []string{"repo", "download", repo.Path()}); code != protocol.ExitNotFound {
69 t.Fatalf("exit %d: %q", code, errOut.String())
70 }
71}