Skip to content

Commit

Permalink
Adding units
Browse files Browse the repository at this point in the history
Signed-off-by: David Wertenteil <[email protected]>
  • Loading branch information
David Wertenteil committed Feb 5, 2023
1 parent 753fd76 commit f144cee
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 1 deletion.
40 changes: 40 additions & 0 deletions reporthandling/apis/statuses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestCompare(t *testing.T) {
assert.Equal(t, StatusFailed, Compare(StatusPassed, StatusFailed))
assert.Equal(t, StatusSkipped, Compare(StatusSkipped, StatusPassed))
assert.Equal(t, StatusPassed, Compare(StatusPassed, StatusPassed))
assert.Equal(t, StatusUnknown, Compare(StatusUnknown, StatusUnknown))
}

func TestCompareStatusAndSubStatus(t *testing.T) {
Expand All @@ -29,3 +30,42 @@ func TestCompareStatusAndSubStatus(t *testing.T) {
assert.Equal(t, makeIS(StatusSkipped, SubStatusManualReview), makeIS(CompareStatusAndSubStatus(StatusPassed, StatusSkipped, SubStatusUnknown, SubStatusManualReview)))
assert.Equal(t, makeIS(StatusSkipped, SubStatusRequiresReview), makeIS(CompareStatusAndSubStatus(StatusPassed, StatusSkipped, SubStatusUnknown, SubStatusRequiresReview)))
}

func TestConvertStatusToNewStatus(t *testing.T) {
tests := []struct {
name string
status ScanningStatus
expected ScanningStatus
sub ScanningSubStatus
}{
{
name: "StatusExcluded",
status: StatusExcluded,
expected: StatusPassed,
sub: SubStatusException,
},
{
name: "StatusIrrelevant",
status: StatusIrrelevant,
expected: StatusPassed,
sub: SubStatusIrrelevant,
},
{
name: "StatusPassed",
status: StatusPassed,
expected: StatusPassed,
sub: SubStatusUnknown,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actualStatus, actualSub := ConvertStatusToNewStatus(test.status)
if actualStatus != test.expected {
t.Errorf("Expected status %s, but got %s", test.expected, actualStatus)
}
if actualSub != test.sub {
t.Errorf("Expected sub status %s, but got %s", test.sub, actualSub)
}
})
}
}
46 changes: 46 additions & 0 deletions reporthandling/apis/statusinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package apis

import "testing"

func TestIsPassed(t *testing.T) {
s := &StatusInfo{InnerStatus: StatusPassed}
if !s.IsPassed() {
t.Fatalf("Expected status to be passed, got %v", s.InnerStatus)
}
}

func TestIsFailed(t *testing.T) {
s := &StatusInfo{InnerStatus: StatusFailed}
if !s.IsFailed() {
t.Fatalf("Expected status to be failed, got %v", s.InnerStatus)
}
}

func TestIsSkipped(t *testing.T) {
s := &StatusInfo{InnerStatus: StatusSkipped}
if !s.IsSkipped() {
t.Fatalf("Expected status to be skipped, got %v", s.InnerStatus)
}
}

func TestGetSubStatus(t *testing.T) {
s := &StatusInfo{SubStatus: SubStatusException}
if s.GetSubStatus() != SubStatusException {
t.Fatalf("Expected sub-status to be %v, got %v", SubStatusException, s.GetSubStatus())
}
}

func TestStatus(t *testing.T) {
s := &StatusInfo{InnerStatus: StatusPassed}
if s.Status() != StatusPassed {
t.Fatalf("Expected status to be %v, got %v", StatusPassed, s.Status())
}
}

func TestInfo(t *testing.T) {
info := "Test info"
s := &StatusInfo{InnerInfo: info}
if s.Info() != info {
t.Fatalf("Expected info to be %v, got %v", info, s.Info())
}
}
135 changes: 135 additions & 0 deletions reporthandling/helpers/v1/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package v1

import (
"testing"

"github.com/kubescape/opa-utils/reporthandling/apis"
)

