@@ -3,10 +3,13 @@ package e2e_test
33import (
44 "bytes"
55 "context"
6+ "slices"
67 "strings"
78 "testing"
9+ "time"
810
911 "github.com/neuvector/runtime-enforcer/api/v1alpha1"
12+ "github.com/neuvector/runtime-enforcer/internal/policygenerator"
1013 "github.com/stretchr/testify/assert"
1114 "github.com/stretchr/testify/require"
1215 corev1 "k8s.io/api/core/v1"
@@ -55,7 +58,7 @@ func getMainTest() types.Feature {
5558 return ctx
5659 }).
5760 Assess ("required resources become available" , IfRequiredResourcesAreCreated ).
58- Assess ("the workload security proposal is created successfully for the ubuntu pod" ,
61+ Assess ("the workload policy proposal is created successfully for the ubuntu pod" ,
5962 func (ctx context.Context , t * testing.T , _ * envconf.Config ) context.Context {
6063 r := ctx .Value (key ("client" )).(* resources.Resources )
6164
@@ -86,7 +89,7 @@ func getMainTest() types.Feature {
8689 id := ctx .Value (key ("group" )).(string )
8790 r := ctx .Value (key ("client" )).(* resources.Resources )
8891
89- t .Log ("waiting for security policy proposal to be created: " , id )
92+ t .Log ("waiting for workload policy proposal to be created: " , id )
9093
9194 proposal := v1alpha1.WorkloadPolicyProposal {
9295 ObjectMeta : metav1.ObjectMeta {
@@ -113,9 +116,9 @@ func getMainTest() types.Feature {
113116
114117 return context .WithValue (ctx , key ("proposal" ), & proposal )
115118 }).
116- Assess ("a proposal is promoted to a security policy and the WP is created" ,
119+ Assess ("a proposal is promoted to a workload policy and the WP is created" ,
117120 func (ctx context.Context , t * testing.T , _ * envconf.Config ) context.Context {
118- t .Log ("create a security policy" )
121+ t .Log ("create a workload policy" )
119122
120123 r := ctx .Value (key ("client" )).(* resources.Resources )
121124 proposal := ctx .Value (key ("proposal" )).(* v1alpha1.WorkloadPolicyProposal )
@@ -129,7 +132,7 @@ func getMainTest() types.Feature {
129132 Mode : "protect" ,
130133 Selector : proposal .Spec .Selector ,
131134 RulesByContainer : map [string ]* v1alpha1.WorkloadPolicyRules {
132- "ubuntu" : & v1alpha1. WorkloadPolicyRules {
135+ "ubuntu" : {
133136 Executables : v1alpha1.WorkloadPolicyExecutables {
134137 Allowed : proposal .Spec .RulesByContainer ["ubuntu" ].Executables .Allowed ,
135138 AllowedPrefixes : proposal .Spec .RulesByContainer ["ubuntu" ].Executables .AllowedPrefixes ,
@@ -173,15 +176,194 @@ func getMainTest() types.Feature {
173176
174177 return ctx
175178 }).
176- Assess ("delete security policy" , func (ctx context.Context , t * testing.T , _ * envconf.Config ) context.Context {
177- r := ctx .Value (key ("client" )).(* resources.Resources )
178- policy := ctx .Value (key ("policy" )).(* v1alpha1.WorkloadPolicy )
179+ Assess ("the WorkloadPolicy has the finalizer set" ,
180+ func (ctx context.Context , t * testing.T , _ * envconf.Config ) context.Context {
181+ r := ctx .Value (key ("client" )).(* resources.Resources )
182+ policy := & v1alpha1.WorkloadPolicy {
183+ ObjectMeta : metav1.ObjectMeta {
184+ Name : "test-policy" ,
185+ Namespace : workloadNamespace ,
186+ },
187+ }
188+
189+ err := wait .For (
190+ conditions .New (r ).ResourceMatch (
191+ policy ,
192+ func (obj k8s.Object ) bool {
193+ wp := obj .(* v1alpha1.WorkloadPolicy )
194+ return slices .Contains (wp .Finalizers , v1alpha1 .WorkloadPolicyFinalizer )
195+ },
196+ ),
197+ wait .WithTimeout (DefaultOperationTimeout ),
198+ )
199+ require .NoError (t , err , "WorkloadPolicy finalizer is not set" )
179200
180- err := r .Delete (ctx , policy )
181- require .NoError (t , err )
201+ return ctx
202+ }).
203+ Assess ("Verify a non-referenced WorkloadPolicy can be deleted" ,
204+ func (ctx context.Context , t * testing.T , _ * envconf.Config ) context.Context {
205+ var err error
206+ r := ctx .Value (key ("client" )).(* resources.Resources )
207+ nonReferencedPolicyName := "non-referenced-wp"
182208
183- return ctx
184- }).
209+ // Create a new WorkloadPolicy
210+ nonReferencedPolicy := v1alpha1.WorkloadPolicy {
211+ ObjectMeta : metav1.ObjectMeta {
212+ Name : nonReferencedPolicyName ,
213+ Namespace : workloadNamespace ,
214+ },
215+ Spec : v1alpha1.WorkloadPolicySpec {
216+ Mode : "monitor" ,
217+ RulesByContainer : map [string ]* v1alpha1.WorkloadPolicyRules {
218+ "ubuntu" : {
219+ Executables : v1alpha1.WorkloadPolicyExecutables {
220+ Allowed : []string {"/bin/true" },
221+ },
222+ },
223+ },
224+ Severity : 9 ,
225+ Message : "non-referenced-wp" ,
226+ Tags : []string {"non-referenced-wp-policy" },
227+ },
228+ }
229+ require .NoError (
230+ t ,
231+ r .Create (ctx , & nonReferencedPolicy ),
232+ "failed to create non-referenced WorkloadPolicy" ,
233+ )
234+
235+ err = r .Delete (ctx , & nonReferencedPolicy )
236+ require .NoError (t , err , "failed to delete non-referenced WorkloadPolicy" )
237+
238+ // Wait for the WorkloadPolicy to be deleted
239+ err = wait .For (
240+ conditions .New (r ).ResourceDeleted (& nonReferencedPolicy ),
241+ wait .WithTimeout (time .Minute * 2 ),
242+ wait .WithInterval (time .Second * 5 ),
243+ )
244+ require .NoError (
245+ t ,
246+ err ,
247+ "policy was not deleted within timeout" ,
248+ )
249+
250+ return ctx
251+ }).
252+ Assess ("Verify a referenced WorkloadPolicy cannot be deleted" ,
253+ func (ctx context.Context , t * testing.T , _ * envconf.Config ) context.Context {
254+ var err error
255+ r := ctx .Value (key ("client" )).(* resources.Resources )
256+ referencedPolicyName := "referenced-wp"
257+ podName := "referenced-wp-pod"
258+
259+ // Create a new WorkloadPolicy
260+ referencedPolicy := v1alpha1.WorkloadPolicy {
261+ ObjectMeta : metav1.ObjectMeta {
262+ Name : referencedPolicyName ,
263+ Namespace : workloadNamespace ,
264+ },
265+ Spec : v1alpha1.WorkloadPolicySpec {
266+ Mode : "monitor" ,
267+ RulesByContainer : map [string ]* v1alpha1.WorkloadPolicyRules {
268+ "ubuntu" : {
269+ Executables : v1alpha1.WorkloadPolicyExecutables {
270+ Allowed : []string {"/bin/true" },
271+ },
272+ },
273+ },
274+ Severity : 9 ,
275+ Message : "referenced-wp" ,
276+ Tags : []string {"referenced-wp-policy" },
277+ },
278+ }
279+ require .NoError (
280+ t ,
281+ r .Create (ctx , & referencedPolicy ),
282+ "failed to create referenced WorkloadPolicy" ,
283+ )
284+
285+ pod := corev1.Pod {
286+ ObjectMeta : metav1.ObjectMeta {
287+ Name : podName ,
288+ Namespace : workloadNamespace ,
289+ Labels : map [string ]string {
290+ policygenerator .PolicyLabelKey : referencedPolicyName ,
291+ },
292+ },
293+ Spec : corev1.PodSpec {
294+ Containers : []corev1.Container {
295+ {
296+ Name : "pause" ,
297+ Image : "registry.k8s.io/pause" ,
298+ },
299+ },
300+ },
301+ }
302+ require .NoError (
303+ t ,
304+ r .Create (ctx , & pod ),
305+ "failed to create Pod" ,
306+ )
307+
308+ // Try to delete the referenced policy
309+ require .NoError (
310+ t ,
311+ r .Delete (ctx , & referencedPolicy ),
312+ "failed to issue delete request for WorkloadPolicy" ,
313+ )
314+
315+ // Verify the policy still exists (should not be deleted due to finalizer)
316+ err = wait .For (
317+ conditions .New (r ).ResourceMatch (
318+ & referencedPolicy ,
319+ func (obj k8s.Object ) bool {
320+ wp := obj .(* v1alpha1.WorkloadPolicy )
321+ return wp .DeletionTimestamp != nil &&
322+ slices .Contains (wp .Finalizers , v1alpha1 .WorkloadPolicyFinalizer )
323+ },
324+ ),
325+ wait .WithTimeout (30 * time .Second ),
326+ wait .WithInterval (5 * time .Second ),
327+ )
328+ require .NoError (
329+ t ,
330+ err ,
331+ "WorkloadPolicy should still exist while referenced by Pod" ,
332+ )
333+
334+ // Clean up pod, then policy should be deleted automatically
335+ require .NoError (
336+ t ,
337+ r .Delete (ctx , & pod ),
338+ "failed to delete Pod" ,
339+ )
340+
341+ // Wait for the pod to be deleted
342+ err = wait .For (
343+ conditions .New (r ).ResourceDeleted (& pod ),
344+ wait .WithTimeout (2 * time .Minute ),
345+ wait .WithInterval (5 * time .Second ),
346+ )
347+ require .NoError (
348+ t ,
349+ err ,
350+ "Pod was not deleted within timeout" ,
351+ )
352+
353+ // Now the policy should be deleted automatically
354+ err = wait .For (
355+ conditions .New (r ).ResourceDeleted (& referencedPolicy ),
356+ wait .WithTimeout (2 * time .Minute ),
357+ wait .WithInterval (5 * time .Second ),
358+ )
359+ require .NoError (
360+ t ,
361+ err ,
362+ "WorkloadPolicy should be deleted after Pod is removed" ,
363+ )
364+
365+ return ctx
366+ }).
185367 Teardown (func (ctx context.Context , t * testing.T , _ * envconf.Config ) context.Context {
186368 t .Log ("uninstalling test resources" )
187369
0 commit comments