Add CinderVolumes.create_volume_from_snapshot

First create a volume-snapshot, then create a volume
from this snapshot.

Change-Id: I29b4e6803806e2970e400920f3718e26e31911ce
This commit is contained in:
chenhb-zte 2016-05-26 15:19:12 -04:00
parent a29fcb15f3
commit 58a69efe29
5 changed files with 121 additions and 1 deletions

View File

@ -683,3 +683,21 @@
sla:
failure_rate:
max: 0
CinderVolumes.create_volume_from_snapshot:
-
args:
do_delete: true
runner:
type: "constant"
times: 3
concurrency: 2
context:
users:
tenants: 2
users_per_tenant: 2
volumes:
size: 1
sla:
failure_rate:
max: 0

View File

@ -582,3 +582,30 @@ class CinderVolumes(cinder_utils.CinderScenario,
source_vol = self._create_volume(source_vol.size,
source_volid=source_vol.id,
atomic_action=False, **kwargs)
@validation.required_services(consts.Service.CINDER)
@validation.required_contexts("volumes")
@validation.required_openstack(users=True)
@scenario.configure(context={"cleanup": ["cinder"]})
def create_volume_from_snapshot(self, do_delete=True,
create_snapshot_kwargs=None,
**kwargs):
"""Create a volume-snapshot, then create a volume from this snapshot.
:param do_delete: if True, a snapshot and a volume will
be deleted after creation.
:param create_snapshot_kwargs: optional args to create a snapshot
:param kwargs: optional args to create a volume
"""
create_snapshot_kwargs = create_snapshot_kwargs or {}
src_volume = random.choice(self.context["tenant"]["volumes"])
snapshot = self._create_snapshot(src_volume["id"],
**create_snapshot_kwargs)
volume = self._create_volume(src_volume["size"],
snapshot_id=snapshot.id,
**kwargs)
if do_delete:
self._delete_snapshot(snapshot)
self._delete_volume(volume)

View File

@ -0,0 +1,23 @@
{
"CinderVolumes.create_volume_from_snapshot": [
{
"args": {
"do_delete": true
},
"runner": {
"type": "constant",
"times": 3,
"concurrency": 2
},
"context": {
"users": {
"tenants": 2,
"users_per_tenant": 2
},
"volumes": {
"size": 1
}
}
}
]
}

View File

@ -0,0 +1,15 @@
---
CinderVolumes.create_volume_from_snapshot:
-
args:
do_delete: true
runner:
type: "constant"
times: 3
concurrency: 2
context:
users:
tenants: 2
users_per_tenant: 2
volumes:
size: 1

View File

@ -36,7 +36,7 @@ class CinderServersTestCase(test.ScenarioTestCase):
"user": {"tenant_id": "fake",
"credential": mock.MagicMock()},
"tenant": {"id": "fake", "name": "fake",
"volumes": [{"id": "uuid"}],
"volumes": [{"id": "uuid", "size": 1}],
"servers": [1]}})
return context
@ -522,3 +522,40 @@ class CinderServersTestCase(test.ScenarioTestCase):
self._test_atomic_action_timer(scenario.atomic_actions(),
"cinder.clone_volume")
scenario._create_volume.assert_has_calls(expected)
def test_create_volume_from_snapshot(self):
fake_snapshot = mock.MagicMock(id=1)
fake_volume = mock.MagicMock()
create_snapshot_args = {"force": False}
scenario = volumes.CinderVolumes(self._get_context())
scenario._create_snapshot = mock.MagicMock(return_value=fake_snapshot)
scenario._create_volume = mock.MagicMock(return_value=fake_volume)
scenario._delete_snapshot = mock.MagicMock()
scenario._delete_volume = mock.MagicMock()
scenario.create_volume_from_snapshot(fakearg="f")
scenario._create_snapshot.assert_called_once_with("uuid")
scenario._create_volume.assert_called_once_with(
1, snapshot_id=fake_snapshot.id, fakearg="f")
scenario._delete_snapshot.assert_called_once_with(fake_snapshot)
scenario._delete_volume.assert_called_once_with(fake_volume)
scenario._create_snapshot.reset_mock()
scenario._create_volume.reset_mock()
scenario._delete_snapshot.reset_mock()
scenario._delete_volume.reset_mock()
scenario.create_volume_from_snapshot(
do_delete=False,
create_snapshot_kwargs=create_snapshot_args,
fakearg="f"
)
scenario._create_snapshot.assert_called_once_with(
"uuid", **create_snapshot_args)
scenario._create_volume.assert_called_once_with(
1, snapshot_id=fake_snapshot.id, fakearg="f")
self.assertFalse(scenario._delete_snapshot.called)
self.assertFalse(scenario._delete_volume.called)