@@ -18,13 +18,15 @@ package bpfmanagent
1818
1919import (
2020 "context"
21+ "fmt"
2122 "reflect"
2223 "testing"
2324
2425 bpfmaniov1alpha1 "github.com/bpfman/bpfman-operator/apis/v1alpha1"
2526 agenttestutils "github.com/bpfman/bpfman-operator/controllers/bpfman-agent/internal/test-utils"
2627 "github.com/bpfman/bpfman-operator/internal"
2728 testutils "github.com/bpfman/bpfman-operator/internal/test-utils"
29+ "github.com/bpfman/bpfman-operator/pkg/helpers"
2830
2931 "github.com/stretchr/testify/require"
3032 v1 "k8s.io/api/core/v1"
@@ -82,6 +84,8 @@ func TestClBpfApplicationControllerCreate(t *testing.T) {
8284 bpfmaniov1alpha1 .XdpProceedOnValue ("pass" ),
8385 bpfmaniov1alpha1 .XdpProceedOnValue ("dispatcher_return" ),
8486 }
87+ tcProceedOn = []bpfmaniov1alpha1.TcProceedOnValue {bpfmaniov1alpha1 .TcProceedOnValue ("ok" ),
88+ bpfmaniov1alpha1 .TcProceedOnValue ("shot" )}
8589 ctx = context .TODO ()
8690 )
8791
@@ -233,6 +237,147 @@ func TestClBpfApplicationControllerCreate(t *testing.T) {
233237 },
234238 },
235239 },
240+ {
241+ testName : "link priority test XDP" ,
242+ programs : []bpfmaniov1alpha1.ClBpfApplicationProgram {
243+ // XDP Program
244+ {
245+ Name : fmt .Sprintf ("%s-%d" , testXdpBpfFunctionName , 0 ),
246+ Type : bpfmaniov1alpha1 .ProgTypeXDP ,
247+ XDP : & bpfmaniov1alpha1.ClXdpProgramInfo {
248+ Links : []bpfmaniov1alpha1.ClXdpAttachInfo {
249+ {
250+ InterfaceSelector : interfaceSelector ,
251+ NetworkNamespaces : nil ,
252+ Priority : nil , // nil priority should yield 1000.
253+ ProceedOn : proceedOn ,
254+ },
255+ },
256+ },
257+ },
258+ {
259+ Name : fmt .Sprintf ("%s-%d" , testXdpBpfFunctionName , 1 ),
260+ Type : bpfmaniov1alpha1 .ProgTypeXDP ,
261+ XDP : & bpfmaniov1alpha1.ClXdpProgramInfo {
262+ Links : []bpfmaniov1alpha1.ClXdpAttachInfo {
263+ {
264+ InterfaceSelector : interfaceSelector ,
265+ NetworkNamespaces : nil ,
266+ Priority : ptr .To (int32 (999 )),
267+ ProceedOn : proceedOn ,
268+ },
269+ },
270+ },
271+ },
272+ {
273+ Name : fmt .Sprintf ("%s-%d" , testXdpBpfFunctionName , 2 ),
274+ Type : bpfmaniov1alpha1 .ProgTypeXDP ,
275+ XDP : & bpfmaniov1alpha1.ClXdpProgramInfo {
276+ Links : []bpfmaniov1alpha1.ClXdpAttachInfo {
277+ {
278+ InterfaceSelector : interfaceSelector ,
279+ NetworkNamespaces : nil ,
280+ Priority : ptr .To (int32 (0 )), // 0 priority should be allowed.
281+ ProceedOn : proceedOn ,
282+ },
283+ },
284+ },
285+ },
286+ },
287+ },
288+ {
289+ testName : "link priority test TC" ,
290+ programs : []bpfmaniov1alpha1.ClBpfApplicationProgram {
291+ // TC Program
292+ {
293+ Name : fmt .Sprintf ("%s-%d" , testTcBpfFunctionName , 0 ),
294+ Type : bpfmaniov1alpha1 .ProgTypeTC ,
295+ TC : & bpfmaniov1alpha1.ClTcProgramInfo {
296+ Links : []bpfmaniov1alpha1.ClTcAttachInfo {
297+ {
298+ InterfaceSelector : interfaceSelector ,
299+ NetworkNamespaces : nil ,
300+ Priority : nil , // nil priority should yield 1000.
301+ ProceedOn : tcProceedOn ,
302+ },
303+ },
304+ },
305+ },
306+ {
307+ Name : fmt .Sprintf ("%s-%d" , testTcBpfFunctionName , 1 ),
308+ Type : bpfmaniov1alpha1 .ProgTypeTC ,
309+ TC : & bpfmaniov1alpha1.ClTcProgramInfo {
310+ Links : []bpfmaniov1alpha1.ClTcAttachInfo {
311+ {
312+ InterfaceSelector : interfaceSelector ,
313+ NetworkNamespaces : nil ,
314+ Priority : ptr .To (int32 (999 )),
315+ ProceedOn : tcProceedOn ,
316+ },
317+ },
318+ },
319+ },
320+ {
321+ Name : fmt .Sprintf ("%s-%d" , testTcBpfFunctionName , 2 ),
322+ Type : bpfmaniov1alpha1 .ProgTypeTC ,
323+ TC : & bpfmaniov1alpha1.ClTcProgramInfo {
324+ Links : []bpfmaniov1alpha1.ClTcAttachInfo {
325+ {
326+ InterfaceSelector : interfaceSelector ,
327+ NetworkNamespaces : nil ,
328+ Priority : ptr .To (int32 (0 )), // 0 priority should be allowed.
329+ ProceedOn : tcProceedOn ,
330+ },
331+ },
332+ },
333+ },
334+ },
335+ },
336+ {
337+ testName : "link priority test TCX" ,
338+ programs : []bpfmaniov1alpha1.ClBpfApplicationProgram {
339+ // TCX Program
340+ {
341+ Name : fmt .Sprintf ("%s-%d" , testTcxBpfFunctionName , 0 ),
342+ Type : bpfmaniov1alpha1 .ProgTypeTCX ,
343+ TCX : & bpfmaniov1alpha1.ClTcxProgramInfo {
344+ Links : []bpfmaniov1alpha1.ClTcxAttachInfo {
345+ {
346+ InterfaceSelector : interfaceSelector ,
347+ NetworkNamespaces : nil ,
348+ Priority : nil , // nil priority should yield 1000.
349+ },
350+ },
351+ },
352+ },
353+ {
354+ Name : fmt .Sprintf ("%s-%d" , testTcxBpfFunctionName , 1 ),
355+ Type : bpfmaniov1alpha1 .ProgTypeTCX ,
356+ TCX : & bpfmaniov1alpha1.ClTcxProgramInfo {
357+ Links : []bpfmaniov1alpha1.ClTcxAttachInfo {
358+ {
359+ InterfaceSelector : interfaceSelector ,
360+ NetworkNamespaces : nil ,
361+ Priority : ptr .To (int32 (999 )),
362+ },
363+ },
364+ },
365+ },
366+ {
367+ Name : fmt .Sprintf ("%s-%d" , testTcxBpfFunctionName , 2 ),
368+ Type : bpfmaniov1alpha1 .ProgTypeTCX ,
369+ TCX : & bpfmaniov1alpha1.ClTcxProgramInfo {
370+ Links : []bpfmaniov1alpha1.ClTcxAttachInfo {
371+ {
372+ InterfaceSelector : interfaceSelector ,
373+ NetworkNamespaces : nil ,
374+ Priority : ptr .To (int32 (0 )), // 0 priority should be allowed.
375+ },
376+ },
377+ },
378+ },
379+ },
380+ },
236381 }
237382
238383 for _ , tc := range tcs {
@@ -325,8 +470,34 @@ func createFakeClusterReconciler(objs []runtime.Object, bpfApp *bpfmaniov1alpha1
325470// successfully attached and that the number of programs matches the expected count.
326471func verifyClusterBpfProgramState (t * testing.T , bpfAppState * bpfmaniov1alpha1.ClusterBpfApplicationState ,
327472 programs []bpfmaniov1alpha1.ClBpfApplicationProgram ) {
473+ require .Equal (t , len (programs ), len (bpfAppState .Status .Programs ))
474+
475+ numMatches := 0
328476 for _ , program := range bpfAppState .Status .Programs {
329477 require .Equal (t , bpfmaniov1alpha1 .ProgAttachSuccess , program .ProgramLinkStatus )
478+ for _ , expectedProgram := range programs {
479+ if program .Name != expectedProgram .Name {
480+ continue
481+ }
482+ switch {
483+ case program .XDP != nil :
484+ require .NotNil (t , program .XDP .Links [0 ].Priority )
485+ if * program .XDP .Links [0 ].Priority != helpers .GetPriority (expectedProgram .XDP .Links [0 ].Priority ) {
486+ continue
487+ }
488+ case program .TC != nil :
489+ require .NotNil (t , program .TC .Links [0 ].Priority )
490+ if * program .TC .Links [0 ].Priority != helpers .GetPriority (expectedProgram .TC .Links [0 ].Priority ) {
491+ continue
492+ }
493+ case program .TCX != nil :
494+ require .NotNil (t , program .TCX .Links [0 ].Priority )
495+ if * program .TCX .Links [0 ].Priority != helpers .GetPriority (expectedProgram .TCX .Links [0 ].Priority ) {
496+ continue
497+ }
498+ }
499+ numMatches ++
500+ }
330501 }
331- require .Equal (t , len (programs ), len ( bpfAppState . Status . Programs ) )
502+ require .Equal (t , len (programs ), numMatches )
332503}
0 commit comments