Skip to content

Commit

Permalink
Updated graylog connector test to work with pagination
Browse files Browse the repository at this point in the history
The test always returned non-empty results, which resulted in an infinite loop
  • Loading branch information
DerGeras committed Aug 27, 2024
1 parent 6274838 commit 806a4f8
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions pkg/connectors/graylog/connector_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package graylog

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -33,6 +36,23 @@ func TestConnector(t *testing.T) {
func testConnector(endpoints map[string]string) (*Connector, *httptest.Server) {
mockServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)

b, _ := io.ReadAll(req.Body)
buf := bytes.NewBuffer(b)
decoder := json.NewDecoder(buf)

var request eventsSearchParameters
err := decoder.Decode(&request)
if err != nil {
panic(err)
}

if request.Page > 1 {
if _, err := res.Write([]byte(mockEventEmptyResult)); err != nil {
panic(err)
}
}

for endpoint, body := range endpoints {
if strings.Contains(req.URL.Path, endpoint) {
if _, err := res.Write([]byte(body)); err != nil {
Expand Down Expand Up @@ -88,14 +108,14 @@ const mockEventSearchResult = `
],
"parameters": {
"page": 1,
"per_page": 25,
"per_page": 100,
"timerange": {
"type": "relative",
"range": 60
"range": 600
},
"query": "",
"filter": {
"alerts": "only",
"alerts": "include",
"event_definitions": []
},
"sort_by": "timestamp",
Expand All @@ -121,3 +141,34 @@ const mockEventSearchResult = `
}
}
`

const mockEventEmptyResult = `
{
"events": [],
"used_indices": [
"gl-events_24",
"gl-system-events_1"
],
"parameters": {
"page": 2,
"per_page": 100,
"timerange": {
"type": "relative",
"range": 600
},
"query": "",
"filter": {
"alerts": "include",
"event_definitions": []
},
"sort_by": "timestamp",
"sort_direction": "desc"
},
"total_events": 4,
"duration": 3,
"context": {
"event_definitions": {},
"streams": {}
}
}
`

0 comments on commit 806a4f8

Please sign in to comment.