Skip to content

Commit

Permalink
Add basic system tests to heartbeat (#7272)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruflin authored and exekias committed Jun 6, 2018
1 parent 75a6bf9 commit e94ea2b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 13 deletions.
22 changes: 9 additions & 13 deletions heartbeat/tests/system/config/heartbeat.yml.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
heartbeat.monitors:
{% for monitor in monitors -%}
- type: {{ monitor.type }}
schedule: '{{ monitor.schedule|default("@every 1s") }}'

{%- if monitor.tags is defined %}
tags:
{% for tag in monitor.tags -%}
- '{{ tag }}'
{% endfor %}
{% endif -%}

{%- if monitor.hosts is defined %}
hosts:
{%- for host in monitor.hosts %}
Expand All @@ -15,19 +24,6 @@ heartbeat.monitors:
{% endfor %}
{% endif -%}

{%- if monitor.schedule is defined %}
schedule: '{{ monitor.schedule }}'
{%- else -%}
schedule: '@every 1s'
{% endif -%}

{%- if monitor.tags is defined %}
tags:
{% for tag in monitor.tags -%}
- '{{ tag }}'
{% endfor %}
{% endif -%}

{%- if monitor.fields is defined %}
{% if monitor.fields_under_root %}fields_under_root: true{% endif %}
fields:
Expand Down
80 changes: 80 additions & 0 deletions heartbeat/tests/system/test_monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from heartbeat import BaseTest
import BaseHTTPServer
import threading
from parameterized import parameterized


class Test(BaseTest):

@parameterized.expand([
"200", "404"
])
def test_http(self, status_code):
"""
Test http server
"""
status_code = int(status_code)
server = self.start_server("hello world", status_code)

self.render_config_template(
monitors=[{
"type": "http",
"urls": ["http://localhost:8181"],
}],
)

proc = self.start_beat()
self.wait_until(lambda: self.log_contains("heartbeat is running"))

self.wait_until(
lambda: self.output_has(lines=1))

proc.check_kill_and_wait()

server.shutdown()
output = self.read_output()
assert status_code == output[0]["http.response.status"]

@parameterized.expand([
("8181", "up"),
("8182", "down"),
])
def test_tcp(self, port, status):
"""
Test tcp server
"""
server = self.start_server("hello world", 200)
self.render_config_template(
monitors=[{
"type": "tcp",
"hosts": ["localhost:" + port],
}],
)

proc = self.start_beat()
self.wait_until(lambda: self.log_contains("heartbeat is running"))

self.wait_until(
lambda: self.output_has(lines=1))

proc.check_kill_and_wait()

server.shutdown()

output = self.read_output()
assert status == output[0]["monitor.status"]

def start_server(self, content, status_code):
class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(status_code)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(content)

server = BaseHTTPServer.HTTPServer(('localhost', 8181), HTTPHandler)

thread = threading.Thread(target=server.serve_forever)
thread.start()

return server

0 comments on commit e94ea2b

Please sign in to comment.