-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi-logs_test.go
241 lines (198 loc) · 5.92 KB
/
api-logs_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
qt "github.com/frankban/quicktest"
"github.com/prometheus/client_golang/prometheus"
//"github.com/prometheus/client_golang/prometheus/testutil"
tscg "github.com/tailscale/tailscale-client-go/tailscale"
)
var (
//go:embed testdata/devices.json
jsonDevices []byte
//go:embed testdata/devices-02.json
jsonDevicesTwo []byte
//go:embed testdata/logs.one.json
logOne []byte
//go:embed testdata/logs.two.json
logTwo []byte
//go:embed testdata/logs.three.json
logThree []byte
)
type FakeClientAPI struct {
DevicesJson []byte
}
func (f *FakeClientAPI) SetDevices(json []byte) {
f.DevicesJson = json
}
func (f *FakeClientAPI) Devices(ctx context.Context) ([]tscg.Device, error) {
resp := make(map[string][]tscg.Device)
err := json.Unmarshal(f.DevicesJson, &resp)
if err != nil {
fmt.Printf("ERR FakeClientAPI.Devices(): %s", err)
return nil, err
}
return resp["devices"], nil
}
type FakeClientLog struct {
JsonData []byte
}
func (f *FakeClientLog) SetJson(jsonD []byte) {
f.JsonData = jsonD
}
func (f *FakeClientLog) Get(url string) (*http.Response, error) {
recorder := httptest.NewRecorder()
_, err := recorder.Write(f.JsonData)
if err != nil {
return nil, err
}
recorder.Header().Set("Content-Type", "application/json")
response := recorder.Result()
return response, nil
}
var (
app AppConfig
flClient FakeClientLog
faClient FakeClientAPI
)
func TestMain(m *testing.M) {
app = AppConfig{
APIMetrics: map[string]*prometheus.GaugeVec{},
LogMetrics: map[string]*prometheus.CounterVec{},
SleepIntervalSeconds: *waitTimeSecs,
LMData: &LogMetricData{},
}
app.LMData.Init()
app.registerLogMetrics()
app.registerAPIMetrics()
flClient = FakeClientLog{}
faClient = FakeClientAPI{}
exitVal := m.Run()
os.Exit(exitVal)
}
func TestAPIMetrics(t *testing.T) {
app.LMData.Init()
faClient.SetDevices(jsonDevices)
app.updateAPIMetrics(&faClient)
mName := "tailscale_hosts"
c := qt.New(t)
hostToMetric := gatherLabels("hostname", mName, t)
c.Assert(len(hostToMetric), qt.Equals, 2)
// TODO: Pull this from the json truth
hello := hostToMetric["hello"]
c.Assert(len(hello), qt.Equals, 6)
c.Assert(hello["hostname"], qt.Equals, "hello")
c.Assert(hello["update_available"], qt.Equals, "false")
c.Assert(hello["os"], qt.Equals, "linux")
c.Assert(hello["is_external"], qt.Equals, "true")
c.Assert(hello["user"], qt.Equals, "[email protected]")
c.Assert(hello["client_version"], qt.Equals, "1.1.1")
foo := hostToMetric["foo"]
c.Assert(len(foo), qt.Equals, 6)
c.Assert(foo["hostname"], qt.Equals, "foo")
c.Assert(foo["update_available"], qt.Equals, "true")
c.Assert(foo["os"], qt.Equals, "macos")
c.Assert(foo["is_external"], qt.Equals, "false")
c.Assert(foo["user"], qt.Equals, "[email protected]")
c.Assert(foo["client_version"], qt.Equals, "2.2.2")
}
func TestLogMetrics(t *testing.T) {
app.LMData.Init()
flClient.SetJson(logOne)
app.getNewLogData(&flClient)
app.consumeNewLogData()
mName := "tailscale_tx_packets"
c := qt.New(t)
srcToMetric := gatherLabels("src", mName, t)
c.Assert(len(srcToMetric), qt.Equals, 3)
src := "100.111.22.33"
checkValues(src, mName, t, 40.0)
mName = "tailscale_rx_packets"
checkValues(src, mName, t, 22.0)
mName = "tailscale_tx_bytes"
checkValues(src, mName, t, 3.0)
mName = "tailscale_rx_bytes"
checkValues(src, mName, t, 60.0)
// Make a new call to get new counters and check again the metric values
// the second log file matches the first one so the values should just double.
flClient.SetJson(logTwo)
app.getNewLogData(&flClient)
app.consumeNewLogData()
mName = "tailscale_tx_packets"
checkValues(src, mName, t, 80.0)
mName = "tailscale_rx_packets"
checkValues(src, mName, t, 622.0)
mName = "tailscale_tx_bytes"
checkValues(src, mName, t, 5.0)
mName = "tailscale_rx_bytes"
checkValues(src, mName, t, 260.0)
}
func checkValues(src, mName string, t *testing.T, expected float64) {
c := qt.New(t)
val, found := getMetricValueWithSrc(src, mName, t)
fmt.Printf("\n%f, %t\n", val, found)
c.Assert(found, qt.Equals, true)
c.Assert(val, qt.Equals, expected)
}
func TestResolveNames(t *testing.T) {
c := qt.New(t)
app.LMData.Init()
flClient.SetJson(jsonDevicesTwo)
tailNet := "dummy"
app.NamesByAddr = mustMakeNamesByAddr(&tailNet, &flClient)
flClient.SetJson(logThree)
app.getNewLogData(&flClient)
app.consumeNewLogData()
mName := "tailscale_tx_packets"
src := "hello"
val, found := getMetricValueWithSrc(src, mName, t)
fmt.Printf("\n%f, %t\n", val, found)
c.Assert(found, qt.Equals, true)
c.Assert(val, qt.Equals, 130.0)
}
func getMetricValueWithSrc(src, mName string, t *testing.T) (float64, bool) {
metrics, err := prometheus.DefaultGatherer.Gather()
if err != nil {
t.Fatalf("Error gathering metrics: name: %s err=%s", mName, err)
}
//srcLabels := gatherLabels("src", mName, t)
for _, mf := range metrics {
if *mf.Name == mName {
for _, metric := range mf.GetMetric() {
//fmt.Printf("\n %s %f %v\n", *mf.Name, metric.Counter.GetValue(), srcLabels[src])
for _, label := range metric.GetLabel() {
//fmt.Printf("%s %s %s \n", *mf.Name, label.GetName(), label.GetValue())
if label.GetName() == "src" && label.GetValue() == src {
return metric.Counter.GetValue(), true
}
}
}
}
}
return 0.0, false
}
func gatherLabels(key, mName string, t *testing.T) map[string]map[string]string {
metrics, err := prometheus.DefaultGatherer.Gather()
if err != nil {
t.Fatalf("Error gathering metrics: key: %s, name: %s err=%s", key, mName, err)
}
hostToMetric := map[string]map[string]string{}
for _, mf := range metrics {
if *mf.Name == mName {
for _, metric := range mf.GetMetric() {
labels := make(map[string]string)
for _, label := range metric.GetLabel() {
labels[label.GetName()] = label.GetValue()
}
hostToMetric[labels[key]] = labels
}
}
}
return hostToMetric
}