forked from ti-mo/conntrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_integration_test.go
85 lines (62 loc) · 1.75 KB
/
stats_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
//+build integration
package conntrack
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConnStats(t *testing.T) {
if !findKsym("ctnetlink_ct_stat_cpu_dump") {
t.Skip("Per-CPU stats not implemented in this kernel")
}
c, _, err := makeNSConn()
require.NoError(t, err)
stats, err := c.Stats()
require.NoError(t, err)
for i, s := range stats {
// Make sure the array index corresponds to the CPUID of each entry.
assert.EqualValues(t, i, s.CPUID)
}
}
func TestConnStatsExpect(t *testing.T) {
if !findKsym("ctnetlink_exp_stat_cpu_dump") {
t.Skip("Per-CPU Expect stats not implemented in this kernel")
}
c, _, err := makeNSConn()
require.NoError(t, err)
statsExpect, err := c.StatsExpect()
require.NoError(t, err)
for i, s := range statsExpect {
// Make sure the array index corresponds to the CPUID of each entry.
assert.EqualValues(t, i, s.CPUID)
}
}
func TestConnStatsGlobal(t *testing.T) {
if !findKsym("ctnetlink_stat_ct") {
t.Skip("Global stats not implemented in this kernel")
}
c, _, err := makeNSConn()
require.NoError(t, err)
numFlows := 42
var f Flow
// Create IPv4 flows
for i := 1; i <= numFlows; i++ {
f = NewFlow(6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8), 1234, uint16(i), 120, 0)
err = c.Create(f)
require.NoError(t, err, "creating IPv4 flow", i)
}
// Create IPv6 flows
for i := 1; i <= numFlows; i++ {
err = c.Create(NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::200e"),
net.ParseIP("2a00:1450:400e:804::200f"),
1234, uint16(i), 120, 0,
))
require.NoError(t, err, "creating IPv6 flow", i)
}
sg, err := c.StatsGlobal()
require.NoError(t, err, "query StatsGlobal")
assert.EqualValues(t, numFlows*2, sg.Entries)
}