Commit c6cac699 authored by Sonja Happ's avatar Sonja Happ
Browse files

#26 complete implementation of healthz endpoint and testing

parent 16356688
......@@ -35,7 +35,7 @@ func ConnectAMQP(uri string) error {
// connect to broker
client.connection, err = amqp.Dial(uri)
if err != nil {
return fmt.Errorf("AMQP: failed to connect to RabbitMQ broker")
return fmt.Errorf("AMQP: failed to connect to RabbitMQ broker %v", uri)
}
// create channel
......@@ -166,8 +166,12 @@ func PingAMQP() error {
func CheckConnection() error {
if client.connection.IsClosed() {
return fmt.Errorf("connection to broker is closed")
if client.connection != nil {
if client.connection.IsClosed() {
return fmt.Errorf("connection to broker is closed")
}
} else {
return fmt.Errorf("connection is nil")
}
return nil
......
package healthz
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/amqp"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"github.com/gin-gonic/gin"
......@@ -18,7 +19,7 @@ func RegisterHealthzEndpoint(r *gin.RouterGroup) {
// @Produce json
// @Tags healthz
// @Success 200 "Backend is healthy, database and AMQP broker connections are alive"
// @Failure 500 "Backend is NOT healthy"
// @Failure 500 {object} docs.ResponseError "Backend is NOT healthy"
// @Router /healthz [get]
func getHealth(c *gin.Context) {
......@@ -26,14 +27,19 @@ func getHealth(c *gin.Context) {
db := database.GetDB()
err := db.DB().Ping()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": err})
return
}
// check if connection to AMQP broker is alive if backend was started with AMQP client
if len(database.AMQP_URL) != 0 {
err = amqp.CheckConnection()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": err})
fmt.Println(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
"success:": false,
"message": err.Error(),
})
return
}
}
......
......@@ -2,6 +2,7 @@ package healthz
import (
"git.rwth-aachen.de/acs/public/villas/web-backend-go/amqp"
//"git.rwth-aachen.de/acs/public/villas/web-backend-go/amqp"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user"
......@@ -9,18 +10,15 @@ import (
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"net/http"
//"net/http"
"testing"
)
var router *gin.Engine
var db *gorm.DB
const test_amqp_url = "amqp://villas:villas@rabbitmq:5672"
//const test_amqp_url = "amqp://rabbit@goofy:5672"
func TestHealthz(t *testing.T) {
database.AMQP_URL = test_amqp_url
// connect DB
db = database.InitDB(database.DB_NAME, true)
defer db.Close()
......@@ -39,14 +37,30 @@ func TestHealthz(t *testing.T) {
"/api/authenticate", "POST", helper.UserACredentials)
assert.NoError(t, err)
// connect AMQP client
// close db connection
err = db.Close()
assert.NoError(t, err)
// test healthz endpoint for unconnected DB and AMQP client
code, resp, err := helper.TestEndpoint(router, token, "api/healthz", http.MethodGet, nil)
assert.NoError(t, err)
assert.Equalf(t, 500, code, "Response body: \n%v\n", resp)
// reconnect DB
db = database.InitDB(database.DB_NAME, false)
defer db.Close()
// test healthz endpoint for connected DB and unconnected AMQP client
code, resp, err = helper.TestEndpoint(router, token, "api/healthz", http.MethodGet, nil)
assert.NoError(t, err)
assert.Equalf(t, 500, code, "Response body: \n%v\n", resp)
// connect AMQP client (make sure that AMQP_URL is set via command line parameter -amqp)
err = amqp.ConnectAMQP(database.AMQP_URL)
assert.NoError(t, err)
// test healthz endpoint for connected DB and AMQP client
code, resp, err := helper.TestEndpoint(router, token, "api/healthz", http.MethodGet, nil)
code, resp, err = helper.TestEndpoint(router, token, "api/healthz", http.MethodGet, nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
}
......@@ -14,8 +14,7 @@ func userToContext(ctx *gin.Context, user_id uint) {
var user User
err := user.ByID(user_id)
if err != nil {
helper.UnauthorizedAbort(ctx, "Authentication failed (user not found)")
if helper.DBError(ctx, err) {
return
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment