From 982c423dfefe85d498887d5d57ef6b5c8e4105cf Mon Sep 17 00:00:00 2001 From: Thang Pham Date: Mon, 16 Mar 2015 11:48:07 -0400 Subject: [PATCH] Create unit tests for volume objects 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 | 4 +- cinder/tests/fake_volume.py | 4 +- cinder/tests/objects/test_volume.py | 74 +++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 cinder/tests/objects/test_volume.py diff --git a/cinder/objects/volume.py b/cinder/objects/volume.py index 6ba556b3a..ccc53e938 100644 --- a/cinder/objects/volume.py +++ b/cinder/objects/volume.py @@ -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) diff --git a/cinder/tests/fake_volume.py b/cinder/tests/fake_volume.py index 624d30d2f..5aba19012 100644 --- a/cinder/tests/fake_volume.py +++ b/cinder/tests/fake_volume.py @@ -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 index 000000000..840f30c13 --- /dev/null +++ b/cinder/tests/objects/test_volume.py @@ -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]) -- 2.45.2