Change get_sample_data API to not require the custom notification tuple

This commit is contained in:
Joseph Schorr 2017-07-14 16:48:31 +03:00
parent e7dbc4ee91
commit e7fec9dd20
3 changed files with 21 additions and 31 deletions

View file

@ -8,10 +8,10 @@ from endpoints.api import (RepositoryParamResource, nickname, resource, require_
path_param, disallow_for_app_repositories) path_param, disallow_for_app_repositories)
from endpoints.exception import NotFound from endpoints.exception import NotFound
from notifications import build_notification_data from notifications import build_notification_data
from notifications.models_interface import Repository
from notifications.notificationevent import NotificationEvent from notifications.notificationevent import NotificationEvent
from notifications.notificationmethod import (NotificationMethod, from notifications.notificationmethod import (NotificationMethod,
CannotValidateNotificationMethodException) CannotValidateNotificationMethodException)
from workers.notificationworker.models_pre_oci import notification
from endpoints.api.repositorynotification_models_pre_oci import pre_oci_model as model from endpoints.api.repositorynotification_models_pre_oci import pre_oci_model as model
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -153,6 +153,8 @@ class TestRepositoryNotification(RepositoryParamResource):
def post(self, namespace_name, repository_name, uuid): def post(self, namespace_name, repository_name, uuid):
""" Queues a test notification for this repository. """ """ Queues a test notification for this repository. """
test_note = model.queue_test_notification(uuid) test_note = model.queue_test_notification(uuid)
event_info = NotificationEvent.get_event(test_note.event_name)
sample_data = event_info.get_sample_data(Repository(namespace, repository), event_config)
if not test_note: if not test_note:
raise InvalidRequest("No repository notification found for: %s, %s, %s" % (namespace_name, repository_name, uuid)) raise InvalidRequest("No repository notification found for: %s, %s, %s" % (namespace_name, repository_name, uuid))

View file

