Skip to content

Commit

Permalink
Fix flaky test test_stats (elastic#17034) (elastic#17039)
Browse files Browse the repository at this point in the history
Tests on stats API can finish even before the mocked beat has completed
its initialization, so it can receive the signal to stop before it can
handle signals, terminating with an status code -15 (killed by SIGTERM),
instead of the expected status code 0.

Check that the beat has been completely started before trying to kill
it, and move common code to setUp/tearDown.

Fix also an incorrect error message.

(cherry picked from commit 825cf05)
  • Loading branch information
jsoriano authored Mar 17, 2020
1 parent d384371 commit fec800f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
4 changes: 2 additions & 2 deletions libbeat/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func (s *Server) Start() {
s.log.Info("Starting stats endpoint")
go func(l net.Listener) {
s.log.Infof("Metrics endpoint listening on: %s (configured: %s)", l.Addr().String(), s.config.Host)
http.Serve(l, s.mux)
s.log.Infof("Finished starting stats endpoint: %s", l.Addr().String())
err := http.Serve(l, s.mux)
s.log.Infof("Stats endpoint (%s) finished: %v", l.Addr().String(), err)
}(s.l)
}

Expand Down
35 changes: 11 additions & 24 deletions libbeat/tests/system/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@


class Test(BaseTest):
def setUp(self):
super(BaseTest, self).setUp()
self.render_config_template()
self.proc = self.start_beat(extra_args=["-E", "http.enabled=true"])
self.wait_until(lambda: self.log_contains("Starting stats endpoint"))

def tearDown(self):
super(BaseTest, self).tearDown()
# Wait till the beat is completely started so it can handle SIGTERM
self.wait_until(lambda: self.log_contains("mockbeat start running."))
self.proc.check_kill_and_wait()

def test_root(self):
"""
Test / http endpoint
"""
self.render_config_template(
)

proc = self.start_beat(extra_args=["-E", "http.enabled=true"])
self.wait_until(lambda: self.log_contains("Starting stats endpoint"))

r = requests.get("http://localhost:5066")
assert r.status_code == 200

Expand All @@ -24,18 +29,10 @@ def test_root(self):
assert data["beat"] == "mockbeat"
assert data["version"] == "9.9.9"

proc.check_kill_and_wait()

def test_stats(self):
"""
Test /stats http endpoint
"""
self.render_config_template(
)

proc = self.start_beat(extra_args=["-E", "http.enabled=true"])
self.wait_until(lambda: self.log_contains("Starting stats endpoint"))

r = requests.get("http://localhost:5066/stats")
assert r.status_code == 200

Expand All @@ -44,19 +41,9 @@ def test_stats(self):
# Test one data point
assert data["libbeat"]["config"]["scans"] == 0

proc.check_kill_and_wait()

def test_error(self):
"""
Test not existing http endpoint
"""
self.render_config_template(
)

proc = self.start_beat(extra_args=["-E", "http.enabled=true"])
self.wait_until(lambda: self.log_contains("Starting stats endpoint"))

r = requests.get("http://localhost:5066/not-exist")
assert r.status_code == 404

proc.check_kill_and_wait()

0 comments on commit fec800f

Please sign in to comment.