]> granicus.if.org Git - icinga2/commitdiff
Add eventhandler test
authorJohannes Meyer <johannes.meyer@netways.de>
Fri, 20 Dec 2013 08:58:05 +0000 (09:58 +0100)
committerJohannes Meyer <johannes.meyer@netways.de>
Fri, 20 Dec 2013 08:59:12 +0000 (09:59 +0100)
resolves #5372

.vagrant-puppet/manifests/default.pp
test/jenkins/eventhandler.test [new file with mode: 0755]
test/jenkins/files/configs/eventhandler.conf [new file with mode: 0644]
test/jenkins/run_tests.conf [changed mode: 0644->0755]

index 9f06d91e9b9b80986641ca7d9032b7bf1cc922cc..348267cb77384c3e9a05e40803aa80a96e80ef54 100644 (file)
@@ -18,6 +18,6 @@ file { '/etc/motd':
 }
 
 user { 'vagrant':
-  groups  => 'icingacmd',
-  require => Group['icingacmd']
+  groups  => ['icinga', 'icingacmd'],
+  require => [User['icinga'], Group['icingacmd']]
 }
diff --git a/test/jenkins/eventhandler.test b/test/jenkins/eventhandler.test
new file mode 100755 (executable)
index 0000000..090fc06
--- /dev/null
@@ -0,0 +1,210 @@
+#!/usr/bin/env python
+from __future__ import unicode_literals
+
+import os
+import sys
+import time
+
+import utils
+
+
+LS_HOST_COLUMNS = [
+    'name',
+    'name',
+    'display_name',
+    'display_name',
+    None,
+    'state',
+    'state_type',
+    'current_attempt',
+    'max_check_attempts',
+    None,
+    'last_state',
+    None,
+    'last_state_change',
+    None,
+    'latency',
+    'execution_time',
+    'plugin_output',
+    None,
+    'last_check',
+    'address',
+    'address6'
+    ]
+
+LS_SVC_COLUMNS = [
+    'description',
+    'display_name',
+    'display_name',
+    None,
+    'state',
+    'state_type',
+    'current_attempt',
+    'max_check_attempts',
+    None,
+    'last_state',
+    None,
+    'last_state_change',
+    None,
+    'latency',
+    'execution_time',
+    'plugin_output',
+    'perf_data',
+    'last_check',
+    'host_num_services',
+    'host_num_services_ok',
+    'host_num_services_warn',
+    'host_num_services_unknown',
+    'host_num_services_crit'
+    ]
+
+STATE_MAP = {
+    'SOFT': 0,
+    'HARD': 1
+    }
+
+
+def send_command(command):
+    try:
+        return send_query('COMMAND [{0}] {1}'.format(int(time.time()), command))
+    except utils.LiveStatusError, error:
+        sys.stderr.write('Failed to execute command: {0}\n\n{1}\n'
+                         ''.format(command, error))
+
+
+def send_query(query):
+    response = LIVESTATUS.query(query + '\nColumnHeaders: on')
+    if response:
+        header, result = response.pop(0), {}
+        return [dict((header[i], v) for i, v in enumerate(r)) for r in response]
+    return []
+
+
+def get_one(query):
+    return next(iter(send_query(query)), {})
+
+
+def get_event_output():
+    try:
+        with open('/tmp/test_event.out') as f:
+            remove = True
+            return f.read().rstrip().split('|')
+    except (IOError, OSError):
+        remove = False
+    finally:
+        if remove:
+            os.system('sudo rm -f /tmp/test_event.out')
+
+
+def convert_output(value):
+    try:
+        return int(value)
+    except ValueError:
+        try:
+            return float(value)
+        except ValueError:
+            return STATE_MAP.get(value, value)
+
+
+def success(msg):
+    print '[OK] {0}'.format(msg).encode('utf-8')
+
+
+def fail(msg):
+    print '[FAIL] {0}'.format(msg).encode('utf-8')
+
+
+def info(msg):
+    print '[INFO] {0}'.format(msg).encode('utf-8')
+
+
+def main():
+    send_command('CHANGE_HOST_EVENT_HANDLER;localhost;test_event')
+    host_info = get_one('GET hosts\nFilter: name = localhost'
+                        '\nColumns: event_handler')
+    if host_info.get('event_handler') != 'test_event':
+        fail('Could not assign eventcommand "test_event" to host "localhost"')
+        return 1
+    success('Successfully assigned an eventcommand to host "localhost"')
+
+    send_command('PROCESS_HOST_CHECK_RESULT;localhost;1;A negative result to'
+                 ' trigger an eventhandler|some interesting perfdata!')
+    event_output = get_event_output()
+    if not event_output:
+        send_command('CHANGE_HOST_EVENT_HANDLER;localhost;')
+        fail('Could not trigger the eventcommand')
+        return 1
+    success('Successfully triggered the eventcommand')
+    failure = False
+
+    info('Checking host macros...')
+    host_info = get_one('GET hosts\nFilter: name = localhost\nColumns: {0}'
+                        ''.format(' '.join(c for c in LS_HOST_COLUMNS if c)))
+    if event_output[0] != host_info['name']*2:
+        failure = True
+        fail('Escaping environment variables seems not to properly working')
+        fail(' Expected: {0!r} Got: {1!r}'.format(host_info['name']*2,
+                                                  event_output[0]))
+    for i, column in enumerate(LS_HOST_COLUMNS[1:], 1):
+        if column is not None:
+            output_value = convert_output(event_output[i])
+            if output_value != host_info[column]:
+                failure = True
+                fail('{0!r} is unequal to {1!r} ({2})'.format(output_value,
+                                                              host_info[column],
+                                                              column))
+
+    info('Checking service macros...')
+    svc_info = get_one('GET services\nFilter: description = ping4\nColumns: {0}'
+                       ''.format(' '.join(c for c in LS_SVC_COLUMNS if c)))
+    for i, column in enumerate(LS_SVC_COLUMNS, len(LS_HOST_COLUMNS)):
+        if column is not None:
+            output_value = convert_output(event_output[i])
+            if output_value != svc_info[column]:
+                failure = True
+                fail('{0!r} is unequal to {1!r} ({2})'.format(output_value,
+                                                              svc_info[column],
+                                                              column))
+
+    info('Checking global macros...')
+
+    info('Checking command macros...')
+    if convert_output(event_output[-1]) != 1337:
+        failure = True
+        fail('The command macro "custom_macro" is not being substituted')
+
+    send_command('DISABLE_HOST_EVENT_HANDLER;localhost')
+    send_command('PROCESS_HOST_CHECK_RESULT;localhost;0;A positive result that'
+                 ' should not trigger an eventhandler')
+    if get_event_output():
+        failure = True
+        fail('Could not disable the eventcommand')
+    else:
+        success('Successfully disabled the eventcommand')
+
+    send_command('ENABLE_HOST_EVENT_HANDLER;localhost')
+    host_info = get_one('GET hosts\nFilter: name = localhost'
+                        '\nColumns: event_handler_enabled')
+    if host_info['event_handler_enabled'] != 1:
+        failure = True
+        fail('Could not re-enable the eventcommand')
+    else:
+        success('Successfully re-enabled the eventcommand')
+
+    send_command('CHANGE_HOST_EVENT_HANDLER;localhost;')
+    host_info = get_one('GET hosts\nFilter: name = localhost'
+                        '\nColumns: event_handler')
+    if host_info['event_handler']:
+        failure = True
+        fail('Could not remove eventcommand "test_event"'
+             ' assigned to host "localhost"')
+    else:
+        success('Successfully removed the eventcommand'
+                ' assigned to host "localhost"')
+
+    return 1 if failure else 0
+
+
+if __name__ == '__main__':
+    with utils.LiveStatusSocket('/var/run/icinga2/cmd/livestatus') as LIVESTATUS:
+        sys.exit(main())
diff --git a/test/jenkins/files/configs/eventhandler.conf b/test/jenkins/files/configs/eventhandler.conf
new file mode 100644 (file)
index 0000000..6e79da7
--- /dev/null
@@ -0,0 +1,59 @@
+object EventCommand "test_event" inherits "plugin-event-command" {
+    command = {{{echo "\
+$$HOSTNAME$HOSTNAME$\
+|$HOSTNAME$\
+|$HOSTDISPLAYNAME$\
+|$HOSTALIAS$\
+|$HOSTSTATE$\
+|$HOSTSTATEID$\
+|$HOSTSTATETYPE$\
+|$HOSTATTEMPT$\
+|$MAXHOSTATTEMPT$\
+|$LASTHOSTSTATE$\
+|$LASTHOSTSTATEID$\
+|$LASTHOSTSTATETYPE$\
+|$LASTHOSTSTATECHANGE$\
+|$HOSTDURATIONSEC$\
+|$HOSTLATENCY$\
+|$HOSTEXECUTIONTIME$\
+|$HOSTOUTPUT$\
+|$HOSTPERFDATA$\
+|$LASTHOSTCHECK$\
+|$HOSTADDRESS$\
+|$HOSTADDRESS6$\
+|$SERVICEDESC$\
+|$SERVICEDISPLAYNAME$\
+|$SERVICECHECKCOMMAND$\
+|$SERVICESTATE$\
+|$SERVICESTATEID$\
+|$SERVICESTATETYPE$\
+|$SERVICEATTEMPT$\
+|$MAXSERVICEATTEMPT$\
+|$LASTSERVICESTATE$\
+|$LASTSERVICESTATEID$\
+|$LASTSERVICESTATETYPE$\
+|$LASTSERVICESTATECHANGE$\
+|$SERVICEDURATIONSEC$\
+|$SERVICELATENCY$\
+|$SERVICEEXECUTIONTIME$\
+|$SERVICEOUTPUT$\
+|$SERVICEPERFDATA$\
+|$LASTSERVICECHECK$\
+|$TOTALHOSTSERVICES$\
+|$TOTALHOSTSERVICESOK$\
+|$TOTALHOSTSERVICESWARNING$\
+|$TOTALHOSTSERVICESUNKNOWN$\
+|$TOTALHOSTSERVICESCRITICAL$\
+|$TIMET$\
+|$LONGDATETIME$\
+|$SHORTDATETIME$\
+|$DATE$\
+|$TIME$\
+|$custom_macro$\
+" > /tmp/test_event.out}}},
+
+    export_macros = ["HOSTNAME"],
+    macros = {
+        custom_macro = 1337
+    }
+}
old mode 100644 (file)
new mode 100755 (executable)
index b16faf8..a0b6d92
                 "clean": ["/tmp/utils.py*"],
                 "exec": ["sudo service icinga2 restart"]
             }
+        },
+        "eventhandler.test": {
+            "setup": {
+                "copy": [
+                    "files/utils.py >> /tmp/utils.py",
+                    "files/configs/eventhandler.conf >> /tmp/eventhandler.conf"
+                ],
+                "exec": [
+                    "sudo mv /tmp/eventhandler.conf /etc/icinga2/conf.d/",
+                    "sudo service icinga2 restart"
+                ]
+            },
+            "teardown": {
+                "clean": ["/tmp/utils.py*"],
+                "exec": [
+                    "sudo rm /etc/icinga2/conf.d/eventhandler.conf",
+                    "sudo service icinga2 restart"
+                ]
+            }
         }
     }
 }