Skip to content

Commit

Permalink
add unit tests for trigger prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Lathrop committed May 7, 2024
1 parent 35bb15b commit 00e0379
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
dependencies:
- setup_zabbix
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
- name: test - do not run tests for Zabbix < 6.4
meta: end_play
when: zabbix_version is version('6.4', '<')

- block:
# setup stuff
- include_tasks: zabbix_setup.yml

# zabbix_item module tests
- include_tasks: zabbix_tests.yml

# tear down stuff set up earlier
- include_tasks: zabbix_teardown.yml
always:
- name: "cleanup host item if tests failed"
community.zabbix.zabbix_itemprototype:
host_name: ExampleHost
discoveryrule_name: ExampleHostRule
name: ExampleHostItem
state: absent
ignore_errors: true

- name: "cleanup template item if tests failed"
community.zabbix.zabbix_itemprototype:
template_name: ExampleTemplate
discoveryrule_name: ExampleTemplateRule
name: ExampleTemplateItem
state: absent
ignore_errors: true

- name: cleanup discovery rule on host if tests failed
community.zabbix.zabbix_discoveryrule:
name: ExampleHostRule
host_name: ExampleHost
state: absent
ignore_errors: true

- name: cleanup discovery rule on template if tests failed
community.zabbix.zabbix_discoveryrule:
name: ExampleTemplateRule
template_name: ExampleTemplate
state: absent
ignore_errors: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---

- name: Create test template
community.zabbix.zabbix_template:
template_name: ExampleTemplate
template_groups:
- Templates

- name: Create test host
community.zabbix.zabbix_host:
host_name: ExampleHost
host_groups:
- Linux servers
- Zabbix servers
link_templates:
- ExampleTemplate
status: enabled
state: present
interfaces:
- type: 1
main: 1
useip: 1
ip: 10.1.1.1
dns: ""
port: "10050"

- name: Create new Zabbix discoveryrule on host
community.zabbix.zabbix_discoveryrule:
name: ExampleHostRule
host_name: ExampleHost
params:
type: zabbix_agent_active
key: 'vfs.fs.discovery'
interval: 1m
enabled: True
state: present

- name: Create host item prototype
community.zabbix.zabbix_itemprototype:
name: '{% raw %}{#FSNAME}:ExampleItemPrototype{% endraw %}'
discoveryrule_name: ExampleHostRule
host_name: ExampleHost
params:
type: zabbix_agent_active
key: '{% raw %}vfs.fs.size[{#FSNAME}, pused]{% endraw %}'
value_type: numeric_unsigned
interval: 1m
state: present
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- name: remove test template item
community.zabbix.zabbix_item:
name: ExampleTemplateItem
template_name: ExampleTemplate
state: absent

- name: remove test host
community.zabbix.zabbix_host:
host_name: ExampleHost
state: absent

- name: remove test template
community.zabbix.zabbix_template:
template_name: ExampleTemplate
state: absent
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
---

- name: test - create new Zabbix trigger on host with many options set
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
host_name: ExampleHost
description: test host trigger
params:
severity: warning
expression: '{% raw %}last(/ExampleHost/vfs.fs.size[{#FSNAME}, pused])>80{% endraw %}'
manual_close: True
enabled: True
tags:
- tag: tag
value: value
state: present
register: zbxhosttrigger_new

- name: assert that trigger was created
ansible.builtin.assert:
that: zbxhosttrigger_new is changed

- name: test - create same Zabbix trigger once again
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
host_name: ExampleHost
description: test host trigger
params:
severity: warning
expression: '{% raw %}last(/ExampleHost/vfs.fs.size[{#FSNAME}, pused])>80{% endraw %}'
manual_close: True
enabled: True
tags:
- tag: tag
value: value
state: present
register: zbxhosttrigger_existing

- name: assert that nothing has been changed
ansible.builtin.assert:
that: not zbxhosttrigger_existing is changed

- name: test - update existing zabbix trigger
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
host_name: ExampleHost
params:
manual_close: False
state: present
register: zbxhosttrigger_changed

- name: expect to succeed and that things changed
ansible.builtin.assert:
that: zbxhosttrigger_changed is changed

- name: test - attempt to delete previously created zabbix trigger
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
host_name: ExampleHost
state: absent
register: zbxhosttrigger_existing_delete

- name: assert that trigger was deleted
ansible.builtin.assert:
that: zbxhosttrigger_existing_delete is changed

- name: test - attempt to delete non-existing zabbix trigger
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
host_name: ExampleHost
state: absent
register: zbxhosttrigger_missing_delete

- name: assert that nothing has been changed
ansible.builtin.assert:
that: not zbxhosttrigger_missing_delete is changed

- name: remove host rule
community.zabbix.zabbix_discoveryrule:
name: ExampleHostRule
host_name: ExampleHost
state: absent

- name: Create new Zabbix discoveryrule on template
community.zabbix.zabbix_discoveryrule:
name: ExampleTemplateRule
template_name: ExampleTemplate
params:
type: zabbix_agent_active
key: 'vfs.fs.discovery'
interval: 1m
enabled: True
state: present

- name: Create template item prototype
community.zabbix.zabbix_itemprototype:
name: '{% raw %}{#FSNAME}:ExampleItemPrototype{% endraw %}'
discoveryrule_name: ExampleTemplateRule
template_name: ExampleTemplate
params:
type: zabbix_agent_active
key: '{% raw %}vfs.fs.size[{#FSNAME}, pused]{% endraw %}'
value_type: numeric_unsigned
interval: 1m
state: present

- name: test - create new Zabbix trigger on template with many options set
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
template_name: ExampleTemplate
description: test template trigger
params:
severity: warning
expression: '{% raw %}last(/ExampleTemplate/vfs.fs.size[{#FSNAME}, pused])>80{% endraw %}'
manual_close: True
enabled: True
tags:
- tag: tag
value: value
state: present
register: zbxtemptrigger_new

- name: assert that trigger was created
ansible.builtin.assert:
that: zbxtemptrigger_new is changed

- name: test - create same Zabbix trigger once again
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
template_name: ExampleTemplate
description: test template trigger
params:
severity: warning
expression: '{% raw %}last(/ExampleTemplate/vfs.fs.size[{#FSNAME}, pused])>80{% endraw %}'
manual_close: True
enabled: True
tags:
- tag: tag
value: value
state: present
register: zbxtemptrigger_existing

- name: assert that nothing has been changed
ansible.builtin.assert:
that: not zbxtemptrigger_existing is changed

- name: test - update existing zabbix trigger
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
template_name: ExampleTemplate
params:
manual_close: False
state: present
register: zbxtemptrigger_changed

- name: expect to succeed and that things changed
ansible.builtin.assert:
that: zbxtemptrigger_changed is changed

- name: test - attempt to delete previously created zabbix trigger
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
template_name: ExampleTemplate
state: absent
register: zbxtemptrigger_existing_delete

- name: assert that trigger was deleted
ansible.builtin.assert:
that: zbxtemptrigger_existing_delete is changed

- name: test - attempt to delete non-existing zabbix trigger
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
template_name: ExampleTemplate
state: absent
register: zbxtemptrigger_missing_delete

- name: assert that nothing has been changed
ansible.builtin.assert:
that: not zbxtemptrigger_missing_delete is changed

0 comments on commit 00e0379

Please sign in to comment.