mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 20:36:12 +02:00
send metrics immediately if agents are added or removed (#2296)
This commit is contained in:
parent
9beb5388cb
commit
507da49b5a
4 changed files with 254 additions and 160 deletions
|
@ -1057,90 +1057,6 @@ func TestAPICPush(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAPICSendMetrics(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
duration time.Duration
|
||||
expectedCalls int
|
||||
setUp func(*apic)
|
||||
metricsInterval time.Duration
|
||||
}{
|
||||
{
|
||||
name: "basic",
|
||||
duration: time.Millisecond * 30,
|
||||
metricsInterval: time.Millisecond * 5,
|
||||
expectedCalls: 5,
|
||||
setUp: func(api *apic) {},
|
||||
},
|
||||
{
|
||||
name: "with some metrics",
|
||||
duration: time.Millisecond * 30,
|
||||
metricsInterval: time.Millisecond * 5,
|
||||
expectedCalls: 5,
|
||||
setUp: func(api *apic) {
|
||||
api.dbClient.Ent.Machine.Delete().ExecX(context.Background())
|
||||
api.dbClient.Ent.Machine.Create().
|
||||
SetMachineId("1234").
|
||||
SetPassword(testPassword.String()).
|
||||
SetIpAddress("1.2.3.4").
|
||||
SetScenarios("crowdsecurity/test").
|
||||
SetLastPush(time.Time{}).
|
||||
SetUpdatedAt(time.Time{}).
|
||||
ExecX(context.Background())
|
||||
|
||||
api.dbClient.Ent.Bouncer.Delete().ExecX(context.Background())
|
||||
api.dbClient.Ent.Bouncer.Create().
|
||||
SetIPAddress("1.2.3.6").
|
||||
SetName("someBouncer").
|
||||
SetAPIKey("foobar").
|
||||
SetRevoked(false).
|
||||
SetLastPull(time.Time{}).
|
||||
ExecX(context.Background())
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
httpmock.RegisterResponder("POST", "http://api.crowdsec.net/api/metrics/", httpmock.NewBytesResponder(200, []byte{}))
|
||||
httpmock.Activate()
|
||||
defer httpmock.Deactivate()
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
url, err := url.ParseRequestURI("http://api.crowdsec.net/")
|
||||
require.NoError(t, err)
|
||||
|
||||
apiClient, err := apiclient.NewDefaultClient(
|
||||
url,
|
||||
"/api",
|
||||
fmt.Sprintf("crowdsec/%s", version.String()),
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
api := getAPIC(t)
|
||||
api.pushInterval = time.Millisecond
|
||||
api.pushIntervalFirst = time.Millisecond
|
||||
api.apiClient = apiClient
|
||||
api.metricsInterval = tc.metricsInterval
|
||||
api.metricsIntervalFirst = tc.metricsInterval
|
||||
tc.setUp(api)
|
||||
|
||||
stop := make(chan bool)
|
||||
httpmock.ZeroCallCounters()
|
||||
go api.SendMetrics(stop)
|
||||
time.Sleep(tc.duration)
|
||||
stop <- true
|
||||
|
||||
info := httpmock.GetCallCountInfo()
|
||||
noResponderCalls := info["NO_RESPONDER"]
|
||||
responderCalls := info["POST http://api.crowdsec.net/api/metrics/"]
|
||||
assert.LessOrEqual(t, absDiff(tc.expectedCalls, responderCalls), 2)
|
||||
assert.Zero(t, noResponderCalls)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPICPull(t *testing.T) {
|
||||
api := getAPIC(t)
|
||||
tests := []struct {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue