]> granicus.if.org Git - icinga2/commitdiff
Add checkresult tests
authorJohannes Meyer <johannes.meyer@netways.de>
Mon, 9 Dec 2013 15:03:31 +0000 (16:03 +0100)
committerJohannes Meyer <johannes.meyer@netways.de>
Mon, 16 Dec 2013 14:37:37 +0000 (15:37 +0100)
refs #5223

test/jenkins/checkresult.test [new file with mode: 0755]
test/jenkins/files/configs/checkresult.conf [new file with mode: 0644]
test/jenkins/files/wait_for_ido.sh [new file with mode: 0755]
test/jenkins/ido_mysql.test
test/jenkins/ido_pgsql.test
test/jenkins/run_tests.conf

diff --git a/test/jenkins/checkresult.test b/test/jenkins/checkresult.test
new file mode 100755 (executable)
index 0000000..530145c
--- /dev/null
@@ -0,0 +1,85 @@
+#!/usr/bin/env python
+from __future__ import unicode_literals
+
+import os
+import sys
+import time
+
+from ido_mysql import run_query
+
+
+STATE_OK = 0
+TYPE_PASSIVE_CHECK = 1
+
+CHECK_INTERVAL = 300 # seconds
+CHECKRESULT_READ_INTERVAL = 5 # seconds
+CHECKRESULT_LOCATION = '/tmp/icinga2/checkresults'
+CHECKRESULT_TEMPLATE = """
+host_name=%(hostname)s
+service_description=%(servicename)s
+check_type=%(check_type)s
+check_options=0
+scheduled_check=0
+reschedule_check=0
+latency=0
+start_time=%(start_time)s
+finish_time=%(finish_time)s
+early_timeout=0
+exited_ok=%(excited_ok)s
+return_code=%(return_code)s
+output=%(output)s
+"""
+
+
+def main():
+    # We need to wait a bit first as Icinga processes a
+    # checkresult only if its newer than the last check
+    query = 'select unix_timestamp(s.last_check) as last_check ' \
+            'from icinga_servicestatus as s ' \
+            'inner join icinga_services as c ' \
+            'on s.service_object_id = c.service_object_id ' \
+            "where c.display_name = 'PassiveService1'"
+    state_time = float(next(iter(run_query(query)), {}).get('last_check', '0'))
+    if state_time == 0:
+        print '"PassiveService1" seems not to have been checked yet'
+        return 1
+
+    if (state_time + CHECK_INTERVAL) - time.time() < 30:
+        time.sleep(45)
+
+    # Now pass the checkresult in
+    with open(os.path.join(CHECKRESULT_LOCATION, 'cfoobar'), 'w') as f:
+        f.write(CHECKRESULT_TEMPLATE % {
+            'hostname': 'nsca-ng',
+            'servicename': 'PassiveService1',
+            'check_type': TYPE_PASSIVE_CHECK,
+            'start_time': time.time(),
+            'finish_time': time.time(),
+            'excited_ok': '1',
+            'return_code': STATE_OK,
+            'output': 'Passing in CheckResult header files works!'
+            })
+
+    # And notfiy Icinga that the file has been completely written...
+    with open(os.path.join(CHECKRESULT_LOCATION, 'cfoobar.ok'), 'w') as f:
+        pass
+
+    # Lastly check whether the service changed its state
+    time.sleep(CHECKRESULT_READ_INTERVAL * 2)
+
+    query = 'select s.output ' \
+            'from icinga_servicestatus as s ' \
+            'inner join icinga_services as c ' \
+            'on s.service_object_id = c.service_object_id ' \
+            "where c.display_name = 'PassiveService1'"
+    output = next(iter(run_query(query)), {}).get('output', '')
+    if output != 'Passing in CheckResult header files works!':
+        print 'Checkresult header files seem not to be processed properly'
+        return 1
+
+    print 'Checkresult header files are processed properly'
+    return 0
+
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/test/jenkins/files/configs/checkresult.conf b/test/jenkins/files/configs/checkresult.conf
new file mode 100644 (file)
index 0000000..d5cfd93
--- /dev/null
@@ -0,0 +1,5 @@
+library "compat"
+
+object CheckResultReader "reader" {
+    spool_dir = "/tmp/icinga2/checkresults"
+}
diff --git a/test/jenkins/files/wait_for_ido.sh b/test/jenkins/files/wait_for_ido.sh
new file mode 100755 (executable)
index 0000000..776f4fc
--- /dev/null
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+TIMEOUT=30
+
+case $1 in
+    mysql)
+        TYPE='MySQL'
+        CMD='/usr/bin/mysql -t -D icinga -u icinga --password=icinga -e'
+        ;;
+    pgsql)
+        TYPE='PostgreSQL'
+        CMD='/usr/bin/psql -nq -U icinga -d icinga -c'
+        export PGPASSWORD='icinga'
+        ;;
+    *)
+        echo "No IDO type specifier given!"
+        exit 1
+        ;;
+esac
+
+tries=1
+while true
+do
+    out="`$CMD 'select * from icinga_hosts'`"
+
+    if [ $tries -lt $TIMEOUT ] && [ "$out" == "" ];
+    then
+        sleep 1
+        tries=$(($tries + 1))
+    else
+        if [ $tries -eq $TIMEOUT ];
+        then
+            echo "IDO ($TYPE) does not have any hosts or is not responding" >&2
+        fi
+
+        break
+    fi
+done
index 6f92085964c1ae0a2a1d98480a84cc3428953653..3d1d20a58aec08bb9db07a986463fc261a86f915 100755 (executable)
@@ -4,7 +4,12 @@ from __future__ import unicode_literals
 import sys
 import subprocess
 
