-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Frank Jogeleit <[email protected]>
- Loading branch information
Frank Jogeleit
committed
Apr 24, 2024
1 parent
5bf9e4b
commit feb0548
Showing
6 changed files
with
329 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
pkg/crd/api/policyreport/v1alpha2/clusterpolicyreport_types_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
pkg/crd/api/policyreport/v1alpha2/policyreport_types_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
}) | ||
} |