mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
* adding corrections to cmd Signed-off-by: David Wertenteil <dwertent@armosec.io> * remove decorative line Signed-off-by: David Wertenteil <dwertent@armosec.io> * wip: changed results indicator Signed-off-by: David Wertenteil <dwertent@armosec.io> * replace status test with icons Signed-off-by: David Wertenteil <dwertent@armosec.io> * print workloads in a different line Signed-off-by: David Wertenteil <dwertent@armosec.io> * update display Signed-off-by: David Wertenteil <dwertent@armosec.io> * deprecate commands Signed-off-by: David Wertenteil <dwertent@armosec.io> * removed unused functions Signed-off-by: David Wertenteil <dwertent@armosec.io> * fixed tests Signed-off-by: David Wertenteil <dwertent@armosec.io> * update cloud provider detection Signed-off-by: David Wertenteil <dwertent@armosec.io> * rename column name Signed-off-by: David Wertenteil <dwertent@armosec.io> --------- Signed-off-by: David Wertenteil <dwertent@armosec.io>
335 lines
8.1 KiB
Go
335 lines
8.1 KiB
Go
package cautils
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/armosec/armoapi-go/armotypes"
|
|
"github.com/kubescape/opa-utils/reporthandling"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestIsControlFitToScanScope(t *testing.T) {
|
|
tests := []struct {
|
|
scanInfo *ScanInfo
|
|
Control reporthandling.Control
|
|
expected_res bool
|
|
}{
|
|
{
|
|
scanInfo: &ScanInfo{
|
|
InputPatterns: []string{
|
|
"./testdata/any_file_for_test.json",
|
|
},
|
|
},
|
|
Control: reporthandling.Control{
|
|
ScanningScope: &reporthandling.ScanningScope{
|
|
Matches: []reporthandling.ScanningScopeType{
|
|
reporthandling.ScopeFile,
|
|
},
|
|
},
|
|
},
|
|
expected_res: true,
|
|
},
|
|
{
|
|
scanInfo: &ScanInfo{
|
|
InputPatterns: []string{
|
|
"./testdata/any_file_for_test.json",
|
|
},
|
|
},
|
|
Control: reporthandling.Control{
|
|
ScanningScope: &reporthandling.ScanningScope{
|
|
|
|
Matches: []reporthandling.ScanningScopeType{
|
|
reporthandling.ScopeCluster,
|
|
reporthandling.ScopeFile,
|
|
},
|
|
},
|
|
},
|
|
expected_res: true,
|
|
},
|
|
{
|
|
scanInfo: &ScanInfo{},
|
|
Control: reporthandling.Control{
|
|
ScanningScope: &reporthandling.ScanningScope{
|
|
|
|
Matches: []reporthandling.ScanningScopeType{
|
|
reporthandling.ScopeCluster,
|
|
},
|
|
},
|
|
},
|
|
expected_res: true,
|
|
},
|
|
{
|
|
scanInfo: &ScanInfo{
|
|
InputPatterns: []string{
|
|
"./testdata/any_file_for_test.json",
|
|
},
|
|
},
|
|
Control: reporthandling.Control{
|
|
ScanningScope: &reporthandling.ScanningScope{
|
|
|
|
Matches: []reporthandling.ScanningScopeType{
|
|
reporthandling.ScopeCloudGKE,
|
|
},
|
|
},
|
|
},
|
|
expected_res: false,
|
|
},
|
|
{
|
|
scanInfo: &ScanInfo{},
|
|
Control: reporthandling.Control{
|
|
ScanningScope: &reporthandling.ScanningScope{
|
|
Matches: []reporthandling.ScanningScopeType{
|
|
reporthandling.ScopeCloudEKS,
|
|
},
|
|
},
|
|
},
|
|
expected_res: false,
|
|
},
|
|
{
|
|
scanInfo: &ScanInfo{},
|
|
Control: reporthandling.Control{
|
|
ScanningScope: &reporthandling.ScanningScope{
|
|
Matches: []reporthandling.ScanningScopeType{
|
|
reporthandling.ScopeCloud,
|
|
},
|
|
},
|
|
},
|
|
expected_res: false,
|
|
}}
|
|
for i := range tests {
|
|
assert.Equal(t, tests[i].expected_res, isControlFitToScanScope(tests[i].Control, GetScanningScope(tests[i].scanInfo)), fmt.Sprintf("tests_true index %d", i))
|
|
}
|
|
}
|
|
|
|
func TestIsScanningScopeMatchToControlScope(t *testing.T) {
|
|
tests := []struct {
|
|
scanScope reporthandling.ScanningScopeType
|
|
controlScope reporthandling.ScanningScopeType
|
|
expected bool
|
|
}{
|
|
{
|
|
scanScope: reporthandling.ScopeFile,
|
|
controlScope: reporthandling.ScopeFile,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: ScopeCluster,
|
|
controlScope: ScopeCluster,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloud,
|
|
controlScope: reporthandling.ScopeCloud,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudAKS,
|
|
controlScope: reporthandling.ScopeCloudAKS,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudEKS,
|
|
controlScope: reporthandling.ScopeCloudEKS,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudGKE,
|
|
controlScope: reporthandling.ScopeCloudGKE,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: ScopeCluster,
|
|
controlScope: reporthandling.ScopeCloud,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloud,
|
|
controlScope: ScopeCluster,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudAKS,
|
|
controlScope: ScopeCluster,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudEKS,
|
|
controlScope: ScopeCluster,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudGKE,
|
|
controlScope: ScopeCluster,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloud,
|
|
controlScope: reporthandling.ScopeCloudAKS,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudAKS,
|
|
controlScope: reporthandling.ScopeCloud,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudEKS,
|
|
controlScope: reporthandling.ScopeCloud,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudGKE,
|
|
controlScope: reporthandling.ScopeCloud,
|
|
expected: true,
|
|
},
|
|
{
|
|
scanScope: ScopeCluster,
|
|
controlScope: reporthandling.ScopeCloudAKS,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: ScopeCluster,
|
|
controlScope: reporthandling.ScopeCloudEKS,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: ScopeCluster,
|
|
controlScope: reporthandling.ScopeCloudGKE,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeFile,
|
|
controlScope: ScopeCluster,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeFile,
|
|
controlScope: reporthandling.ScopeCloud,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeFile,
|
|
controlScope: reporthandling.ScopeCloudAKS,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeFile,
|
|
controlScope: reporthandling.ScopeCloudEKS,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeFile,
|
|
controlScope: reporthandling.ScopeCloudGKE,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloud,
|
|
controlScope: reporthandling.ScopeCloudEKS,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloud,
|
|
controlScope: reporthandling.ScopeCloudGKE,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudAKS,
|
|
controlScope: reporthandling.ScopeCloudEKS,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudAKS,
|
|
controlScope: reporthandling.ScopeCloudGKE,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudEKS,
|
|
controlScope: reporthandling.ScopeCloudAKS,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudEKS,
|
|
controlScope: reporthandling.ScopeCloudGKE,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudGKE,
|
|
controlScope: reporthandling.ScopeCloudAKS,
|
|
expected: false,
|
|
},
|
|
{
|
|
scanScope: reporthandling.ScopeCloudGKE,
|
|
controlScope: reporthandling.ScopeCloudEKS,
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
result := isScanningScopeMatchToControlScope(test.scanScope, test.controlScope)
|
|
assert.Equal(t, test.expected, result, fmt.Sprintf("scanScope: %v, controlScope: %v", test.scanScope, test.controlScope))
|
|
}
|
|
}
|
|
|
|
func TestIsFrameworkFitToScanScope(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
framework reporthandling.Framework
|
|
scanScopeMatch reporthandling.ScanningScopeType
|
|
want bool
|
|
}{
|
|
{
|
|
name: "Framework with nil ScanningScope should return true",
|
|
framework: reporthandling.Framework{
|
|
PortalBase: armotypes.PortalBase{
|
|
Name: "test-framework",
|
|
},
|
|
},
|
|
scanScopeMatch: reporthandling.ScopeFile,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "Framework with empty ScanningScope.Matches should return true",
|
|
framework: reporthandling.Framework{
|
|
PortalBase: armotypes.PortalBase{
|
|
Name: "test-framework",
|
|
}, ScanningScope: &reporthandling.ScanningScope{},
|
|
},
|
|
scanScopeMatch: reporthandling.ScopeFile,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "Framework with matching ScanningScope.Matches should return true",
|
|
framework: reporthandling.Framework{
|
|
PortalBase: armotypes.PortalBase{
|
|
Name: "test-framework",
|
|
}, ScanningScope: &reporthandling.ScanningScope{
|
|
Matches: []reporthandling.ScanningScopeType{reporthandling.ScopeFile},
|
|
},
|
|
},
|
|
scanScopeMatch: reporthandling.ScopeFile,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "Framework with non-matching ScanningScope.Matches should return false",
|
|
framework: reporthandling.Framework{
|
|
PortalBase: armotypes.PortalBase{
|
|
Name: "test-framework",
|
|
}, ScanningScope: &reporthandling.ScanningScope{
|
|
Matches: []reporthandling.ScanningScopeType{reporthandling.ScopeCluster},
|
|
},
|
|
},
|
|
scanScopeMatch: reporthandling.ScopeFile,
|
|
want: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := isFrameworkFitToScanScope(tt.framework, tt.scanScopeMatch); got != tt.want {
|
|
t.Errorf("isFrameworkFitToScanScope() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|