github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/link/cgroup_test.go (about)

     1  package link
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/cilium/ebpf"
     7  	"github.com/cilium/ebpf/internal/testutils"
     8  )
     9  
    10  func TestAttachCgroup(t *testing.T) {
    11  	cgroup, prog := mustCgroupFixtures(t)
    12  
    13  	link, err := AttachCgroup(CgroupOptions{
    14  		Path:    cgroup.Name(),
    15  		Attach:  ebpf.AttachCGroupInetEgress,
    16  		Program: prog,
    17  	})
    18  	testutils.SkipIfNotSupported(t, err)
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	defer link.Close()
    23  
    24  	if haveBPFLink() == nil {
    25  		if _, ok := link.(*linkCgroup); !ok {
    26  			t.Fatalf("Have support for bpf_link, but got %T instead of linkCgroup", link)
    27  		}
    28  	} else {
    29  		if _, ok := link.(*progAttachCgroup); !ok {
    30  			t.Fatalf("Expected progAttachCgroup, got %T instead", link)
    31  		}
    32  	}
    33  }
    34  
    35  func TestProgAttachCgroup(t *testing.T) {
    36  	cgroup, prog := mustCgroupFixtures(t)
    37  
    38  	link, err := newProgAttachCgroup(cgroup, ebpf.AttachCGroupInetEgress, prog, 0)
    39  	if err != nil {
    40  		t.Fatal("Can't create link:", err)
    41  	}
    42  
    43  	testLink(t, link, prog)
    44  }
    45  
    46  func TestProgAttachCgroupAllowMulti(t *testing.T) {
    47  	cgroup, prog := mustCgroupFixtures(t)
    48  
    49  	link, err := newProgAttachCgroup(cgroup, ebpf.AttachCGroupInetEgress, prog, flagAllowMulti)
    50  	testutils.SkipIfNotSupported(t, err)
    51  	if err != nil {
    52  		t.Fatal("Can't create link:", err)
    53  	}
    54  
    55  	// It's currently not possible for a program to replace
    56  	// itself.
    57  	prog2 := mustLoadProgram(t, ebpf.CGroupSKB, ebpf.AttachCGroupInetEgress, "")
    58  	testLink(t, link, prog2)
    59  }
    60  
    61  func TestLinkCgroup(t *testing.T) {
    62  	cgroup, prog := mustCgroupFixtures(t)
    63  
    64  	link, err := newLinkCgroup(cgroup, ebpf.AttachCGroupInetEgress, prog)
    65  	testutils.SkipIfNotSupported(t, err)
    66  	if err != nil {
    67  		t.Fatal("Can't create link:", err)
    68  	}
    69  
    70  	testLink(t, link, prog)
    71  }