]> granicus.if.org Git - icinga2/commitdiff
Refactor IDO query code
authorJohannes Meyer <johannes.meyer@netways.de>
Tue, 10 Dec 2013 13:55:33 +0000 (14:55 +0100)
committerJohannes Meyer <johannes.meyer@netways.de>
Mon, 16 Dec 2013 14:37:38 +0000 (15:37 +0100)
refs #5223

test/jenkins/checkresult.test
test/jenkins/files/ido_tests.py
test/jenkins/files/utils.py [new file with mode: 0644]
test/jenkins/ido_mysql.test
test/jenkins/ido_pgsql.test
test/jenkins/run_tests.conf

index 530145c24e7246d2891710f44c7b3f640242276d..6cd923f4c12b8ff48d3ec5db7141d295d8cd87f3 100755 (executable)
@@ -5,7 +5,7 @@ import os
 import sys
 import time
 
-from ido_mysql import run_query
+import utils
 
 
 STATE_OK = 0
@@ -32,6 +32,8 @@ output=%(output)s
 
 
 def main():
+    run_query = lambda q: utils.run_mysql_query(q, b'/usr/bin/mysql')
+
     # 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 ' \
index a6d3131e10c901454cb08f3dfc17b48835b4a1f3..e3db1174ac1a3a5412d7fc359d92e0207e505fbb 100644 (file)
@@ -2,7 +2,6 @@ from __future__ import unicode_literals
 
 from datetime import datetime, timedelta
 
-
 CHECK_INTERVAL = 10 # minutes; The actual interval are 5 minutes but as other
                     # tests might restart Icinga we need to take any
                     # rescheduling into account
diff --git a/test/jenkins/files/utils.py b/test/jenkins/files/utils.py
new file mode 100644 (file)
index 0000000..25940b1
--- /dev/null
@@ -0,0 +1,80 @@
+from __future__ import unicode_literals
+
+import os
+import subprocess
+
+__all__ = ['parse_statusdata', 'run_mysql_query', 'run_pgsql_query']
+
+
+MYSQL_PARAMS = b"-t -D icinga -u icinga --password=icinga -e".split()
+MYSQL_SEPARATOR = '|'
+
+PGSQL_PARAMS = b"-nq -U icinga -d icinga -c".split()
+PGSQL_SEPARATOR = '|'
+PGSQL_ENVIRONMENT = {
+    b'PGPASSWORD': b'icinga'
+    }
+
+
+def parse_statusdata(data, intelligent_cast=True):
+    parsed_data, data_type, type_data = {}, '', {}
+    for line in (l for l in data.split(os.linesep)
+                   if l and not l.startswith('#')):
+        if '{' in line:
+            data_type = line.partition('{')[0].strip()
+        elif '}' in line:
+            parsed_data.setdefault(data_type, []).append(type_data)
+        else:
+            key, _, value = line.partition('=')
+
+            if intelligent_cast:
+                value = _cast_status_value(value)
+
+            type_data[key.strip()] = value
+
+    return parsed_data
+
+
+def _cast_status_value(value):
+    try:
+        return int(value)
+    except ValueError:
+        try:
+            return float(value)
+        except ValueError:
+            return value
+
+
+def run_mysql_query(query, path):
+    p = subprocess.Popen([path] + MYSQL_PARAMS + [query.encode('utf-8')],
+                         stdout=subprocess.PIPE)
+    return _parse_mysql_result([l.decode('utf-8') for l in p.stdout.readlines()])
+
+
+def _parse_mysql_result(resultset):
+    result, header = [], None
+    for line in (l for l in resultset if MYSQL_SEPARATOR in l):
+        columns = [c.strip() for c in line[1:-3].split(MYSQL_SEPARATOR)]
+        if header is None:
+            header = columns
+        else:
+            result.append(dict((header[i], v) for i, v in enumerate(columns)))
+    return result
+
+
+def run_pgsql_query(query, path):
+    p = subprocess.Popen([path] + PGSQL_PARAMS + [query.encode('utf-8')],
+                         stdout=subprocess.PIPE, env=PGSQL_ENVIRONMENT)
+    return _parse_pgsql_result([l.decode('utf-8') for l in p.stdout.readlines()])
+
+
+def _parse_pgsql_result(resultset):
+    result, header = [], None
+    for line in (l for l in resultset if PGSQL_SEPARATOR in l):
+        columns = [c.strip() for c in line.split(PGSQL_SEPARATOR)]
+        if header is None:
+            header = columns
+        else:
+            result.append(dict((header[i], v) for i, v in enumerate(columns)))
+    return result
+
index 3d1d20a58aec08bb9db07a986463fc261a86f915..e1b6613edecc3abc17c6f15c38d41389dee06649 100755 (executable)
@@ -2,38 +2,14 @@
 from __future__ import unicode_literals
 
 import sys