@ -40,7 +40,7 @@ class NotificationEvent(object):
'notification_data': notification_data 'notification_data': notification_data
}) })
def get_sample_data(self, notification): def get_sample_data(self, repository, event_config):
""" """
Returns sample data for testing the raising of this notification, with an example notification. Returns sample data for testing the raising of this notification, with an example notification.
""" """
@ -95,8 +95,8 @@ class RepoPushEvent(NotificationEvent):
def get_summary(self, event_data, notification_data): def get_summary(self, event_data, notification_data):
return 'Repository %s updated' % (event_data['repository']) return 'Repository %s updated' % (event_data['repository'])
def get_sample_data(self, notification): def get_sample_data(self, repository, event_config):
return build_event_data(notification.repository, { return build_event_data(repository, {
'updated_tags': {'latest': 'someimageid', 'foo': 'anotherimage'}, 'updated_tags': {'latest': 'someimageid', 'foo': 'anotherimage'},
'pruned_image_count': 3 'pruned_image_count': 3
}) })
@ -129,10 +129,9 @@ class VulnerabilityFoundEvent(NotificationEvent):
return 'info' return 'info'
def get_sample_data(self, notification): def get_sample_data(self, repository, event_config):
event_config = notification.event_config_dict
level = event_config.get(VulnerabilityFoundEvent.CONFIG_LEVEL, 'Critical') level = event_config.get(VulnerabilityFoundEvent.CONFIG_LEVEL, 'Critical')
return build_event_data(notification.repository, { return build_event_data(repository, {
'tags': ['latest', 'prod', 'foo', 'bar', 'baz'], 'tags': ['latest', 'prod', 'foo', 'bar', 'baz'],
'image': 'some-image-id', 'image': 'some-image-id',
'vulnerability': { 'vulnerability': {
@ -217,9 +216,9 @@ class BuildQueueEvent(BaseBuildEvent):
def get_level(self, event_data, notification_data): def get_level(self, event_data, notification_data):
return 'info' return 'info'
def get_sample_data(self, notification): def get_sample_data(self, repository, event_config):
build_uuid = 'fake-build-id' build_uuid = 'fake-build-id'
return build_event_data(notification.repository, { return build_event_data(repository, {
'is_manual': False, 'is_manual': False,
'build_id': build_uuid, 'build_id': build_uuid,
'build_name': 'some-fake-build', 'build_name': 'some-fake-build',
@ -255,9 +254,9 @@ class BuildStartEvent(BaseBuildEvent):
def get_level(self, event_data, notification_data): def get_level(self, event_data, notification_data):
return 'info' return 'info'
def get_sample_data(self, notification): def get_sample_data(self, repository, event_config):
build_uuid = 'fake-build-id' build_uuid = 'fake-build-id'
return build_event_data(notification.repository, { return build_event_data(repository, {
'build_id': build_uuid, 'build_id': build_uuid,
'build_name': 'some-fake-build', 'build_name': 'some-fake-build',
'docker_tags': ['latest', 'foo', 'bar'], 'docker_tags': ['latest', 'foo', 'bar'],
@ -282,9 +281,9 @@ class BuildSuccessEvent(BaseBuildEvent):
def get_level(self, event_data, notification_data): def get_level(self, event_data, notification_data):
return 'success' return 'success'
def get_sample_data(self, notification): def get_sample_data(self, repository, event_config):
build_uuid = 'fake-build-id' build_uuid = 'fake-build-id'
return build_event_data(notification.repository, { return build_event_data(repository, {
'build_id': build_uuid, 'build_id': build_uuid,
'build_name': 'some-fake-build', 'build_name': 'some-fake-build',
'docker_tags': ['latest', 'foo', 'bar'], 'docker_tags': ['latest', 'foo', 'bar'],
@ -310,9 +309,9 @@ class BuildFailureEvent(BaseBuildEvent):
def get_level(self, event_data, notification_data): def get_level(self, event_data, notification_data):
return 'error' return 'error'
def get_sample_data(self, notification): def get_sample_data(self, repository, event_config):
build_uuid = 'fake-build-id' build_uuid = 'fake-build-id'
return build_event_data(notification.repository, { return build_event_data(repository, {
'build_id': build_uuid, 'build_id': build_uuid,
'build_name': 'some-fake-build', 'build_name': 'some-fake-build',
'docker_tags': ['latest', 'foo', 'bar'], 'docker_tags': ['latest', 'foo', 'bar'],
@ -349,9 +348,9 @@ class BuildCancelledEvent(BaseBuildEvent):
def get_level(self, event_data, notification_data): def get_level(self, event_data, notification_data):
return 'info' return 'info'
def get_sample_data(self, notification): def get_sample_data(self, repository, event_config):
build_uuid = 'fake-build-id' build_uuid = 'fake-build-id'
return build_event_data(notification.repository, { return build_event_data(repository, {
'build_id': build_uuid, 'build_id': build_uuid,
'build_name': 'some-fake-build', 'build_name': 'some-fake-build',
'docker_tags': ['latest', 'foo', 'bar'], 'docker_tags': ['latest', 'foo', 'bar'],

View file

@ -1,25 +1,14 @@
from notifications.models_interface import Repository
from notifications.notificationevent import NotificationEvent from notifications.notificationevent import NotificationEvent
from util.morecollections import AttrDict
from test.fixtures import * from test.fixtures import *
def test_all_notifications(app): def test_all_notifications(initialized_db):
# Create a test notification.
test_notification = AttrDict({
'repository': AttrDict({
'namespace_name': AttrDict(dict(username='foo')),
'name': 'bar',
}),
'event_config_dict': {
'level': 'low',
},
})
for subc in NotificationEvent.__subclasses__(): for subc in NotificationEvent.__subclasses__():
if subc.event_name() is not None: if subc.event_name() is not None:
# Create the notification event. # Create the notification event.
found = NotificationEvent.get_event(subc.event_name()) found = NotificationEvent.get_event(subc.event_name())
sample_data = found.get_sample_data(test_notification) sample_data = found.get_sample_data(Repository('foo', 'bar'), {'level': 'low'})
# Make sure all calls succeed. # Make sure all calls succeed.
notification_data = { notification_data = {