Skip to content

Commit

Permalink
added test cases and go vet fixes renaming packages and file names to…
Browse files Browse the repository at this point in the history
… lower case
  • Loading branch information
s0s01qp committed Jul 21, 2021
1 parent 3df77fc commit ead78e1
Show file tree
Hide file tree
Showing 33 changed files with 196 additions and 30 deletions.
4 changes: 2 additions & 2 deletions actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fastHTTP
package fasthttp

import (
"github.com/valyala/fasthttp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fastHTTP
package fasthttp

import (
"github.com/valyala/fasthttp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fastHTTP
package fasthttp

import (
"github.com/valyala/fasthttp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fastHTTP
package fasthttp

import (
"github.com/valyala/fasthttp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fastHTTP
package fasthttp

import (
"github.com/valyala/fasthttp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fastHTTP
package fasthttp

import (
"github.com/sinhashubham95/go-actuator/models"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fastHTTP
package fasthttp

import (
"github.com/valyala/fasthttp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fastHTTP
package fasthttp

import (
"github.com/valyala/fasthttp"
Expand Down
22 changes: 22 additions & 0 deletions controllers/gin/commons_test.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions controllers/gin/env_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion controllers/netHTTP/env.go → controllers/nethttp/env.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package netHTTP
package nethttp

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package netHTTP
package nethttp

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package netHTTP
package nethttp

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package netHTTP
package nethttp

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package netHTTP
package nethttp

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package netHTTP
package nethttp

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package netHTTP
package nethttp

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package netHTTP
package nethttp

import (
"net/http"
Expand Down
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions core/info_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
11 changes: 11 additions & 0 deletions core/metrics_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
11 changes: 0 additions & 11 deletions core/ping.go

This file was deleted.

15 changes: 15 additions & 0 deletions core/shutdown_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
File renamed without changes.
13 changes: 13 additions & 0 deletions core/threaddump_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
24 changes: 24 additions & 0 deletions flags/flags_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
2 changes: 1 addition & 1 deletion models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions models/configs_test.go
Original file line number Diff line number Diff line change
@@ -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{[email protected]: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)
}
17 changes: 17 additions & 0 deletions models/endpoints_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit ead78e1

Please sign in to comment.