forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wireguard_test.go
84 lines (72 loc) · 2.25 KB
/
wireguard_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
package wireguard
import (
"net"
"testing"
"time"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func TestWireguard_gatherDeviceMetrics(t *testing.T) {
var acc testutil.Accumulator
wg := &Wireguard{}
device := &wgtypes.Device{
Name: "wg0",
Type: wgtypes.LinuxKernel,
ListenPort: 1,
FirewallMark: 2,
Peers: []wgtypes.Peer{{}, {}},
}
expectFields := map[string]interface{}{
"listen_port": 1,
"firewall_mark": 2,
}
expectGauges := map[string]interface{}{
"peers": 2,
}
expectTags := map[string]string{
"name": "wg0",
"type": "linux_kernel",
}
wg.gatherDeviceMetrics(&acc, device)
assert.Equal(t, 3, acc.NFields())
acc.AssertDoesNotContainMeasurement(t, measurementPeer)
acc.AssertContainsTaggedFields(t, measurementDevice, expectFields, expectTags)
acc.AssertContainsTaggedFields(t, measurementDevice, expectGauges, expectTags)
}
func TestWireguard_gatherDevicePeerMetrics(t *testing.T) {
var acc testutil.Accumulator
pubkey, _ := wgtypes.ParseKey("NZTRIrv/ClTcQoNAnChEot+WL7OH7uEGQmx8oAN9rWE=")
wg := &Wireguard{}
device := &wgtypes.Device{
Name: "wg0",
}
peer := wgtypes.Peer{
PublicKey: pubkey,
PersistentKeepaliveInterval: 1 * time.Minute,
LastHandshakeTime: time.Unix(100, 0),
ReceiveBytes: int64(40),
TransmitBytes: int64(60),
AllowedIPs: []net.IPNet{{}, {}},
ProtocolVersion: 0,
}
expectFields := map[string]interface{}{
"persistent_keepalive_interval_ns": int64(60000000000),
"protocol_version": 0,
"allowed_ips": 2,
}
expectGauges := map[string]interface{}{
"last_handshake_time_ns": int64(100000000000),
"rx_bytes": int64(40),
"tx_bytes": int64(60),
}
expectTags := map[string]string{
"device": "wg0",
"public_key": pubkey.String(),
}
wg.gatherDevicePeerMetrics(&acc, device, peer)
assert.Equal(t, 6, acc.NFields())
acc.AssertDoesNotContainMeasurement(t, measurementDevice)
acc.AssertContainsTaggedFields(t, measurementPeer, expectFields, expectTags)
acc.AssertContainsTaggedFields(t, measurementPeer, expectGauges, expectTags)
}