diff --git a/cinder/tests/unit/api/v1/test_snapshots.py b/cinder/tests/unit/api/v1/test_snapshots.py index 3f95282e945..e3c593d99ba 100644 --- a/cinder/tests/unit/api/v1/test_snapshots.py +++ b/cinder/tests/unit/api/v1/test_snapshots.py @@ -108,7 +108,7 @@ class SnapshotApiTest(test.TestCase): self.stubs.Set(volume.api.API, "create_snapshot_force", stub_snapshot_create) - self.stubs.Set(volume.api.API, 'get', stubs.stub_volume_get) + self.mock_object(volume.api.API, 'get', stubs.stub_volume_api_get) snapshot = {"volume_id": fake.VOLUME_ID, "force": force_param, "display_name": "Snapshot Test Name", @@ -128,7 +128,7 @@ class SnapshotApiTest(test.TestCase): self.stubs.Set(volume.api.API, "create_snapshot_force", stub_snapshot_create) - self.stubs.Set(volume.api.API, 'get', stubs.stub_volume_get) + self.mock_object(volume.api.API, 'get', stubs.stub_volume_api_get) snapshot = {"volume_id": fake.VOLUME_ID, "force": force_param, "display_name": "Snapshot Test Name", @@ -145,7 +145,7 @@ class SnapshotApiTest(test.TestCase): self.stubs.Set(volume.api.API, "create_snapshot_force", stub_snapshot_create) - self.stubs.Set(volume.api.API, 'get', stubs.stub_volume_get) + self.mock_object(volume.api.API, 'get', stubs.stub_volume_api_get) snapshot = {"volume_id": fake.SNAPSHOT_ID, "force": "**&&^^%%$$##@@", "display_name": "Snapshot Test Name", diff --git a/cinder/tests/unit/test_volume.py b/cinder/tests/unit/test_volume.py index 76f6ab71dc1..700bcff99bf 100644 --- a/cinder/tests/unit/test_volume.py +++ b/cinder/tests/unit/test_volume.py @@ -3120,6 +3120,19 @@ class VolumeTestCase(base.BaseVolumeTestCase): 'fake_description', fake.CONSISTENCY_GROUP_ID) + def test_create_snapshot_failed_host_is_None(self): + """Test exception handling when create snapshot and host is None.""" + test_volume = tests_utils.create_volume( + self.context, + host=None) + volume_api = cinder.volume.api.API() + self.assertRaises(exception.InvalidVolume, + volume_api.create_snapshot, + self.context, + test_volume, + 'fake_name', + 'fake_description') + def test_cannot_delete_volume_in_use(self): """Test volume can't be deleted in in-use status.""" self._test_cannot_delete_volume('in-use') diff --git a/cinder/volume/api.py b/cinder/volume/api.py index 6a711ae77cf..36ec63308a0 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -772,6 +772,11 @@ class API(base.Base): group_snapshot_id=None): check_policy(context, 'create_snapshot', volume) + if not volume.host: + msg = _("The snapshot cannot be created because volume has " + "not been scheduled to any host.") + raise exception.InvalidVolume(reason=msg) + if volume['status'] == 'maintenance': LOG.info(_LI('Unable to create the snapshot for volume, ' 'because it is in maintenance.'), resource=volume)