carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package console
2
3import (
4 "bytes"
5 "context"
6 "log"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11)
12
13// captureLog redirects the global logger for one test. Callers must not be
14// parallel.
15func captureLog(t *testing.T) *bytes.Buffer {
16 t.Helper()
17 var buf bytes.Buffer
18 prev := log.Writer()
19 log.SetOutput(&buf)
20 t.Cleanup(func() { log.SetOutput(prev) })
21 return &buf
22}
23
24func TestClaimCheckFailureIsLogged(t *testing.T) {
25 cases := []struct {
26 op string
27 breakStore func(t *testing.T, fx *consoleFixture)
28 }{
29 {"HasAppIn", func(t *testing.T, fx *consoleFixture) {
30 if _, err := fx.st.db.Exec(`DROP TABLE apps`); err != nil {
31 t.Fatal(err)
32 }
33 }},
34 {"HasOwners", func(t *testing.T, fx *consoleFixture) {
35 if _, err := fx.st.db.Exec(`DROP TABLE owners`); err != nil {
36 t.Fatal(err)
37 }
38 }},
39 {"apexHolder", func(t *testing.T, fx *consoleFixture) {
40 // A registry path whose parent is a regular file cannot be opened.
41 file := filepath.Join(t.TempDir(), "file")
42 if err := os.WriteFile(file, nil, 0o600); err != nil {
43 t.Fatal(err)
44 }
45 fx.srv.cfg.AppsDomain = "apps.test"
46 fx.srv.cfg.Registry = filepath.Join(file, "registry.db")
47 }},
48 }
49 for _, tc := range cases {
50 t.Run(tc.op, func(t *testing.T) {
51 fx := newConsoleFixture(t)
52 acct := opsAccount(t, fx)
53 tc.breakStore(t, fx)
54 buf := captureLog(t)
55 outcome, placed, held := fx.srv.claimApp(context.Background(), acct, "logged-site", "paul@keymail.dev", "", true, regionReq{})
56 if outcome != claimCheckFailed || placed != "" || held != nil {
57 t.Fatalf("claimApp = %v, %q, %v; want claimCheckFailed", outcome, placed, held)
58 }
59 out := buf.String()
60 for _, want := range []string{
61 `account="` + acct.Sqid + `"`,
62 `app="logged-site"`,
63 `op="` + tc.op + `"`,
64 "err=",
65 } {
66 if !strings.Contains(out, want) {
67 t.Errorf("log %q lacks %s", out, want)
68 }
69 }
70 if n := strings.Count(out, "claim check failed"); n != 1 {
71 t.Errorf("claim check failed logged %d times: %q", n, out)
72 }
73 })
74 }
75}
76