Move notification objects to a separate package

The versioned notification object model is moved to the recently
introduced nova.notification.objects package and the related
unit test is moved to the respective test package

Implements: bp versioned-notification-transformation-newton
Change-Id: I5caee4118da1c883e34fd1eec0fc55b1cfcd4e09
This commit is contained in:
Balazs Gibizer 2016-04-13 15:59:56 +02:00
parent 5e074a59d2
commit cdfecc65e7
4 changed files with 143 additions and 101 deletions

View File

@ -0,0 +1,63 @@
# Copyright (c) 2016 OpenStack Foundation
# 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.
from nova.notifications.objects import base
from nova.objects import base as object_base
from nova.objects import fields
@base.notification_sample('service-update.json')
@object_base.NovaObjectRegistry.register
class ServiceStatusNotification(base.NotificationBase):
# Version 1.0: Initial version
VERSION = '1.0'
fields = {
'payload': fields.ObjectField('ServiceStatusPayload')
}
@object_base.NovaObjectRegistry.register
class ServiceStatusPayload(base.NotificationPayloadBase):
SCHEMA = {
'host': ('service', 'host'),
'binary': ('service', 'binary'),
'topic': ('service', 'topic'),
'report_count': ('service', 'report_count'),
'disabled': ('service', 'disabled'),
'disabled_reason': ('service', 'disabled_reason'),
'availability_zone': ('service', 'availability_zone'),
'last_seen_up': ('service', 'last_seen_up'),
'forced_down': ('service', 'forced_down'),
'version': ('service', 'version')
}
# Version 1.0: Initial version
VERSION = '1.0'
fields = {
'host': fields.StringField(nullable=True),
'binary': fields.StringField(nullable=True),
'topic': fields.StringField(nullable=True),
'report_count': fields.IntegerField(),
'disabled': fields.BooleanField(),
'disabled_reason': fields.StringField(nullable=True),
'availability_zone': fields.StringField(nullable=True),
'last_seen_up': fields.DateTimeField(nullable=True),
'forced_down': fields.BooleanField(),
'version': fields.IntegerField(),
}
def __init__(self, service):
super(ServiceStatusPayload, self).__init__()
self.populate_schema(service=service)

View File

