Skip to content

Commit

Permalink
simple health check end point (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmikey authored Apr 3, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 6485ecf commit 83f196d
Showing 9 changed files with 74 additions and 5 deletions.
1 change: 1 addition & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ const (
executeEndpoint = "/api/v1/functions/execute"
installEndpoint = "/api/v1/functions/install"
resultEndpoint = "/api/v1/functions/requests/result"
healthEndpoint = "/api/v1/health"
)

func setupAPI(t *testing.T) *api.API {
21 changes: 21 additions & 0 deletions api/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package api

import (
"net/http"

"github.com/blocklessnetworking/b7s/models/response"
"github.com/labstack/echo/v4"
)

// Execute implements the REST API endpoint for function execution.
func (a *API) Health(ctx echo.Context) error {

// respond with health check
resp := response.Health{
Type: "health",
Code: http.StatusOK,
}

// Send the response.
return ctx.JSON(http.StatusOK, resp)
}
32 changes: 32 additions & 0 deletions api/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package api_test

import (
"encoding/json"
"net/http"
"testing"

"github.com/blocklessnetworking/b7s/models/response"
"github.com/stretchr/testify/require"
)

func TestAPI_HealthResult(t *testing.T) {
t.Run("nominal case", func(t *testing.T) {
t.Parallel()

api := setupAPI(t)


rec, ctx, err := setupRecorder(healthEndpoint, nil)
require.NoError(t, err)

err = api.Health(ctx)
require.NoError(t, err)

var res response.Health
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &res))


require.Equal(t, http.StatusOK, res.Code)
require.Equal(t, http.StatusOK, rec.Result().StatusCode)
})
}
13 changes: 12 additions & 1 deletion api/install_test.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,9 @@ package api_test

import (
"context"
"encoding/json"
"net/http"
"strconv"
"testing"
"time"

@@ -11,6 +13,7 @@ import (

"github.com/blocklessnetworking/b7s/api"
"github.com/blocklessnetworking/b7s/models/api/request"
"github.com/blocklessnetworking/b7s/models/response"
"github.com/blocklessnetworking/b7s/testing/mocks"
)

@@ -84,7 +87,15 @@ func TestAPI_FunctionInstall_HandlesErrors(t *testing.T) {
err = api.Install(ctx)
require.NoError(t, err)

require.Equal(t, http.StatusRequestTimeout, rec.Result().StatusCode)
require.Equal(t, http.StatusOK, rec.Result().StatusCode)

var res = response.InstallFunction{}
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &res))

num, err := strconv.Atoi(res.Code)
require.NoError(t, err)

require.Equal(t, http.StatusRequestTimeout, num)
})
t.Run("node fails to install function", func(t *testing.T) {
t.Parallel()
1 change: 1 addition & 0 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
@@ -192,6 +192,7 @@ func run() int {
api := api.New(log, node)

// Set endpoint handlers.
server.GET("/api/v1/health", api.Health)
server.POST("/api/v1/functions/execute", api.Execute)
server.POST("/api/v1/functions/install", api.Install)
server.POST("/api/v1/functions/requests/result", api.ExecutionResult)
2 changes: 1 addition & 1 deletion models/response/health.go
Original file line number Diff line number Diff line change
@@ -8,5 +8,5 @@ import (
type Health struct {
Type string `json:"type,omitempty"`
From peer.ID `json:"from,omitempty"`
Code string `json:"code,omitempty"`
Code int `json:"code,omitempty"`
}
3 changes: 2 additions & 1 deletion node/handlers_internal_test.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package node

import (
"context"
"net/http"
"sync"
"testing"

@@ -24,7 +25,7 @@ func TestNode_Handlers(t *testing.T) {

msg := response.Health{
Type: blockless.MessageHealthCheck,
Code: response.CodeOK,
Code: http.StatusOK,
}

payload := serialize(t, msg)
3 changes: 2 additions & 1 deletion node/health.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package node

import (
"context"
"net/http"
"time"

"github.com/blocklessnetworking/b7s/models/blockless"
@@ -20,7 +21,7 @@ func (n *Node) HealthPing(ctx context.Context) {

msg := response.Health{
Type: blockless.MessageHealthCheck,
Code: response.CodeOK,
Code: http.StatusOK,
}

err := n.publish(ctx, msg)
3 changes: 2 additions & 1 deletion node/health_internal_test.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package node
import (
"context"
"encoding/json"
"net/http"
"testing"
"time"

@@ -74,6 +75,6 @@ func TestNode_Health(t *testing.T) {
require.NoError(t, err)

require.Equal(t, blockless.MessageHealthCheck, received.Type)
require.Equal(t, response.CodeOK, received.Code)
require.Equal(t, http.StatusOK, received.Code)
}
}

0 comments on commit 83f196d

Please sign in to comment.