apic_metrics_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package apiserver
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "testing"
  7. "time"
  8. "github.com/jarcoal/httpmock"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "github.com/crowdsecurity/go-cs-lib/pkg/version"
  12. "github.com/crowdsecurity/crowdsec/pkg/apiclient"
  13. )
  14. func TestAPICSendMetrics(t *testing.T) {
  15. tests := []struct {
  16. name string
  17. duration time.Duration
  18. expectedCalls int
  19. setUp func(*apic)
  20. metricsInterval time.Duration
  21. }{
  22. {
  23. name: "basic",
  24. duration: time.Millisecond * 30,
  25. metricsInterval: time.Millisecond * 5,
  26. expectedCalls: 5,
  27. setUp: func(api *apic) {},
  28. },
  29. {
  30. name: "with some metrics",
  31. duration: time.Millisecond * 30,
  32. metricsInterval: time.Millisecond * 5,
  33. expectedCalls: 5,
  34. setUp: func(api *apic) {
  35. api.dbClient.Ent.Machine.Delete().ExecX(context.Background())
  36. api.dbClient.Ent.Machine.Create().
  37. SetMachineId("1234").
  38. SetPassword(testPassword.String()).
  39. SetIpAddress("1.2.3.4").
  40. SetScenarios("crowdsecurity/test").
  41. SetLastPush(time.Time{}).
  42. SetUpdatedAt(time.Time{}).
  43. ExecX(context.Background())
  44. api.dbClient.Ent.Bouncer.Delete().ExecX(context.Background())
  45. api.dbClient.Ent.Bouncer.Create().
  46. SetIPAddress("1.2.3.6").
  47. SetName("someBouncer").
  48. SetAPIKey("foobar").
  49. SetRevoked(false).
  50. SetLastPull(time.Time{}).
  51. ExecX(context.Background())
  52. },
  53. },
  54. }
  55. httpmock.RegisterResponder("POST", "http://api.crowdsec.net/api/metrics/", httpmock.NewBytesResponder(200, []byte{}))
  56. httpmock.Activate()
  57. defer httpmock.Deactivate()
  58. for _, tc := range tests {
  59. tc := tc
  60. t.Run(tc.name, func(t *testing.T) {
  61. url, err := url.ParseRequestURI("http://api.crowdsec.net/")
  62. require.NoError(t, err)
  63. apiClient, err := apiclient.NewDefaultClient(
  64. url,
  65. "/api",
  66. fmt.Sprintf("crowdsec/%s", version.String()),
  67. nil,
  68. )
  69. require.NoError(t, err)
  70. api := getAPIC(t)
  71. api.pushInterval = time.Millisecond
  72. api.pushIntervalFirst = time.Millisecond
  73. api.apiClient = apiClient
  74. api.metricsInterval = tc.metricsInterval
  75. api.metricsIntervalFirst = tc.metricsInterval
  76. tc.setUp(api)
  77. stop := make(chan bool)
  78. httpmock.ZeroCallCounters()
  79. go api.SendMetrics(stop)
  80. time.Sleep(tc.duration)
  81. stop <- true
  82. info := httpmock.GetCallCountInfo()
  83. noResponderCalls := info["NO_RESPONDER"]
  84. responderCalls := info["POST http://api.crowdsec.net/api/metrics/"]
  85. assert.LessOrEqual(t, absDiff(tc.expectedCalls, responderCalls), 2)
  86. assert.Zero(t, noResponderCalls)
  87. })
  88. }
  89. }