e2e/release_test.go

7a667c7b40578f9e76675c36ac11f236a882d711
gitbay/e2e/release_test.go history · blame · raw

143 lines · 6563 bytes

  1package e2e
  2
  3import (
  4	"fmt"
  5	"net/http"
  6	"os"
  7	"path/filepath"
  8	"strings"
  9	"testing"
 10)
 11
 12func TestReleases(t *testing.T) {
 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	work := t.TempDir()
 21	env := inst.gitEnv(aliceKey)
 22	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
 23	dir := filepath.Join(work, "w")
 24	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
 25	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
 26	mustGit(t, dir, env, "add", ".")
 27	mustGit(t, dir, env, "commit", "-q", "-m", "base")
 28	mustGit(t, dir, env, "tag", "-a", "v1.0", "-m", "first")
 29	mustGit(t, dir, env, "push", "-q", "origin", "main", "v1.0")
 30
 31	// Create: tag must exist; duplicates refused.
 32	if _, errOut, code := inst.ssh(t, aliceKey, "", "release", "create", "alice/app", "v9.9"); code != 3 || !strings.Contains(errOut, "push the tag first") {
 33		t.Fatalf("missing tag: exit %d, %s", code, errOut)
 34	}
 35	if _, errOut, code := inst.ssh(t, aliceKey, "", "release", "create", "alice/app", "v1.0",
 36		"--title", "'First light'", "--notes", "'the **first** release'"); code != 0 {
 37		t.Fatalf("release create: %s", errOut)
 38	}
 39	if _, _, code := inst.ssh(t, aliceKey, "", "release", "create", "alice/app", "v1.0"); code != 2 {
 40		t.Fatal("duplicate release accepted")
 41	}
 42
 43	// Edit: notes replace from stdin, absent flags keep their field.
 44	if _, _, code := inst.ssh(t, aliceKey, "", "release", "edit", "alice/app", "v1.0"); code != 2 {
 45		t.Fatal("edit with nothing to change accepted")
 46	}
 47	if _, errOut, code := inst.ssh(t, aliceKey, "the **rebuilt** notes\n", "release", "edit", "alice/app", "v1.0", "--file", "-"); code != 0 {
 48		t.Fatalf("release edit: %s", errOut)
 49	}
 50	out, _, _ := inst.ssh(t, aliceKey, "", "release", "show", "alice/app", "v1.0")
 51	if !strings.Contains(out, "the **rebuilt** notes") || !strings.Contains(out, "First light") {
 52		t.Fatalf("edit lost a field:\n%s", out)
 53	}
 54	if _, _, code := inst.ssh(t, aliceKey, "", "release", "edit", "alice/app", "v1.0", "--title", "'Second light'"); code != 0 {
 55		t.Fatal("title edit failed")
 56	}
 57	out, _, _ = inst.ssh(t, aliceKey, "", "release", "show", "alice/app", "v1.0")
 58	if !strings.Contains(out, "Second light") || !strings.Contains(out, "the **rebuilt** notes") {
 59		t.Fatalf("title edit lost notes:\n%s", out)
 60	}
 61	if _, _, code := inst.ssh(t, aliceKey, "", "release", "edit", "alice/app", "v9.9", "--title", "x"); code != 3 {
 62		t.Fatal("edit of missing release accepted")
 63	}
 64
 65	// Assets: upload from stdin, validation, dedup, round-trip.
 66	payload := "BINARY\x01\x02payload for the release asset\n"
 67	if _, errOut, code := inst.ssh(t, aliceKey, payload, "release", "asset", "add", "alice/app", "v1.0", "tool-linux-amd64"); code != 0 {
 68		t.Fatalf("asset add: %s", errOut)
 69	}
 70	if _, _, code := inst.ssh(t, aliceKey, payload, "release", "asset", "add", "alice/app", "v1.0", "tool-linux-amd64"); code != 2 {
 71		t.Fatal("duplicate asset accepted")
 72	}
 73	if _, errOut, code := inst.ssh(t, aliceKey, "", "release", "asset", "add", "alice/app", "v1.0", "empty-file"); code != 2 || !strings.Contains(errOut, "empty asset") {
 74		t.Fatalf("empty asset: exit %d, %s", code, errOut)
 75	}
 76	if _, _, code := inst.ssh(t, aliceKey, "x", "release", "asset", "add", "alice/app", "v1.0", "../evil"); code != 2 {
 77		t.Fatal("bad asset name accepted")
 78	}
 79	got, _, code := inst.ssh(t, aliceKey, "", "release", "asset", "get", "alice/app", "v1.0", "tool-linux-amd64")
 80	if code != 0 || got != payload {
 81		t.Fatalf("asset round-trip: exit %d, %q", code, got)
 82	}
 83	out, _, _ = inst.ssh(t, aliceKey, "", "release", "show", "alice/app", "v1.0", "--json")
 84	if !strings.Contains(out, `"name":"tool-linux-amd64"`) ||
 85		!strings.Contains(out, fmt.Sprintf(`"size":%d`, len(payload))) ||
 86		!strings.Contains(out, `"sha256":"`) || !strings.Contains(out, "Second light") {
 87		t.Fatalf("release show: %s", out)
 88	}
 89
 90	// Web: page renders notes and assets; download streams exact bytes.
 91	status, body := inst.get(t, "/alice/app/releases")
 92	if status != 200 || !strings.Contains(body, "Second light") ||
 93		!strings.Contains(body, "<strong>rebuilt</strong>") ||
 94		!strings.Contains(body, "tool-linux-amd64") {
 95		t.Fatalf("releases page: %d\n%s", status, body)
 96	}
 97	resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/app/releases/download/v1.0/tool-linux-amd64", inst.httpPort))
 98	if err != nil {
 99		t.Fatal(err)
100	}
101	dl := make([]byte, len(payload)+10)
102	n, _ := resp.Body.Read(dl)
103	resp.Body.Close()
104	if resp.StatusCode != 200 || string(dl[:n]) != payload ||
105		!strings.Contains(resp.Header.Get("Content-Disposition"), "tool-linux-amd64") {
106		t.Fatalf("web download: %d, %q", resp.StatusCode, dl[:n])
107	}
108	if resp, _ := http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/app/releases/download/v1.0/nope", inst.httpPort)); resp.StatusCode != 404 {
109		t.Fatalf("missing asset download: %d", resp.StatusCode)
110	}
111
112	// Remove, then delete: DB rows and disk both go.
113	if _, _, code := inst.ssh(t, aliceKey, "", "release", "asset", "remove", "alice/app", "v1.0", "tool-linux-amd64"); code != 0 {
114		t.Fatal("asset remove failed")
115	}
116	if _, _, code := inst.ssh(t, aliceKey, "", "release", "asset", "get", "alice/app", "v1.0", "tool-linux-amd64"); code != 3 {
117		t.Fatal("removed asset still served")
118	}
119	if _, _, code := inst.ssh(t, aliceKey, payload, "release", "asset", "add", "alice/app", "v1.0", "again"); code != 0 {
120		t.Fatal("re-add failed")
121	}
122	assetRoot := filepath.Join(inst.root, "repos", "alice", "app.git", "gitbay-releases")
123	if fis, err := os.ReadDir(assetRoot); err != nil || len(fis) == 0 {
124		t.Fatal("asset dir missing on disk")
125	}
126	if _, _, code := inst.ssh(t, aliceKey, "", "release", "delete", "alice/app", "v1.0", "--yes"); code != 0 {
127		t.Fatal("release delete failed")
128	}
129	if fis, _ := os.ReadDir(assetRoot); len(fis) != 0 {
130		t.Fatal("asset files survived release delete")
131	}
132	if out, _, _ := inst.ssh(t, aliceKey, "", "release", "list", "alice/app", "--json"); strings.Contains(out, "v1.0") {
133		t.Fatalf("deleted release listed: %s", out)
134	}
135
136	// Archived repos refuse release writes.
137	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "archive", "alice/app"); code != 0 {
138		t.Fatal("archive failed")
139	}
140	if _, errOut, code := inst.ssh(t, aliceKey, "", "release", "create", "alice/app", "v1.0"); code != 4 || !strings.Contains(errOut, "archived") {
141		t.Fatalf("archived release create: exit %d, %s", code, errOut)
142	}
143}