]> granicus.if.org Git - icinga2/commitdiff
Add external commands test [WIP]
authorJohannes Meyer <johannes.meyer@netways.de>
Thu, 12 Dec 2013 08:04:40 +0000 (09:04 +0100)
committerJohannes Meyer <johannes.meyer@netways.de>
Mon, 16 Dec 2013 14:37:38 +0000 (15:37 +0100)
refs #5223

.vagrant-puppet/manifests/default.pp
test/jenkins/commands_dummy.test [new file with mode: 0755]
test/jenkins/files/utils.py
test/jenkins/run_tests.conf

index a1becaa7f1f9bfc0145c5391bcc0949947a9f91e..9f06d91e9b9b80986641ca7d9032b7bf1cc922cc 100644 (file)
@@ -16,3 +16,8 @@ file { '/etc/motd':
   owner => root,
   group => root
 }
+
+user { 'vagrant':
+  groups  => 'icingacmd',
+  require => Group['icingacmd']
+}
diff --git a/test/jenkins/commands_dummy.test b/test/jenkins/commands_dummy.test
new file mode 100755 (executable)
index 0000000..9ffdf74
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+from __future__ import unicode_literals
+
+import sys
+import time
+import random
+
+import utils
+
+
+USERNAME = 'Icinga 2 Admin'
+
+
+def send_command(command):
+    return LIVESTATUS.query('COMMAND [{0}] {1}'.format(int(time.time()), command))
+
+
+def test_host_comments(hostname):
+    print repr(send_command('ADD_HOST_COMMENT;'+hostname+';0;'+USERNAME+';test'))
+    return True
+
+
+def test_service_comments(hostname, servicename):
+    pass
+
+
+def main():
+    failure = False
+
+    # Check whether host comments are properly processed
+    if not test_host_comments('localhost') or not test_host_comments('nsca-ng'):
+        failure = True
+
+    return 1 if failure else 0
+
+
+if __name__ == '__main__':
+    with utils.LiveStatusSocket('/var/run/icinga2/cmd/livestatus') as LIVESTATUS:
+        sys.exit(main())
+
index 25940b1d76eba7b15af6f993cf404826acb7995e..8827ea1112e7d395cd9af209b4fc68318e58b26d 100644 (file)
@@ -1,9 +1,12 @@
 from __future__ import unicode_literals
 
 import os
+import json
+import socket
 import subprocess
 
-__all__ = ['parse_statusdata', 'run_mysql_query', 'run_pgsql_query']
+__all__ = ['parse_statusdata', 'run_mysql_query', 'run_pgsql_query',
+           'LiveStatusSocket']
 
 
 MYSQL_PARAMS = b"-t -D icinga -u icinga --password=icinga -e".split()
@@ -78,3 +81,44 @@ def _parse_pgsql_result(resultset):
             result.append(dict((header[i], v) for i, v in enumerate(columns)))
     return result
 
+
+class LiveStatusSocket(object):
+    options = [
+        'OutputFormat: json',
+        'KeepAlive: on',
+        'ResponseHeader: fixed16'
+        ]
+
+    def __init__(self, path):
+        self.path = path
+
+    def __enter__(self):
+        self.connect()
+        return self
+
+    def __exit__(self, exc_type, exc_value, tb):
+        self.close()
+
+    def connect(self):
+        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+        self.sock.connect(self.path)
+
+    def close(self):
+        self.sock.shutdown(socket.SHUT_RDWR)
+        self.sock.close()
+
+    def query(self, command):
+        full_command = '\n'.join([command] + self.options)
+        self.send(full_command + '\n')
+        return self.recv()
+
+    def send(self, query):
+        print repr(query)
+        self.sock.sendall(query.encode('utf-8'))
+
+    def recv(self):
+        response_header = self.sock.recv(16)
+        response_code = int(response_header[:3])
+        response_length = int(response_header[3:].strip())
+        return json.loads(self.sock.recv(response_length).decode('utf-8'))
+
index 545659c3672ba0e3a24e84be854c70e922a48082..b16faf8828500938f2d8e1f560130a786f5ba07f 100644 (file)
                     "/tmp/wait_for_ido.sh pgsql && rm /tmp/wait_for_ido.sh"
                 ]
             }
+        },
+        "external_commands.test": {
+            "setup": {
+                "copy": ["files/utils.py >> /tmp/utils.py"]
+            },
+            "teardown": {
+                "clean": ["/tmp/utils.py*"],
+                "exec": ["sudo service icinga2 restart"]
+            }
         }
     }
 }