]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Create unit tests for volume objects
authorThang Pham <thang.g.pham@gmail.com>
Mon, 16 Mar 2015 15:48:07 +0000 (11:48 -0400)
committerThang Pham <thang.g.pham@gmail.com>
Thu, 26 Mar 2015 19:15:26 +0000 (19:15 +0000)
This is a follow up patch to cinder objects that adds
missing unit tests for volume objects. It also adds
a @base.remotable decorator to the volume save method,
which makes sure a context is properly supplied to the
method, whether it is passed in or taken from the
context stashed in the object.

Change-Id: I9febfdf94c4d8c82defedf0ceaec03800552affe
Implements: blueprint cinder-objects
Closes-Bug: #1434571

cinder/objects/volume.py
cinder/tests/fake_volume.py
cinder/tests/objects/test_volume.py [new file with mode: 0644]

index 6ba556b3a2f5f87c87c2bbaccd9fbbf8c5004a50..ccc53e93811e56fb86578514cdd904f68ac7279c 100644 (file)
@@ -119,8 +119,8 @@ class Volume(base.CinderPersistentObject, base.CinderObject,
         db_volume = db.volume_create(context, updates)
         self._from_db_object(context, self, db_volume)
 
-    def save(self):
-        context = self._context
+    @base.remotable
+    def save(self, context):
         updates = self.obj_get_changes()
         if updates:
             db.volume_update(context, self.id, updates)
index 624d30d2f65323adf158ea9e312eb8731ebf0317..5aba19012d7b5f85e364ee719975b3b278c7d2e8 100644 (file)
@@ -18,9 +18,9 @@ from cinder.objects import fields
 
 def fake_db_volume(**updates):
     db_volume = {
-        'id': 1,
+        'id': '1',
         'size': 1,
-        'name': 'fake',
+        'name': 'volume-1',
         'availability_zone': 'fake_availability_zone',
         'status': 'available',
         'attach_status': 'detached',
diff --git a/cinder/tests/objects/test_volume.py b/cinder/tests/objects/test_volume.py
new file mode 100644 (file)
index 0000000..840f30c
--- /dev/null
@@ -0,0 +1,74 @@
+#    Copyright 2015 SimpliVity Corp.
+#
+#    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 cinder import objects
+from cinder.tests import fake_volume
+from cinder.tests.objects import test_objects
+
+
+class TestVolume(test_objects._LocalTest):
+    @staticmethod
+    def _compare(test, db, obj):
+        for field, value in db.items():
+            test.assertEqual(db[field], obj[field])
+
+    @mock.patch('cinder.db.volume_get')
+    def test_get_by_id(self, volume_get):
+        db_volume = fake_volume.fake_db_volume()
+        volume_get.return_value = db_volume
+        volume = objects.Volume.get_by_id(self.context, 1)
+        self._compare(self, db_volume, volume)
+
+    @mock.patch('cinder.db.volume_create')
+    def test_create(self, volume_create):
+        db_volume = fake_volume.fake_db_volume()
+        volume_create.return_value = db_volume
+        volume = objects.Volume(context=self.context)
+        volume.create()
+        self.assertEqual(db_volume['id'], volume.id)
+
+    @mock.patch('cinder.db.volume_update')
+    def test_save(self, volume_update):
+        db_volume = fake_volume.fake_db_volume()
+        volume = objects.Volume._from_db_object(self.context,
+                                                objects.Volume(), db_volume)
+        volume.display_name = 'foobar'
+        volume.save()
+        volume_update.assert_called_once_with(self.context, volume.id,
+                                              {'display_name': 'foobar'})
+
+    @mock.patch('cinder.db.volume_destroy')
+    def test_destroy(self, volume_destroy):
+        db_volume = fake_volume.fake_db_volume()
+        volume = objects.Volume._from_db_object(self.context,
+                                                objects.Volume(), db_volume)
+        volume.destroy()
+        volume_destroy.assert_called_once_with(self.context, '1')
+
+
+class TestVolumeList(test_objects._LocalTest):
+    @mock.patch('cinder.db.volume_get_all')
+    def test_get_all(self, volume_get_all):
+        db_volume = fake_volume.fake_db_volume()
+        volume_get_all.return_value = [db_volume]
+
+        volumes = objects.VolumeList.get_all(self.context,
+                                             mock.sentinel.marker,
+                                             mock.sentinel.limit,
+                                             mock.sentinel.sort_key,
+                                             mock.sentinel.sort_dir)
+        self.assertEqual(1, len(volumes))
+        TestVolume._compare(self, db_volume, volumes[0])