Skip to content

Commit

Permalink
Improve Test Coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <[email protected]>
  • Loading branch information
Frank Jogeleit committed Apr 28, 2024
1 parent 48d5c0f commit d67fc61
Show file tree
Hide file tree
Showing 25 changed files with 598 additions and 80 deletions.
3 changes: 2 additions & 1 deletion pkg/api/v1/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package v1_test
import (
"testing"

"github.com/stretchr/testify/assert"

v1 "github.com/kyverno/policy-reporter/pkg/api/v1"
"github.com/kyverno/policy-reporter/pkg/database"
"github.com/stretchr/testify/assert"
)

func TestMapping(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions pkg/api/v2/views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package v2_test
import (
"testing"

"github.com/stretchr/testify/assert"

v2 "github.com/kyverno/policy-reporter/pkg/api/v2"
"github.com/kyverno/policy-reporter/pkg/config"
"github.com/kyverno/policy-reporter/pkg/database"
"github.com/stretchr/testify/assert"
)

func TestV2Views(t *testing.T) {
Expand Down Expand Up @@ -330,7 +331,6 @@ func TestV2Views(t *testing.T) {
Valid: true,
Channels: []*config.Target[config.GCSOptions]{
{

Name: "Target 2",
MinimumPriority: "warning",
Config: &config.GCSOptions{
Expand All @@ -340,7 +340,6 @@ func TestV2Views(t *testing.T) {
Valid: true,
},
{

Name: "Target 2",
MinimumPriority: "warning",
Config: &config.GCSOptions{
Expand All @@ -354,5 +353,4 @@ func TestV2Views(t *testing.T) {

assert.Equal(t, 2, len(targets))
})

}
1 change: 1 addition & 0 deletions pkg/cache/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

goredis "github.com/go-redis/redis/v8"

"github.com/kyverno/policy-reporter/pkg/cache"
"github.com/kyverno/policy-reporter/pkg/fixtures"
)
Expand Down
18 changes: 17 additions & 1 deletion pkg/config/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/client-go/rest"

"github.com/kyverno/policy-reporter/pkg/config"
Expand Down Expand Up @@ -218,6 +219,9 @@ var testConfig = &config.Config{
},
},
Targets: targets,
Logging: config.Logging{
Development: true,
},
}

func Test_ResolveTargets(t *testing.T) {
Expand Down Expand Up @@ -326,7 +330,11 @@ func Test_ResolvePolicyStore(t *testing.T) {
}

func Test_ResolveAPIServer(t *testing.T) {
resolver := config.NewResolver(&config.Config{}, &rest.Config{})
resolver := config.NewResolver(&config.Config{
API: config.API{
BasicAuth: config.BasicAuth{Username: "user", Password: "password"},
},
}, &rest.Config{})

server, _ := resolver.Server(context.Background(), nil)
if server == nil {
Expand Down Expand Up @@ -576,6 +584,14 @@ func Test_ResolveLogger(t *testing.T) {
}
}

func Test_Logger(t *testing.T) {
resolver := config.NewResolver(&config.Config{}, &rest.Config{})

logger, _ := resolver.Logger()

assert.NotNil(t, logger)
}

func Test_ResolveEnableLeaderElection(t *testing.T) {
t.Run("general disabled", func(t *testing.T) {
resolver := config.NewResolver(&config.Config{
Expand Down
12 changes: 7 additions & 5 deletions pkg/config/target_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/kyverno/policy-reporter/pkg/target/http"
"github.com/kyverno/policy-reporter/pkg/target/kinesis"
"github.com/kyverno/policy-reporter/pkg/target/loki"
"github.com/kyverno/policy-reporter/pkg/target/provider/aws"
gs "github.com/kyverno/policy-reporter/pkg/target/provider/gcs"
"github.com/kyverno/policy-reporter/pkg/target/s3"
"github.com/kyverno/policy-reporter/pkg/target/securityhub"
"github.com/kyverno/policy-reporter/pkg/target/slack"
Expand Down Expand Up @@ -431,14 +433,14 @@ func (f *TargetFactory) createS3Client(config, parent *Target[S3Options]) target

config.MapBaseParent(parent)

s3Client := helper.NewS3Client(
s3Client := aws.NewS3Client(
config.Config.AccessKeyID,
config.Config.SecretAccessKey,
config.Config.Region,
config.Config.Endpoint,
config.Config.Bucket,
config.Config.PathStyle,
helper.WithKMS(config.Config.BucketKeyEnabled, &config.Config.KmsKeyID, &config.Config.ServerSideEncryption),
aws.WithKMS(config.Config.BucketKeyEnabled, &config.Config.KmsKeyID, &config.Config.ServerSideEncryption),
)

sugar.Infof("%s configured", config.Name)
Expand Down Expand Up @@ -485,7 +487,7 @@ func (f *TargetFactory) createKinesisClient(config, parent *Target[KinesisOption

config.MapBaseParent(parent)

kinesisClient := helper.NewKinesisClient(
kinesisClient := aws.NewKinesisClient(
config.Config.AccessKeyID,
config.Config.SecretAccessKey,
config.Config.Region,
Expand Down Expand Up @@ -533,7 +535,7 @@ func (f *TargetFactory) createSecurityHub(config, parent *Target[SecurityHubOpti

setInt(&config.Config.DelayInSeconds, parent.Config.DelayInSeconds)

client := helper.NewHubClient(
client := aws.NewHubClient(
config.Config.AccessKeyID,
config.Config.SecretAccessKey,
config.Config.Region,
Expand Down Expand Up @@ -584,7 +586,7 @@ func (f *TargetFactory) createGCSClient(config, parent *Target[GCSOptions]) targ

config.MapBaseParent(parent)

gcsClient := helper.NewGCSClient(
gcsClient := gs.NewClient(
context.Background(),
config.Config.Credentials,
config.Config.Bucket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package v1alpha2_test
import (
"testing"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
)

func TestClusterPolicyReport(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/crd/api/policyreport/v1alpha2/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"encoding/json"
"testing"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
corev1 "k8s.io/api/core/v1"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
)

func TestCommon(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/crd/api/policyreport/v1alpha2/policyreport_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package v1alpha2_test
import (
"testing"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
)

func TestPolicyReport(t *testing.T) {
Expand Down
43 changes: 43 additions & 0 deletions pkg/helper/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package helper_test

import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"

"github.com/kyverno/policy-reporter/pkg/helper"
)

func TestSendJSONResponse(t *testing.T) {
t.Run("success response", func(t *testing.T) {
w := httptest.NewRecorder()

helper.SendJSONResponse(w, []string{"default", "user"}, nil)

assert.Equal(t, http.StatusOK, w.Code)

resp := make([]string, 0, 2)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, []string{"default", "user"}, resp)
})

t.Run("error response", func(t *testing.T) {
w := httptest.NewRecorder()

helper.SendJSONResponse(w, nil, errors.New("error"))

assert.Equal(t, http.StatusInternalServerError, w.Code)

resp := make(map[string]string, 0)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, map[string]string{"message": "error"}, resp)
})
}
29 changes: 4 additions & 25 deletions pkg/helper/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package helper

import (
"encoding/json"
"strings"
)

Expand All @@ -24,19 +23,6 @@ func ToList[T any, R comparable](mapping map[R]T) []T {
return list
}

func Merge[T comparable, R any](first, second map[T]R) map[T]R {
merged := make(map[T]R, len(first)+len(second))

for k, v := range first {
merged[k] = v
}
for k, v := range second {
merged[k] = v
}

return merged
}

func Map[T any, R any](source []T, cb func(T) R) []R {
list := make([]R, 0, len(source))
for _, i := range source {
Expand All @@ -46,17 +32,6 @@ func Map[T any, R any](source []T, cb func(T) R) []R {
return list
}

func ConvertJSONToMap(s string) map[string]string {
m := make(map[string]string)
if s == "" {
return m
}

_ = json.Unmarshal([]byte(s), &m)

return m
}

func ConvertMap(m map[string]any) map[string]string {
n := make(map[string]string, len(m))
for k, v := range m {
Expand All @@ -75,3 +50,7 @@ func Defaults(s, f string) string {

return f
}

func ToPointer[T any](s T) *T {
return &s
}
53 changes: 53 additions & 0 deletions pkg/helper/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package helper_test

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/kyverno/policy-reporter/pkg/helper"
)

func TestContains(t *testing.T) {
assert.True(t, helper.Contains("kyverno", []string{"test", "kyverno", "trivy"}))
assert.False(t, helper.Contains("kube-bench", []string{"test", "kyverno", "trivy"}))
}

func TestToList(t *testing.T) {
result := helper.ToList(map[string]string{
"first": "kyverno",
"second": "trivy",
})

assert.Equal(t, 2, len(result))
assert.Contains(t, result, "kyverno")
assert.Contains(t, result, "trivy")
}

func TestMap(t *testing.T) {
assert.Equal(t, []string{"kyverno", "trivy"}, helper.Map([]string{"source_kyverno", "source_trivy"}, func(value string) string {
return strings.TrimPrefix(value, "source_")
}))
}

func TestConvertMap(t *testing.T) {
assert.Equal(t, map[string]string{"first": "kyverno", "second": "trivy"}, helper.ConvertMap(map[string]any{
"first": "kyverno",
"second": "trivy",
"third": 3,
}))
}

func TestDetauls(t *testing.T) {
assert.Equal(t, "fallback", helper.Defaults("", "fallback"))
assert.Equal(t, "value", helper.Defaults("value", "fallback"))
}

func TestToPointer(t *testing.T) {
value := "test"
number := 5

assert.Equal(t, &value, helper.ToPointer(value))
assert.Equal(t, &number, helper.ToPointer(number))
}
Loading

0 comments on commit d67fc61

Please sign in to comment.