-
Notifications
You must be signed in to change notification settings - Fork 6
/
metrics_test.go
145 lines (138 loc) · 5.2 KB
/
metrics_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
136
137
138
139
140
141
142
143
144
145
package main
import (
"fmt"
"net"
"testing"
"time"
"github.com/mdlayher/promtest"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"inet.af/netaddr"
)
func TestCollector(t *testing.T) {
// Fake public keys used to identify devices and peers.
var (
pubDevA = newWgKey()
pubDevB = newWgKey()
pubPeerA = newWgKey()
pubPeerB = newWgKey()
pubPeerC = newWgKey()
userA = "userA@example.com"
userB = "userB@example.com"
)
tests := []struct {
name string
devices func() ([]*wgtypes.Device, error)
leaseManager *fileLeaseManager
metrics []string
}{
{
name: "ok",
devices: func() ([]*wgtypes.Device, error) {
return []*wgtypes.Device{
{
Name: "wg0",
PublicKey: pubDevA,
Peers: []wgtypes.Peer{{
PublicKey: pubPeerA,
Endpoint: &net.UDPAddr{
IP: net.ParseIP("1.1.1.1"),
Port: 51820,
},
LastHandshakeTime: time.Unix(10, 0),
ReceiveBytes: 1,
TransmitBytes: 2,
AllowedIPs: []net.IPNet{
net.IPNet{
IP: net.ParseIP("10.0.0.1"),
Mask: net.CIDRMask(32, 32),
},
net.IPNet{
IP: net.ParseIP("10.0.0.2"),
Mask: net.CIDRMask(32, 32),
},
},
}},
},
{
Name: "wg1",
PublicKey: pubDevB,
Peers: []wgtypes.Peer{
{
PublicKey: pubPeerB,
AllowedIPs: []net.IPNet{
net.IPNet{
IP: net.ParseIP("10.0.0.3"),
Mask: net.CIDRMask(32, 32),
},
},
},
{
PublicKey: pubPeerC, // Not in the leases
AllowedIPs: []net.IPNet{
net.IPNet{
IP: net.ParseIP("10.0.0.4"),
Mask: net.CIDRMask(32, 32),
},
},
},
},
},
}, nil
},
leaseManager: &fileLeaseManager{
wgRecords: map[string]WGRecord{
userA: WGRecord{
PubKey: pubPeerA.String(),
IP: netaddr.MustParseIP("10.0.0.1"),
expires: time.Unix(100, 0),
},
userB: WGRecord{
PubKey: pubPeerB.String(),
IP: netaddr.MustParseIP("10.0.0.3"),
},
},
},
metrics: []string{
fmt.Sprintf(`wiresteward_wg_device_info{device="wg0",public_key="%v"} 1`, pubDevA.String()),
fmt.Sprintf(`wiresteward_wg_device_info{device="wg1",public_key="%v"} 1`, pubDevB.String()),
fmt.Sprintf(`wiresteward_wg_peer_info{device="wg0",public_key="%v",username="%s"} 1`, pubPeerA.String(), userA),
fmt.Sprintf(`wiresteward_wg_peer_info{device="wg1",public_key="%v",username="%s"} 1`, pubPeerB.String(), userB),
fmt.Sprintf(`wiresteward_wg_peer_info{device="wg1",public_key="%v",username=""} 1`, pubPeerC.String()),
fmt.Sprintf(`wiresteward_wg_peer_allowed_ips_info{allowed_ips="10.0.0.1/32",device="wg0",public_key="%v",username="%s"} 1`, pubPeerA.String(), userA),
fmt.Sprintf(`wiresteward_wg_peer_allowed_ips_info{allowed_ips="10.0.0.2/32",device="wg0",public_key="%v",username="%s"} 1`, pubPeerA.String(), userA),
fmt.Sprintf(`wiresteward_wg_peer_allowed_ips_info{allowed_ips="10.0.0.3/32",device="wg1",public_key="%v",username="%s"} 1`, pubPeerB.String(), userB),
fmt.Sprintf(`wiresteward_wg_peer_allowed_ips_info{allowed_ips="10.0.0.4/32",device="wg1",public_key="%v",username=""} 1`, pubPeerC.String()),
fmt.Sprintf(`wiresteward_wg_peer_last_handshake_seconds{device="wg0",public_key="%v",username="%s"} 10`, pubPeerA.String(), userA),
fmt.Sprintf(`wiresteward_wg_peer_last_handshake_seconds{device="wg1",public_key="%v",username="%s"} 0`, pubPeerB.String(), userB),
fmt.Sprintf(`wiresteward_wg_peer_last_handshake_seconds{device="wg1",public_key="%v",username=""} 0`, pubPeerC.String()),
fmt.Sprintf(`wiresteward_wg_peer_receive_bytes_total{device="wg0",public_key="%v",username="%s"} 1`, pubPeerA.String(), userA),
fmt.Sprintf(`wiresteward_wg_peer_receive_bytes_total{device="wg1",public_key="%v",username="%s"} 0`, pubPeerB.String(), userB),
fmt.Sprintf(`wiresteward_wg_peer_receive_bytes_total{device="wg1",public_key="%v",username=""} 0`, pubPeerC.String()),
fmt.Sprintf(`wiresteward_wg_peer_transmit_bytes_total{device="wg0",public_key="%v",username="%s"} 2`, pubPeerA.String(), userA),
fmt.Sprintf(`wiresteward_wg_peer_transmit_bytes_total{device="wg1",public_key="%v",username="%s"} 0`, pubPeerB.String(), userB),
fmt.Sprintf(`wiresteward_wg_peer_transmit_bytes_total{device="wg1",public_key="%v",username=""} 0`, pubPeerC.String()),
fmt.Sprintf(`wiresteward_peer_lease_expiry_time{address="10.0.0.1",public_key="%v",username="%s"} 100`, pubPeerA.String(), userA),
fmt.Sprintf(`wiresteward_peer_lease_expiry_time{address="10.0.0.3",public_key="%v",username="%s"} 0`, pubPeerB.String(), userB),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
body := promtest.Collect(t, newMetricsCollector(tt.devices, tt.leaseManager))
if !promtest.Lint(t, body) {
t.Fatal("one or more promlint errors found")
}
if !promtest.Match(t, body, tt.metrics) {
t.Fatal("metrics did not match whitelist")
}
})
}
}
// return a wg key or panic
func newWgKey() wgtypes.Key {
key, err := wgtypes.GenerateKey()
if err != nil {
panic(fmt.Sprintf("Cannot generate new wg key: %v", err))
}
return key
}