@@ -434,7 +434,7 @@ func TestCollectionSpec_LoadAndAssign_LazyLoading(t *testing.T) {
434
434
}
435
435
}
436
436
437
- func TestCollectionAssign (t * testing.T ) {
437
+ func TestCollectionSpecAssign (t * testing.T ) {
438
438
var specs struct {
439
439
Program * ProgramSpec `ebpf:"prog1"`
440
440
Map * MapSpec `ebpf:"map1"`
@@ -559,6 +559,89 @@ func TestAssignValues(t *testing.T) {
559
559
560
560
}
561
561
562
+ func TestCollectionAssign (t * testing.T ) {
563
+ var objs struct {
564
+ Program * Program `ebpf:"prog1"`
565
+ Map * Map `ebpf:"map1"`
566
+ }
567
+
568
+ cs := & CollectionSpec {
569
+ Maps : map [string ]* MapSpec {
570
+ "map1" : {
571
+ Type : Array ,
572
+ KeySize : 4 ,
573
+ ValueSize : 4 ,
574
+ MaxEntries : 1 ,
575
+ },
576
+ },
577
+ Programs : map [string ]* ProgramSpec {
578
+ "prog1" : {
579
+ Type : SocketFilter ,
580
+ Instructions : asm.Instructions {
581
+ asm .LoadImm (asm .R0 , 0 , asm .DWord ),
582
+ asm .Return (),
583
+ },
584
+ License : "MIT" ,
585
+ },
586
+ },
587
+ }
588
+
589
+ coll , err := NewCollection (cs )
590
+ qt .Assert (t , err , qt .IsNil )
591
+ defer coll .Close ()
592
+
593
+ qt .Assert (t , coll .Assign (& objs ), qt .IsNil )
594
+ defer objs .Program .Close ()
595
+ defer objs .Map .Close ()
596
+
597
+ // Check that objs has received ownership of map and prog
598
+ qt .Assert (t , objs .Program .FD () >= 0 , qt .IsTrue )
599
+ qt .Assert (t , objs .Map .FD () >= 0 , qt .IsTrue )
600
+
601
+ // Check that the collection has lost ownership
602
+ qt .Assert (t , coll .Programs ["prog1" ], qt .IsNil )
603
+ qt .Assert (t , coll .Maps ["map1" ], qt .IsNil )
604
+ }
605
+
606
+ func TestCollectionAssignFail (t * testing.T ) {
607
+ // `map2` does not exist
608
+ var objs struct {
609
+ Program * Program `ebpf:"prog1"`
610
+ Map * Map `ebpf:"map2"`
611
+ }
612
+
613
+ cs := & CollectionSpec {
614
+ Maps : map [string ]* MapSpec {
615
+ "map1" : {
616
+ Type : Array ,
617
+ KeySize : 4 ,
618
+ ValueSize : 4 ,
619
+ MaxEntries : 1 ,
620
+ },
621
+ },
622
+ Programs : map [string ]* ProgramSpec {
623
+ "prog1" : {
624
+ Type : SocketFilter ,
625
+ Instructions : asm.Instructions {
626
+ asm .LoadImm (asm .R0 , 0 , asm .DWord ),
627
+ asm .Return (),
628
+ },
629
+ License : "MIT" ,
630
+ },
631
+ },
632
+ }
633
+
634
+ coll , err := NewCollection (cs )
635
+ qt .Assert (t , err , qt .IsNil )
636
+ defer coll .Close ()
637
+
638
+ qt .Assert (t , coll .Assign (& objs ), qt .IsNotNil )
639
+
640
+ // Check that the collection has retained ownership
641
+ qt .Assert (t , coll .Programs ["prog1" ], qt .IsNotNil )
642
+ qt .Assert (t , coll .Maps ["map1" ], qt .IsNotNil )
643
+ }
644
+
562
645
func TestIncompleteLoadAndAssign (t * testing.T ) {
563
646
spec := & CollectionSpec {
564
647
Programs : map [string ]* ProgramSpec {
0 commit comments