-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Filebeat][httpjson] Adding possibility to use first_event in cursor …
…context (#23437) (#23491) * adding possibility to use first_event context in httpjson * fixing typo * Add transform context state test * updating changelog Co-authored-by: Marc Guasch <marc.guasch@elastic.co> (cherry picked from commit d4e85e0) Co-authored-by: Marius Iversen <marius.iversen@elastic.co>
- Loading branch information
Showing
8 changed files
with
166 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
x-pack/filebeat/input/httpjson/internal/v2/request_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package v2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/v7/libbeat/common" | ||
"github.com/elastic/beats/v7/libbeat/logp" | ||
beattest "github.com/elastic/beats/v7/libbeat/publisher/testing" | ||
) | ||
|
||
func TestCtxAfterDoRequest(t *testing.T) { | ||
registerRequestTransforms() | ||
t.Cleanup(func() { registeredTransforms = newRegistry() }) | ||
|
||
// mock timeNow func to return a fixed value | ||
timeNow = func() time.Time { | ||
t, _ := time.Parse(time.RFC3339, "2002-10-02T15:00:00Z") | ||
return t | ||
} | ||
t.Cleanup(func() { timeNow = time.Now }) | ||
|
||
// test with dateCursorHandler to have different payloads each request | ||
testServer := httptest.NewServer(dateCursorHandler()) | ||
t.Cleanup(testServer.Close) | ||
|
||
cfg := common.MustNewConfigFrom(map[string]interface{}{ | ||
"interval": 1, | ||
"request.method": "GET", | ||
"request.url": testServer.URL, | ||
"request.transforms": []interface{}{ | ||
map[string]interface{}{ | ||
"set": map[string]interface{}{ | ||
"target": "url.params.$filter", | ||
"value": "alertCreationTime ge [[.cursor.timestamp]]", | ||
"default": `alertCreationTime ge [[formatDate (now (parseDuration "-10m")) "2006-01-02T15:04:05Z"]]`, | ||
}, | ||
}, | ||
}, | ||
"cursor": map[string]interface{}{ | ||
"timestamp": map[string]interface{}{ | ||
"value": `[[index .last_response.body "@timestamp"]]`, | ||
}, | ||
}, | ||
}) | ||
|
||
config := defaultConfig() | ||
assert.NoError(t, cfg.Unpack(&config)) | ||
|
||
log := logp.NewLogger("") | ||
ctx := context.Background() | ||
client, err := newHTTPClient(ctx, config, nil, log) | ||
assert.NoError(t, err) | ||
|
||
requestFactory := newRequestFactory(config.Request, nil, log) | ||
pagination := newPagination(config, client, log) | ||
responseProcessor := newResponseProcessor(config.Response, pagination, log) | ||
|
||
requester := newRequester(client, requestFactory, responseProcessor, log) | ||
|
||
trCtx := emptyTransformContext() | ||
trCtx.cursor = newCursor(config.Cursor, log) | ||
|
||
// first request | ||
assert.NoError(t, requester.doRequest(ctx, trCtx, statelessPublisher{&beattest.FakeClient{}})) | ||
|
||
assert.EqualValues( | ||
t, | ||
common.MapStr{"timestamp": "2002-10-02T15:00:00Z"}, | ||
trCtx.cursorMap(), | ||
) | ||
assert.EqualValues( | ||
t, | ||
&common.MapStr{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"}, | ||
trCtx.firstEventClone(), | ||
) | ||
assert.EqualValues( | ||
t, | ||
&common.MapStr{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"}, | ||
trCtx.lastEventClone(), | ||
) | ||
lastResp := trCtx.lastResponseClone() | ||
// ignore since has dynamic date and content length values | ||
// and is not relevant | ||
lastResp.header = nil | ||
assert.EqualValues(t, | ||
&response{ | ||
page: 1, | ||
url: newURL(fmt.Sprintf("%s?%s", testServer.URL, "%24filter=alertCreationTime+ge+2002-10-02T14%3A50%3A00Z")), | ||
body: common.MapStr{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"}, | ||
}, | ||
lastResp, | ||
) | ||
|
||
// second request | ||
assert.NoError(t, requester.doRequest(ctx, trCtx, statelessPublisher{&beattest.FakeClient{}})) | ||
|
||
assert.EqualValues( | ||
t, | ||
common.MapStr{"timestamp": "2002-10-02T15:00:01Z"}, | ||
trCtx.cursorMap(), | ||
) | ||
|
||
// this does not change | ||
assert.EqualValues( | ||
t, | ||
&common.MapStr{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"}, | ||
trCtx.firstEventClone(), | ||
) | ||
|
||
assert.EqualValues( | ||
t, | ||
&common.MapStr{"@timestamp": "2002-10-02T15:00:01Z", "foo": "bar"}, | ||
trCtx.lastEventClone(), | ||
) | ||
|
||
lastResp = trCtx.lastResponseClone() | ||
lastResp.header = nil | ||
assert.EqualValues(t, | ||
&response{ | ||
page: 1, | ||
url: newURL(fmt.Sprintf("%s?%s", testServer.URL, "%24filter=alertCreationTime+ge+2002-10-02T15%3A00%3A00Z")), | ||
body: common.MapStr{"@timestamp": "2002-10-02T15:00:01Z", "foo": "bar"}, | ||
}, | ||
lastResp, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters