Repository navigation
Expand file tree
/
Copy pathrender_test.go
More file actions
451 lines (427 loc) · 14.5 KB
/
Copy pathrender_test.go
File metadata and controls
451 lines (427 loc) · 14.5 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package main
import (
"bytes"
"encoding/json"
"fmt"
"html"
"io/fs"
"math"
"net/http/httptest"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
)
func TestDashboardServesEmbeddedVersytlThroughProxyPrefixes(t *testing.T) {
db := openTestDB(t)
handler := makeHandler(db, "node-a")
for _, asset := range []struct{ path, contentType string }{
{"/assets/ui/dashboard.js", "javascript"},
{"/nested/assets/ui/dashboard.js?pulsed_node=node-a", "javascript"},
{"/nested/assets/branding/pulsed-logo.png?pulsed_node=node-a", "image/png"},
} {
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, httptest.NewRequest("GET", asset.path, nil))
if rr.Code != 200 || !strings.Contains(rr.Header().Get("Content-Type"), asset.contentType) {
t.Fatalf("asset %s: status %d, type %s", asset.path, rr.Code, rr.Header().Get("Content-Type"))
}
}
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, httptest.NewRequest("GET", "/assets/ui/not-found.js", nil))
if rr.Code != 404 {
t.Fatalf("unknown asset status %d", rr.Code)
}
}
func TestDashboardSnapshotsRetainActualCPUObservations(t *testing.T) {
db := openTestDB(t)
handler := makeHandler(db, "serving-peer")
name := "edge</script><script>alert(1)</script>"
now := time.Now()
for i := 0; i < 2; i++ {
if err := dbSet(db, NodeStats{Name: name, CPU: []float64{float64(20 + i*10)}, UpdatedAt: now.Add(time.Duration(i-2) * time.Second).UnixNano()}); err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, httptest.NewRequest("GET", "/", nil))
if rr.Code != 200 || strings.Contains(rr.Body.String(), name) {
t.Fatal("snapshot contains unescaped node markup or failed to render")
}
match := regexp.MustCompile(`id="snapshot-data" hidden data-snapshot="([^"]+)"`).FindStringSubmatch(rr.Body.String())
if len(match) != 2 {
t.Fatal("missing snapshot payload")
}
var snapshot dashboardSnapshot
if err := json.Unmarshal([]byte(html.UnescapeString(match[1])), &snapshot); err != nil {
t.Fatal(err)
}
if snapshot.ServingNode != "serving-peer" || len(snapshot.History[name]) != i+1 || snapshot.History[name][i].Average != float64(20+i*10) {
t.Fatalf("incorrect snapshot: %+v", snapshot)
}
}
}
func TestDashboardCoreBandsPreserveEveryCoreAndHotspots(t *testing.T) {
for _, count := range []int{0, 1, 8, 32, 33, 64, 192, 256, 1024, 2048} {
t.Run(fmt.Sprint(count), func(t *testing.T) {
cpus := make([]float64, count)
if count > 0 {
cpus[count-1] = 99
}
cell := dashboardCell(NodeStats{CPU: cpus, UpdatedAt: time.Now().UnixNano()})
if len(cell.Cores) != count || len(cell.Bands) != min(count, maxCoreBands) {
t.Fatalf("%d cores: got %d readings, %d bands", count, len(cell.Cores), len(cell.Bands))
}
next := 0
for _, band := range cell.Bands {
if band.First != next || band.Last < band.First {
t.Fatalf("gap or overlap: %+v after %d", band, next)
}
next = band.Last + 1
}
if next != count {
t.Fatalf("bands cover %d of %d cores", next, count)
}
if count > 0 && cell.Bands[len(cell.Bands)-1].Peak != 99 {
t.Fatal("hotspot lost in overview")
}
})
}
}
func TestDashboardMissingOfflineAndOutOfRangeMetrics(t *testing.T) {
cell := dashboardCell(NodeStats{UpdatedAt: time.Now().UnixNano(), CPU: []float64{-1, 150, math.NaN(), math.Inf(1)}})
for _, core := range cell.Cores {
if core.Percent < 0 || core.Percent > 100 || math.IsNaN(core.Percent) || math.IsInf(core.Percent, 0) {
t.Fatalf("invalid meter: %+v", core)
}
}
if cell.MemPct != 0 {
t.Fatal("missing memory is not zero-safe")
}
cell = dashboardCell(NodeStats{CPU: []float64{99}})
if len(cell.Cores) != 0 || len(cell.Bands) != 0 || cell.AgeLabel != "No heartbeat available" {
t.Fatalf("offline node exposes old core readings: %+v", cell)
}
}
func TestDashboardResponsiveFixtures(t *testing.T) {
now := time.Now()
var nodes []cellData
var sourceNodes []NodeStats
for i, count := range []int{4, 192, 8, 64, 256, 1024, 0, 16, 32, 2, 48, 128} {
stats := NodeStats{Name: fmt.Sprintf("rack-%02d", i+1), CPU: make([]float64, count), MemUsed: uint64(i+1) * 8 << 30, MemTotal: 128 << 30, UpdatedAt: now.UnixNano(), Version: appVersion, Load: [3]float64{float64(i) * 1.4, 0.4, 0.6}}
for j := range stats.CPU {
stats.CPU[j] = float64((j*13 + i*7) % 101)
}
if i == 0 {
stats.Name = "edge-01"
}
if i == 1 {
stats.Name = "compute-192"
}
if i == 5 {
stats.Name = "large-host-1024-with-a-very-long-name.cluster.internal"
}
if i == 6 {
stats.MemTotal = 0
}
if i == 8 {
stats.UpdatedAt = now.Add(-10 * time.Second).UnixNano()
}
if i == 9 {
stats.UpdatedAt = 0
}
nodes = append(nodes, dashboardCell(stats))
sourceNodes = append(sourceNodes, stats)
}
var buf bytes.Buffer
history := make(map[string][]cpuObservation)
for i, node := range nodes {
if i == 6 {
continue // A node without CPU samples must have an honest empty state.
}
for j := 0; j < 120; j++ {
if j > 40 && j < 55 {
continue // A missed-heartbeat interval for the graph's gap handling.
}
history[node.Name] = append(history[node.Name], cpuObservation{
At: now.Add(time.Duration(j-120) * 2 * time.Second).UnixMilli(),
Average: 20 + float64((j*3+i*7)%40), Peak: 55 + float64((j+i*13)%40), TTLSeconds: 15,
})
}
}
err := pageTmpl.Execute(&buf, pageData{Nodes: nodes, Theme: "dark", Palette: "monochrome", RefreshMs: 3000, RefreshLabel: "3.0s", RefreshURL: "/dashboard.html?theme=dark&palette=monochrome", BestHint: "Preview · synthetic mixed-hardware cluster", Summary: clusterSummary{Fresh: 10, Stale: 1, Offline: 1, HasHot: true, Hottest: "compute-192", HotCPU: 49, HotLoad: 1.4}, Snapshot: dashboardSnapshot{GeneratedAt: now.UnixMilli(), ServingNode: "preview-peer", Nodes: nodes, History: history}})
if err != nil {
t.Fatal(err)
}
body := buf.String()
if strings.Contains(body, "ZgotmplZ") {
t.Fatal("template produced unsafe value placeholder")
}
coreStrip := regexp.MustCompile(`(?s)id="cluster-cores"[^>]*>(.*?)</div>`).FindStringSubmatch(body)
expectedCores := 0
for _, node := range nodes {
expectedCores += len(node.Cores)
}
if len(coreStrip) != 2 || strings.Count(coreStrip[1], `class="core-bar"`) != expectedCores {
t.Fatal("cluster overview lost available core readings")
}
if !strings.Contains(coreStrip[1], `data-node="compute-192" data-core="191"`) || strings.Contains(coreStrip[1], `data-node="rack-10"`) {
t.Fatal("cluster bars lost node/core identity or exposed offline readings")
}
if !strings.Contains(body, "Inspect 1024 logical CPUs") || !strings.Contains(body, "CPU 1023 usage") {
t.Fatal("high-core detail truncated")
}
if strings.Contains(body, "<pre>") || strings.Contains(body, "location.replace('?'") {
t.Fatal("legacy layout still active")
}
if dir := os.Getenv("PULSED_PREVIEW_DIR"); dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "dashboard.html"), buf.Bytes(), 0644); err != nil {
t.Fatal(err)
}
api := makeAPISnapshot(sourceNodes, &cpuHistory{nodes: history}, "preview-peer", httptest.NewRequest("GET", "/api/v1/snapshot", nil), now)
api.RefreshURL = "" // Preview fixtures never redirect to a real peer.
for i := range api.Nodes {
api.Nodes[i].CPU.Cores = nodes[i].Cores
for j := range api.History[nodes[i].Name] {
cores := make([]uint16, nodes[i].CoreCount)
for core := range cores {
// Synthetic heterogeneous activity, including genuinely idle CPUs.
if core%5 != 0 {
cores[core] = uint16((j*17 + core*37 + i*71) % 1001)
}
}
api.History[nodes[i].Name][j].CoreTenths = cores
}
}
encoded, err := json.Marshal(api)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "snapshot.json"), encoded, 0644); err != nil {
t.Fatal(err)
}
if err := fs.WalkDir(templateFS, "templates/assets", func(path string, entry fs.DirEntry, err error) error {
if err != nil || entry.IsDir() {
return err
}
target := filepath.Join(dir, strings.TrimPrefix(path, "templates/"))
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
return err
}
content, err := templateFS.ReadFile(path)
if err != nil {
return err
}
return os.WriteFile(target, content, 0644)
}); err != nil {
t.Fatal(err)
}
}
}
func TestDashboardRefreshKeepsProxyRoute(t *testing.T) {
db := openTestDB(t)
rr := httptest.NewRecorder()
makeHandler(db, "node-a").ServeHTTP(rr, httptest.NewRequest("GET", "/dashboard?pulsed_node=node-a&theme=light", nil))
if rr.Code != 200 {
t.Fatalf("status %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), `/dashboard?pulsed_node=node-a`) {
t.Fatal("refresh lost the proxy route")
}
if !strings.Contains(rr.Body.String(), `./assets/ui/dashboard.js?pulsed_node=node-a`) {
t.Fatal("UI asset lost the proxy node query")
}
}
func TestDashboardDoesNotLinkNodesWithoutWebURL(t *testing.T) {
db := openTestDB(t)
now := time.Now().UnixNano()
nodes := []NodeStats{
{
Name: "sync-only",
Version: appVersion,
UpdatedAt: now,
CPU: []float64{10},
MemTotal: 100,
MemUsed: 25,
},
{
Name: "web-node",
Version: appVersion,
WebURL: "https://pulsed.example.com/?pulsed_node=web-node",
UpdatedAt: now,
CPU: []float64{20},
MemTotal: 100,
MemUsed: 30,
},
}
for _, node := range nodes {
if err := dbSet(db, node); err != nil {
t.Fatalf("set %s: %v", node.Name, err)
}
}
req := httptest.NewRequest("GET", "/?theme=dark&palette=monochrome", nil)
rr := httptest.NewRecorder()
makeHandler(db, "web-node").ServeHTTP(rr, req)
if rr.Code != 200 {
t.Fatalf("status = %d, want 200", rr.Code)
}
body := rr.Body.String()
if !strings.Contains(body, `<span class="node-name">sync-only</span>`) {
t.Fatalf("sync-only node was not rendered as non-link text:\n%s", body)
}
if strings.Contains(body, `<a href="/">sync-only</a>`) || strings.Contains(body, `>sync-only</a>`) {
t.Fatalf("sync-only node rendered as a link:\n%s", body)
}
if !strings.Contains(body, `>web-node</a>`) {
t.Fatalf("web node was not rendered as a link:\n%s", body)
}
}
func TestNodeHealthDistinguishesFreshStaleOffline(t *testing.T) {
now := time.Now()
cases := []struct {
name string
node NodeStats
want healthState
}{
{
name: "fresh",
node: NodeStats{UpdatedAt: now.Add(-2 * time.Second).UnixNano(), TTLSeconds: 10},
want: healthFresh,
},
{
name: "stale",
node: NodeStats{UpdatedAt: now.Add(-7 * time.Second).UnixNano(), TTLSeconds: 10},
want: healthStale,
},
{
name: "offline",
node: NodeStats{UpdatedAt: now.Add(-11 * time.Second).UnixNano(), TTLSeconds: 10},
want: healthOffline,
},
{
name: "zero timestamp offline",
node: NodeStats{},
want: healthOffline,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := nodeHealth(tc.node).State; got != tc.want {
t.Fatalf("state = %s, want %s", got, tc.want)
}
})
}
}
func TestSummarizeClusterCountsStatesAndHottestOnlineNode(t *testing.T) {
now := time.Now()
summary := summarizeCluster([]NodeStats{
{Name: "fresh-hot", UpdatedAt: now.UnixNano(), TTLSeconds: 10, CPU: []float64{20}, Load: [3]float64{0.5}},
{Name: "stale-hot", UpdatedAt: now.Add(-7 * time.Second).UnixNano(), TTLSeconds: 10, CPU: []float64{70}, Load: [3]float64{1.2}},
{Name: "offline", UpdatedAt: 0, TTLSeconds: 10, CPU: []float64{99}, Load: [3]float64{9}},
})
if summary.Fresh != 1 || summary.Stale != 1 || summary.Offline != 1 {
t.Fatalf("counts = fresh %d stale %d offline %d, want 1/1/1", summary.Fresh, summary.Stale, summary.Offline)
}
if !summary.HasHot || summary.Hottest != "fresh-hot" {
t.Fatalf("hottest = %q has=%t, want fresh-hot", summary.Hottest, summary.HasHot)
}
}
func TestDashboardRendersClusterSummaryAndStateData(t *testing.T) {
db := openTestDB(t)
now := time.Now()
nodes := []NodeStats{
{Name: "fresh", Version: appVersion, UpdatedAt: now.UnixNano(), TTLSeconds: 10, CPU: []float64{20}, MemTotal: 100, MemUsed: 30},
{Name: "stale", Version: appVersion, UpdatedAt: now.Add(-7 * time.Second).UnixNano(), TTLSeconds: 10, CPU: []float64{80}, MemTotal: 100, MemUsed: 40},
{Name: "offline", Version: appVersion, UpdatedAt: 0, TTLSeconds: 10, CPU: []float64{90}, MemTotal: 100, MemUsed: 50},
}
for _, node := range nodes {
if err := dbSet(db, node); err != nil {
t.Fatalf("set %s: %v", node.Name, err)
}
}
req := httptest.NewRequest("GET", "/?theme=dark&palette=monochrome", nil)
rr := httptest.NewRecorder()
makeHandler(db, "fresh").ServeHTTP(rr, req)
body := rr.Body.String()
for _, want := range []string{
`online 1`,
`stale 1`,
`offline 1`,
`data-state="stale"`,
`id="sort-select"`,
`id="hide-offline"`,
`value="cpuAvg"`,
`value="cpuMax"`,
`value="memPct"`,
`value="memUsed"`,
`value="memTotal"`,
`value="load1"`,
`value="load5"`,
`value="load15"`,
`data-cpu-avg="20.000"`,
`data-cpu-max="20.000"`,
`data-mem-pct="30.000"`,
`data-mem-used="30"`,
`data-mem-total="100"`,
`data-load1="0.000"`,
`data-load5="0.000"`,
`data-load15="0.000"`,
} {
if !strings.Contains(body, want) {
t.Fatalf("dashboard missing %q:\n%s", want, body)
}
}
}
func TestRenderANSIMarksLiveVersionMismatch(t *testing.T) {
oldVersion := appVersion
appVersion = "v2.0.0"
t.Cleanup(func() { appVersion = oldVersion })
out := renderANSI(NodeStats{
Name: "old-live-node",
Version: "v1.0.0",
UpdatedAt: time.Now().UnixNano(),
TTLSeconds: 10,
CPU: []float64{5},
MemTotal: 100,
MemUsed: 20,
})
for _, want := range []string{"old-live-node", "version v1.0.0 (local v2.0.0)"} {
if !strings.Contains(out, want) {
t.Fatalf("render missing %q:\n%s", want, out)
}
}
}
func TestDashboardRendersLiveVersionMismatch(t *testing.T) {
oldVersion := appVersion
appVersion = "v2.0.0"
t.Cleanup(func() { appVersion = oldVersion })
db := openTestDB(t)
if err := dbSet(db, NodeStats{
Name: "old-live-node",
Version: "v1.0.0",
UpdatedAt: time.Now().UnixNano(),
TTLSeconds: 10,
CPU: []float64{5},
MemTotal: 100,
MemUsed: 20,
}); err != nil {
t.Fatalf("set old-live-node: %v", err)
}
req := httptest.NewRequest("GET", "/?theme=dark&palette=monochrome", nil)
rr := httptest.NewRecorder()
makeHandler(db, "old-live-node").ServeHTTP(rr, req)
body := rr.Body.String()
if !strings.Contains(body, "version v1.0.0 (local v2.0.0)") {
t.Fatalf("dashboard missing version mismatch:\n%s", body)
}
}
func TestSetNodeStatsTTLUsesConfiguredTTL(t *testing.T) {
stats := NodeStats{Name: "node-a"}
setNodeStatsTTL(&stats, 42*time.Second)
if stats.TTLSeconds != 42 {
t.Fatalf("ttl seconds = %d, want 42", stats.TTLSeconds)
}
}