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