Add fake_cgsnapshot for unittest use

This patch add the missing fake object generator
for CG snapshot.

Change-Id: Ifee32b2d649d6a37813d116ac981cd89aeb52237
Cloese-bug: #1563770
This commit is contained in:
Peter Wang 2016-03-30 16:53:08 +08:00
parent 657943b0cb
commit 0e5e631087
2 changed files with 70 additions and 7 deletions

View File

@ -0,0 +1,51 @@
# Copyright 2016 EMC 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.
from oslo_versionedobjects import fields
from cinder import objects
from cinder.tests.unit import fake_constants as fake
def fake_db_cgsnapshot(**updates):
db_values = {
'id': fake.cgsnapshot_id,
'consistencygroup_id': fake.consistency_group_id,
'user_id': fake.user_id,
'project_id': fake.project_id,
}
for name, field in objects.CGSnapshot.fields.items():
if name in db_values:
continue
if field.nullable:
db_values[name] = None
elif field.default != fields.UnspecifiedDefault:
db_values[name] = field.default
else:
raise Exception('fake_db_snapshot needs help with %s' %
name)
if updates:
db_values.update(updates)
return db_values
def fake_cgsnapshot_obj(context, **updates):
expected_attrs = updates.pop('expected_attrs', None)
return objects.CGSnapshot._from_db_object(context,
objects.CGSnapshot(),
fake_db_cgsnapshot(
**updates),
expected_attrs=expected_attrs)

View File

