mirror of
https://github.com/kubescape/kubescape.git
synced 2026-02-14 18:09:55 +00:00
Adding tests for core/pkg/resourcesprioritization
Signed-off-by: Mehdi Moussaif <m.moussaif42@gmail.com>
This commit is contained in:
@@ -42,10 +42,13 @@ func (mock *AttackTracksGetterMock) GetAttackTracks() ([]v1alpha1.AttackTrack, e
|
||||
mock_2 := v1alpha1.GetAttackTrackMock(v1alpha1.AttackTrackStep{
|
||||
Name: "Z",
|
||||
})
|
||||
mock_3 := v1alpha1.GetAttackTrackMock(v1alpha1.AttackTrackStep{})
|
||||
m1 := mock_1.(*v1alpha1.AttackTrack)
|
||||
m2 := mock_2.(*v1alpha1.AttackTrack)
|
||||
m3 := mock_3.(*v1alpha1.AttackTrack)
|
||||
m2.Metadata["name"] = "TestAttackTrack_2"
|
||||
return []v1alpha1.AttackTrack{*m1, *m2}, nil
|
||||
m3.Metadata["name"] = "TestAttackTrack_3"
|
||||
return []v1alpha1.AttackTrack{*m1, *m2, *m3}, nil
|
||||
}
|
||||
|
||||
func ControlMock(id string, baseScore float32, tags, categories []string) reporthandling.Control {
|
||||
@@ -103,9 +106,10 @@ func ResourceAssociatedControlMock(controlID string, status apis.ScanningStatus)
|
||||
func TestNewResourcesPrioritizationHandler(t *testing.T) {
|
||||
handler, err := NewResourcesPrioritizationHandler(context.TODO(), &AttackTracksGetterMock{}, false)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, handler.attackTracks, 2)
|
||||
assert.Len(t, handler.attackTracks, 3)
|
||||
assert.Equal(t, handler.attackTracks[0].GetName(), "TestAttackTrack")
|
||||
assert.Equal(t, handler.attackTracks[1].GetName(), "TestAttackTrack_2")
|
||||
assert.Equal(t, handler.attackTracks[2].GetName(), "TestAttackTrack_3")
|
||||
}
|
||||
|
||||
func TestResourcesPrioritizationHandler_PrioritizeResources(t *testing.T) {
|
||||
@@ -219,3 +223,125 @@ func TestResourcesPrioritizationHandler_isSupportedKind(t *testing.T) {
|
||||
assert.False(t, handler.isSupportedKind(WorkloadMockWithKind("ConfigMap")))
|
||||
assert.False(t, handler.isSupportedKind(WorkloadMockWithKind("ServiceAccount")))
|
||||
}
|
||||
|
||||
type AttackTrackControlsLookupMock struct {
|
||||
lookup map[string]map[string][]v1alpha1.IAttackTrackControl
|
||||
}
|
||||
|
||||
func (mock *AttackTrackControlsLookupMock) GetAssociatedControls(attackTrack, category string) []v1alpha1.IAttackTrackControl {
|
||||
return mock.lookup[attackTrack][category]
|
||||
}
|
||||
|
||||
func (mock *AttackTrackControlsLookupMock) HasAssociatedControls(attackTrack string) bool {
|
||||
return len(mock.lookup[attackTrack]) > 0
|
||||
}
|
||||
|
||||
type AttackTrackControlMock struct {
|
||||
id string
|
||||
baseScore float64
|
||||
categories []string
|
||||
}
|
||||
|
||||
func (mock *AttackTrackControlMock) GetControlId() string {
|
||||
return mock.id
|
||||
}
|
||||
|
||||
func (mock *AttackTrackControlMock) GetScore() float64 {
|
||||
return mock.baseScore
|
||||
}
|
||||
|
||||
func (mock *AttackTrackControlMock) GetAttackTrackCategories(attackTrack string) []string {
|
||||
return mock.categories
|
||||
}
|
||||
|
||||
func (mock *AttackTrackControlMock) GetControlTypeTags() []string {
|
||||
return []string{"security"}
|
||||
}
|
||||
|
||||
func (mock *AttackTrackControlMock) GetSeverity() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func NewAttackTrackControlsLookupMock() *AttackTrackControlsLookupMock {
|
||||
return &AttackTrackControlsLookupMock{
|
||||
lookup: map[string]map[string][]v1alpha1.IAttackTrackControl{
|
||||
"A": {
|
||||
"security": {
|
||||
&AttackTrackControlMock{id: "C-001", baseScore: 3, categories: []string{"D"}},
|
||||
&AttackTrackControlMock{id: "C-002", baseScore: 4, categories: []string{"B", "C"}},
|
||||
},
|
||||
"compliance": {
|
||||
&AttackTrackControlMock{id: "C-003", baseScore: 10, categories: []string{"E"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourcesPrioritizationHandler_copyAttackTrack(t *testing.T) {
|
||||
handler := &ResourcesPrioritizationHandler{}
|
||||
type args struct {
|
||||
attackTrack v1alpha1.IAttackTrack
|
||||
lookup v1alpha1.IAttackTrackControlsLookup
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want v1alpha1.IAttackTrack
|
||||
}{
|
||||
{
|
||||
name: "copy attack track",
|
||||
args: args{
|
||||
attackTrack: v1alpha1.GetAttackTrackMock(v1alpha1.AttackTrackStep{
|
||||
Name: "A",
|
||||
SubSteps: []v1alpha1.AttackTrackStep{
|
||||
{
|
||||
Name: "B",
|
||||
SubSteps: []v1alpha1.AttackTrackStep{
|
||||
{
|
||||
Name: "C",
|
||||
},
|
||||
{
|
||||
Name: "D",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "E",
|
||||
},
|
||||
},
|
||||
}),
|
||||
lookup: NewAttackTrackControlsLookupMock(),
|
||||
},
|
||||
want: v1alpha1.GetAttackTrackMock(v1alpha1.AttackTrackStep{
|
||||
Name: "A",
|
||||
SubSteps: []v1alpha1.AttackTrackStep{
|
||||
{
|
||||
Name: "B",
|
||||
SubSteps: []v1alpha1.AttackTrackStep{
|
||||
{
|
||||
Name: "C",
|
||||
Controls: []v1alpha1.IAttackTrackControl{},
|
||||
},
|
||||
{
|
||||
Name: "D",
|
||||
Controls: []v1alpha1.IAttackTrackControl{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "E",
|
||||
Controls: []v1alpha1.IAttackTrackControl{},
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := handler.copyAttackTrack(tt.args.attackTrack, tt.args.lookup); got == tt.want {
|
||||
t.Errorf("ResourcesPrioritizationHandler.copyAttackTrack() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user