Merge branch 'feature/test-external-commands-5345' into next

resolves #5345
This commit is contained in:
Johannes Meyer 2014-01-20 17:21:19 +01:00
parent f8e6673123
commit 876587d727
8 changed files with 1305 additions and 5 deletions

View File

@ -13,6 +13,10 @@ class icinga2 {
alias => 'icinga2-doc'
}
package { 'mailx':
ensure => installed,
}
service { 'icinga2':
enable => true,
ensure => running,

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,33 @@
/**
* A new service group required by external_commands.test::test_servicegroup_commands
*/
object ServiceGroup "aservicegroup" {
display_name = "aServiceGroup"
}
/**
* The two default hostgroups
*/
object HostGroup "linux-servers" {
display_name = "Linux Servers"
}
object HostGroup "windows-servers" {
display_name = "Windows Servers"
}
/**
* This template is essentially the same as the default one but with a servicegroup added
*/
template Service "generic-service" {
max_check_attempts = 3,
check_interval = 5m,
retry_interval = 1m,
enable_perfdata = true,
groups = ["aservicegroup"],
notifications["mail-icingaadmin"] = {
templates = [ "mail-notification" ],
user_groups = [ "icingaadmins" ]
}
}

View File

@ -0,0 +1,15 @@
/**
* This is a copy of the default configuration file "ido-mysql.conf" with the "categories" attribute added
*/
library "db_ido_mysql"
object IdoMysqlConnection "ido-mysql" {
user = "icinga",
password = "icinga",
host = "localhost",
database = "icinga",
categories = (DbCatCheck | DbCatConfig | DbCatState | DbCatAcknowledgement |
DbCatComment | DbCatDowntime | DbCatEventHandler | DbCatExternalCommand | DbCatFlapping |
DbCatLog | DbCatNotification | DbCatProgramStatus | DbCatRetention | DbCatStateHistory)
}

View File

@ -0,0 +1,3 @@
template Notification "mail-notification" {
notification_interval = 10
}

View File

@ -1,6 +1,7 @@
from __future__ import unicode_literals
import os
import time
import json
import socket
import subprocess
@ -61,7 +62,8 @@ def _parse_mysql_result(resultset):
if header is None:
header = columns
else:
result.append(dict((header[i], v) for i, v in enumerate(columns)))
result.append(dict((header[i], v if v != 'NULL' else None)
for i, v in enumerate(columns)))
return result
@ -96,6 +98,8 @@ class LiveStatusSocket(object):
def __init__(self, path):
self.path = path
self._connected = False
def __enter__(self):
self.connect()
return self
@ -106,10 +110,22 @@ class LiveStatusSocket(object):
def connect(self):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(self.path)
self._connected = True
def reconnect(self, timeout=30):
start = time.time()
while not self._connected and time.time() - start < timeout:
try:
self.connect()
except socket.error, error:
if error.errno != 111:
raise
def close(self):
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
if self._connected:
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
self._connected = False
def query(self, command):
self.send(command)
@ -121,10 +137,16 @@ class LiveStatusSocket(object):
return response
def send(self, query):
if not self._connected:
raise RuntimeError('Tried to write to closed socket')
full_query = '\n'.join([query] + self.options)
self.sock.sendall((full_query + '\n\n').encode('utf-8'))
def recv(self):
if not self._connected:
raise RuntimeError('Tried to read from closed socket')
response = b''
response_header = self.sock.recv(16)
response_code = int(response_header[:3])

View File

@ -49,11 +49,31 @@
},
"external_commands.test": {
"setup": {
"copy": ["files/utils.py >> /tmp/utils.py"]
"copy": [
"files/utils.py >> /tmp/utils.py",
"files/configs/notifications.conf >> /tmp/no_spam.conf",
"files/configs/ido_checkresults.conf >> /tmp/ido_checkresults.conf",
"files/configs/groups.conf >> /tmp/groups.conf"
],
"exec": [
"sudo mv /etc/icinga2/conf.d/generic-service.conf /etc/icinga2/conf.d/generic-service.conf.bak",
"sudo mv /etc/icinga2/conf.d/groups.conf /etc/icinga2/conf.d/groups.conf.bak",
"sudo mv /tmp/groups.conf /etc/icinga2/conf.d/",
"sudo mv /tmp/ido_checkresults.conf /etc/icinga2/conf.d/",
"sudo mv /tmp/no_spam.conf /etc/icinga2/conf.d/",
"sudo service icinga2 restart"
]
},
"teardown": {
"clean": ["/tmp/utils.py*"],
"exec": ["sudo service icinga2 restart"]
"exec": [
"sudo rm /etc/icinga2/conf.d/groups.conf",
"sudo mv /etc/icinga2/conf.d/groups.conf.bak /etc/icinga2/conf.d/groups.conf",
"sudo mv /etc/icinga2/conf.d/generic-service.conf.bak /etc/icinga2/conf.d/generic-service.conf",
"sudo rm /etc/icinga2/conf.d/ido_checkresults.conf",
"sudo rm /etc/icinga2/conf.d/no_spam.conf",
"sudo service icinga2 restart"
]
}
},
"eventhandler.test": {

View File

@ -68,6 +68,7 @@ class TestSuite(object):
self._copy_test(path)
self._results[test_name] = self._run_test(path)
self._apply_setup_routines(test_name, 'teardown')
self._remove_test(test_name)
def _apply_setup_routines(self, test_name, context):
instructions = next((t[1].get(context)
@ -98,6 +99,10 @@ class TestSuite(object):
self._copy_file(path, os.path.join(self._config['settings']['test_root'],
os.path.basename(path)))
def _remove_test(self, test_name):
test_root = self._config['settings']['test_root']
self._remove_file(os.path.join(test_root, test_name))
def _run_test(self, path):
command = self._config['commands']['exec']
target = os.path.join(self._config['settings']['test_root'],