@ -25,6 +25,7 @@ from cinder import context
from cinder import exception from cinder import exception
from cinder.objects import fields from cinder.objects import fields
from cinder import test from cinder import test
from cinder.tests.unit import fake_cgsnapshot
from cinder.tests.unit import fake_consistencygroup from cinder.tests.unit import fake_consistencygroup
from cinder.tests.unit import fake_snapshot from cinder.tests.unit import fake_snapshot
from cinder.tests.unit import fake_volume from cinder.tests.unit import fake_volume
@ -2776,6 +2777,7 @@ Time Remaining: 0 second(s)
def test_create_volume_snapshot_failed(self): def test_create_volume_snapshot_failed(self):
test_snapshot = EMCVNXCLIDriverTestData.convert_snapshot( test_snapshot = EMCVNXCLIDriverTestData.convert_snapshot(
self.testData.test_snapshot1) self.testData.test_snapshot1)
commands = [self.testData.SNAP_CREATE_CMD(test_snapshot.name)] commands = [self.testData.SNAP_CREATE_CMD(test_snapshot.name)]
results = [FAKE_ERROR_RETURN] results = [FAKE_ERROR_RETURN]
fake_cli = self.driverSetup(commands, results) fake_cli = self.driverSetup(commands, results)
@ -4236,7 +4238,9 @@ Time Remaining: 0 second(s)
snapshot_obj = fake_snapshot.fake_snapshot_obj( snapshot_obj = fake_snapshot.fake_snapshot_obj(
self.testData.SNAPS_IN_SNAP_GROUP()) self.testData.SNAPS_IN_SNAP_GROUP())
snapshot_obj.consistencygroup_id = cg_name snapshot_obj.consistencygroup_id = cg_name
self.driver.create_cgsnapshot(None, self.testData.test_cgsnapshot, cgsnap = fake_cgsnapshot.fake_cgsnapshot_obj(
None, **self.testData.test_cgsnapshot)
self.driver.create_cgsnapshot(None, cgsnap,
[snapshot_obj]) [snapshot_obj])
expect_cmd = [ expect_cmd = [
mock.call( mock.call(
@ -4257,7 +4261,9 @@ Time Remaining: 0 second(s)
snapshot_obj = fake_snapshot.fake_snapshot_obj( snapshot_obj = fake_snapshot.fake_snapshot_obj(
self.testData.SNAPS_IN_SNAP_GROUP()) self.testData.SNAPS_IN_SNAP_GROUP())
snapshot_obj.consistencygroup_id = cg_name snapshot_obj.consistencygroup_id = cg_name
self.driver.create_cgsnapshot(None, self.testData.test_cgsnapshot, cgsnap = fake_cgsnapshot.fake_cgsnapshot_obj(
None, **self.testData.test_cgsnapshot)
self.driver.create_cgsnapshot(None, cgsnap,
[snapshot_obj]) [snapshot_obj])
expect_cmd = [ expect_cmd = [
mock.call( mock.call(
@ -4278,8 +4284,10 @@ Time Remaining: 0 second(s)
self.testData.SNAPS_IN_SNAP_GROUP()) self.testData.SNAPS_IN_SNAP_GROUP())
cg_name = self.testData.test_cgsnapshot['consistencygroup_id'] cg_name = self.testData.test_cgsnapshot['consistencygroup_id']
snapshot_obj.consistencygroup_id = cg_name snapshot_obj.consistencygroup_id = cg_name
cg_snap = fake_cgsnapshot.fake_cgsnapshot_obj(
None, **self.testData.test_cgsnapshot)
self.driver.delete_cgsnapshot(None, self.driver.delete_cgsnapshot(None,
self.testData.test_cgsnapshot, cg_snap,
[snapshot_obj]) [snapshot_obj])
expect_cmd = [ expect_cmd = [
mock.call( mock.call(
@ -4497,7 +4505,8 @@ Time Remaining: 0 second(s)
'id': '222222', 'id': '222222',
'consistencygroup_id': 'new_cg_id', 'consistencygroup_id': 'new_cg_id',
'provider_location': None}) 'provider_location': None})
src_cgsnap = self.testData.test_cgsnapshot src_cgsnap = fake_cgsnapshot.fake_cgsnapshot_obj(
None, **self.testData.test_cgsnapshot)
snap1_in_src_cgsnap = EMCVNXCLIDriverTestData.convert_snapshot( snap1_in_src_cgsnap = EMCVNXCLIDriverTestData.convert_snapshot(
self.testData.test_member_cgsnapshot) self.testData.test_member_cgsnapshot)
@ -4616,7 +4625,8 @@ Time Remaining: 0 second(s)
new_cg = fake_consistencygroup.fake_consistencyobject_obj( new_cg = fake_consistencygroup.fake_consistencyobject_obj(
None, **self.testData.test_cg) None, **self.testData.test_cg)
vol1_in_new_cg = self.testData.test_volume_cg vol1_in_new_cg = self.testData.test_volume_cg
src_cgsnap = self.testData.test_cgsnapshot src_cgsnap = fake_cgsnapshot.fake_cgsnapshot_obj(
None, **self.testData.test_cgsnapshot)
snap1_in_src_cgsnap = fake_snapshot.fake_snapshot_obj( snap1_in_src_cgsnap = fake_snapshot.fake_snapshot_obj(
None, **self.testData.test_member_cgsnapshot) None, **self.testData.test_member_cgsnapshot)
src_cg = fake_consistencygroup.fake_consistencyobject_obj( src_cg = fake_consistencygroup.fake_consistencyobject_obj(
@ -4634,7 +4644,8 @@ Time Remaining: 0 second(s)
def test_create_cg_from_src_failed_with_invalid_source(self): def test_create_cg_from_src_failed_with_invalid_source(self):
new_cg = fake_consistencygroup.fake_consistencyobject_obj( new_cg = fake_consistencygroup.fake_consistencyobject_obj(
None, **self.testData.test_cg) None, **self.testData.test_cg)
src_cgsnap = self.testData.test_cgsnapshot src_cgsnap = fake_cgsnapshot.fake_cgsnapshot_obj(
None, **self.testData.test_cgsnapshot)
vol1_in_new_cg = self.testData.test_volume_cg vol1_in_new_cg = self.testData.test_volume_cg
src_cg = fake_consistencygroup.fake_consistencyobject_obj( src_cg = fake_consistencygroup.fake_consistencyobject_obj(
@ -4663,7 +4674,8 @@ Time Remaining: 0 second(s)
'id': '222222', 'id': '222222',
'consistencygroup_id': 'new_cg_id', 'consistencygroup_id': 'new_cg_id',
'provider_location': None}) 'provider_location': None})
src_cgsnap = self.testData.test_cgsnapshot src_cgsnap = fake_cgsnapshot.fake_cgsnapshot_obj(
None, **self.testData.test_cgsnapshot)
snap1_in_src_cgsnap = EMCVNXCLIDriverTestData.convert_snapshot( snap1_in_src_cgsnap = EMCVNXCLIDriverTestData.convert_snapshot(
self.testData.test_member_cgsnapshot) self.testData.test_member_cgsnapshot)
snap2_in_src_cgsnap = EMCVNXCLIDriverTestData.convert_snapshot( snap2_in_src_cgsnap = EMCVNXCLIDriverTestData.convert_snapshot(