Implemented audit.create notification

In this changeset, I implemented the audit.create notification.

Change-Id: Ia092ca3a3dc951e3313a07f15a98aec5818e9ab0
Partially-Implements: blueprint audit-versioned-notifications-api
This commit is contained in:
Vincent Françoise
2016-10-20 09:37:49 +02:00
parent 9405eb0806
commit 0c0a9c84d6
16 changed files with 332 additions and 88 deletions

View File

@@ -16,32 +16,33 @@ import freezegun
import mock
from watcher.common import exception
from watcher.notifications import audit as auditnotifs
from watcher.tests import base as testbase
from watcher import notifications
from watcher.tests.db import base
from watcher.tests.objects import utils
class TestAuditNotification(testbase.TestCase):
class TestAuditNotification(base.DbTestCase):
@mock.patch.object(auditnotifs.AuditUpdateNotification, '_emit')
@mock.patch.object(notifications.audit.AuditUpdateNotification, '_emit')
def test_send_version_invalid_audit(self, mock_emit):
audit = utils.get_test_audit(mock.Mock(), state='DOESNOTMATTER',
goal_id=1)
self.assertRaises(
exception.InvalidAudit,
auditnotifs.send_update,
notifications.audit.send_update,
mock.MagicMock(), audit, 'host', 'node0')
@freezegun.freeze_time('2016-10-18T09:52:05.219414')
@mock.patch.object(auditnotifs.AuditUpdateNotification, '_emit')
@mock.patch.object(notifications.audit.AuditUpdateNotification, '_emit')
def test_send_version_audit_update_with_strategy(self, mock_emit):
goal = utils.get_test_goal(mock.Mock(), id=1)
strategy = utils.get_test_strategy(mock.Mock(), id=1)
audit = utils.get_test_audit(mock.Mock(), state='ONGOING',
goal_id=goal.id, strategy_id=strategy.id,
goal=goal, strategy=strategy)
auditnotifs.send_update(
goal = utils.create_test_goal(mock.Mock())
strategy = utils.create_test_strategy(mock.Mock())
audit = utils.create_test_audit(
mock.Mock(), state='ONGOING',
goal_id=goal.id, strategy_id=strategy.id,
goal=goal, strategy=strategy)
notifications.audit.send_update(
mock.MagicMock(), audit, 'host', 'node0', old_state='PENDING')
self.assertEqual(1, mock_emit.call_count)
@@ -62,7 +63,7 @@ class TestAuditNotification(testbase.TestCase):
"uuid": "cb3d0b58-4415-4d90-b75b-1e96878730e3",
"name": "TEST",
"parameters_spec": {},
"created_at": None,
"created_at": "2016-10-18T09:52:05Z",
"display_name": "test strategy",
"deleted_at": None
},
@@ -78,7 +79,7 @@ class TestAuditNotification(testbase.TestCase):
"uuid": "f7ad87ae-4298-91cf-93a0-f35a852e3652",
"name": "TEST",
"efficacy_specification": [],
"created_at": None,
"created_at": "2016-10-18T09:52:05Z",
"display_name": "test goal",
"deleted_at": None
},
@@ -88,7 +89,7 @@ class TestAuditNotification(testbase.TestCase):
"scope": [],
"state": "ONGOING",
"updated_at": None,
"created_at": None,
"created_at": "2016-10-18T09:52:05Z",
"state_update": {
"watcher_object.namespace": "watcher",
"watcher_object.version": "1.0",
@@ -106,12 +107,12 @@ class TestAuditNotification(testbase.TestCase):
)
@freezegun.freeze_time('2016-10-18T09:52:05.219414')
@mock.patch.object(auditnotifs.AuditUpdateNotification, '_emit')
@mock.patch.object(notifications.audit.AuditUpdateNotification, '_emit')
def test_send_version_audit_update_without_strategy(self, mock_emit):
goal = utils.get_test_goal(mock.Mock(), id=1)
goal = utils.create_test_goal(mock.Mock(), id=1)
audit = utils.get_test_audit(
mock.Mock(), state='ONGOING', goal_id=goal.id, goal=goal)
auditnotifs.send_update(
notifications.audit.send_update(
mock.MagicMock(), audit, 'host', 'node0', old_state='PENDING')
self.assertEqual(1, mock_emit.call_count)
@@ -134,7 +135,7 @@ class TestAuditNotification(testbase.TestCase):
"uuid": "f7ad87ae-4298-91cf-93a0-f35a852e3652",
"name": "TEST",
"efficacy_specification": [],
"created_at": None,
"created_at": "2016-10-18T09:52:05Z",
"display_name": "test goal",
"deleted_at": None
},
@@ -161,3 +162,67 @@ class TestAuditNotification(testbase.TestCase):
},
payload
)
@freezegun.freeze_time('2016-10-18T09:52:05.219414')
@mock.patch.object(notifications.audit.AuditCreateNotification, '_emit')
def test_send_version_audit_create(self, mock_emit):
goal = utils.create_test_goal(mock.Mock())
strategy = utils.create_test_strategy(mock.Mock())
audit = utils.get_test_audit(
mock.Mock(), state='PENDING',
goal_id=goal.id, strategy_id=strategy.id,
goal=goal.as_dict(), strategy=strategy.as_dict())
notifications.audit.send_create(
mock.MagicMock(), audit, 'host', 'node0')
self.assertEqual(1, mock_emit.call_count)
notification = mock_emit.call_args_list[0][1]
payload = notification['payload']
self.assertDictEqual(
{
"watcher_object.namespace": "watcher",
"watcher_object.version": "1.0",
"watcher_object.data": {
"interval": 3600,
"strategy": {
"watcher_object.namespace": "watcher",
"watcher_object.version": "1.0",
"watcher_object.data": {
"updated_at": None,
"uuid": "cb3d0b58-4415-4d90-b75b-1e96878730e3",
"name": "TEST",
"parameters_spec": {},
"created_at": "2016-10-18T09:52:05Z",
"display_name": "test strategy",
"deleted_at": None
},
"watcher_object.name": "StrategyPayload"
},
"parameters": {},
"uuid": "10a47dd1-4874-4298-91cf-eff046dbdb8d",
"goal": {
"watcher_object.namespace": "watcher",
"watcher_object.version": "1.0",
"watcher_object.data": {
"updated_at": None,
"uuid": "f7ad87ae-4298-91cf-93a0-f35a852e3652",
"name": "TEST",
"efficacy_specification": [],
"created_at": "2016-10-18T09:52:05Z",
"display_name": "test goal",
"deleted_at": None
},
"watcher_object.name": "GoalPayload"
},
"deleted_at": None,
"scope": [],
"state": "PENDING",
"updated_at": None,
"created_at": None,
"audit_type": "ONESHOT"
},
"watcher_object.name": "AuditCreatePayload"
},
payload
)

View File

@@ -258,6 +258,8 @@ expected_notification_fingerprints = {
'AuditStateUpdatePayload': '1.0-1a1b606bf14a2c468800c2b010801ce5',
'AuditUpdateNotification': '1.0-9b69de0724fda8310d05e18418178866',
'AuditUpdatePayload': '1.0-d3aace28d9eb978c1ecf833e108f61f7',
'AuditCreateNotification': '1.0-9b69de0724fda8310d05e18418178866',
'AuditCreatePayload': '1.0-30c85c834648c8ca11f54fc5e084d86b',
'GoalPayload': '1.0-fa1fecb8b01dd047eef808ded4d50d1a',
'StrategyPayload': '1.0-94f01c137b083ac236ae82573c1fcfc1',
}