-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_test.go
More file actions
165 lines (154 loc) · 4.59 KB
/
render_test.go
File metadata and controls
165 lines (154 loc) · 4.59 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
package main
import (
"net/http/httptest"
"strings"
"testing"
"time"
)
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://psstd.example.com/?psstd_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 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)
}
}