snap = db.snapshot_get(context.get_admin_context(), snapshot['id'])
self.assertEquals(snap['display_name'], 'test update name')
+ def test_volume_get_active_by_window(self):
+ # Find all all volumes valid within a timeframe window.
+ try: # Not in window
+ db.volume_create(
+ self.context,
+ {
+ 'id': 1,
+ 'host': 'devstack',
+ 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1),
+ 'deleted': True, 'status': 'deleted',
+ 'deleted_at': datetime.datetime(1, 2, 1, 1, 1, 1),
+ }
+ )
+ except exception.VolumeNotFound:
+ pass
+
+ try: # In - deleted in window
+ db.volume_create(
+ self.context,
+ {
+ 'id': 2,
+ 'host': 'devstack',
+ 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1),
+ 'deleted': True, 'status': 'deleted',
+ 'deleted_at': datetime.datetime(1, 3, 10, 1, 1, 1),
+ }
+ )
+ except exception.VolumeNotFound:
+ pass
+
+ try: # In - deleted after window
+ db.volume_create(
+ self.context,
+ {
+ 'id': 3,
+ 'host': 'devstack',
+ 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1),
+ 'deleted': True, 'status': 'deleted',
+ 'deleted_at': datetime.datetime(1, 5, 1, 1, 1, 1),
+ }
+ )
+ except exception.VolumeNotFound:
+ pass
+
+ # In - created in window
+ db.volume_create(
+ self.context,
+ {
+ 'id': 4,
+ 'host': 'devstack',
+ 'created_at': datetime.datetime(1, 3, 10, 1, 1, 1),
+ }
+ )
+
+ # Not of window.
+ db.volume_create(
+ self.context,
+ {
+ 'id': 5,
+ 'host': 'devstack',
+ 'created_at': datetime.datetime(1, 5, 1, 1, 1, 1),
+ }
+ )
+
+ volumes = db.volume_get_active_by_window(
+ self.context,
+ datetime.datetime(1, 3, 1, 1, 1, 1),
+ datetime.datetime(1, 4, 1, 1, 1, 1))
+ self.assertEqual(len(volumes), 3)
+ self.assertEqual(volumes[0].id, u'2')
+ self.assertEqual(volumes[1].id, u'3')
+ self.assertEqual(volumes[2].id, u'4')
+
+ def test_snapshot_get_active_by_window(self):
+ # Find all all snapshots valid within a timeframe window.
+ vol = db.volume_create(self.context, {'id': 1})
+
+ try: # Not in window
+ db.snapshot_create(
+ self.context,
+ {
+ 'id': 1,
+ 'host': 'devstack',
+ 'volume_id': 1,
+ 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1),
+ 'deleted': True, 'status': 'deleted',
+ 'deleted_at': datetime.datetime(1, 2, 1, 1, 1, 1),
+ }
+ )
+ except exception.SnapshotNotFound:
+ pass
+
+ try: # In - deleted in window
+ db.snapshot_create(
+ self.context,
+ {
+ 'id': 2,
+ 'host': 'devstack',
+ 'volume_id': 1,
+ 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1),
+ 'deleted': True, 'status': 'deleted',
+ 'deleted_at': datetime.datetime(1, 3, 10, 1, 1, 1),
+ }
+ )
+ except exception.SnapshotNotFound:
+ pass
+
+ try: # In - deleted after window
+ db.snapshot_create(
+ self.context,
+ {
+ 'id': 3,
+ 'host': 'devstack',
+ 'volume_id': 1,
+ 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1),
+ 'deleted': True, 'status': 'deleted',
+ 'deleted_at': datetime.datetime(1, 5, 1, 1, 1, 1),
+ }
+ )
+ except exception.SnapshotNotFound:
+ pass
+
+ # In - created in window
+ db.snapshot_create(
+ self.context,
+ {
+ 'id': 4,
+ 'host': 'devstack',
+ 'volume_id': 1,
+ 'created_at': datetime.datetime(1, 3, 10, 1, 1, 1),
+ }
+ )
+
+ # Not of window.
+ db.snapshot_create(
+ self.context,
+ {
+ 'id': 5,
+ 'host': 'devstack',
+ 'volume_id': 1,
+ 'created_at': datetime.datetime(1, 5, 1, 1, 1, 1),
+ }
+ )
+
+ snapshots = db.snapshot_get_active_by_window(
+ self.context,
+ datetime.datetime(1, 3, 1, 1, 1, 1),
+ datetime.datetime(1, 4, 1, 1, 1, 1))
+ self.assertEqual(len(snapshots), 3)
+ self.assertEqual(snapshots[0].id, u'2')
+ self.assertEqual(snapshots[1].id, u'3')
+ self.assertEqual(snapshots[2].id, u'4')
+
class DriverTestCase(test.TestCase):
"""Base Test class for Drivers."""
'exists', extra_usage_info=extra_usage_info)
-def _usage_from_volume(context, volume_ref, **kw):
- def null_safe_str(s):
- return str(s) if s else ''
+def null_safe_str(s):
+ return str(s) if s else ''
+
+def _usage_from_volume(context, volume_ref, **kw):
usage_info = dict(tenant_id=volume_ref['project_id'],
user_id=volume_ref['user_id'],
volume_id=volume_ref['id'],
notifier_api.notify(context, 'volume.%s' % host,
'volume.%s' % event_suffix,
notifier_api.INFO, usage_info)
+
+
+def _usage_from_snapshot(context, snapshot_ref, **extra_usage_info):
+ usage_info = {
+ 'tenant_id': snapshot_ref['project_id'],
+ 'user_id': snapshot_ref['user_id'],
+ 'volume_id': snapshot_ref['volume_id'],
+ 'volume_size': snapshot_ref['volume_size'],
+ 'snapshot_id': snapshot_ref['id'],
+ 'display_name': snapshot_ref['display_name'],
+ 'created_at': str(snapshot_ref['created_at']),
+ 'status': snapshot_ref['status'],
+ 'deleted': null_safe_str(snapshot_ref['deleted'])
+ }
+
+ usage_info.update(extra_usage_info)
+ return usage_info
+
+
+def notify_about_snapshot_usage(context, snapshot, event_suffix,
+ extra_usage_info=None, host=None):
+ if not host:
+ host = FLAGS.host
+
+ if not extra_usage_info:
+ extra_usage_info = {}
+
+ usage_info = _usage_from_snapshot(context, snapshot, **extra_usage_info)
+
+ notifier_api.notify(context, 'snapshot.%s' % host,
+ 'snapshot.%s' % event_suffix,
+ notifier_api.INFO, usage_info)