func TestNewStatus(t *testing.T) {
tests := []struct {
name string
status apis.ScanningStatus
want apis.ScanningStatus
}{
{
name: "Test passed status",
status: apis.StatusPassed,
want: apis.StatusPassed,
},
{
name: "Test failed status",
status: apis.StatusFailed,
want: apis.StatusFailed,
},
{
name: "Test skipped status",
status: apis.StatusSkipped,
want: apis.StatusSkipped,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewStatus(tt.status)
if got := s.Status(); got != tt.want {
t.Errorf("NewStatus().Status() = %v, want %v", got, tt.want)
}
})
}
}

func TestNewStatusInfo(t *testing.T) {
tests := []struct {
name string
status apis.ScanningStatus
subStatus apis.ScanningSubStatus
info string
wantStatus apis.ScanningStatus
wantSubStatus apis.ScanningSubStatus
wantInfo string
}{
{
name: "Test passed status with ignore sub status and info",
status: apis.StatusPassed,
subStatus: apis.SubStatusException,
info: string(apis.SubStatusConfigurationInfo),
wantStatus: apis.StatusPassed,
wantSubStatus: apis.SubStatusException,
wantInfo: string(apis.SubStatusConfigurationInfo),
},
{
name: "Test failed status and info",
status: apis.StatusFailed,
subStatus: "",
info: string(apis.SubStatusManualReviewInfo),
wantStatus: apis.StatusFailed,
wantSubStatus: "",
wantInfo: string(apis.SubStatusManualReviewInfo),
},
{
name: "Test skipped status with irrelevant sub status without info",
status: apis.StatusSkipped,
subStatus: apis.SubStatusConfiguration,
info: "",
wantStatus: apis.StatusSkipped,
wantSubStatus: apis.SubStatusConfiguration,
wantInfo: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewStatusInfo(tt.status, tt.subStatus, tt.info)
if got := s.Status(); got != tt.wantStatus {
t.Errorf("NewStatusInfo().Status() = %v, want %v", got, tt.wantStatus)
}
if got := s.GetSubStatus(); got != tt.wantSubStatus {
t.Errorf("NewStatusInfo().GetSubStatus() = %v, want %v", got, tt.wantSubStatus)
}
if got := s.Info(); got != tt.wantInfo {
t.Errorf("NewStatusInfo().Info() = %v, want %v", got, tt.wantInfo)
}
})
}
}

func TestGetSubStatus(t *testing.T) {
status := &Status{subStatus: apis.SubStatusIntegration}
if status.GetSubStatus() != apis.SubStatusIntegration {
t.Errorf("Expected subStatus to be %s, got %s", apis.SubStatusIntegration, status.GetSubStatus())
}
}

func TestStatus(t *testing.T) {
status := &Status{status: apis.StatusFailed}
if status.Status() != apis.StatusFailed {
t.Errorf("Expected status to be %s, got %s", apis.StatusFailed, status.Status())
}
}

func TestInfo(t *testing.T) {
status := &Status{}
if status.Info() != "" {
t.Errorf("Expected Info to be empty string, got %s", status.Info())
}
}

func TestIsPassed(t *testing.T) {
status := &Status{status: apis.StatusPassed}
if !status.IsPassed() {
t.Errorf("Expected IsPassed to be true, got false")
}
}

func TestIsFailed(t *testing.T) {
status := &Status{status: apis.StatusFailed}
if !status.IsFailed() {
t.Errorf("Expected IsFailed to be true, got false")
}
}

