MCPcopy
hub / github.com/grafana/dskit / TestPage

Function TestPage

kv/memberlist/http_status_handler_test.go:23–73  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

21)
22
23func TestPage(t *testing.T) {
24 // Setup codec
25 c := dataCodec{}
26
27 // Create and configure KVInitService
28 var cfg KVConfig
29 flagext.DefaultValues(&cfg)
30 cfg.TCPTransport = TCPTransportConfig{
31 BindAddrs: getLocalhostAddrs(),
32 }
33 cfg.Codecs = []codec.Codec{c}
34
35 kvs := NewKVInitService(&cfg, log.NewNopLogger(), &staticDNSProviderMock{}, prometheus.NewPedanticRegistry())
36 require.NoError(t, services.StartAndAwaitRunning(context.Background(), kvs))
37 t.Cleanup(func() {
38 _ = services.StopAndAwaitTerminated(context.Background(), kvs)
39 })
40
41 // Get KV client and populate data
42 mkv, err := kvs.GetMemberlistKV()
43 require.NoError(t, err)
44
45 kv, err := NewClient(mkv, c)
46 require.NoError(t, err)
47
48 // Insert test data
49 err = kv.CAS(context.Background(), "test-key", func(in interface{}) (out interface{}, retry bool, err error) {
50 return &data{Members: map[string]member{"member1": {Timestamp: time.Now().Unix(), State: ACTIVE}}}, true, nil
51 })
52 require.NoError(t, err)
53
54 // Test HTTP handler
55 recorder := httptest.NewRecorder()
56 kvs.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/memberlist", nil))
57
58 assert.Equal(t, http.StatusOK, recorder.Code)
59 assert.Equal(t, "text/html", recorder.Header().Get("Content-Type"))
60
61 // Verify Size column header
62 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("(?m)%s", strings.Join([]string{
63 "<th>", "Size", "</th>",
64 }, `\s*`))), recorder.Body.String())
65
66 // Verify size value appears in a table cell (non-zero)
67 assert.Regexp(t, regexp.MustCompile(`<td>\d+</td>`), recorder.Body.String())
68
69 // Verify Total row exists
70 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("(?m)%s", strings.Join([]string{
71 "<strong>", "Total:", "</strong>",
72 }, `\s*`))), recorder.Body.String())
73}

Callers

nothing calls this directly

Calls 13

GetMemberlistKVMethod · 0.95
CASMethod · 0.95
ServeHTTPMethod · 0.95
DefaultValuesFunction · 0.92
StartAndAwaitRunningFunction · 0.92
StopAndAwaitTerminatedFunction · 0.92
getLocalhostAddrsFunction · 0.85
NewKVInitServiceFunction · 0.85
NewClientFunction · 0.70
GetMethod · 0.65
StringMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected