From bc9c4c9e4f77762aa777f5d78ba626b665c909c2 Mon Sep 17 00:00:00 2001 From: urso Date: Sun, 2 Jul 2017 20:17:28 +0200 Subject: [PATCH] Enhance fields/tags tests in heartbeat system tests --- .../tests/system/config/heartbeat.yml.j2 | 59 +++++++++++-- heartbeat/tests/system/test_base.py | 87 +++++++++++++++++-- 2 files changed, 135 insertions(+), 11 deletions(-) diff --git a/heartbeat/tests/system/config/heartbeat.yml.j2 b/heartbeat/tests/system/config/heartbeat.yml.j2 index 96c918fe6dd..664715c60b6 100644 --- a/heartbeat/tests/system/config/heartbeat.yml.j2 +++ b/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") }} diff --git a/heartbeat/tests/system/test_base.py b/heartbeat/tests/system/test_base.py index 45b2f09b413..a6e29aa0976 100644 --- a/heartbeat/tests/system/test_base.py +++ b/heartbeat/tests/system/test_base.py @@ -9,20 +9,96 @@ 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() @@ -30,6 +106,5 @@ def test_monitor_config(self): 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