Skip to content

Commit

Permalink
Merge pull request #23 from CiscoZeus/update-api
Browse files Browse the repository at this point in the history
Adds support for alerts and trigalerts
  • Loading branch information
victorfonsec4 committed Jan 23, 2017
2 parents 89f958f + 78d5bd3 commit d1067de
Show file tree
Hide file tree
Showing 10 changed files with 751 additions and 259 deletions.
2 changes: 2 additions & 0 deletions example/zeus-sample-syslog-file.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def splitfunction(syslogFile):
{"timestamp": gettime(line), "message": getmsg(line)}
for line in syslogFile]
return t


path = os.getcwd() + "/example_syslog"
token = raw_input("Enter Token: ")

Expand Down
232 changes: 192 additions & 40 deletions tests/test_zeus_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
import json
import mock
import unittest
from zeus.interfaces.utils import validateDates, ZeusException

from zeus import client

FAKE_TOKEN = 'ZeUsRoCkS'
FAKE_SERVER = 'http://zeus.rocks'
FAKE_SERVER = 'https://zeus.rocks'


class TestZeusClient(unittest.TestCase):
Expand All @@ -38,76 +39,81 @@ def setUp(self):
# Setting up a Zeus client with a fake token:
self.z = client.ZeusClient(FAKE_TOKEN, FAKE_SERVER)

def test_initialization_no_http(self):
def test_initialization_no_https(self):
z = client.ZeusClient(FAKE_TOKEN, "zeus.rocks")
assert z.server == "http://zeus.rocks"
assert z.server == "https://zeus.rocks"
z = client.ZeusClient(FAKE_TOKEN, "http://zeus.rocks")
assert z.server == "https://zeus.rocks"

