Skip to content

Commit

Permalink
Extend Tests
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <frank.jogeleit@lovoo.com>
  • Loading branch information
Frank Jogeleit committed Mar 26, 2024
1 parent ac3b98f commit 92a8bde
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 3 deletions.
75 changes: 75 additions & 0 deletions pkg/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/kyverno/policy-reporter/pkg/api"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)

var check = func() error {
Expand Down Expand Up @@ -61,3 +62,77 @@ func TestWithProfiling(t *testing.T) {
assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
}

type testHandler struct{}

func (h *testHandler) Register(group *gin.RouterGroup) error {
group.GET("", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, nil)
})

return nil
}

func TestWithCustomHandler(t *testing.T) {
gin.SetMode(gin.ReleaseMode)

server := api.NewServer(gin.New(), api.WithProfiling())
server.Register("/test", &testHandler{})

req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
}

func TestWithRecover(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
server := api.NewServer(engine, api.WithRecovery())

engine.GET("/recover", func(ctx *gin.Context) {
panic("recover")
})

req, _ := http.NewRequest("GET", "/recover", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

assert := assert.New(t)
assert.Equal(http.StatusInternalServerError, w.Code)
}

func TestWithZapLoggingRecover(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
server := api.NewServer(engine, api.WithLogging(zap.L()))

engine.GET("/recover", func(ctx *gin.Context) {
panic("recover")
})

req, _ := http.NewRequest("GET", "/recover", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

assert := assert.New(t)
assert.Equal(http.StatusInternalServerError, w.Code)
}

func TestWithPort(t *testing.T) {
gin.SetMode(gin.ReleaseMode)

server := api.NewServer(gin.New(), api.WithProfiling(), api.WithPort(8082))
req, _ := http.NewRequest("GET", "/debug/pprof/", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
}
6 changes: 3 additions & 3 deletions pkg/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func BuildFilter(ctx *gin.Context) db.Filter {
continue
}

if l, ok := exclude[strings.TrimSpace(parts[length-2])]; ok {
exclude[strings.TrimSpace(parts[length-2])] = append(l, strings.TrimSpace(parts[length-1]))
if l, ok := exclude[strings.TrimSpace(parts[0])]; ok {
exclude[strings.TrimSpace(parts[0])] = append(l, strings.TrimSpace(parts[1]))
} else {
exclude[strings.TrimSpace(parts[length-2])] = []string{strings.TrimSpace(parts[length-1])}
exclude[strings.TrimSpace(parts[0])] = []string{strings.TrimSpace(parts[1])}
}
}

Expand Down
112 changes: 112 additions & 0 deletions pkg/api/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package api_test

import (
"errors"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/gin-gonic/gin"
"github.com/kyverno/policy-reporter/pkg/api"
"github.com/stretchr/testify/assert"

db "github.com/kyverno/policy-reporter/pkg/database"
)

func TestSendResponseSuccess(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
server := api.NewServer(engine, api.WithRecovery())

engine.GET("/send", func(ctx *gin.Context) {
api.SendResponse(ctx, "data", "", nil)
})

req, _ := http.NewRequest("GET", "/send", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
assert.Equal(`"data"`, string(w.Body.Bytes()))
}

func TestSendResponseError(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
server := api.NewServer(engine, api.WithRecovery())

engine.GET("/send", func(ctx *gin.Context) {
api.SendResponse(ctx, nil, "errorMsg", errors.New("error"))
})

req, _ := http.NewRequest("GET", "/send", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

assert := assert.New(t)
assert.Equal(http.StatusInternalServerError, w.Code)
assert.Equal("", string(w.Body.Bytes()))
}

func TestBuildFilter(t *testing.T) {
filter := api.BuildFilter(&gin.Context{
Request: &http.Request{
URL: &url.URL{
RawQuery: "labels=env:test&labels=app:nginx&labels=invalid&exclude=kyverno:Pod&exclude=kyverno:Job&exclude=kyverno&status=pass&namespaced=true",
},
},
})

assert := assert.New(t)
assert.Equal(db.Filter{
ReportLabel: map[string]string{
"env": "test",
"app": "nginx",
},
Exclude: map[string][]string{
"kyverno": {"Pod", "Job"},
},
Status: []string{"pass"},
Namespaced: true,
}, filter)
}

func TestPaginationDefaults(t *testing.T) {
pagination := api.BuildPagination(&gin.Context{
Request: &http.Request{
URL: &url.URL{
RawQuery: "",
},
},
}, []string{"namespace", "source"})

assert := assert.New(t)
assert.Equal(db.Pagination{
Page: 0,
Offset: 0,
SortBy: []string{"namespace", "source"},
Direction: "ASC",
}, pagination)
}

func TestPaginationFromURL(t *testing.T) {
pagination := api.BuildPagination(&gin.Context{
Request: &http.Request{
URL: &url.URL{
RawQuery: "page=5&offset=10&direction=desc&sortBy=namespace&sortBy=kind",
},
},
}, []string{"namespace", "source"})

assert := assert.New(t)
assert.Equal(db.Pagination{
Page: 5,
Offset: 10,
SortBy: []string{"namespace", "kind"},
Direction: "DESC",
}, pagination)
}

0 comments on commit 92a8bde

Please sign in to comment.