Skip to content

Commit 407a6d9

Browse files
author
Alexander Maretskiy
committed
Set of small fixes
1 parent ed48a1c commit 407a6d9

File tree

10 files changed

+90
-39
lines changed

10 files changed

+90
-39
lines changed

notify/api/v1/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ def send_notification(backends):
9191
result["result"][backend][drv_name] = {"error": str(e)}
9292
result["errors"] += 1
9393
except Exception as e:
94-
LOG.error(("Backend '{}' driver '{}' "
95-
"error: {}").format(backend, drv_name, e))
94+
LOG.error("Backend '{}' driver '{}': {}: {}".format(
95+
backend, drv_name, type(e), e))
9696
error = "Something has went wrong!"
9797
result["result"][backend][drv_name] = {"error": error}
9898
result["errors"] += 1

notify/config.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"type": "elastic",
3333
"connection": [{"host": "127.0.0.1", "port": 9200}]
3434
},
35-
"notify_backends": {}
35+
"notify_backends": {},
36+
"logging": {"level": "INFO"}
3637
}
3738

3839
CONF_SCHEMA = {
@@ -69,23 +70,13 @@
6970
"notify_backends": {
7071
"type": "object",
7172
"properties": {
72-
"*": {
73-
"type": "object",
74-
"properties": {
75-
"salesforce": {
76-
"type": "object",
77-
"properties": {
78-
"atuh_url": {"type": "string"},
79-
"username": {"type": "string"},
80-
"password": {"type": "string"},
81-
"environment": {"type": "string"},
82-
"client_id": {"type": "string"},
83-
"client_secret": {"type": "string"},
84-
"organization_id": {"type": "string"}
85-
}
86-
}
87-
}
88-
}
73+
"*": {"type": "object"}
74+
}
75+
},
76+
"logging": {
77+
"type": "object",
78+
"properties": {
79+
"level": {"type": "string"}
8980
}
9081
}
9182
},

notify/driver.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ class Driver(object):
6868
"severity": {
6969
"enum": ["OK", "INFO", "UNKNOWN", "WARNING",
7070
"CRITICAL", "DOWN"]},
71-
"who": {"type": "array",
72-
"items": {"type": "string"},
73-
"minItems": 1,
74-
"uniqueItems": True},
71+
"who": {"type": "string"},
7572
"what": {"type": "string"},
7673
"affected_hosts": {"type": "array"}
7774
},
@@ -112,12 +109,11 @@ def __init__(self, config):
112109
self.config = config
113110

114111
def notify(self, payload):
115-
"""Send notification alert.
112+
"""Send notification payload.
116113
117114
This method must be overriden by specific driver implementation.
118115
119-
:param payload: alert data
120-
:type payload: dict, validated api.PAYLOAD_SCHEMA
116+
:param payload: payload dict, valid for PAYLOAD_SCHEMA
121117
:returns: status whether notification is successful
122118
:rtype: bool
123119
"""

raml/response_examples/200/notify_backends.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
"failed": 1,
44
"passed": 2,
55
"payload": {
6-
"description": "This is a dummy alert, just for testing.",
6+
"description": "This is a dummy payload, just for testing.",
77
"region": "farfaraway",
88
"severity": "INFO",
99
"what": "Hooray!",
10-
"who": [
11-
"Alice",
12-
"Bob"
13-
]
10+
"who": "John Doe"
1411
},
1512
"result": {
1613
"dummy": {

raml/schemas/post/notify_backends.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
"region": {"type": "string"},
99
"description": {"type": "object"},
1010
"severity": {"enum": ["OK", "INFO", "UNKNOWN", "WARNING", "CRITICAL", "DOWN"]},
11-
"who": {
12-
"type": "array",
13-
"items": {"type": "string"}
14-
},
11+
"who": {"type": "string"},
1512
"what": {"type": "string"},
1613
"affected_hosts": {"type": "array"}
1714
},

test-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pytest>=2.7,<=2.9.2
33
pytest-cov>=2.2.1,<=2.3.0
44
pytest-html==1.10.1
55

6-
coverage>=3.6
6+
coverage>=3.6,!=4.3.0
77
ddt>=1.0.1
88
mock>=2.0
99

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
curl -XPOST -H 'Content-Type: application/json' localhost:5000/api/v1/notify/dummy,dummyrand,dummyerr -d '
22
{
33
"region": "farfaraway",
4-
"description": "This is a dummy alert, just for testing.",
4+
"description": "This is a dummy payload, just for testing.",
55
"severity": "INFO",
66
"who": ["Alice", "Bob"],
77
"what": "Hooray!"

tests/unit/drivers/__init__.py

Whitespace-only changes.

tests/unit/drivers/test_dummy.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2016: Mirantis Inc.
2+
# All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
16+
import mock
17+
18+
from notify import driver
19+
from notify.drivers import dummy_err
20+
from notify.drivers import dummy_err_explained
21+
from notify.drivers import dummy_fail
22+
from notify.drivers import dummy_pass
23+
from notify.drivers import dummy_random
24+
from tests.unit import test
25+
26+
27+
class DummyErrDriverTestCase(test.TestCase):
28+
29+
def test_notify(self):
30+
e = self.assertRaises(ValueError,
31+
dummy_err.Driver({}).notify, self.payload)
32+
self.assertEqual("This error message is for logging only!", str(e))
33+
34+
35+
class DummyErrExplainedDriverTestCase(test.TestCase):
36+
37+
def test_notify(self):
38+
e = self.assertRaises(driver.ExplainedError,
39+
dummy_err_explained.Driver({}).notify,
40+
self.payload)
41+
self.assertEqual("This error message must appear in API response!",
42+
str(e))
43+
44+
45+
class DummyFailDriverTestCase(test.TestCase):
46+
47+
def test_notify(self):
48+
self.assertFalse(dummy_fail.Driver({}).notify(self.payload))
49+
50+
51+
class DummyPassDriverTestCase(test.TestCase):
52+
53+
def test_notify(self):
54+
self.assertTrue(dummy_pass.Driver({}).notify(self.payload))
55+
56+
57+
class DummyRandomDriverTestCase(test.TestCase):
58+
59+
@mock.patch("notify.drivers.dummy_random.random.random")
60+
def test_notify(self, mock_random):
61+
drv = dummy_random.Driver({})
62+
63+
mock_random.return_value = 0.49
64+
self.assertTrue(drv.notify(self.payload))
65+
66+
mock_random.return_value = 0.51
67+
self.assertFalse(drv.notify(self.payload))
68+
69+
drv = dummy_random.Driver({"probability": 0.53})
70+
self.assertTrue(drv.notify(self.payload))

tests/unit/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def setUp(self):
3131
"region": "farfaraway",
3232
"severity": "INFO",
3333
"what": "Hooray!",
34-
"who": ["Alice", "Bob"]}
34+
"who": "John Doe"}
3535

3636
def get(self, *args, **kwargs):
3737
rv = self.client.get(*args, **kwargs)

0 commit comments

Comments
 (0)