Repository navigation
Expand file tree
/
Copy pathintegration_test.go
More file actions
349 lines (298 loc) · 10.3 KB
/
Copy pathintegration_test.go
File metadata and controls
349 lines (298 loc) · 10.3 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"testing"
"time"
cfzones "github.com/cloudflare/cloudflare-go/v7/zones"
"github.com/lablabs/cloudflare-exporter/cfetch"
"github.com/lablabs/cloudflare-exporter/cfgql"
"github.com/lablabs/cloudflare-exporter/converge"
"github.com/lablabs/cloudflare-exporter/vmpush"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------
// liveGQLClient implements cfgql.GQLClient against the real Cloudflare API.
type liveGQLClient struct {
token string
}
type gqlResponse struct {
Data any `json:"data"`
Errors []*gqlError `json:"errors"`
}
type gqlError struct {
Message string `json:"message"`
}
func (e *gqlError) Error() string { return e.Message }
func (c *liveGQLClient) RunGQL(ctx context.Context, req *cfgql.GQLRequest, dest any) error {
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(req); err != nil {
return fmt.Errorf("marshal: %w", err)
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost,
"https://api.cloudflare.com/client/v4/graphql/", &body)
if err != nil {
return fmt.Errorf("new request: %w", err)
}
httpReq.Header.Set("Accept", "application/json; charset=utf-8")
httpReq.Header.Set("Authorization", "Bearer "+c.token)
resp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return fmt.Errorf("do request: %w", err)
}
defer resp.Body.Close()
gResp := gqlResponse{Data: dest}
if err := json.NewDecoder(resp.Body).Decode(&gResp); err != nil {
return fmt.Errorf("decode: %w", err)
}
errs := make([]error, 0, len(gResp.Errors))
for _, e := range gResp.Errors {
errs = append(errs, e)
}
return errors.Join(errs...)
}
func skipUnlessIntegration(t *testing.T) (token, zoneID string) {
t.Helper()
token = os.Getenv("CF_API_TOKEN")
zoneID = os.Getenv("CF_TEST_ZONE_ID")
if token == "" || zoneID == "" {
t.Skip("skipping: CF_API_TOKEN and CF_TEST_ZONE_ID required")
}
return token, zoneID
}
func skipUnlessVM(t *testing.T) string {
t.Helper()
ep := os.Getenv("VM_PUSH_ENDPOINT")
if ep == "" {
t.Skip("skipping: VM_PUSH_ENDPOINT required")
}
return ep
}
func newSink(t *testing.T, endpoint string) *vmpush.Sink {
t.Helper()
sink := vmpush.New(vmpush.Config{
Endpoint: endpoint,
Username: os.Getenv("VM_PUSH_USER"),
Password: os.Getenv("VM_PUSH_PASSWORD"),
})
require.NoError(t, sink.Ping(context.Background()), "VM endpoint unreachable")
return sink
}
// vmQueryBaseURL derives the query base from the push endpoint.
// http://host:port/api/v1/write → http://host:port
func vmQueryBaseURL(pushEndpoint string) string {
return strings.Replace(pushEndpoint, "/api/v1/write", "", 1)
}
// vmQueryRange issues a query_range request and returns the parsed result.
type vmSeries struct {
Metric map[string]string
Values []vmSample
}
type vmSample struct {
Time time.Time
Value float64
}
func vmQueryRange(t *testing.T, pushEndpoint, query string, start, end time.Time, step int) []vmSeries {
t.Helper()
queryURL := vmQueryBaseURL(pushEndpoint) + "/api/v1/query_range"
params := url.Values{
"query": {query},
"start": {strconv.FormatInt(start.Unix(), 10)},
"end": {strconv.FormatInt(end.Unix(), 10)},
"step": {strconv.Itoa(step)},
}
resp, err := http.Get(queryURL + "?" + params.Encode())
require.NoError(t, err, "VM query failed")
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode, "VM query status: %s", string(body))
var raw struct {
Status string `json:"status"`
Data struct {
Result []struct {
Metric map[string]string `json:"metric"`
Values [][]any `json:"values"`
} `json:"result"`
} `json:"data"`
}
require.NoError(t, json.Unmarshal(body, &raw), "VM response: %s", string(body))
require.Equal(t, "success", raw.Status, "VM query failed: %s", string(body))
var out []vmSeries
for _, r := range raw.Data.Result {
s := vmSeries{Metric: r.Metric}
for _, v := range r.Values {
ts := time.Unix(int64(v[0].(float64)), 0)
val, err := strconv.ParseFloat(v[1].(string), 64)
if err != nil {
continue
}
s.Values = append(s.Values, vmSample{Time: ts, Value: val})
}
out = append(out, s)
}
return out
}
// runConvergeUntilBackfill starts the full converge.Run loop and blocks
// until the backfill snapshot is pushed or the timeout expires.
func runConvergeUntilBackfill(t *testing.T, cfg converge.Config, fetcher converge.Fetcher, sink converge.Sink, timeout time.Duration) {
t.Helper()
backfillDone := make(chan struct{})
backfillDoneCB := func() {
close(
backfillDone)
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
logCtx := converge.ContextWithLogger(ctx,
logrus.WithField("component", t.Name()))
go func() {
if err := converge.Run(logCtx, cfg, fetcher, sink, backfillDoneCB); err != nil && ctx.Err() == nil {
t.Errorf("converge.Run failed: %v", err)
}
}()
select {
case <-backfillDone:
t.Log("backfill snapshot pushed")
case <-ctx.Done():
t.Fatal("timed out waiting for backfill to complete")
}
// Let VM index the data.
time.Sleep(1 * time.Second)
cancel()
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
// TestIntegrationBackfillAndPush exercises the full pipeline: fetch from
// Cloudflare, run the converge runner with backfill, push the snapshot to
// VictoriaMetrics, then query VM to verify the pushed data spans the
// expected time range.
//
// Requires: CF_API_TOKEN, CF_TEST_ZONE_ID, VM_PUSH_ENDPOINT
func TestIntegrationBackfillAndPush(t *testing.T) {
token, zoneID := skipUnlessIntegration(t)
vmEndpoint := skipUnlessVM(t)
sink := newSink(t, vmEndpoint)
client := &liveGQLClient{token: token}
zones := []cfzones.Zone{{ID: zoneID, Name: "integration-test"}}
fetcher := cfetch.New(client, zones, nil)
startTime := time.Now()
cfg := converge.Config{
Threshold: 1,
PollInterval: 10 * time.Second,
Lookback: 10 * time.Minute,
MaxBackfill: 20 * time.Minute,
BackfillChunk: 10 * time.Minute,
BackfillCallsPerTick: 5,
}
runConvergeUntilBackfill(t, cfg, fetcher, sink, 2*time.Minute)
// Query VM to verify data spans the expected range.
expectedOldest := startTime.Add(-cfg.MaxBackfill)
expectedNewest := startTime.Add(-2 * time.Minute)
results := vmQueryRange(t, vmEndpoint,
`cloudflare_zone_requests_total{zone="integration-test"}`,
expectedOldest.Add(-5*time.Minute), time.Now(), 60)
require.NotEmpty(t, results, "no cloudflare_zone_requests_total data in VM")
var oldest, newest time.Time
for _, series := range results {
for _, v := range series.Values {
if oldest.IsZero() || v.Time.Before(oldest) {
oldest = v.Time
}
if newest.IsZero() || v.Time.After(newest) {
newest = v.Time
}
}
}
t.Logf("VM data range: %s to %s", oldest.Format(time.RFC3339), newest.Format(time.RFC3339))
assert.WithinDuration(t, expectedOldest, oldest, 5*time.Minute,
"oldest data point should be near the backfill start")
assert.WithinDuration(t, expectedNewest, newest, 5*time.Minute,
"newest data point should be recent")
}
// TestIntegrationNoRateSpikes runs a 60-minute backfill through the full
// converge pipeline, then queries VM for rate() values and asserts that
// no single data point exceeds 10x the median rate. A spike like that
// indicates the counter chain cascade bug where intermediate prefix-sum
// states leak to the sink during backfill.
//
// Requires: CF_API_TOKEN, CF_TEST_ZONE_ID, VM_PUSH_ENDPOINT
func TestIntegrationNoRateSpikes(t *testing.T) {
token, zoneID := skipUnlessIntegration(t)
vmEndpoint := skipUnlessVM(t)
sink := newSink(t, vmEndpoint)
client := &liveGQLClient{token: token}
zones := []cfzones.Zone{{ID: zoneID, Name: "integration-spike-test"}}
fetcher := cfetch.New(client, zones, nil)
startTime := time.Now()
cfg := converge.Config{
Threshold: 1,
PollInterval: 10 * time.Second,
Lookback: 10 * time.Minute,
MaxBackfill: 60 * time.Minute,
BackfillChunk: 10 * time.Minute,
BackfillCallsPerTick: 10,
}
runConvergeUntilBackfill(t, cfg, fetcher, sink, 3*time.Minute)
// Query the per-minute rate over the full backfill window.
queryStart := startTime.Add(-cfg.MaxBackfill)
results := vmQueryRange(t, vmEndpoint,
`rate(cloudflare_zone_requests_total{zone="integration-spike-test"}[2m]) * 60`,
queryStart, time.Now(), 60)
require.NotEmpty(t, results, "no rate data returned from VM")
// Collect all non-zero rate values across all series.
var allRates []float64
for _, series := range results {
for _, v := range series.Values {
if v.Value > 0 && !math.IsNaN(v.Value) && !math.IsInf(v.Value, 0) {
allRates = append(allRates, v.Value)
}
}
}
require.NotEmpty(t, allRates, "no non-zero rate values found")
sort.Float64s(allRates)
median := allRates[len(allRates)/2]
maxRate := allRates[len(allRates)-1]
spikeMultiplier := 10.0
t.Logf("rate samples: %d", len(allRates))
t.Logf("median rate: %.2f req/min", median)
t.Logf("max rate: %.2f req/min", maxRate)
t.Logf("p95 rate: %.2f req/min", allRates[int(float64(len(allRates))*0.95)])
t.Logf("p99 rate: %.2f req/min", allRates[int(float64(len(allRates))*0.99)])
t.Logf("spike threshold (%.0fx median): %.2f req/min", spikeMultiplier, median*spikeMultiplier)
if maxRate > median*spikeMultiplier {
// Log the spike timestamps for debugging.
threshold := median * spikeMultiplier
t.Logf("SPIKES above %.2f req/min:", threshold)
for _, series := range results {
for _, v := range series.Values {
if v.Value > threshold {
t.Logf(" %s %.2f req/min (%.1fx median)",
v.Time.Format(time.RFC3339), v.Value, v.Value/median)
}
}
}
}
assert.LessOrEqual(t, maxRate, median*spikeMultiplier,
"rate spike detected: max %.2f req/min exceeds %.0fx median (%.2f req/min); "+
"this indicates counter chain cascade artifacts during backfill",
maxRate, spikeMultiplier, median)
}