Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <frank.jogeleit@lovoo.com>
  • Loading branch information
fjogeleit committed Apr 24, 2024
1 parent 5bf9e4b commit feb0548
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (r *ClusterPolicyReport) GetID() string {
}

func (r *ClusterPolicyReport) GetKinds() []string {
if r.GetScope() != nil {
return []string{r.Scope.Kind}
}

list := make([]string, 0)
for _, k := range r.Results {
if !k.HasResource() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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"
)

func TestClusterPolicyReport(t *testing.T) {
t.Run("GetSource Fallback", func(t *testing.T) {
cpolr := &v1alpha2.ClusterPolicyReport{}

if s := cpolr.GetSource(); s != "" {
t.Errorf("expected empty source, got: %s", s)
}
})

t.Run("GetSource From Result", func(t *testing.T) {
cpolr := &v1alpha2.ClusterPolicyReport{Results: []v1alpha2.PolicyReportResult{{Source: "Kyverno"}}}

if s := cpolr.GetSource(); s != "Kyverno" {
t.Errorf("expected 'Kyverno' as source, got: %s", s)
}
})

t.Run("GetID", func(t *testing.T) {
cpolr := &v1alpha2.ClusterPolicyReport{ObjectMeta: v1.ObjectMeta{Name: "cpolr-namespace-default"}}

if s := cpolr.GetID(); s != "762077495299640259" {
t.Errorf("unexpected ID, expected '762077495299640259', got: %s", s)
}
})

t.Run("GetKinds from Scope", func(t *testing.T) {
cpolr := &v1alpha2.ClusterPolicyReport{Scope: &corev1.ObjectReference{Kind: "Deployment"}}

if len(cpolr.GetKinds()) != 1 && cpolr.GetKinds()[0] != "Deployment" {
t.Errorf("expected Deployment, got: %s", cpolr.GetKinds()[0])
}
})

t.Run("GetKinds from Results", func(t *testing.T) {
cpolr := &v1alpha2.ClusterPolicyReport{Results: []v1alpha2.PolicyReportResult{
{},
{Resources: []corev1.ObjectReference{{Kind: "Pod"}}},
{Resources: []corev1.ObjectReference{{Kind: "Pod"}}},
{Resources: []corev1.ObjectReference{{Kind: "Deployment"}}},
}}

if len(cpolr.GetKinds()) != 2 && cpolr.GetKinds()[1] != "Deployment" {
t.Errorf("expected Deployment, got: %s", cpolr.GetKinds()[1])
}
})

t.Run("GetSeverities from Results", func(t *testing.T) {
cpolr := &v1alpha2.ClusterPolicyReport{Results: []v1alpha2.PolicyReportResult{
{Severity: v1alpha2.SeverityHigh},
{Severity: v1alpha2.SeverityHigh},
{Severity: v1alpha2.SeverityCritical},
}}

if len(cpolr.GetSeverities()) != 2 && cpolr.GetSeverities()[1] != "critical" {
t.Errorf("expected critical severity, got: %s", cpolr.GetSeverities()[1])
}
})
}
8 changes: 0 additions & 8 deletions pkg/crd/api/policyreport/v1alpha2/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package v1alpha2

import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -152,13 +151,6 @@ type PolicyReportSummary struct {
Skip int `json:"skip"`
}

func (prs PolicyReportSummary) ToMap() map[string]interface{} {
b, _ := json.Marshal(&prs)
var m map[string]interface{}
_ = json.Unmarshal(b, &m)
return m
}

// +kubebuilder:validation:Enum=pass;fail;warn;error;skip

// PolicyResult has one of the following values:
Expand Down
185 changes: 185 additions & 0 deletions pkg/crd/api/policyreport/v1alpha2/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package v1alpha2_test

import (
"encoding/json"
"testing"

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

func TestCommon(t *testing.T) {
t.Run("Priority.String", func(t *testing.T) {
if v1alpha2.DefaultPriority.String() != "" {
t.Error("unexpected default priority mapping")
}

if v1alpha2.DebugPriority.String() != "debug" {
t.Error("unexpected debug priority mapping")
}

if v1alpha2.InfoPriority.String() != "info" {
t.Error("unexpected info mapping")
}

if v1alpha2.WarningPriority.String() != "warning" {
t.Error("unexpected warning mapping")
}

if v1alpha2.ErrorPriority.String() != "error" {
t.Error("unexpected error mapping")
}

if v1alpha2.CriticalPriority.String() != "critical" {
t.Error("unexpected critical mapping")
}
})

t.Run("Priority.MarshalJSON", func(t *testing.T) {
v, err := json.Marshal(v1alpha2.WarningPriority)
if err != nil {
t.Fatalf("unexpected marshal error: %s", err.Error())
}

if string(v) != `"warning"` {
t.Fatalf("unexpected marshal value: %s", v)
}
})

t.Run("NewPriority", func(t *testing.T) {
if v1alpha2.NewPriority("") != v1alpha2.DefaultPriority {
t.Error("unexpected prioriry created")
}

if v1alpha2.NewPriority("debug") != v1alpha2.DebugPriority {
t.Error("unexpected prioriry created")
}

if v1alpha2.NewPriority("info") != v1alpha2.InfoPriority {
t.Error("unexpected prioriry created")
}

if v1alpha2.NewPriority("warning") != v1alpha2.WarningPriority {
t.Error("unexpected prioriry created")
}

if v1alpha2.NewPriority("error") != v1alpha2.ErrorPriority {
t.Error("unexpected prioriry created")
}

if v1alpha2.NewPriority("critical") != v1alpha2.CriticalPriority {
t.Error("unexpected prioriry created")
}
})

t.Run("PriorityFromSeverity", func(t *testing.T) {
if v1alpha2.PriorityFromSeverity(v1alpha2.SeverityCritical) != v1alpha2.CriticalPriority {
t.Error("unexpected prioriry created")
}

if v1alpha2.PriorityFromSeverity(v1alpha2.SeverityHigh) != v1alpha2.ErrorPriority {
t.Error("unexpected prioriry created")
}

if v1alpha2.PriorityFromSeverity(v1alpha2.SeverityMedium) != v1alpha2.WarningPriority {
t.Error("unexpected prioriry created")
}

if v1alpha2.PriorityFromSeverity(v1alpha2.SeverityInfo) != v1alpha2.InfoPriority {
t.Error("unexpected prioriry created")
}

if v1alpha2.PriorityFromSeverity(v1alpha2.SeverityLow) != v1alpha2.InfoPriority {
t.Error("unexpected prioriry created")
}
if v1alpha2.PriorityFromSeverity("") != v1alpha2.DebugPriority {
t.Error("unexpected prioriry created")
}
})
}

func TestPolicyReportResul(t *testing.T) {
t.Run("GetResource Without Resources", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{}

if r.GetResource() != nil {
t.Error("expected nil resource for empty result")
}
})
t.Run("GetResource With Resources", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{Resources: []corev1.ObjectReference{{Name: "test"}}}

if r.GetResource().Name != "test" {
t.Error("expected result resource returned")
}
})
t.Run("GetKind Without Resource", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{}

if r.GetKind() != "" {
t.Error("expected result kind to be empty string")
}
})
t.Run("GetKind", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{Resources: []corev1.ObjectReference{{Name: "test", Kind: "Pod"}}}

if r.GetKind() != "Pod" {
t.Error("expected result kind to be Pod")
}
})
t.Run("GetID from Result With Resource", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{Resources: []corev1.ObjectReference{{Name: "test", Kind: "Pod"}}}

if r.GetID() != "18007334074686647077" {
t.Errorf("expected result kind to be '18007334074686647077', got :%s", r.GetID())
}
})
t.Run("GetID from Result With ID Property", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{Resources: []corev1.ObjectReference{{Name: "test", Kind: "Pod"}}, Properties: map[string]string{"resultID": "result-id"}}

if r.GetID() != "result-id" {
t.Errorf("expected result kind to be 'result-id', got :%s", r.GetID())
}
})
t.Run("GetID cached", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{Resources: []corev1.ObjectReference{{Name: "test", Kind: "Pod"}}, Properties: map[string]string{"resultID": "result-id"}}

if r.GetID() != "result-id" {
t.Errorf("expected result kind to be 'result-id', got :%s", r.GetID())
}

r.Properties["resultID"] = "test"

if r.GetID() != "result-id" {
t.Errorf("expected result ID doesn't change, got :%s", r.GetID())
}
})
t.Run("ToResourceString with Namespace and Kind", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{Resources: []corev1.ObjectReference{{Name: "test", Namespace: "default", Kind: "Pod"}}}

if r.ResourceString() != "default/pod/test" {
t.Errorf("expected result resource string 'default/pod/name', got: %s", r.ResourceString())
}
})
t.Run("ToResourceString with Kind", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{Resources: []corev1.ObjectReference{{Name: "test", Kind: "Namespace"}}}

if r.ResourceString() != "namespace/test" {
t.Errorf("expected result resource string 'namespace/test', got: %s", r.ResourceString())
}
})
t.Run("ToResourceString with Name", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{Resources: []corev1.ObjectReference{{Name: "test"}}}

if r.ResourceString() != "test" {
t.Errorf("expected result resource string 'test', got :%s", r.ResourceString())
}
})
t.Run("ToResourceString Without Resource", func(t *testing.T) {
r := &v1alpha2.PolicyReportResult{}

if r.ResourceString() != "" {
t.Errorf("expected result resource string to be empty, got :%s", r.ResourceString())
}
})
}
4 changes: 4 additions & 0 deletions pkg/crd/api/policyreport/v1alpha2/policyreport_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func (r *PolicyReport) GetSource() string {
}