@ -20,6 +20,7 @@ from nova import db
from nova import exception
from nova.i18n import _LW
from nova.notifications.objects import base as notification
from nova.notifications.objects import service as service_notification
from nova import objects
from nova.objects import base
from nova.objects import fields
@ -305,8 +306,8 @@ class Service(base.NovaPersistentObject, base.NovaObject,
# every other field change. See the comment in save() too.
if set(updates.keys()).intersection(
{'disabled', 'disabled_reason', 'forced_down'}):
payload = ServiceStatusPayload(self)
ServiceStatusNotification(
payload = service_notification.ServiceStatusPayload(self)
service_notification.ServiceStatusNotification(
publisher=notification.NotificationPublisher.from_service_obj(
self),
event_type=notification.EventType(
@ -428,48 +429,3 @@ class ServiceList(base.ObjectListBase, base.NovaObject):
context, db_services)
return base.obj_make_list(context, cls(context), objects.Service,
db_services)
@notification.notification_sample('service-update.json')
@base.NovaObjectRegistry.register
class ServiceStatusNotification(notification.NotificationBase):
# Version 1.0: Initial version
VERSION = '1.0'
fields = {
'payload': fields.ObjectField('ServiceStatusPayload')
}
@base.NovaObjectRegistry.register
class ServiceStatusPayload(notification.NotificationPayloadBase):
SCHEMA = {
'host': ('service', 'host'),
'binary': ('service', 'binary'),
'topic': ('service', 'topic'),
'report_count': ('service', 'report_count'),
'disabled': ('service', 'disabled'),
'disabled_reason': ('service', 'disabled_reason'),
'availability_zone': ('service', 'availability_zone'),
'last_seen_up': ('service', 'last_seen_up'),
'forced_down': ('service', 'forced_down'),
'version': ('service', 'version')
}
# Version 1.0: Initial version
VERSION = '1.0'
fields = {
'host': fields.StringField(nullable=True),
'binary': fields.StringField(nullable=True),
'topic': fields.StringField(nullable=True),
'report_count': fields.IntegerField(),
'disabled': fields.BooleanField(),
'disabled_reason': fields.StringField(nullable=True),
'availability_zone': fields.StringField(nullable=True),
'last_seen_up': fields.DateTimeField(nullable=True),
'forced_down': fields.BooleanField(),
'version': fields.IntegerField(),
}
def __init__(self, service):
super(ServiceStatusPayload, self).__init__()
self.populate_schema(service=service)

View File

@ -0,0 +1,77 @@
# Copyright (c) 2016 OpenStack Foundation
# 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 oslo_utils import timeutils
from nova import context
from nova.notifications.objects import service as service_notification
from nova import objects
from nova.objects import fields
from nova import test
from nova.tests.unit.objects.test_service import fake_service
class TestServiceStatusNotification(test.TestCase):
def setUp(self):
self.ctxt = context.get_admin_context()
super(TestServiceStatusNotification, self).setUp()
@mock.patch('nova.notifications.objects.service.ServiceStatusNotification')
def _verify_notification(self, service_obj, mock_notification):
service_obj.save()
self.assertTrue(mock_notification.called)
event_type = mock_notification.call_args[1]['event_type']
priority = mock_notification.call_args[1]['priority']
publisher = mock_notification.call_args[1]['publisher']
payload = mock_notification.call_args[1]['payload']
self.assertEqual(service_obj.host, publisher.host)
self.assertEqual(service_obj.binary, publisher.binary)
self.assertEqual(fields.NotificationPriority.INFO, priority)
self.assertEqual('service', event_type.object)
self.assertEqual(fields.NotificationAction.UPDATE,
event_type.action)
for field in service_notification.ServiceStatusPayload.SCHEMA:
if field in fake_service:
self.assertEqual(fake_service[field], getattr(payload, field))
mock_notification.return_value.emit.assert_called_once_with(self.ctxt)
@mock.patch('nova.db.service_update')
def test_service_update_with_notification(self, mock_db_service_update):
service_obj = objects.Service(context=self.ctxt, id=fake_service['id'])
mock_db_service_update.return_value = fake_service
for key, value in {'disabled': True,
'disabled_reason': 'my reason',
'forced_down': True}.items():
setattr(service_obj, key, value)
self._verify_notification(service_obj)
@mock.patch('nova.notifications.objects.service.ServiceStatusNotification')
@mock.patch('nova.db.service_update')
def test_service_update_without_notification(self,
mock_db_service_update,
mock_notification):
service_obj = objects.Service(context=self.ctxt, id=fake_service['id'])
mock_db_service_update.return_value = fake_service
for key, value in {'report_count': 13,
'last_seen_up': timeutils.utcnow()}.items():
setattr(service_obj, key, value)
service_obj.save()
self.assertFalse(mock_notification.called)

View File

@ -23,7 +23,6 @@ from nova import db
from nova import exception
from nova import objects
from nova.objects import aggregate
from nova.objects import fields
from nova.objects import service
from nova import test
from nova.tests.unit.objects import test_compute_node
@ -446,56 +445,3 @@ class TestServiceVersion(test.TestCase):
obj = objects.Service()
obj._from_db_object(self.ctxt, obj, fake_different_service)
self.assertEqual(fake_version, obj.version)
class TestServiceStatusNotification(test.TestCase):
def setUp(self):
self.ctxt = context.get_admin_context()
super(TestServiceStatusNotification, self).setUp()
@mock.patch('nova.objects.service.ServiceStatusNotification')
def _verify_notification(self, service_obj, mock_notification):
service_obj.save()
self.assertTrue(mock_notification.called)
event_type = mock_notification.call_args[1]['event_type']
priority = mock_notification.call_args[1]['priority']
publisher = mock_notification.call_args[1]['publisher']
payload = mock_notification.call_args[1]['payload']
self.assertEqual(service_obj.host, publisher.host)
self.assertEqual(service_obj.binary, publisher.binary)
self.assertEqual(fields.NotificationPriority.INFO, priority)
self.assertEqual('service', event_type.object)
self.assertEqual(fields.NotificationAction.UPDATE,
event_type.action)
for field in service.ServiceStatusPayload.SCHEMA:
if field in fake_service:
self.assertEqual(fake_service[field], getattr(payload, field))
mock_notification.return_value.emit.assert_called_once_with(self.ctxt)
@mock.patch('nova.db.service_update')
def test_service_update_with_notification(self, mock_db_service_update):
service_obj = objects.Service(context=self.ctxt, id=fake_service['id'])
mock_db_service_update.return_value = fake_service
for key, value in {'disabled': True,
'disabled_reason': 'my reason',
'forced_down': True}.items():
setattr(service_obj, key, value)
self._verify_notification(service_obj)
@mock.patch('nova.objects.service.ServiceStatusNotification')
@mock.patch('nova.db.service_update')
def test_service_update_without_notification(self,
mock_db_service_update,
mock_notification):
service_obj = objects.Service(context=self.ctxt, id=fake_service['id'])
mock_db_service_update.return_value = fake_service
for key, value in {'report_count': 13,
'last_seen_up': timeutils.utcnow()}.items():
setattr(service_obj, key, value)
service_obj.save()
self.assertFalse(mock_notification.called)