Skip to content

Commit

Permalink
Enhance fields/tags tests in heartbeat system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
urso committed Jul 3, 2017
1 parent 49a674c commit bc9c4c9
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 11 deletions.
59 changes: 54 additions & 5 deletions heartbeat/tests/system/config/heartbeat.yml.j2
@@ -1,11 +1,60 @@
heartbeat.monitors:
- type: http
urls: ["http://localhost:9200"]
{% for monitor in monitors -%}
- type: {{ monitor.type }}
{%- if monitor.hosts is defined %}
hosts:
{%- for host in monitor.hosts %}
- '{{ host }}'
{% endfor -%}
{% endif -%}

{%- if monitor.urls is defined %}
urls:
{%- for url in monitor.urls %}
- '{{ url }}'
{% endfor %}
{% endif -%}

{%- if monitor.schedule is defined %}
schedule: '{{ monitor.schedule }}'
{%- else -%}
schedule: '@every 1s'
tags: ["http_monitor_tags"]
fields_under_root: true
{% 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:
hello: world
{% for k, v in monitor.fields.items() -%}
{{ k }}: {{ v }}
{% endfor %}
{% endif %}
{% endfor -%}

{%- if shipper_name %}
name: {{ shipper_name }}
{% endif %}

{%- if tags %}
tags:
{% for tag in tags -%}
- {{ tag }}
{% endfor -%}
{% endif %}

{%- if fields %}
{% if fields_under_root %}fields_under_root: true{% endif %}
fields:
{% for k, v in fields.items() -%}
{{ k }}: {{ v }}
{% endfor -%}
{% endif %}

output.file:
path: {{ output_file_path|default(beat.working_dir + "/output") }}
Expand Down
87 changes: 81 additions & 6 deletions heartbeat/tests/system/test_base.py
Expand Up @@ -9,27 +9,102 @@ def test_base(self):
"""
Basic test with exiting Heartbeat normally
"""

config = {
"monitors": [
{
"type": "http",
"urls": ["http://localhost:9200"],
}
]
}

self.render_config_template(
path=os.path.abspath(self.working_dir) + "/log/*"
path=os.path.abspath(self.working_dir) + "/log/*",
**config
)

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

def test_monitor_config(self):
def test_fields_under_root(self):
"""
Basic test with fields and tags in monitor
"""

self.run_fields(
local={
"tags": ["local"],
"fields_under_root": True,
"fields": {"local": "field", "env": "dev"},
},
top={
"tags": ["global"],
"fields": {
"global": "field",
"env": "prod",
"level": "overwrite"
},
"fields_under_root": True,
},
expected={
"tags": ["global", "local"],
"global": "field",
"local": "field",
"env": "dev"
}
)

def test_fields_not_under_root(self):
"""
Basic test with fields and tags (not under root)
"""
self.run_fields(
local={
"tags": ["local"],
"fields": {"local": "field", "env": "dev", "num": 1}
},
top={
"tags": ["global"],
"fields": {
"global": "field",
"env": "prod",
"level": "overwrite",
"num": 0
}
},
expected={
"tags": ["global", "local"],
"fields.global": "field",
"fields.local": "field",
"fields.env": "dev"
}
)

def run_fields(self, expected, local=None, top=None):
monitor = {
"type": "http",
"urls": ["http://localhost:9200"],
}
if local:
monitor.update(local)

config = {
"monitors": [monitor]
}
if top:
config.update(top)

self.render_config_template(
path=os.path.abspath(self.working_dir) + "/*"
path=os.path.abspath(self.working_dir) + "/*",
**config
)

heartbeat_proc = self.start_beat()
self.wait_until(lambda: self.output_lines() > 0)
heartbeat_proc.check_kill_and_wait()

doc = self.read_output()[0]
assert doc["hello"] == "world"
assert doc["tags"] == ["http_monitor_tags"]
assert "fields" not in doc
self.assertDictContainsSubset(expected, doc)
return doc

0 comments on commit bc9c4c9

Please sign in to comment.