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