def test_validate_dates(self):
# normal
from_date = 12345
to_date = 12346
self.z._validateDates(from_date, to_date)
validateDates(from_date, to_date)
# None value
from_date = None
to_date = 12346
self.z._validateDates(from_date, to_date)
validateDates(from_date, to_date)
# invalid value
from_date = 'wrongvalue'
to_date = 12346
self.assertRaises(
client.ZeusException, self.z._validateDates, from_date, to_date)
ZeusException, validateDates, from_date, to_date)
# inversed order
from_date = 12346
to_date = 12345
self.assertRaises(
client.ZeusException, self.z._validateDates, from_date, to_date)
ZeusException, validateDates, from_date, to_date)

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_post_empty_log(self, mock_requests):
logs = []
self.z.sendLog('ZeusTest', logs)
mock_requests.post.assert_called_with(FAKE_SERVER + '/logs/' +
FAKE_TOKEN + '/ZeusTest/',
data={"logs": json.dumps(logs)})
data={"logs": json.dumps(logs)},
headers=None)

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_post_single_log(self, mock_requests):
logs = [{"timestamp": 123541423, "key": "TestLog", "key2": 123}]
self.z.sendLog('ZeusTest', logs)
mock_requests.post.assert_called_with(FAKE_SERVER + '/logs/' +
FAKE_TOKEN + '/ZeusTest/',
data={"logs": json.dumps(logs)})
data={"logs": json.dumps(logs)},
headers=None)

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_post_single_log_wrong_name(self, mock_requests):
logs = [{"message": "TestLog", "value": 23}]
self.assertRaises(
client.ZeusException, self.z.sendLog, 'W.rongName', logs)
ZeusException, self.z.sendLog, 'W.rongName', logs)
self.assertRaises(
client.ZeusException, self.z.sendLog, '_WrongName', logs)
ZeusException, self.z.sendLog, '_WrongName', logs)
self.assertRaises(
client.ZeusException, self.z.sendLog, 'W#?rongName', logs)
ZeusException, self.z.sendLog, 'W#?rongName', logs)
self.assertRaises(
client.ZeusException, self.z.sendLog, 'W-rongName', logs)
ZeusException, self.z.sendLog, 'W-rongName', logs)
self.assertRaises(
client.ZeusException, self.z.sendLog, None, logs)
ZeusException, self.z.sendLog, None, logs)
self.assertRaises(
client.ZeusException, self.z.sendLog, '', logs)
ZeusException, self.z.sendLog, '', logs)
self.assertRaises(
client.ZeusException, self.z.sendLog, '0123456789ABCDEF' * 16,
ZeusException, self.z.sendLog, '0123456789ABCDEF' * 16,
logs)

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_post_multiple_logs(self, mock_requests):
logs = [{"timestamp": 123541423, "message": "TestLog"},
{"timestamp": 123541424, "message": "TestLog2"},
{"timestamp": 123541425, "message": "TestLog3"}, ]
self.z.sendLog('ZeusTest', logs)
mock_requests.post.assert_called_with(FAKE_SERVER + '/logs/' +
FAKE_TOKEN + '/ZeusTest/',
data={"logs": json.dumps(logs)})
data={"logs": json.dumps(logs)},
headers=None)

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_get_logs(self, mock_requests):
self.z.getLog('ZeusTest',
attribute_name='message',
Expand All @@ -127,40 +133,42 @@ def test_get_logs(self, mock_requests):
'offset': 23,
'limit': 10})

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_post_empty_metric(self, mock_requests):
metrics = []
self.z.sendMetric('ZeusTest', metrics)
mock_requests.post.assert_called_with(FAKE_SERVER + '/metrics/' +
FAKE_TOKEN + '/ZeusTest/',
data={"metrics":
json.dumps(metrics)})
json.dumps(metrics)},
headers=None)

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_post_single_metric(self, mock_requests):
metrics = [{"timestamp": 123541423, "value": 0}]
self.z.sendMetric('Zeus.Test', metrics)
mock_requests.post.assert_called_with(FAKE_SERVER + '/metrics/' +
FAKE_TOKEN + '/Zeus.Test/',
data={"metrics":
json.dumps(metrics)})
json.dumps(metrics)},
headers=None)

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_post_single_metric_wrong_name(self, mock_requests):
metrics = [{"timestamp": 123541423, "value": 0}]
self.assertRaises(
client.ZeusException, self.z.sendMetric, '_WrongName', metrics)
ZeusException, self.z.sendMetric, '_WrongName', metrics)
self.assertRaises(
client.ZeusException, self.z.sendMetric, 'W#?rongName', metrics)
ZeusException, self.z.sendMetric, 'W#?rongName', metrics)
self.assertRaises(
client.ZeusException, self.z.sendMetric, None, metrics)
ZeusException, self.z.sendMetric, None, metrics)
self.assertRaises(
client.ZeusException, self.z.sendMetric, '', metrics)
ZeusException, self.z.sendMetric, '', metrics)
self.assertRaises(
client.ZeusException, self.z.sendMetric, '0123456789ABCDEF' * 16,
ZeusException, self.z.sendMetric, '0123456789ABCDEF' * 16,
metrics)

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_post_multiple_metrics(self, mock_requests):
metrics = [{"timestamp": 123541423, "value": 0},
{"timestamp": 123541424, "value": 1},
Expand All @@ -169,9 +177,10 @@ def test_post_multiple_metrics(self, mock_requests):
mock_requests.post.assert_called_with(FAKE_SERVER + '/metrics/' +
FAKE_TOKEN + '/ZeusTest/',
data={"metrics":
json.dumps(metrics)})
json.dumps(metrics)},
headers=None)

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_get_metric_values(self, mock_requests):
self.z.getMetric(metric_name='ZeusTest',
aggregator_function='sum',
Expand All @@ -197,7 +206,7 @@ def test_get_metric_values(self, mock_requests):
'limit': 10,
'offset': 20})

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_get_metric_names(self, mock_requests):
self.z.getMetricNames(metric_name='ZeusTest',
limit=10,
Expand All @@ -208,20 +217,163 @@ def test_get_metric_names(self, mock_requests):
'limit': 10,
'offset': 20})

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_get_delete_metric(self, mock_requests):
self.z.deleteMetric('ZeusTest')
mock_requests.delete.assert_called_with(FAKE_SERVER + '/metrics/' +
FAKE_TOKEN + '/ZeusTest/')

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def test_create_alert(self, mock_requests):

alert_name = "testerino"
username = "pelegrino"
token = FAKE_TOKEN
alerts_type = "metric"
alert_expression = "cpu.value > 20"
alert_severity = "S1"
metric_name = "cpu.value"
emails = "john1234@gmail.com"
status = "active"
notify_period = 60

