Skip to content

Commit 07c3b9c

Browse files
committed
filters: add unit tests for namespace regex filter
This commit adds tests to verify the namespace regex filter works correctly. Signed-off-by: Aritra Dey <[email protected]>
1 parent 5bc226c commit 07c3b9c

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

pkg/filters/namespace_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,102 @@ func TestNamespace(t *testing.T) {
6666
ev = event.Event{Event: &tetragon.GetEventsResponse{Event: &tetragon.GetEventsResponse_ProcessExec{ProcessExec: &tetragon.ProcessExec{Process: &tetragon.Process{}}}}}
6767
assert.True(t, fl.MatchOne(&ev))
6868
}
69+
70+
func TestNamespaceRegex(t *testing.T) {
71+
f := []*tetragon.Filter{{NamespaceRegex: []string{"test-.*"}}}
72+
fl, err := BuildFilterList(context.Background(), f, []OnBuildFilter{&NamespaceRegexFilter{}})
73+
require.NoError(t, err)
74+
75+
ev := event.Event{
76+
Event: &tetragon.GetEventsResponse{
77+
Event: &tetragon.GetEventsResponse_ProcessExec{
78+
ProcessExec: &tetragon.ProcessExec{Process: &tetragon.Process{Pod: &tetragon.Pod{Namespace: "test-app"}}},
79+
},
80+
},
81+
}
82+
assert.True(t, fl.MatchOne(&ev), "should match test-app namespace")
83+
84+
ev = event.Event{
85+
Event: &tetragon.GetEventsResponse{
86+
Event: &tetragon.GetEventsResponse_ProcessExec{
87+
ProcessExec: &tetragon.ProcessExec{Process: &tetragon.Process{Pod: &tetragon.Pod{Namespace: "test-system"}}},
88+
},
89+
},
90+
}
91+
assert.True(t, fl.MatchOne(&ev), "should match test-system namespace")
92+
93+
ev = event.Event{
94+
Event: &tetragon.GetEventsResponse{
95+
Event: &tetragon.GetEventsResponse_ProcessExec{
96+
ProcessExec: &tetragon.ProcessExec{Process: &tetragon.Process{Pod: &tetragon.Pod{Namespace: "kube-system"}}},
97+
},
98+
},
99+
}
100+
assert.False(t, fl.MatchOne(&ev), "should not match kube-system namespace")
101+
102+
ev = event.Event{
103+
Event: &tetragon.GetEventsResponse{
104+
Event: &tetragon.GetEventsResponse_ProcessExec{
105+
ProcessExec: &tetragon.ProcessExec{Process: &tetragon.Process{Pod: &tetragon.Pod{Namespace: "default"}}},
106+
},
107+
},
108+
}
109+
assert.False(t, fl.MatchOne(&ev), "should not match default namespace")
110+
111+
ev = event.Event{
112+
Event: &tetragon.GetEventsResponse{
113+
Event: &tetragon.GetEventsResponse_ProcessExec{
114+
ProcessExec: &tetragon.ProcessExec{Process: &tetragon.Process{}},
115+
},
116+
},
117+
}
118+
assert.False(t, fl.MatchOne(&ev), "should not match process without pod info")
119+
120+
ev = event.Event{
121+
Event: &tetragon.GetEventsResponse{
122+
Event: &tetragon.GetEventsResponse_ProcessExec{
123+
ProcessExec: &tetragon.ProcessExec{},
124+
},
125+
},
126+
}
127+
assert.False(t, fl.MatchOne(&ev), "should not match nil process")
128+
}
129+
130+
func TestNamespaceRegexMultiplePatterns(t *testing.T) {
131+
f := []*tetragon.Filter{{NamespaceRegex: []string{"prod-.*", "staging-.*"}}}
132+
fl, err := BuildFilterList(context.Background(), f, []OnBuildFilter{&NamespaceRegexFilter{}})
133+
require.NoError(t, err)
134+
135+
ev := event.Event{
136+
Event: &tetragon.GetEventsResponse{
137+
Event: &tetragon.GetEventsResponse_ProcessExec{
138+
ProcessExec: &tetragon.ProcessExec{Process: &tetragon.Process{Pod: &tetragon.Pod{Namespace: "prod-app"}}},
139+
},
140+
},
141+
}
142+
assert.True(t, fl.MatchOne(&ev), "should match prod-app namespace")
143+
144+
ev = event.Event{
145+
Event: &tetragon.GetEventsResponse{
146+
Event: &tetragon.GetEventsResponse_ProcessExec{
147+
ProcessExec: &tetragon.ProcessExec{Process: &tetragon.Process{Pod: &tetragon.Pod{Namespace: "staging-app"}}},
148+
},
149+
},
150+
}
151+
assert.True(t, fl.MatchOne(&ev), "should match staging-app namespace")
152+
153+
ev = event.Event{
154+
Event: &tetragon.GetEventsResponse{
155+
Event: &tetragon.GetEventsResponse_ProcessExec{
156+
ProcessExec: &tetragon.ProcessExec{Process: &tetragon.Process{Pod: &tetragon.Pod{Namespace: "dev-app"}}},
157+
},
158+
},
159+
}
160+
assert.False(t, fl.MatchOne(&ev), "should not match dev-app namespace")
161+
}
162+
163+
func TestNamespaceRegexInvalidPattern(t *testing.T) {
164+
f := []*tetragon.Filter{{NamespaceRegex: []string{"[invalid"}}}
165+
_, err := BuildFilterList(context.Background(), f, []OnBuildFilter{&NamespaceRegexFilter{}})
166+
require.Error(t, err, "should return error for invalid regex")
167+
}

0 commit comments

Comments
 (0)