|
1 | 1 | package server |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "io" |
5 | 6 | "net/http" |
6 | 7 | "net/http/httptest" |
@@ -45,6 +46,103 @@ func TestRegisterCustomHTTPEndpoints(t *testing.T) { |
45 | 46 | mockStore.AssertExpectations(t) |
46 | 47 | } |
47 | 48 |
|
| 49 | +type testRaftNodeSource struct { |
| 50 | + isLeader bool |
| 51 | + leaderID string |
| 52 | + nodeID string |
| 53 | +} |
| 54 | + |
| 55 | +func (t testRaftNodeSource) IsLeader() bool { |
| 56 | + return t.isLeader |
| 57 | +} |
| 58 | + |
| 59 | +func (t testRaftNodeSource) LeaderID() string { |
| 60 | + return t.leaderID |
| 61 | +} |
| 62 | + |
| 63 | +func (t testRaftNodeSource) NodeID() string { |
| 64 | + return t.nodeID |
| 65 | +} |
| 66 | + |
| 67 | +func TestRegisterCustomHTTPEndpoints_RaftNodeStatus(t *testing.T) { |
| 68 | + type bodyShape struct { |
| 69 | + IsLeader bool `json:"is_leader"` |
| 70 | + NodeID string `json:"node_id"` |
| 71 | + } |
| 72 | + |
| 73 | + cases := []struct { |
| 74 | + name string |
| 75 | + node testRaftNodeSource |
| 76 | + method string |
| 77 | + wantStatus int |
| 78 | + wantIsLeader bool |
| 79 | + wantNodeID string |
| 80 | + skipBodyDecode bool |
| 81 | + }{ |
| 82 | + { |
| 83 | + // leaderID == nodeID: handler derives is_leader=true from LeaderID(), |
| 84 | + // regardless of the IsLeader() field on testRaftNodeSource. |
| 85 | + name: "leader matches — is_leader true", |
| 86 | + node: testRaftNodeSource{leaderID: "node-a", nodeID: "node-a"}, |
| 87 | + method: http.MethodGet, |
| 88 | + wantStatus: http.StatusOK, |
| 89 | + wantIsLeader: true, |
| 90 | + wantNodeID: "node-a", |
| 91 | + }, |
| 92 | + { |
| 93 | + // leaderID != nodeID: handler derives is_leader=false. |
| 94 | + name: "leader differs — is_leader false", |
| 95 | + node: testRaftNodeSource{leaderID: "node-b", nodeID: "node-a"}, |
| 96 | + method: http.MethodGet, |
| 97 | + wantStatus: http.StatusOK, |
| 98 | + wantIsLeader: false, |
| 99 | + wantNodeID: "node-a", |
| 100 | + }, |
| 101 | + { |
| 102 | + // empty leaderID: fallback — is_leader=false (no elected leader known). |
| 103 | + name: "empty leaderID fallback — is_leader false", |
| 104 | + node: testRaftNodeSource{leaderID: "", nodeID: "node-a"}, |
| 105 | + method: http.MethodGet, |
| 106 | + wantStatus: http.StatusOK, |
| 107 | + wantIsLeader: false, |
| 108 | + wantNodeID: "node-a", |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "non-GET method — 405", |
| 112 | + node: testRaftNodeSource{}, |
| 113 | + method: http.MethodPost, |
| 114 | + wantStatus: http.StatusMethodNotAllowed, |
| 115 | + skipBodyDecode: true, |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + for _, tc := range cases { |
| 120 | + t.Run(tc.name, func(t *testing.T) { |
| 121 | + mux := http.NewServeMux() |
| 122 | + RegisterCustomHTTPEndpoints(mux, nil, nil, config.DefaultConfig(), nil, zerolog.Nop(), tc.node) |
| 123 | + |
| 124 | + ts := httptest.NewServer(mux) |
| 125 | + t.Cleanup(ts.Close) |
| 126 | + |
| 127 | + req, err := http.NewRequest(tc.method, ts.URL+"/raft/node", nil) |
| 128 | + require.NoError(t, err) |
| 129 | + resp, err := http.DefaultClient.Do(req) //nolint:gosec // test-only request to httptest server |
| 130 | + require.NoError(t, err) |
| 131 | + t.Cleanup(func() { _ = resp.Body.Close() }) |
| 132 | + |
| 133 | + require.Equal(t, tc.wantStatus, resp.StatusCode) |
| 134 | + if tc.skipBodyDecode { |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + var body bodyShape |
| 139 | + require.NoError(t, json.NewDecoder(resp.Body).Decode(&body)) |
| 140 | + assert.Equal(t, tc.wantIsLeader, body.IsLeader) |
| 141 | + assert.Equal(t, tc.wantNodeID, body.NodeID) |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
| 145 | + |
48 | 146 | func TestHealthReady_aggregatorBlockDelay(t *testing.T) { |
49 | 147 | logger := zerolog.Nop() |
50 | 148 |
|
|
0 commit comments