self.z.createAlert(alert_name, username, alerts_type,
alert_expression, alert_severity, metric_name,
emails, status, notify_period)
data = {
'alert_name': alert_name,
'username': username,
'token': token,
'alerts_type': alerts_type,
'alert_expression': alert_expression,
'alert_severity': alert_severity,
'metric_name': metric_name,
'emails': emails,
'status': status,
'notify_period': notify_period
}

mock_requests.post.assert_called_with(
FAKE_SERVER + '/alerts/' +
FAKE_TOKEN, data=json.dumps(data),
headers={'content-type': 'application/json'}
)

@mock.patch('zeus.interfaces.rest.requests')
def test_modify_alert(self, mock_requests):

alert_id = 42
alert_name = "testerino"
username = "pelegrino"
token = FAKE_TOKEN
alerts_type = "metric"
alert_expression = "cpu.value > 20"
alert_severity = "S1"
metric_name = "cpu.value"
emails = "john1234@gmail.com"
status = "active"
notify_period = 60

self.z.modifyAlert(alert_id, alert_name, username, alerts_type,
alert_expression, alert_severity, metric_name,
emails, status, notify_period)
data = {
'alert_name': alert_name,
'username': username,
'token': token,
'alerts_type': alerts_type,
'alert_expression': alert_expression,
'alert_severity': alert_severity,
'metric_name': metric_name,
'emails': emails,
'status': status,
'notify_period': notify_period
}

path = FAKE_SERVER + '/alerts/' + FAKE_TOKEN + '/' + str(alert_id)
mock_requests.put.assert_called_with(
path, data=json.dumps(data),
headers={'content-type': 'application/json'}
)

@mock.patch('zeus.interfaces.rest.requests')
def test_get_alerts(self, mock_requests):
self.z.getAlerts()

path = FAKE_SERVER + '/alerts/' + FAKE_TOKEN
mock_requests.get.assert_called_with(path, params=None)

@mock.patch('zeus.interfaces.rest.requests')
def test_get_alert(self, mock_requests):
alert_id = 42
self.z.getAlert(alert_id)

path = FAKE_SERVER + '/alerts/' + FAKE_TOKEN + '/' + str(alert_id)
mock_requests.get.assert_called_with(path, params=None)

@mock.patch('zeus.interfaces.rest.requests')
def test_delete_alert(self, mock_requests):
alert_id = 42
self.z.deleteAlert(alert_id)

path = FAKE_SERVER + '/alerts/' + FAKE_TOKEN + '/' + str(alert_id)
mock_requests.delete.assert_called_with(path)

@mock.patch('zeus.interfaces.rest.requests')
def test_enable_alerts(self, mock_requests):

alert_id_list = [19, 42]
self.z.enableAlerts(alert_id_list)
data = {
'id': alert_id_list
}

path = FAKE_SERVER + '/alerts/' + FAKE_TOKEN + '/enable'
mock_requests.post.assert_called_with(
path, data=json.dumps(data),
headers={'content-type': 'application/json'}
)

@mock.patch('zeus.interfaces.rest.requests')
def test_disable_alerts(self, mock_requests):

alert_id_list = [19, 42]
self.z.disableAlerts(alert_id_list)
data = {
'id': alert_id_list
}

path = FAKE_SERVER + '/alerts/' + FAKE_TOKEN + '/disable'
mock_requests.post.assert_called_with(
path, data=json.dumps(data),
headers={'content-type': 'application/json'}
)

@mock.patch('zeus.interfaces.rest.requests')
def test_get_triggered_alerts(self, mock_requests):

self.z.getTriggeredAlerts()

path = FAKE_SERVER + '/triggeredalerts/' + FAKE_TOKEN
mock_requests.get.assert_called_with(path, params=None)

@mock.patch('zeus.interfaces.rest.requests')
def test_get_triggered_alerts_last_24h(self, mock_requests):

self.z.getTriggeredAlertsLast24Hours()

path = FAKE_SERVER + '/triggeredalerts/' + FAKE_TOKEN + "/last24"
mock_requests.get.assert_called_with(path, params=None)

@mock.patch('zeus.interfaces.rest.requests')
def test_get_delete_metric_wrong_name(self, mock_requests):
self.assertRaises(
client.ZeusException, self.z.deleteMetric, '_WrongName')
ZeusException, self.z.deleteMetric, '_WrongName')

@mock.patch('zeus.client.requests')
@mock.patch('zeus.interfaces.rest.requests')
def tearDown(self, mock_requests):
pass


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit d1067de

Please sign in to comment.