1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
package daemon
import (
"bytes"
"context"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
)
func TestHealth(t *testing.T) {
srv := httptest.NewServer(Handler(t.TempDir()))
defer srv.Close()
res, err := http.Get(srv.URL + "/health")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
t.Fatalf("status %d", res.StatusCode)
}
b, _ := io.ReadAll(res.Body)
if string(b) != "ok\n" {
t.Fatalf("body %q", b)
}
}
func TestHealthMethodNotAllowed(t *testing.T) {
srv := httptest.NewServer(Handler(t.TempDir()))
defer srv.Close()
req, _ := http.NewRequest(http.MethodPost, srv.URL+"/health", nil)
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != http.StatusMethodNotAllowed {
t.Fatalf("status %d", res.StatusCode)
}
}
func TestReport(t *testing.T) {
fixtures := filepath.Join("..", "..", "fixtures")
h := Handler(fixtures)
srv := httptest.NewServer(h)
defer srv.Close()
res, err := http.Get(srv.URL + "/report?category=Host&metric=Boots&limit=3&output-format=Plaintext")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
t.Fatalf("status %d", res.StatusCode)
}
if ct := res.Header.Get("Content-Type"); ct != "text/plain; charset=utf-8" {
t.Fatalf("Content-Type %q", ct)
}
b, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(b), "Host") {
t.Fatalf("expected report body, got %q", b)
}
}
func TestReportQueryAliases(t *testing.T) {
fixtures := filepath.Join("..", "..", "fixtures")
srv := httptest.NewServer(Handler(fixtures))
defer srv.Close()
res, err := http.Get(srv.URL + "/report?Category=Host&Metric=Uptime&limit=2&OutputFormat=Markdown")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
t.Fatalf("status %d", res.StatusCode)
}
if ct := res.Header.Get("Content-Type"); ct != "text/markdown; charset=utf-8" {
t.Fatalf("Content-Type %q", ct)
}
b, _ := io.ReadAll(res.Body)
body := string(b)
if !strings.Contains(body, "# Top") || !strings.Contains(body, "```") {
t.Fatalf("expected markdown heading and fence, got %q", b)
}
}
func TestReportGemtextContentType(t *testing.T) {
fixtures := filepath.Join("..", "..", "fixtures")
srv := httptest.NewServer(Handler(fixtures))
defer srv.Close()
res, err := http.Get(srv.URL + "/report?output-format=Gemtext&limit=2")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
t.Fatalf("status %d", res.StatusCode)
}
if ct := res.Header.Get("Content-Type"); ct != "text/gemini; charset=utf-8" {
t.Fatalf("Content-Type %q", ct)
}
}
func TestReportBadQuery(t *testing.T) {
srv := httptest.NewServer(Handler(t.TempDir()))
defer srv.Close()
res, err := http.Get(srv.URL + "/report?category=Nope")
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != http.StatusBadRequest {
t.Fatalf("status %d", res.StatusCode)
}
}
func TestRunEmptyStatsDir(t *testing.T) {
err := Run(context.Background(), Config{StatsDir: "", Addr: ":0"})
if err == nil {
t.Fatal("expected error")
}
}
func TestRunEmptyAddr(t *testing.T) {
err := Run(context.Background(), Config{StatsDir: t.TempDir(), Addr: ""})
if err == nil {
t.Fatal("expected error")
}
}
func TestRunWritesDaemonListenToLogOutput(t *testing.T) {
var buf bytes.Buffer
ctx, cancel := context.WithCancel(context.Background())
cfg := Config{StatsDir: t.TempDir(), Addr: "127.0.0.1:0", LogOutput: &buf}
done := make(chan struct{})
go func() {
_ = Run(ctx, cfg)
close(done)
}()
deadline := time.After(2 * time.Second)
for !strings.Contains(buf.String(), "daemon_listen") {
select {
case <-deadline:
t.Fatalf("timeout waiting for daemon_listen, got %q", buf.String())
case <-time.After(5 * time.Millisecond):
}
}
cancel()
<-done
}
func TestAccessLogLineToWriter(t *testing.T) {
var buf bytes.Buffer
h := slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelInfo})
log := slog.New(h)
srv := httptest.NewServer(withAccessLog(log, routes(t.TempDir())))
defer srv.Close()
res, err := http.Get(srv.URL + "/health")
if err != nil {
t.Fatal(err)
}
res.Body.Close()
body := buf.String()
if !strings.Contains(body, "http_request") || !strings.Contains(body, "method=GET") {
t.Fatalf("expected http_request line with method=GET, got %q", body)
}
if !strings.Contains(body, "path=/health") || !strings.Contains(body, "status=200") {
t.Fatalf("expected path and status in log, got %q", body)
}
}
|