Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listen explicitly on localhost on heartbeat TCP tests #15583

Merged
merged 2 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NOTICE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Elastic Beats
Copyright 2014-2019 Elasticsearch BV
Copyright 2014-2020 Elasticsearch BV
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this change but required in the python 3 branch.


This product includes software developed by The Apache Software
Foundation (http://www.apache.org/).
Expand Down
27 changes: 24 additions & 3 deletions heartbeat/monitors/active/tcp/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,43 @@ func setupServer(t *testing.T, serverCreator func(http.Handler) *httptest.Server
return server, port
}

// newLocalhostTestServer starts a server listening on the IP resolved from `localhost`
// httptest.NewServer() binds explicitly on 127.0.0.1 (or [::1] if ipv4 is not available).
// The IP resolved from `localhost` can be a different one, like 127.0.1.1.
func newLocalhostTestServer(handler http.Handler) *httptest.Server {
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
panic("failed to listen on localhost: " + err.Error())
}

server := &httptest.Server{
Listener: listener,
Config: &http.Server{Handler: handler},
}
server.Start()

return server
}

func TestUpEndpointJob(t *testing.T) {
server, port := setupServer(t, httptest.NewServer)
server, port := setupServer(t, newLocalhostTestServer)
defer server.Close()

serverURL, err := url.Parse(server.URL)
require.NoError(t, err)

event := testTCPCheck(t, "localhost", port)

testslike.Test(
t,
lookslike.Strict(lookslike.Compose(
hbtest.BaseChecks("127.0.0.1", "up", "tcp"),
hbtest.BaseChecks(serverURL.Hostname(), "up", "tcp"),
hbtest.SummaryChecks(1, 0),
hbtest.SimpleURLChecks(t, "tcp", "localhost", port),
hbtest.RespondingTCPChecks(),
lookslike.MustCompile(map[string]interface{}{
"resolve": map[string]interface{}{
"ip": "127.0.0.1",
"ip": serverURL.Hostname(),
"rtt.us": isdef.IsDuration,
},
}),
Expand Down