-
Notifications
You must be signed in to change notification settings - Fork 48
/
actions_linux_integration_test.go
132 lines (115 loc) · 2.74 KB
/
actions_linux_integration_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
//go:build integration && linux
// +build integration,linux
package tc
import (
"fmt"
"net"
"os"
"testing"
"github.com/jsimonetti/rtnetlink"
)
func TestTCActions(t *testing.T) {
tcTestIface := "mirror"
rtnl, err := setupDummyInterface(tcTestIface)
if err != nil {
fmt.Fprintf(os.Stderr, "could not setup dummy interface: %v\n", err)
return
}
defer rtnl.Close()
devID, err := net.InterfaceByName(tcTestIface)
if err != nil {
fmt.Fprintf(os.Stderr, "could not get interface ID: %v\n", err)
return
}
defer func(devID uint32, rtnl *rtnetlink.Conn) {
if err := rtnl.Link.Delete(devID); err != nil {
fmt.Fprintf(os.Stderr, "could not delete interface: %v\n", err)
}
}(uint32(devID.Index), rtnl)
config := Config{}
tcSocket, err := Open(&config)
if err != nil {
t.Fatalf("could not open socket for TC: %v", err)
}
defer func() {
if err := tcSocket.Close(); err != nil {
t.Fatalf("could not close TC socket: %v", err)
}
}()
mirrorIf, err := net.InterfaceByName(tcTestIface)
if err != nil {
t.Fatalf("failed t oget mirror interface: %v", err)
}
mirredActionIdx := uint32(42)
gactActionIdx := uint32(1337)
if err := tcSocket.Actions().Add([]*Action{
&Action{
Kind: "mirred",
Mirred: &Mirred{
Parms: &MirredParam{
Index: mirredActionIdx,
Eaction: 4, /* mirror packet to INGRESS */
IfIndex: uint32(mirrorIf.Index),
},
},
},
&Action{
Kind: "gact",
Gact: &Gact{
Parms: &GactParms{
Index: gactActionIdx,
Action: 2, /* drop */
},
},
},
}); err != nil {
t.Fatalf("failed to add mirred action: %v", err)
}
defer func() {
if err := tcSocket.Actions().Delete([]*Action{
&Action{
Kind: "gact",
Index: gactActionIdx,
},
}); err != nil {
t.Fatalf("failed to delete action: %v", err)
}
}()
mirredActions, err := tcSocket.Actions().Get([]*Action{
&Action{
Kind: "mirred",
},
})
if err != nil {
t.Fatalf("failed to get singe action: %v", err)
}
if len(mirredActions) != 1 {
t.Fatalf("expected 1 mirred action but got %d", len(mirredActions))
}
for _, a := range mirredActions {
gotMirredActionIndex := a.Mirred.Parms.Index
if gotMirredActionIndex != mirredActionIdx {
t.Fatalf("expected mirred action index of %d but got %d",
mirredActionIdx, gotMirredActionIndex)
}
}
if err := tcSocket.Actions().Delete([]*Action{
&Action{
Kind: "mirred",
Index: mirredActionIdx,
},
}); err != nil {
t.Fatalf("failed to delete mirred action: %v", err)
}
mirredActions, err = tcSocket.Actions().Get([]*Action{
&Action{
Kind: "mirred",
},
})
if err != nil {
t.Fatalf("failed to get singe action: %v", err)
}
if len(mirredActions) != 0 {
t.Fatalf("expected 0 mirred action but got %d", len(mirredActions))
}
}