Skip to content

Commit

Permalink
Add json responses for health endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Mar 21, 2024
1 parent 236e7ef commit 808c7a4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
6 changes: 6 additions & 0 deletions api/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@ type (
Decimals uint32 `serix:""`
}

// NetworkHealthResponse defines the network health response.
NetworkHealthResponse struct {
// Whether the network is healthy (finalization is not delayed).
IsNetworkHealthy bool `serix:""`
}

// NetworkMetricsResponse defines the network metrics response.
NetworkMetricsResponse struct {
// The current rate of new blocks per second, it's updated when a commitment is committed.
Expand Down
18 changes: 18 additions & 0 deletions api/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ func Test_CoreAPIDeSerialize(t *testing.T) {
SeriErr: nil,
DeSeriErr: nil,
},
{
Name: "ok - NetworkHealthResponse",
Source: &api.NetworkHealthResponse{
IsNetworkHealthy: true,
},
Target: &api.NetworkHealthResponse{},
SeriErr: nil,
DeSeriErr: nil,
},
{
Name: "ok - NetworkMetricsResponse",
Source: &api.NetworkMetricsResponse{
Expand Down Expand Up @@ -434,6 +443,15 @@ func Test_CoreAPIJSONSerialization(t *testing.T) {
"subunit": "glow",
"decimals": 6
}
}`,
},
{
Name: "ok - NetworkHealthResponse",
Source: &api.NetworkHealthResponse{
IsNetworkHealthy: true,
},
Target: `{
"isNetworkHealthy": true
}`,
},
{
Expand Down
6 changes: 6 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ const (
MIMEApplicationVendorIOTASerializerV2 = "application/vnd.iota.serializer-v2"
)

// HealthResponse defines the health response.
type HealthResponse struct {
// Whether the node is healthy.
IsHealthy bool `serix:""`
}

var (
// RouteHealth is the route for querying a node's health status.
RouteHealth = "/health"
Expand Down
24 changes: 12 additions & 12 deletions nodeclient/http_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ func nodeClient(t *testing.T) *nodeclient.Client {
func TestClient_Health(t *testing.T) {
defer gock.Off()

gock.New(nodeAPIUrl).
Get(api.RouteHealth).
Reply(200)
mockGetJSON(api.RouteHealth, 200, &api.HealthResponse{
IsHealthy: true,
})

nodeAPI := nodeClient(t)
healthy, err := nodeAPI.Health(context.Background())
require.NoError(t, err)
require.True(t, healthy)

gock.New(nodeAPIUrl).
Get(api.RouteHealth).
Reply(503)
mockGetJSON(api.RouteHealth, 503, &api.HealthResponse{
IsHealthy: false,
})

healthy, err = nodeAPI.Health(context.Background())
require.NoError(t, err)
Expand All @@ -167,18 +167,18 @@ func TestClient_Health(t *testing.T) {
func TestClient_NetworkHealth(t *testing.T) {
defer gock.Off()

gock.New(nodeAPIUrl).
Get(api.CoreRouteNetworkHealth).
Reply(200)
mockGetJSON(api.CoreRouteNetworkHealth, 200, &api.NetworkHealthResponse{
IsNetworkHealthy: true,
})

nodeAPI := nodeClient(t)
healthy, err := nodeAPI.NetworkHealth(context.Background())
require.NoError(t, err)
require.True(t, healthy)

gock.New(nodeAPIUrl).
Get(api.CoreRouteNetworkHealth).
Reply(503)
mockGetJSON(api.CoreRouteNetworkHealth, 503, &api.NetworkHealthResponse{
IsNetworkHealthy: false,
})

healthy, err = nodeAPI.NetworkHealth(context.Background())
require.NoError(t, err)
Expand Down

0 comments on commit 808c7a4

Please sign in to comment.