From fb2c5e9937d579c0445a46000db1a889bbf62fa0 Mon Sep 17 00:00:00 2001
From: Ivan Kolodyazhny <e0ne@e0ne.info>
Date: Sat, 20 Jun 2015 23:02:41 +0300
Subject: [PATCH] Use elevated context for backup destroy

db.backup_destroy requires admin context to mark backup record as
deleted. Commit Icff37261b367463b71a1268be16f9c97f595bf0c removed admin
context passed to backup_destroy method.

This patch uses elevated context to destroy backup.
Change-Id: I75b9e1fff48569a8aa320f2f02914fc7b6665d79
Closes-Bug: #1467167
---
 cinder/backup/manager.py                 |  1 -
 cinder/objects/backup.py                 |  3 ++-
 cinder/tests/unit/objects/test_backup.py | 13 ++++++++++++-
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/cinder/backup/manager.py b/cinder/backup/manager.py
index cbc0255e7..4c6d7e877 100644
--- a/cinder/backup/manager.py
+++ b/cinder/backup/manager.py
@@ -456,7 +456,6 @@ class BackupManager(manager.SchedulerDependentManager):
             reservations = None
             LOG.exception(_LE("Failed to update usages deleting backup"))
 
-        context = context.elevated()
         backup.destroy()
 
         # Commit the reservations
diff --git a/cinder/objects/backup.py b/cinder/objects/backup.py
index 9132b4181..760a633fc 100644
--- a/cinder/objects/backup.py
+++ b/cinder/objects/backup.py
@@ -105,7 +105,8 @@ class Backup(base.CinderPersistentObject, base.CinderObject,
 
     @base.remotable
     def destroy(self):
-        db.backup_destroy(self._context, self.id)
+        with self.obj_as_admin():
+            db.backup_destroy(self._context, self.id)
 
 
 @base.CinderObjectRegistry.register
diff --git a/cinder/tests/unit/objects/test_backup.py b/cinder/tests/unit/objects/test_backup.py
index e6f08f6d4..3db5aa0da 100644
--- a/cinder/tests/unit/objects/test_backup.py
+++ b/cinder/tests/unit/objects/test_backup.py
@@ -14,6 +14,7 @@
 
 import mock
 
+from cinder import context
 from cinder import objects
 from cinder.tests.unit import fake_volume
 from cinder.tests.unit import objects as test_objects
@@ -32,6 +33,14 @@ fake_backup = {
 
 
 class TestBackup(test_objects.BaseObjectsTestCase):
+    def setUp(self):
+        super(TestBackup, self).setUp()
+        # NOTE (e0ne): base tests contains original RequestContext from
+        # oslo_context. We change it to our RequestContext implementation
+        # to have 'elevated' method
+        self.context = context.RequestContext(self.user_id, self.project_id,
+                                              is_admin=False)
+
     @staticmethod
     def _compare(test, db, obj):
         for field, value in db.items():
@@ -62,7 +71,9 @@ class TestBackup(test_objects.BaseObjectsTestCase):
     def test_destroy(self, backup_destroy):
         backup = objects.Backup(context=self.context, id=1)
         backup.destroy()
-        backup_destroy.assert_called_once_with(self.context, '1')
+        self.assertTrue(backup_destroy.called)
+        admin_context = backup_destroy.call_args[0][0]
+        self.assertTrue(admin_context.is_admin)
 
 
 class TestBackupList(test_objects.BaseObjectsTestCase):
-- 
2.45.2