From ead78e1dee9d783d25b5acdaab06159f28a66910 Mon Sep 17 00:00:00 2001 From: s0s01qp Date: Wed, 21 Jul 2021 10:41:19 +0530 Subject: [PATCH] added test cases and go vet fixes renaming packages and file names to lower case --- actuator.go | 4 +- controllers/{fastHTTP => fasthttp}/env.go | 2 +- .../httpTrace.go => fasthttp/httptrace.go} | 2 +- controllers/{fastHTTP => fasthttp}/info.go | 2 +- controllers/{fastHTTP => fasthttp}/metrics.go | 2 +- controllers/{fastHTTP => fasthttp}/ping.go | 2 +- controllers/{fastHTTP => fasthttp}/route.go | 2 +- .../{fastHTTP => fasthttp}/shutdown.go | 2 +- .../threadDump.go => fasthttp/threaddump.go} | 2 +- controllers/gin/commons_test.go | 22 +++++++++ controllers/gin/env_test.go | 15 +++++++ .../gin/{httpTrace.go => httptrace.go} | 0 .../gin/{threadDump.go => threaddump.go} | 0 controllers/{netHTTP => nethttp}/env.go | 2 +- .../httpTrace.go => nethttp/httptrace.go} | 2 +- controllers/{netHTTP => nethttp}/info.go | 2 +- controllers/{netHTTP => nethttp}/metrics.go | 2 +- controllers/{netHTTP => nethttp}/ping.go | 2 +- controllers/{netHTTP => nethttp}/route.go | 2 +- controllers/{netHTTP => nethttp}/shutdown.go | 2 +- .../threadDump.go => nethttp/threaddump.go} | 2 +- core/{httpTrace.go => httptrace.go} | 0 core/{httpTrace_test.go => httptrace_test.go} | 0 core/info_test.go | 15 +++++++ core/metrics_test.go | 11 +++++ core/ping.go | 11 ----- core/shutdown_test.go | 15 +++++++ core/{threadDump.go => threaddump.go} | 0 core/threaddump_test.go | 13 ++++++ flags/flags_test.go | 24 ++++++++++ models/config.go | 2 +- models/configs_test.go | 45 +++++++++++++++++++ models/endpoints_test.go | 17 +++++++ 33 files changed, 196 insertions(+), 30 deletions(-) rename controllers/{fastHTTP => fasthttp}/env.go (97%) rename controllers/{fastHTTP/httpTrace.go => fasthttp/httptrace.go} (97%) rename controllers/{fastHTTP => fasthttp}/info.go (97%) rename controllers/{fastHTTP => fasthttp}/metrics.go (97%) rename controllers/{fastHTTP => fasthttp}/ping.go (92%) rename controllers/{fastHTTP => fasthttp}/route.go (98%) rename controllers/{fastHTTP => fasthttp}/shutdown.go (94%) rename controllers/{fastHTTP/threadDump.go => fasthttp/threaddump.go} (97%) create mode 100644 controllers/gin/commons_test.go create mode 100644 controllers/gin/env_test.go rename controllers/gin/{httpTrace.go => httptrace.go} (100%) rename controllers/gin/{threadDump.go => threaddump.go} (100%) rename controllers/{netHTTP => nethttp}/env.go (97%) rename controllers/{netHTTP/httpTrace.go => nethttp/httptrace.go} (97%) rename controllers/{netHTTP => nethttp}/info.go (97%) rename controllers/{netHTTP => nethttp}/metrics.go (97%) rename controllers/{netHTTP => nethttp}/ping.go (92%) rename controllers/{netHTTP => nethttp}/route.go (98%) rename controllers/{netHTTP => nethttp}/shutdown.go (94%) rename controllers/{netHTTP/threadDump.go => nethttp/threaddump.go} (97%) rename core/{httpTrace.go => httptrace.go} (100%) rename core/{httpTrace_test.go => httptrace_test.go} (100%) create mode 100644 core/info_test.go create mode 100644 core/metrics_test.go delete mode 100644 core/ping.go create mode 100644 core/shutdown_test.go rename core/{threadDump.go => threaddump.go} (100%) create mode 100644 core/threaddump_test.go create mode 100644 flags/flags_test.go create mode 100644 models/configs_test.go create mode 100644 models/endpoints_test.go diff --git a/actuator.go b/actuator.go index 4182293..91da033 100644 --- a/actuator.go +++ b/actuator.go @@ -5,9 +5,9 @@ import ( "github.com/valyala/fasthttp" "net/http" - fastHTTPControllers "github.com/sinhashubham95/go-actuator/controllers/fastHTTP" + fastHTTPControllers "github.com/sinhashubham95/go-actuator/controllers/fasthttp" ginControllers "github.com/sinhashubham95/go-actuator/controllers/gin" - netHTTPControllers "github.com/sinhashubham95/go-actuator/controllers/netHTTP" + netHTTPControllers "github.com/sinhashubham95/go-actuator/controllers/nethttp" "github.com/sinhashubham95/go-actuator/models" ) diff --git a/controllers/fastHTTP/env.go b/controllers/fasthttp/env.go similarity index 97% rename from controllers/fastHTTP/env.go rename to controllers/fasthttp/env.go index 475776b..30bc58e 100644 --- a/controllers/fastHTTP/env.go +++ b/controllers/fasthttp/env.go @@ -1,4 +1,4 @@ -package fastHTTP +package fasthttp import ( "github.com/valyala/fasthttp" diff --git a/controllers/fastHTTP/httpTrace.go b/controllers/fasthttp/httptrace.go similarity index 97% rename from controllers/fastHTTP/httpTrace.go rename to controllers/fasthttp/httptrace.go index 86752dc..d03b6d5 100644 --- a/controllers/fastHTTP/httpTrace.go +++ b/controllers/fasthttp/httptrace.go @@ -1,4 +1,4 @@ -package fastHTTP +package fasthttp import ( "github.com/valyala/fasthttp" diff --git a/controllers/fastHTTP/info.go b/controllers/fasthttp/info.go similarity index 97% rename from controllers/fastHTTP/info.go rename to controllers/fasthttp/info.go index 999514e..bae443f 100644 --- a/controllers/fastHTTP/info.go +++ b/controllers/fasthttp/info.go @@ -1,4 +1,4 @@ -package fastHTTP +package fasthttp import ( "github.com/valyala/fasthttp" diff --git a/controllers/fastHTTP/metrics.go b/controllers/fasthttp/metrics.go similarity index 97% rename from controllers/fastHTTP/metrics.go rename to controllers/fasthttp/metrics.go index a6e7ec5..60d585b 100644 --- a/controllers/fastHTTP/metrics.go +++ b/controllers/fasthttp/metrics.go @@ -1,4 +1,4 @@ -package fastHTTP +package fasthttp import ( "github.com/valyala/fasthttp" diff --git a/controllers/fastHTTP/ping.go b/controllers/fasthttp/ping.go similarity index 92% rename from controllers/fastHTTP/ping.go rename to controllers/fasthttp/ping.go index 462bede..a3fb292 100644 --- a/controllers/fastHTTP/ping.go +++ b/controllers/fasthttp/ping.go @@ -1,4 +1,4 @@ -package fastHTTP +package fasthttp import ( "github.com/valyala/fasthttp" diff --git a/controllers/fastHTTP/route.go b/controllers/fasthttp/route.go similarity index 98% rename from controllers/fastHTTP/route.go rename to controllers/fasthttp/route.go index 29474c9..7b20156 100644 --- a/controllers/fastHTTP/route.go +++ b/controllers/fasthttp/route.go @@ -1,4 +1,4 @@ -package fastHTTP +package fasthttp import ( "github.com/sinhashubham95/go-actuator/models" diff --git a/controllers/fastHTTP/shutdown.go b/controllers/fasthttp/shutdown.go similarity index 94% rename from controllers/fastHTTP/shutdown.go rename to controllers/fasthttp/shutdown.go index 0f0b769..07233de 100644 --- a/controllers/fastHTTP/shutdown.go +++ b/controllers/fasthttp/shutdown.go @@ -1,4 +1,4 @@ -package fastHTTP +package fasthttp import ( "github.com/valyala/fasthttp" diff --git a/controllers/fastHTTP/threadDump.go b/controllers/fasthttp/threaddump.go similarity index 97% rename from controllers/fastHTTP/threadDump.go rename to controllers/fasthttp/threaddump.go index f32741d..136d040 100644 --- a/controllers/fastHTTP/threadDump.go +++ b/controllers/fasthttp/threaddump.go @@ -1,4 +1,4 @@ -package fastHTTP +package fasthttp import ( "github.com/valyala/fasthttp" diff --git a/controllers/gin/commons_test.go b/controllers/gin/commons_test.go new file mode 100644 index 0000000..383c38c --- /dev/null +++ b/controllers/gin/commons_test.go @@ -0,0 +1,22 @@ +package gin_test + +import ( + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" +) + +func setupRouterAndGetResponse(t *testing.T, endpoint string, handler gin.HandlerFunc) *httptest.ResponseRecorder { + router := gin.Default() + router.GET(endpoint, handler) + + request, err := http.NewRequest(http.MethodGet, endpoint, nil) + assert.NoError(t, err) + + w := httptest.NewRecorder() + router.ServeHTTP(w, request) + + return w +} diff --git a/controllers/gin/env_test.go b/controllers/gin/env_test.go new file mode 100644 index 0000000..52781ad --- /dev/null +++ b/controllers/gin/env_test.go @@ -0,0 +1,15 @@ +package gin_test + +import ( + "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) + assert.Equal(t, http.StatusOK, w.Code) +} diff --git a/controllers/gin/httpTrace.go b/controllers/gin/httptrace.go similarity index 100% rename from controllers/gin/httpTrace.go rename to controllers/gin/httptrace.go diff --git a/controllers/gin/threadDump.go b/controllers/gin/threaddump.go similarity index 100% rename from controllers/gin/threadDump.go rename to controllers/gin/threaddump.go diff --git a/controllers/netHTTP/env.go b/controllers/nethttp/env.go similarity index 97% rename from controllers/netHTTP/env.go rename to controllers/nethttp/env.go index 0963f99..ed97531 100644 --- a/controllers/netHTTP/env.go +++ b/controllers/nethttp/env.go @@ -1,4 +1,4 @@ -package netHTTP +package nethttp import ( "net/http" diff --git a/controllers/netHTTP/httpTrace.go b/controllers/nethttp/httptrace.go similarity index 97% rename from controllers/netHTTP/httpTrace.go rename to controllers/nethttp/httptrace.go index f0dd1c8..6f3f0ae 100644 --- a/controllers/netHTTP/httpTrace.go +++ b/controllers/nethttp/httptrace.go @@ -1,4 +1,4 @@ -package netHTTP +package nethttp import ( "net/http" diff --git a/controllers/netHTTP/info.go b/controllers/nethttp/info.go similarity index 97% rename from controllers/netHTTP/info.go rename to controllers/nethttp/info.go index 96753b1..5baefd0 100644 --- a/controllers/netHTTP/info.go +++ b/controllers/nethttp/info.go @@ -1,4 +1,4 @@ -package netHTTP +package nethttp import ( "net/http" diff --git a/controllers/netHTTP/metrics.go b/controllers/nethttp/metrics.go similarity index 97% rename from controllers/netHTTP/metrics.go rename to controllers/nethttp/metrics.go index d786039..60f4f90 100644 --- a/controllers/netHTTP/metrics.go +++ b/controllers/nethttp/metrics.go @@ -1,4 +1,4 @@ -package netHTTP +package nethttp import ( "net/http" diff --git a/controllers/netHTTP/ping.go b/controllers/nethttp/ping.go similarity index 92% rename from controllers/netHTTP/ping.go rename to controllers/nethttp/ping.go index 37a3739..0a078d6 100644 --- a/controllers/netHTTP/ping.go +++ b/controllers/nethttp/ping.go @@ -1,4 +1,4 @@ -package netHTTP +package nethttp import ( "net/http" diff --git a/controllers/netHTTP/route.go b/controllers/nethttp/route.go similarity index 98% rename from controllers/netHTTP/route.go rename to controllers/nethttp/route.go index d9853b9..252b321 100644 --- a/controllers/netHTTP/route.go +++ b/controllers/nethttp/route.go @@ -1,4 +1,4 @@ -package netHTTP +package nethttp import ( "net/http" diff --git a/controllers/netHTTP/shutdown.go b/controllers/nethttp/shutdown.go similarity index 94% rename from controllers/netHTTP/shutdown.go rename to controllers/nethttp/shutdown.go index a4e9b2b..e5b4f04 100644 --- a/controllers/netHTTP/shutdown.go +++ b/controllers/nethttp/shutdown.go @@ -1,4 +1,4 @@ -package netHTTP +package nethttp import ( "net/http" diff --git a/controllers/netHTTP/threadDump.go b/controllers/nethttp/threaddump.go similarity index 97% rename from controllers/netHTTP/threadDump.go rename to controllers/nethttp/threaddump.go index 498bfda..0b1b765 100644 --- a/controllers/netHTTP/threadDump.go +++ b/controllers/nethttp/threaddump.go @@ -1,4 +1,4 @@ -package netHTTP +package nethttp import ( "net/http" diff --git a/core/httpTrace.go b/core/httptrace.go similarity index 100% rename from core/httpTrace.go rename to core/httptrace.go diff --git a/core/httpTrace_test.go b/core/httptrace_test.go similarity index 100% rename from core/httpTrace_test.go rename to core/httptrace_test.go diff --git a/core/info_test.go b/core/info_test.go new file mode 100644 index 0000000..732fddb --- /dev/null +++ b/core/info_test.go @@ -0,0 +1,15 @@ +package core_test + +import ( + "github.com/sinhashubham95/go-actuator/commons" + "github.com/sinhashubham95/go-actuator/core" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestGetInfo(t *testing.T) { + info := core.GetInfo() + assert.NotEmpty(t, info[commons.Application]) + assert.NotEmpty(t, info[commons.Git]) + assert.NotEmpty(t, info[commons.Runtime]) +} diff --git a/core/metrics_test.go b/core/metrics_test.go new file mode 100644 index 0000000..67ba9d4 --- /dev/null +++ b/core/metrics_test.go @@ -0,0 +1,11 @@ +package core_test + +import ( + "github.com/sinhashubham95/go-actuator/core" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestGetMetrics(t *testing.T) { + assert.NotNil(t, core.GetMetrics()) +} diff --git a/core/ping.go b/core/ping.go deleted file mode 100644 index 631278a..0000000 --- a/core/ping.go +++ /dev/null @@ -1,11 +0,0 @@ -package core - -import ( - "github.com/valyala/fasthttp" - "net/http" -) - -// HandlePing is the handler function for the ping endpoint -func HandlePing(ctx *fasthttp.RequestCtx) { - ctx.SetStatusCode(http.StatusOK) -} diff --git a/core/shutdown_test.go b/core/shutdown_test.go new file mode 100644 index 0000000..cf64d14 --- /dev/null +++ b/core/shutdown_test.go @@ -0,0 +1,15 @@ +package core_test + +import ( + "github.com/sinhashubham95/go-actuator/core" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestShutdown(t *testing.T) { + defer func() { + r := recover() + assert.NotNil(t, r) + }() + core.Shutdown() +} diff --git a/core/threadDump.go b/core/threaddump.go similarity index 100% rename from core/threadDump.go rename to core/threaddump.go diff --git a/core/threaddump_test.go b/core/threaddump_test.go new file mode 100644 index 0000000..0cff492 --- /dev/null +++ b/core/threaddump_test.go @@ -0,0 +1,13 @@ +package core_test + +import ( + "github.com/sinhashubham95/go-actuator/core" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestGetThreadDump(t *testing.T) { + dump, err := core.GetThreadDump() + assert.NoError(t, err) + assert.NotEmpty(t, string(dump)) +} diff --git a/flags/flags_test.go b/flags/flags_test.go new file mode 100644 index 0000000..e17ed20 --- /dev/null +++ b/flags/flags_test.go @@ -0,0 +1,24 @@ +package flags_test + +import ( + "github.com/sinhashubham95/go-actuator/commons" + "github.com/sinhashubham95/go-actuator/flags" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestEnv(t *testing.T) { + assert.Equal(t, commons.EnvDefaultValue, flags.Env()) +} + +func TestName(t *testing.T) { + assert.Equal(t, commons.NameDefaultValue, flags.Name()) +} + +func TestPort(t *testing.T) { + assert.Equal(t, commons.PortDefaultValue, flags.Port()) +} + +func TestVersion(t *testing.T) { + assert.Equal(t, commons.VersionDefaultValue, flags.Version()) +} diff --git a/models/config.go b/models/config.go index 2a8814d..25eb350 100644 --- a/models/config.go +++ b/models/config.go @@ -26,7 +26,7 @@ func (config *Config) Validate() { } for _, endpoint := range config.Endpoints { if !IsValidEndpoint(endpoint) { - panic(errors.New(fmt.Sprintf("Invalid endpoint %d provided.", endpoint))) + panic(errors.New(fmt.Sprintf("invalid endpoint %d provided", endpoint))) } } } diff --git a/models/configs_test.go b/models/configs_test.go new file mode 100644 index 0000000..7664d8d --- /dev/null +++ b/models/configs_test.go @@ -0,0 +1,45 @@ +package models_test + +import ( + "github.com/sinhashubham95/go-actuator/commons" + "github.com/sinhashubham95/go-actuator/models" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestConfigValidateInvalidPrefix(t *testing.T) { + defer func() { + if r := recover(); r != nil { + if err, ok := r.(error); ok { + assert.Error(t, err) + assert.Equal(t, "invalid prefix provided", err.Error()) + } + } + }() + config := &models.Config{ + Prefix: "postgres://user:abc{DEf1=ghi@example.com:5432/db?sslmode=require", + } + config.Validate() +} + +func TestConfigValidateInvalidEndpoints(t *testing.T) { + defer func() { + if r := recover(); r != nil { + if err, ok := r.(error); ok { + assert.Error(t, err) + assert.Equal(t, "invalid endpoint 999 provided", err.Error()) + } + } + }() + config := &models.Config{ + Endpoints: []int{999}, + } + config.Validate() +} + +func TestConfigDefault(t *testing.T) { + config := &models.Config{} + config.Default() + assert.Equal(t, commons.DefaultActuatorPrefix, config.Prefix) + assert.Equal(t, models.Endpoints, config.Endpoints) +} diff --git a/models/endpoints_test.go b/models/endpoints_test.go new file mode 100644 index 0000000..551d461 --- /dev/null +++ b/models/endpoints_test.go @@ -0,0 +1,17 @@ +package models_test + +import ( + "github.com/sinhashubham95/go-actuator/models" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestIsValidEndpoint(t *testing.T) { + assert.True(t, models.IsValidEndpoint(models.Env)) + assert.True(t, models.IsValidEndpoint(models.HTTPTrace)) + assert.True(t, models.IsValidEndpoint(models.Info)) + assert.True(t, models.IsValidEndpoint(models.Metrics)) + assert.True(t, models.IsValidEndpoint(models.Ping)) + assert.True(t, models.IsValidEndpoint(models.Shutdown)) + assert.True(t, models.IsValidEndpoint(models.ThreadDump)) +}