-
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.
x-pack/filebeat/input/http_endpoint: allow http_endpoint instances to…
… share ports
- Loading branch information
Showing
4 changed files
with
294 additions
and
18 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
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,195 @@ | ||
// 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 http_endpoint | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
v2 "github.com/elastic/beats/v7/filebeat/input/v2" | ||
"github.com/elastic/elastic-agent-libs/logp" | ||
"github.com/elastic/elastic-agent-libs/mapstr" | ||
) | ||
|
||
var serverPoolTests = []struct { | ||
name string | ||
cfgs []*httpEndpoint | ||
events []target | ||
want []mapstr.M | ||
}{ | ||
{ | ||
name: "single", | ||
cfgs: []*httpEndpoint{{ | ||
addr: "127.0.0.1:9001", | ||
config: config{ | ||
ResponseCode: 200, | ||
ResponseBody: `{"message": "success"}`, | ||
ListenAddress: "127.0.0.1", | ||
ListenPort: "9001", | ||
URL: "/", | ||
Prefix: "json", | ||
ContentType: "application/json", | ||
}, | ||
}}, | ||
events: []target{ | ||
{url: "http://127.0.0.1:9001/", event: `{"a":1}`}, | ||
{url: "http://127.0.0.1:9001/", event: `{"b":2}`}, | ||
{url: "http://127.0.0.1:9001/", event: `{"c":3}`}, | ||
}, | ||
want: []mapstr.M{ | ||
{"json": mapstr.M{"a": int64(1)}}, | ||
{"json": mapstr.M{"b": int64(2)}}, | ||
{"json": mapstr.M{"c": int64(3)}}, | ||
}, | ||
}, | ||
{ | ||
name: "distinct_ports", | ||
cfgs: []*httpEndpoint{ | ||
{ | ||
addr: "127.0.0.1:9001", | ||
config: config{ | ||
ResponseCode: 200, | ||
ResponseBody: `{"message": "success"}`, | ||
ListenAddress: "127.0.0.1", | ||
ListenPort: "9001", | ||
URL: "/a/", | ||
Prefix: "json", | ||
ContentType: "application/json", | ||
}, | ||
}, | ||
{ | ||
addr: "127.0.0.1:9002", | ||
config: config{ | ||
ResponseCode: 200, | ||
ResponseBody: `{"message": "success"}`, | ||
ListenAddress: "127.0.0.1", | ||
ListenPort: "9002", | ||
URL: "/b/", | ||
Prefix: "json", | ||
ContentType: "application/json", | ||
}, | ||
}, | ||
}, | ||
events: []target{ | ||
{url: "http://127.0.0.1:9001/a/", event: `{"a":1}`}, | ||
{url: "http://127.0.0.1:9002/b/", event: `{"b":2}`}, | ||
{url: "http://127.0.0.1:9001/a/", event: `{"c":3}`}, | ||
}, | ||
want: []mapstr.M{ | ||
{"json": mapstr.M{"a": int64(1)}}, | ||
{"json": mapstr.M{"b": int64(2)}}, | ||
{"json": mapstr.M{"c": int64(3)}}, | ||
}, | ||
}, | ||
{ | ||
name: "shared_ports", | ||
cfgs: []*httpEndpoint{ | ||
{ | ||
addr: "127.0.0.1:9001", | ||
config: config{ | ||
ResponseCode: 200, | ||
ResponseBody: `{"message": "success"}`, | ||
ListenAddress: "127.0.0.1", | ||
ListenPort: "9001", | ||
URL: "/a/", | ||
Prefix: "json", | ||
ContentType: "application/json", | ||
}, | ||
}, | ||
{ | ||
addr: "127.0.0.1:9001", | ||
config: config{ | ||
ResponseCode: 200, | ||
ResponseBody: `{"message": "success"}`, | ||
ListenAddress: "127.0.0.1", | ||
ListenPort: "9001", | ||
URL: "/b/", | ||
Prefix: "json", | ||
ContentType: "application/json", | ||
}, | ||
}, | ||
}, | ||
events: []target{ | ||
{url: "http://127.0.0.1:9001/a/", event: `{"a":1}`}, | ||
{url: "http://127.0.0.1:9001/b/", event: `{"b":2}`}, | ||
{url: "http://127.0.0.1:9001/a/", event: `{"c":3}`}, | ||
}, | ||
want: []mapstr.M{ | ||
{"json": mapstr.M{"a": int64(1)}}, | ||
{"json": mapstr.M{"b": int64(2)}}, | ||
{"json": mapstr.M{"c": int64(3)}}, | ||
}, | ||
}, | ||
} | ||
|
||
type target struct { | ||
url string | ||
event string | ||
} | ||
|
||
func TestServerPool(t *testing.T) { | ||
for _, test := range serverPoolTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
servers := pool{servers: make(map[string]*server)} | ||
|
||
var pub publisher | ||
ctx, cancel := newCtx("server_pool_test", test.name) | ||
var wg sync.WaitGroup | ||
for _, cfg := range test.cfgs { | ||
cfg := cfg | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
_ = servers.serve(ctx, cfg, &pub) // Always returns a non-nil error. | ||
}() | ||
} | ||
time.Sleep(time.Second) | ||
|
||
for i, e := range test.events { | ||
resp, err := http.Post(e.url, "application/json", strings.NewReader(e.event)) | ||
if err != nil { | ||
t.Fatalf("failed to post event #%d: %v", i, err) | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("unexpected response status code: %s (%d)\nresp: %s", | ||
resp.Status, resp.StatusCode, dump(resp.Body)) | ||
} | ||
} | ||
cancel() | ||
wg.Wait() | ||
var got []mapstr.M | ||
for _, e := range pub.events { | ||
got = append(got, e.Fields) | ||
} | ||
if !cmp.Equal(got, test.want) { | ||
t.Errorf("unexpected result:\n--- got\n--- want\n%s", cmp.Diff(got, test.want)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func newCtx(log, id string) (_ v2.Context, cancel func()) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
return v2.Context{ | ||
Logger: logp.NewLogger(log), | ||
ID: id, | ||
Cancelation: ctx, | ||
}, cancel | ||
} | ||
|
||
func dump(r io.ReadCloser) string { | ||
defer r.Close() | ||
var buf bytes.Buffer | ||
io.Copy(&buf, r) | ||
return buf.String() | ||
} |