|
| 1 | +// Copyright (c) 2019 Dropbox, Inc. |
| 2 | +// Full license can be found in the LICENSE file. |
| 3 | + |
| 4 | +package itest |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/dropbox/goebpf" |
| 12 | + "github.com/stretchr/testify/suite" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + tcProgramFilename = "ebpf_prog/tc1.elf" |
| 17 | +) |
| 18 | + |
| 19 | +type tcTestSuite struct { |
| 20 | + suite.Suite |
| 21 | + programFilename string |
| 22 | + programsCount int |
| 23 | +} |
| 24 | + |
| 25 | +// Basic sanity test of BPF core functionality like |
| 26 | +// ReadElf, create maps, load / attach programs |
| 27 | +func (ts *tcTestSuite) TestElfLoad() { |
| 28 | + // This compile ELF file contains 2 BPF(TC type) programs |
| 29 | + eb := goebpf.NewDefaultEbpfSystem() |
| 30 | + err := eb.LoadElf(ts.programFilename) |
| 31 | + ts.NoError(err) |
| 32 | + if err != nil { |
| 33 | + // ELF read error. |
| 34 | + ts.FailNowf("Unable to read %s", ts.programFilename) |
| 35 | + } |
| 36 | + |
| 37 | + // There should be 0 BPF maps recognized by loader |
| 38 | + maps := eb.GetMaps() |
| 39 | + ts.Require().Equal(0, len(maps)) |
| 40 | + |
| 41 | + // Non existing map |
| 42 | + ts.Nil(eb.GetMapByName("something")) |
| 43 | + |
| 44 | + // Also there should few TC eBPF programs recognized |
| 45 | + ts.Require().Equal(ts.programsCount, len(eb.GetPrograms())) |
| 46 | + |
| 47 | + // Check loaded programs and try to pin them |
| 48 | + tc1 := eb.GetProgramByName("tc1") |
| 49 | + tc1.Load() |
| 50 | + path := bpfPath + "/tc1_pin_test" |
| 51 | + err = tc1.Pin(path) |
| 52 | + ts.NoError(err) |
| 53 | + ts.FileExists(path) |
| 54 | + os.Remove(path) |
| 55 | + ts.Equal(goebpf.ProgramTypeSchedCls, tc1.GetType()) |
| 56 | + |
| 57 | + // Check loaded programs and try to pin them |
| 58 | + tc2 := eb.GetProgramByName("tc2") |
| 59 | + tc2.Load() |
| 60 | + path = bpfPath + "/tc2_pin_test" |
| 61 | + err = tc2.Pin(path) |
| 62 | + ts.NoError(err) |
| 63 | + ts.FileExists(path) |
| 64 | + os.Remove(path) |
| 65 | + ts.Equal(goebpf.ProgramTypeSchedAct, tc2.GetType()) |
| 66 | + |
| 67 | + // Check loaded programs and try to pin them |
| 68 | + tc3 := eb.GetProgramByName("tc3") |
| 69 | + tc3.Load() |
| 70 | + path = bpfPath + "/tc3_pin_test" |
| 71 | + err = tc3.Pin(path) |
| 72 | + ts.NoError(err) |
| 73 | + ts.FileExists(path) |
| 74 | + os.Remove(path) |
| 75 | + ts.Equal(goebpf.ProgramTypeSchedAct, tc3.GetType()) |
| 76 | + |
| 77 | + // Non existing program |
| 78 | + ts.Nil(eb.GetProgramByName("something")) |
| 79 | + |
| 80 | + //Run attach and detach, they shouldn't fail as these methods are not implemented for TC |
| 81 | + err = tc1.Attach(0) |
| 82 | + ts.Error(err) |
| 83 | + err = tc1.Detach() |
| 84 | + ts.Error(err) |
| 85 | + err = tc2.Attach(0) |
| 86 | + ts.Error(err) |
| 87 | + err = tc2.Detach() |
| 88 | + ts.Error(err) |
| 89 | + err = tc3.Attach(0) |
| 90 | + ts.Error(err) |
| 91 | + err = tc3.Detach() |
| 92 | + ts.Error(err) |
| 93 | + |
| 94 | + // Unload programs (not required for real use case) |
| 95 | + for _, program := range eb.GetPrograms() { |
| 96 | + err = program.Close() |
| 97 | + ts.NoError(err) |
| 98 | + } |
| 99 | + |
| 100 | + // Negative: close already closed program |
| 101 | + err = tc1.Close() |
| 102 | + ts.Error(err) |
| 103 | + |
| 104 | +} |
| 105 | + |
| 106 | +func (ts *tcTestSuite) TestProgramInfo() { |
| 107 | + // Load test program, don't attach (not required to get info) |
| 108 | + eb := goebpf.NewDefaultEbpfSystem() |
| 109 | + err := eb.LoadElf(ts.programFilename) |
| 110 | + ts.Require().NoError(err) |
| 111 | + prog := eb.GetProgramByName("tc1") |
| 112 | + err = prog.Load() |
| 113 | + ts.Require().NoError(err) |
| 114 | + |
| 115 | + // Get program info by FD (NOT ID, since this program is ours) |
| 116 | + info, err := goebpf.GetProgramInfoByFd(prog.GetFd()) |
| 117 | + ts.NoError(err) |
| 118 | + |
| 119 | + // Check base info |
| 120 | + ts.Equal(prog.GetName(), info.Name) |
| 121 | + ts.Equal(prog.GetFd(), info.Fd) |
| 122 | + ts.Equal(goebpf.ProgramTypeSchedCls, info.Type) |
| 123 | + // Check loaded time |
| 124 | + now := time.Now() |
| 125 | + ts.True(now.Sub(info.LoadTime) < time.Second*10) |
| 126 | + |
| 127 | +} |
| 128 | + |
| 129 | +// Run suite |
| 130 | +func TestTcSuite(t *testing.T) { |
| 131 | + suite.Run(t, &tcTestSuite{ |
| 132 | + programFilename: tcProgramFilename, |
| 133 | + programsCount: 3, |
| 134 | + }) |
| 135 | +} |
0 commit comments