From 7ebb8e16cf9b37898cd167c8e523c7e10fb6891a Mon Sep 17 00:00:00 2001 From: Colin Wilk Date: Sat, 3 Jun 2023 20:35:14 +0200 Subject: Add tests for hook functionality and lineinfile parameters Signed-off-by: Colin Wilk --- molecule/default/converge.yml | 93 +++++++++++++++++++--- .../default/tests/test_check_prometheus_targets.py | 59 +++++++++++--- 2 files changed, 130 insertions(+), 22 deletions(-) diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index a94e801..8f2b399 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -1,5 +1,5 @@ --- -- name: Converge +- name: Simple deploy hosts: application pre_tasks: - name: Create targets @@ -12,24 +12,18 @@ become: true delegate_to: '{{ prometheus_target_host }}' loop: - - /opt/target1.yml - - /opt/target2.yml - - /opt/target3.yml + - /opt/simple_target1.yml + - /opt/simple_target2.yml + - /opt/simple_target3.yml vars: prometheus_target_host: prometheus - prometheus_target_handler_command_enabled: true - prometheus_target_handler_command: - cmd: echo test - prometheus_target_handler_shell_enabled: true - prometheus_target_handler_shell: - cmd: echo test prometheus_target_exporter_defaults: node_exporter: - path: /opt/target1.yml + path: /opt/simple_target1.yml host: '{{ inventory_hostname }}' blackbox_exporter: - path: /opt/target2.yml + path: /opt/simple_target2.yml host: '{{ inventory_hostname }}_AA' roles: @@ -41,4 +35,77 @@ - role: kliwniloc.prometheus_target prometheus_target_exporter: - id: blackbox_exporter - path: /opt/target3.yml + path: /opt/simple_target3.yml + + +- name: Deploy with hooks + hosts: application + pre_tasks: + - name: Create targets + ansible.builtin.file: + path: '{{ item }}' + state: touch + modification_time: preserve + access_time: preserve + mode: '0644' + become: true + delegate_to: '{{ prometheus_target_host }}' + loop: + - /opt/hook_target.yml + - /opt/hook1 + - /opt/hook1 + + vars: + prometheus_target_host: prometheus + prometheus_target_handler_command_enabled: true + prometheus_target_handler_command: + cmd: touch /opt/hook1 + prometheus_target_handler_shell_enabled: true + prometheus_target_handler_shell: + cmd: touch /opt/hook2 + + roles: + - role: kliwniloc.prometheus_target + prometheus_target_exporter: + - id: node_exporter + host: application + path: /opt/hook_target.yml + + +- name: Deploy with lineinfile + hosts: application + pre_tasks: + - name: Create test user + ansible.builtin.user: + name: prometheus + become: true + delegate_to: '{{ prometheus_target_host }}' + + - name: Create target + ansible.builtin.copy: + dest: /opt/lineinfile.yml + owner: prometheus + group: prometheus + mode: '0600' + force: false + # yamllint disable rule:indentation + content: | + - labels: + my: label + targets: + - existing:9100 + # yamllint enable rule:indentation + become: true + delegate_to: '{{ prometheus_target_host }}' + + vars: + prometheus_target_host: prometheus + prometheus_target_strategy_lineinfile_prefix: ' - ' + prometheus_target_strategy_lineinfile_suffix: :9100 + + roles: + - role: kliwniloc.prometheus_target + prometheus_target_exporter: + - id: node_exporter + path: /opt/lineinfile.yml + host: '{{ inventory_hostname }}' diff --git a/molecule/default/tests/test_check_prometheus_targets.py b/molecule/default/tests/test_check_prometheus_targets.py index a793478..2a45661 100644 --- a/molecule/default/tests/test_check_prometheus_targets.py +++ b/molecule/default/tests/test_check_prometheus_targets.py @@ -1,16 +1,57 @@ testinfra_hosts = ['prometheus'] - -def test_check_hosts_added(host): - t1 = host.file('/opt/target1.yml') - t2 = host.file('/opt/target2.yml') - t3 = host.file('/opt/target3.yml') +""" +Test functionality of defining exporters and default fallbacks +""" +def test_check_hosts_added_simple(host): + t1 = host.file('/opt/simple_target1.yml') + t2 = host.file('/opt/simple_target2.yml') + t3 = host.file('/opt/simple_target3.yml') assert t1.exists assert t2.exists assert t3.exists - assert ' - application' in t1.content_string - assert ' - test1' in t2.content_string - assert ' - test2' in t2.content_string - assert ' - application_AA' in t3.content_string + assert t1.content_string == \ + ' - application\n' + + + assert t2.content_string == \ + ' - test1\n' \ + ' - test2\n' + + assert t3.content_string == \ + ' - application_AA\n' + + +""" +Test hook functionality +""" +def test_check_hosts_added_hooks(host): + t1 = host.file('/opt/hook_target.yml') + t2 = host.file('/opt/hook1') + t3 = host.file('/opt/hook2') + + assert t1.content_string == \ + ' - application\n' + + assert t2.exists + assert t3.exists + + +""" +Test lineinfile strategy parameters +""" +def test_check_host_added_lineinfile(host): + t1 = host.file('/opt/lineinfile.yml') + + assert t1.user == 'prometheus' + assert t1.group == 'prometheus' + assert t1.mode == 0o600 + + assert t1.content_string == \ + '- labels:\n' \ + ' my: label\n' \ + ' targets:\n' \ + ' - existing:9100\n' \ + ' - application:9100\n' -- cgit v1.2.3