func (r *PolicyReport) GetKinds() []string {
if r.GetScope() != nil {
return []string{r.Scope.Kind}
}

list := make([]string, 0)
for _, k := range r.Results {
if !k.HasResource() {
Expand Down
68 changes: 68 additions & 0 deletions pkg/crd/api/policyreport/v1alpha2/policyreport_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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"
)

func TestPolicyReport(t *testing.T) {
t.Run("GetSource Fallback", func(t *testing.T) {
cpolr := &v1alpha2.PolicyReport{}

if s := cpolr.GetSource(); s != "" {
t.Errorf("expected empty source, got: %s", s)
}
})

t.Run("GetSource From Result", func(t *testing.T) {
cpolr := &v1alpha2.PolicyReport{Results: []v1alpha2.PolicyReportResult{{Source: "Kyverno"}}}

if s := cpolr.GetSource(); s != "Kyverno" {
t.Errorf("expected 'Kyverno' as source, got: %s", s)
}
})

t.Run("GetID", func(t *testing.T) {
cpolr := &v1alpha2.PolicyReport{ObjectMeta: v1.ObjectMeta{Name: "polr-pod-nginx", Namespace: "default"}}

if s := cpolr.GetID(); s != "1687999035284166534" {
t.Errorf("unexpected ID, expected '1687999035284166534', got: %s", s)
}
})

t.Run("GetKinds from Scope", func(t *testing.T) {
cpolr := &v1alpha2.PolicyReport{Scope: &corev1.ObjectReference{Kind: "Deployment"}}

if len(cpolr.GetKinds()) != 1 && cpolr.GetKinds()[0] != "Deployment" {
t.Errorf("expected Deployment, got: %s", cpolr.GetKinds()[0])
}
})

t.Run("GetKinds from Results", func(t *testing.T) {
cpolr := &v1alpha2.PolicyReport{Results: []v1alpha2.PolicyReportResult{
{},
{Resources: []corev1.ObjectReference{{Kind: "Pod"}}},
{Resources: []corev1.ObjectReference{{Kind: "Pod"}}},
{Resources: []corev1.ObjectReference{{Kind: "Deployment"}}},
}}

if len(cpolr.GetKinds()) != 2 && cpolr.GetKinds()[1] != "Deployment" {
t.Errorf("expected Deployment, got: %s", cpolr.GetKinds()[1])
}
})

t.Run("GetSeverities from Results", func(t *testing.T) {
cpolr := &v1alpha2.PolicyReport{Results: []v1alpha2.PolicyReportResult{
{Severity: v1alpha2.SeverityHigh},
{Severity: v1alpha2.SeverityHigh},
{Severity: v1alpha2.SeverityCritical},
}}

if len(cpolr.GetSeverities()) != 2 && cpolr.GetSeverities()[1] != "critical" {
t.Errorf("expected critical severity, got: %s", cpolr.GetSeverities()[1])
}
})
}

0 comments on commit feb0548

Please sign in to comment.