Skip to content

Commit f3c7e16

Browse files
committed
Adding rule test
Signed-off-by: Amit Schendel <[email protected]>
1 parent 560e154 commit f3c7e16

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package rules
2+
3+
import (
4+
"testing"
5+
6+
"github.com/zeebo/assert"
7+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8+
"k8s.io/apimachinery/pkg/runtime/schema"
9+
"k8s.io/apiserver/pkg/admission"
10+
"k8s.io/apiserver/pkg/authentication/user"
11+
)
12+
13+
func TestProcessEvent(t *testing.T) {
14+
event := admission.NewAttributesRecord(
15+
&unstructured.Unstructured{
16+
Object: map[string]interface{}{
17+
"kind": "PodExecOptions",
18+
},
19+
},
20+
nil,
21+
schema.GroupVersionKind{
22+
Kind: "PodExecOptions",
23+
},
24+
"test-namespace",
25+
"test-pod",
26+
schema.GroupVersionResource{
27+
Resource: "pods",
28+
},
29+
"",
30+
admission.Create,
31+
nil,
32+
false,
33+
&user.DefaultInfo{
34+
Name: "test-user",
35+
Groups: []string{"test-group"},
36+
},
37+
)
38+
39+
rule := CreateRuleR2000ExecToPod()
40+
result := rule.ProcessEvent(event, nil)
41+
42+
assert.NotNil(t, result)
43+
assert.Equal(t, "Exec to pod detected on pod test-pod", result.GetRuleAlert().RuleDescription)
44+
assert.Equal(t, "test-pod", result.GetRuntimeAlertK8sDetails().PodName)
45+
assert.Equal(t, "test-namespace", result.GetRuntimeAlertK8sDetails().Namespace)
46+
}

0 commit comments

Comments
 (0)