func TestIsSkipped(t *testing.T) {
status := &Status{status: apis.StatusSkipped}
if !status.IsSkipped() {
t.Errorf("Expected IsSkipped to be true, got false")
}
}
103 changes: 103 additions & 0 deletions reporthandling/results/v1/reportsummary/resourcecounters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reportsummary
import (
"testing"

"github.com/kubescape/opa-utils/reporthandling/apis"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -48,3 +49,105 @@ func TestNumberOfAll(t *testing.T) {
setResourcesCountersMock()
assert.Equal(t, 27, resourcesCounter.All())
}

func TestStatusCounters_Increase(t *testing.T) {
tests := []struct {
resourceCounters *StatusCounters
name string
status apis.ScanningStatus
expectedPassed int
expectedFailed int
expectedSkipped int
expectedExcluded int
}{
{
name: "Test passed status",
status: apis.StatusPassed,
resourceCounters: &StatusCounters{
FailedResources: 1,
SkippedResources: 2,
PassedResources: 3,
},
expectedFailed: 1,
expectedSkipped: 2,
expectedPassed: 4,
},
{
name: "Test failed status",
status: apis.StatusFailed,
resourceCounters: &StatusCounters{
FailedResources: 1,
SkippedResources: 2,
PassedResources: 3,
},
expectedFailed: 2,
expectedSkipped: 2,
expectedPassed: 3,
},
{
name: "Test skipped status",
status: apis.StatusSkipped,
resourceCounters: &StatusCounters{
FailedResources: 1,
SkippedResources: 2,
PassedResources: 3,
},
expectedFailed: 1,
expectedSkipped: 3,
expectedPassed: 3,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
status := &apis.StatusInfo{InnerStatus: test.status}
test.resourceCounters.Increase(status)
if test.resourceCounters.PassedResources != test.expectedPassed {
t.Errorf("Expected PassedResources to be %d, but got %d", test.expectedPassed, test.resourceCounters.PassedResources)
}
if test.resourceCounters.FailedResources != test.expectedFailed {
t.Errorf("Expected FailedResources to be %d, but got %d", test.expectedFailed, test.resourceCounters.FailedResources)
}
if test.resourceCounters.SkippedResources != test.expectedSkipped {
t.Errorf("Expected SkippedResources to be %d, but got %d", test.expectedSkipped, test.resourceCounters.SkippedResources)
}
})
}
}

func TestSubStatusCounters_Increase(t *testing.T) {
tests := []struct {
subStatusCounters *SubStatusCounters
name string
status apis.ScanningStatus
subStatus apis.ScanningSubStatus
expectedIgnored int
}{
{
name: "Test ignored and passed status",
status: apis.StatusPassed,
subStatus: apis.SubStatusException,
subStatusCounters: &SubStatusCounters{
IgnoredResources: 1,
},
expectedIgnored: 2,
},
{
name: "Test ignored and failed status",
status: apis.StatusFailed,
subStatus: apis.SubStatusException,
subStatusCounters: &SubStatusCounters{
IgnoredResources: 1,
},
expectedIgnored: 1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
status := &apis.StatusInfo{InnerStatus: test.status, SubStatus: test.subStatus}
test.subStatusCounters.Increase(status)
if test.subStatusCounters.IgnoredResources != test.expectedIgnored {
t.Errorf("Expected IgnoredResources to be %d, but got %d", test.expectedIgnored, test.subStatusCounters.IgnoredResources)
}
})
}
}
1 change: 0 additions & 1 deletion reporthandling/results/v1/reportsummary/resourceids.go

This file was deleted.

31 changes: 31 additions & 0 deletions reporthandling/results/v1/reportsummary/summarydetails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,37 @@ func TestUpdateControlsSummaryCountersFailed(t *testing.T) {
assert.Equal(t, 0, v.NumberOfResources().Skipped())
}

for _, v := range controls {
statuses, subStatuses := v.ResourcesCounters()
assert.Equal(t, 1, statuses.All())
assert.Equal(t, 1, statuses.Failed())
assert.Equal(t, 0, statuses.Passed())
assert.Equal(t, 0, statuses.Skipped())
assert.Equal(t, 0, subStatuses.All())
assert.Equal(t, 0, subStatuses.Ignored())
}

}

func TestUpdateControlsSummaryCountersExcluded(t *testing.T) {
controls := map[string]ControlSummary{}

passedControls := mockResultsFailed.ListControlsIDs(nil).Passed()
for i := range passedControls {
controls[passedControls[i]] = ControlSummary{}
}

updateControlsSummaryCounters(&mockResultsException, controls, nil)
for _, v := range controls {
statuses, subStatuses := v.ResourcesCounters()
assert.Equal(t, 1, statuses.All())
assert.Equal(t, 0, statuses.Failed())
assert.Equal(t, 1, statuses.Passed())
assert.Equal(t, 0, statuses.Skipped())
assert.Equal(t, 1, subStatuses.All())
assert.Equal(t, 1, subStatuses.Ignored())
}

}
func TestUpdateControlsSummaryCountersPassed(t *testing.T) {
controls := map[string]ControlSummary{}
Expand Down

0 comments on commit f144cee

Please sign in to comment.