From 84ca455b2a0b9affca3813b5ee4256816341cbc8 Mon Sep 17 00:00:00 2001 From: s0s01qp Date: Wed, 21 Jul 2021 11:03:03 +0530 Subject: [PATCH] added test cases --- commons/constants.go | 1 - controllers/gin/commons_test.go | 12 +++++++++--- controllers/gin/env_test.go | 10 ++++++++-- controllers/gin/httptrace_test.go | 21 +++++++++++++++++++++ controllers/gin/info_test.go | 20 ++++++++++++++++++++ controllers/gin/metrics_test.go | 20 ++++++++++++++++++++ controllers/gin/ping_test.go | 14 ++++++++++++++ controllers/gin/route.go | 11 +++++------ controllers/gin/shutdown_test.go | 14 ++++++++++++++ controllers/gin/threaddump_test.go | 15 +++++++++++++++ 10 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 controllers/gin/httptrace_test.go create mode 100644 controllers/gin/info_test.go create mode 100644 controllers/gin/metrics_test.go create mode 100644 controllers/gin/ping_test.go create mode 100644 controllers/gin/shutdown_test.go create mode 100644 controllers/gin/threaddump_test.go diff --git a/commons/constants.go b/commons/constants.go index b881900..17ec1f4 100644 --- a/commons/constants.go +++ b/commons/constants.go @@ -37,7 +37,6 @@ const ( // Endpoints const ( EnvEndpoint = "/env" - HealthEndpoint = "/health" HTTPTraceEndpoint = "/httpTrace" InfoEndpoint = "/info" MetricsEndpoint = "/metrics" diff --git a/controllers/gin/commons_test.go b/controllers/gin/commons_test.go index 383c38c..2d8d1d4 100644 --- a/controllers/gin/commons_test.go +++ b/controllers/gin/commons_test.go @@ -2,17 +2,23 @@ package gin_test import ( "github.com/gin-gonic/gin" + ginControllers "github.com/sinhashubham95/go-actuator/controllers/gin" + "github.com/sinhashubham95/go-actuator/core" + "github.com/sinhashubham95/go-actuator/models" "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" ) -func setupRouterAndGetResponse(t *testing.T, endpoint string, handler gin.HandlerFunc) *httptest.ResponseRecorder { +func setupRouterAndGetResponse(t *testing.T, endpoint int, path string) *httptest.ResponseRecorder { router := gin.Default() - router.GET(endpoint, handler) + router.Use(core.GINTracer()) + ginControllers.ConfigureHandlers(&models.Config{ + Endpoints: []int{endpoint}, + }, router) - request, err := http.NewRequest(http.MethodGet, endpoint, nil) + request, err := http.NewRequest(http.MethodGet, path, nil) assert.NoError(t, err) w := httptest.NewRecorder() diff --git a/controllers/gin/env_test.go b/controllers/gin/env_test.go index 52781ad..a325697 100644 --- a/controllers/gin/env_test.go +++ b/controllers/gin/env_test.go @@ -1,15 +1,21 @@ package gin_test import ( + "encoding/json" + "github.com/sinhashubham95/go-actuator/models" "github.com/stretchr/testify/assert" "net/http" "testing" "github.com/sinhashubham95/go-actuator/commons" - ginControllers "github.com/sinhashubham95/go-actuator/controllers/gin" ) func TestHandleEnv(t *testing.T) { - w := setupRouterAndGetResponse(t, commons.EnvEndpoint, ginControllers.HandleEnv) + w := setupRouterAndGetResponse(t, models.Env, commons.EnvEndpoint) assert.Equal(t, http.StatusOK, w.Code) + + var env map[string]interface{} + err := json.NewDecoder(w.Body).Decode(&env) + assert.NoError(t, err) + assert.NotEmpty(t, env) } diff --git a/controllers/gin/httptrace_test.go b/controllers/gin/httptrace_test.go new file mode 100644 index 0000000..707f403 --- /dev/null +++ b/controllers/gin/httptrace_test.go @@ -0,0 +1,21 @@ +package gin_test + +import ( + "encoding/json" + "github.com/sinhashubham95/go-actuator/models" + "github.com/stretchr/testify/assert" + "net/http" + "testing" + + "github.com/sinhashubham95/go-actuator/commons" +) + +func TestHandleHTTPTrace(t *testing.T) { + w := setupRouterAndGetResponse(t, models.HTTPTrace, commons.HTTPTraceEndpoint) + assert.Equal(t, http.StatusOK, w.Code) + + var traces []*models.HTTPTraceResult + err := json.NewDecoder(w.Body).Decode(&traces) + assert.NoError(t, err) + assert.NotEmpty(t, traces) +} diff --git a/controllers/gin/info_test.go b/controllers/gin/info_test.go new file mode 100644 index 0000000..b08b9f2 --- /dev/null +++ b/controllers/gin/info_test.go @@ -0,0 +1,20 @@ +package gin_test + +import ( + "encoding/json" + "github.com/sinhashubham95/go-actuator/commons" + "github.com/sinhashubham95/go-actuator/models" + "github.com/stretchr/testify/assert" + "net/http" + "testing" +) + +func TestHandleInfo(t *testing.T) { + w := setupRouterAndGetResponse(t, models.Info, commons.InfoEndpoint) + assert.Equal(t, http.StatusOK, w.Code) + + var info map[string]interface{} + err := json.NewDecoder(w.Body).Decode(&info) + assert.NoError(t, err) + assert.NotEmpty(t, info) +} diff --git a/controllers/gin/metrics_test.go b/controllers/gin/metrics_test.go new file mode 100644 index 0000000..c50e913 --- /dev/null +++ b/controllers/gin/metrics_test.go @@ -0,0 +1,20 @@ +package gin_test + +import ( + "encoding/json" + "github.com/sinhashubham95/go-actuator/commons" + "github.com/sinhashubham95/go-actuator/models" + "github.com/stretchr/testify/assert" + "net/http" + "testing" +) + +func TestHandleMetrics(t *testing.T) { + w := setupRouterAndGetResponse(t, models.Metrics, commons.MetricsEndpoint) + assert.Equal(t, http.StatusOK, w.Code) + + var metrics *models.MemStats + err := json.NewDecoder(w.Body).Decode(&metrics) + assert.NoError(t, err) + assert.NotNil(t, metrics) +} diff --git a/controllers/gin/ping_test.go b/controllers/gin/ping_test.go new file mode 100644 index 0000000..5237574 --- /dev/null +++ b/controllers/gin/ping_test.go @@ -0,0 +1,14 @@ +package gin_test + +import ( + "github.com/sinhashubham95/go-actuator/commons" + "github.com/sinhashubham95/go-actuator/models" + "github.com/stretchr/testify/assert" + "net/http" + "testing" +) + +func TestHandlePing(t *testing.T) { + w := setupRouterAndGetResponse(t, models.Ping, commons.PingEndpoint) + assert.Equal(t, http.StatusOK, w.Code) +} diff --git a/controllers/gin/route.go b/controllers/gin/route.go index bc20c15..895607a 100644 --- a/controllers/gin/route.go +++ b/controllers/gin/route.go @@ -9,7 +9,6 @@ import ( func ConfigureHandlers(config *models.Config, router *gin.Engine) { actuator := router.Group(config.Prefix) - gin.Logger() for _, e := range config.Endpoints { // now one by one add the handler of each endpoint switch e { @@ -18,15 +17,15 @@ func ConfigureHandlers(config *models.Config, router *gin.Engine) { case models.HTTPTrace: actuator.GET(commons.HTTPTraceEndpoint, HandleHTTPTrace) case models.Info: - actuator.GET(commons.HealthEndpoint, HandleInfo) + actuator.GET(commons.InfoEndpoint, HandleInfo) case models.Metrics: - actuator.GET(commons.HealthEndpoint, HandleMetrics) + actuator.GET(commons.MetricsEndpoint, HandleMetrics) case models.Ping: - actuator.GET(commons.HealthEndpoint, HandlePing) + actuator.GET(commons.PingEndpoint, HandlePing) case models.Shutdown: - actuator.GET(commons.HealthEndpoint, HandleShutdown) + actuator.GET(commons.ShutdownEndpoint, HandleShutdown) case models.ThreadDump: - actuator.GET(commons.HealthEndpoint, HandleThreadDump) + actuator.GET(commons.ThreadDumpEndpoint, HandleThreadDump) } } } diff --git a/controllers/gin/shutdown_test.go b/controllers/gin/shutdown_test.go new file mode 100644 index 0000000..fb5782c --- /dev/null +++ b/controllers/gin/shutdown_test.go @@ -0,0 +1,14 @@ +package gin_test + +import ( + "github.com/sinhashubham95/go-actuator/commons" + "github.com/sinhashubham95/go-actuator/models" + "github.com/stretchr/testify/assert" + "net/http" + "testing" +) + +func TestHandleShutdown(t *testing.T) { + w := setupRouterAndGetResponse(t, models.Shutdown, commons.ShutdownEndpoint) + assert.Equal(t, http.StatusInternalServerError, w.Code) +} diff --git a/controllers/gin/threaddump_test.go b/controllers/gin/threaddump_test.go new file mode 100644 index 0000000..c36cda6 --- /dev/null +++ b/controllers/gin/threaddump_test.go @@ -0,0 +1,15 @@ +package gin_test + +import ( + "github.com/sinhashubham95/go-actuator/commons" + "github.com/sinhashubham95/go-actuator/models" + "github.com/stretchr/testify/assert" + "net/http" + "testing" +) + +func TestHandleThreadDump(t *testing.T) { + w := setupRouterAndGetResponse(t, models.ThreadDump, commons.ThreadDumpEndpoint) + assert.Equal(t, http.StatusOK, w.Code) + assert.NotEmpty(t, w.Body.String()) +}