-import subprocess
 
-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()
-SEPARATOR = '|'
-
-
-def run_query(query):
-    p = subprocess.Popen([MYSQL] + PARAMS + [query.encode('utf-8')],
-                         stdout=subprocess.PIPE)
-    return parse_result([l.decode('utf-8') for l in p.stdout.readlines()])
-
-
-def parse_result(resultset):
-    result, header = [], None
-    for line in (l for l in resultset if SEPARATOR in l):
-        columns = [c.strip() for c in line[1:-3].split(SEPARATOR)]
-        if header is None:
-            header = columns
-        else:
-            result.append(dict((header[i], v) for i, v in enumerate(columns)))
-    return result
+import utils
+import ido_tests
 
 
 def main():
+    run_query = lambda q: utils.run_mysql_query(q, b'/usr/bin/mysql')
+
     if not ido_tests.validate_tables([d['Tables_in_icinga']
                                       for d in run_query('show tables')]):
         return 1
@@ -87,8 +63,5 @@ def main():
 
 
 if __name__ == '__main__':
-    if not IDO_TESTS_FOUND:
-        raise
-
     sys.exit(main())
 
index 9011e87d85fe2efb2da1aac069bdc0d4dd9dd0a8..2d7dae7a26e2b76dc57a57e252dcda11fb6f2994 100755 (executable)
@@ -2,41 +2,14 @@
 from __future__ import unicode_literals
 
 import sys
-import subprocess
 
-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()
-SEPARATOR = '|'
-ENVIRONMENT = {
-    b'PGPASSWORD': b'icinga'
-    }
-
-
-def run_query(query):
-    p = subprocess.Popen([PSQL] + PARAMS + [query.encode('utf-8')],
-                         stdout=subprocess.PIPE, env=ENVIRONMENT)
-    return parse_result([l.decode('utf-8') for l in p.stdout.readlines()])
-
-
-def parse_result(resultset):
-    result, header = [], None
-    for line in (l for l in resultset if SEPARATOR in l):
-        columns = [c.strip() for c in line.split(SEPARATOR)]
-        if header is None:
-            header = columns
-        else:
-            result.append(dict((header[i], v) for i, v in enumerate(columns)))
-    return result
+import utils
+import ido_tests
 
 
 def main():
+    run_query = lambda q: utils.run_pgsql_query(q, b'/usr/bin/psql')
+
     if not ido_tests.validate_tables([d['Name'] for d in run_query('\\dt')
                                                 if d['Type'] == 'table']):
         return 1
@@ -92,8 +65,5 @@ def main():
 
 
 if __name__ == '__main__':
-    if not IDO_TESTS_FOUND:
-        raise
-
     sys.exit(main())
 
index d257e00d566bc1eb0e2af5fb2dd262d7a8dc4902..545659c3672ba0e3a24e84be854c70e922a48082 100644 (file)
     "setups": {
         "^ido_[a-z]{2}sql.test$": {
             "setup": {
-                "copy": ["files/ido_tests.py >> /tmp/ido_tests.py"]
+                "copy": [
+                    "files/ido_tests.py >> /tmp/ido_tests.py",
+                    "files/utils.py >> /tmp/utils.py"
+                ]
             },
             "teardown": {
-                "clean": ["/tmp/ido_tests.py", "/tmp/ido_tests.pyc"]
+                "clean": [
+                    "/tmp/ido_tests.py*",
+                    "/tmp/utils.py*"
+                ]
             }
         },
         "checkresult.test": {
@@ -21,7 +27,7 @@
                 "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"
+                    "files/utils.py >> /tmp/utils.py"
                 ],
                 "exec": [
                     "sudo mv /tmp/checkresult.conf /etc/icinga2/conf.d/",
@@ -31,7 +37,7 @@
                 ]
             },
             "teardown": {
-                "clean": ["/tmp/ido_mysql.py*"],
+                "clean": ["/tmp/utils.py*"],
                 "exec": [
                     "sudo rm /etc/icinga2/conf.d/checkresult.conf",
                     "sudo service icinga2 restart",