-import ido_tests
+try:
+    import ido_tests
+    IDO_TESTS_FOUND = True
+except ImportError:
+    IDO_TESTS_FOUND = False
+
 
 MYSQL = b"/usr/bin/mysql"
 PARAMS = b"-t -D icinga -u icinga --password=icinga -e".split()
@@ -82,5 +87,8 @@ def main():
 
 
 if __name__ == '__main__':
+    if not IDO_TESTS_FOUND:
+        raise
+
     sys.exit(main())
 
index b03c33a5b7cf6b8c8a96de44f1a322c38e397d5a..9011e87d85fe2efb2da1aac069bdc0d4dd9dd0a8 100755 (executable)
@@ -4,7 +4,12 @@ from __future__ import unicode_literals
 import sys
 import subprocess
 
-import ido_tests
+try:
+    import ido_tests
+    IDO_TESTS_FOUND = True
+except ImportError:
+    IDO_TESTS_FOUND = False
+
 
 PSQL = b"/usr/bin/psql"
 PARAMS = b"-nq -U icinga -d icinga -c".split()
@@ -87,5 +92,8 @@ def main():
 
 
 if __name__ == '__main__':
+    if not IDO_TESTS_FOUND:
+        raise
+
     sys.exit(main())
 
index 864af44e391bebb3b6606f29c514857ec918a44b..d257e00d566bc1eb0e2af5fb2dd262d7a8dc4902 100644 (file)
             "teardown": {
                 "clean": ["/tmp/ido_tests.py", "/tmp/ido_tests.pyc"]
             }
+        },
+        "checkresult.test": {
+            "setup": {
+                "copy": [
+                    "files/configs/checkresult.conf >> /tmp/checkresult.conf",
+                    "files/wait_for_ido.sh >> /tmp/wait_for_ido.sh",
+                    "ido_mysql.test >> /tmp/ido_mysql.py"
+                ],
+                "exec": [
+                    "sudo mv /tmp/checkresult.conf /etc/icinga2/conf.d/",
+                    "mkdir -p -m 0777 /tmp/icinga2/checkresults",
+                    "sudo service icinga2 restart",
+                    "/tmp/wait_for_ido.sh mysql"
+                ]
+            },
+            "teardown": {
+                "clean": ["/tmp/ido_mysql.py*"],
+                "exec": [
+                    "sudo rm /etc/icinga2/conf.d/checkresult.conf",
+                    "sudo service icinga2 restart",
+                    "rmdir /tmp/icinga2/checkresults",
+                    "/tmp/wait_for_ido.sh mysql",
+                    "/tmp/wait_for_ido.sh pgsql && rm /tmp/wait_for_ido.sh"
+                ]
+            }
         }
     }
 }