Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions notify/api/v1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def send_notification(backends):
result["result"][backend][drv_name] = {"error": str(e)}
result["errors"] += 1
except Exception as e:
LOG.error(("Backend '{}' driver '{}' "
"error: {}").format(backend, drv_name, e))
LOG.error("Backend '{}' driver '{}': {}: {}".format(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logging itself formats the string and you should not do it explicitly because logging does it according to the specified level and can skip the formatting if it is not necessary. So, instead of

    LOG.error("Backend '{}' driver '{}': {}: {}".format(backend, drv_name, type(e), e))

you should do:

        LOG.error("Backend '%s' driver '%s': %s: %s", backend, drv_name, type(e), e)

Unfortunately, logging does not support the format style by default.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed all such occurrences in seecloud/availability#24 and other projects as well.

backend, drv_name, type(e), e))
error = "Something has went wrong!"
result["result"][backend][drv_name] = {"error": error}
result["errors"] += 1
Expand Down
27 changes: 9 additions & 18 deletions notify/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"type": "elastic",
"connection": [{"host": "127.0.0.1", "port": 9200}]
},
"notify_backends": {}
"notify_backends": {},
"logging": {"level": "INFO"}
}

CONF_SCHEMA = {
Expand Down Expand Up @@ -69,23 +70,13 @@
"notify_backends": {
"type": "object",
"properties": {
"*": {
"type": "object",
"properties": {
"salesforce": {
"type": "object",
"properties": {
"atuh_url": {"type": "string"},
"username": {"type": "string"},
"password": {"type": "string"},
"environment": {"type": "string"},
"client_id": {"type": "string"},
"client_secret": {"type": "string"},
"organization_id": {"type": "string"}
}
}
}
}
"*": {"type": "object"}
}
},
"logging": {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest not to add this functionality right now and do it properly later by dying the appropriate configuration of logging throught fileConfig or dictConfig in oss-lib.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is already merged by another pull request (as dependency) so let's fix this later during integration with oss-lib

"type": "object",
"properties": {
"level": {"type": "string"}
}
}
},
Expand Down
10 changes: 3 additions & 7 deletions notify/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ class Driver(object):
"severity": {
"enum": ["OK", "INFO", "UNKNOWN", "WARNING",
"CRITICAL", "DOWN"]},
"who": {"type": "array",
"items": {"type": "string"},
"minItems": 1,
"uniqueItems": True},
"who": {"type": "string"},
"what": {"type": "string"},
"affected_hosts": {"type": "array"}
},
Expand Down Expand Up @@ -112,12 +109,11 @@ def __init__(self, config):
self.config = config

def notify(self, payload):
"""Send notification alert.
"""Send notification payload.

This method must be overriden by specific driver implementation.

:param payload: alert data
:type payload: dict, validated api.PAYLOAD_SCHEMA
:param payload: payload dict, valid for PAYLOAD_SCHEMA
:returns: status whether notification is successful
:rtype: bool
"""
Expand Down
7 changes: 2 additions & 5 deletions raml/response_examples/200/notify_backends.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
"failed": 1,
"passed": 2,
"payload": {
"description": "This is a dummy alert, just for testing.",
"description": "This is a dummy payload, just for testing.",
"region": "farfaraway",
"severity": "INFO",
"what": "Hooray!",
"who": [
"Alice",
"Bob"
]
"who": "John Doe"
},
"result": {
"dummy": {
Expand Down
5 changes: 1 addition & 4 deletions raml/schemas/post/notify_backends.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
"region": {"type": "string"},
"description": {"type": "object"},
"severity": {"enum": ["OK", "INFO", "UNKNOWN", "WARNING", "CRITICAL", "DOWN"]},
"who": {
"type": "array",
"items": {"type": "string"}
},
"who": {"type": "string"},
"what": {"type": "string"},
"affected_hosts": {"type": "array"}
},
Expand Down
2 changes: 1 addition & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pytest>=2.7,<=2.9.2
pytest-cov>=2.2.1,<=2.3.0
pytest-html==1.10.1

coverage>=3.6
coverage>=3.6,!=4.3.0
ddt>=1.0.1
mock>=2.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
curl -XPOST -H 'Content-Type: application/json' localhost:5000/api/v1/notify/dummy,dummyrand,dummyerr -d '
{
"region": "farfaraway",
"description": "This is a dummy alert, just for testing.",
"description": "This is a dummy payload, just for testing.",
"severity": "INFO",
"who": ["Alice", "Bob"],
"what": "Hooray!"
Expand Down
Empty file added tests/unit/drivers/__init__.py
Empty file.
70 changes: 70 additions & 0 deletions tests/unit/drivers/test_dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2016: Mirantis Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import mock

from notify import driver
from notify.drivers import dummy_err
from notify.drivers import dummy_err_explained
from notify.drivers import dummy_fail
from notify.drivers import dummy_pass
from notify.drivers import dummy_random
from tests.unit import test


class DummyErrDriverTestCase(test.TestCase):

def test_notify(self):
e = self.assertRaises(ValueError,
dummy_err.Driver({}).notify, self.payload)
self.assertEqual("This error message is for logging only!", str(e))


class DummyErrExplainedDriverTestCase(test.TestCase):

def test_notify(self):
e = self.assertRaises(driver.ExplainedError,
dummy_err_explained.Driver({}).notify,
self.payload)
self.assertEqual("This error message must appear in API response!",
str(e))


class DummyFailDriverTestCase(test.TestCase):

def test_notify(self):
self.assertFalse(dummy_fail.Driver({}).notify(self.payload))


class DummyPassDriverTestCase(test.TestCase):

def test_notify(self):
self.assertTrue(dummy_pass.Driver({}).notify(self.payload))


class DummyRandomDriverTestCase(test.TestCase):

@mock.patch("notify.drivers.dummy_random.random.random")
def test_notify(self, mock_random):
drv = dummy_random.Driver({})

mock_random.return_value = 0.49
self.assertTrue(drv.notify(self.payload))

mock_random.return_value = 0.51
self.assertFalse(drv.notify(self.payload))

drv = dummy_random.Driver({"probability": 0.53})
self.assertTrue(drv.notify(self.payload))
2 changes: 1 addition & 1 deletion tests/unit/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setUp(self):
"region": "farfaraway",
"severity": "INFO",
"what": "Hooray!",
"who": ["Alice", "Bob"]}
"who": "John Doe"}

def get(self, *args, **kwargs):
rv = self.client.get(*args, **kwargs)
Expand Down