Skip to content

Commit d7b823d

Browse files
cnv-tests-github-webhook-dollierp[bot]vsibirskmyakove
authored
CherryPicked: [cnv-4.22] [Storage] Automate testing of predictable PVC/DV names after VM restore (#5187)
Cherry-pick from `main` branch, original PR: #4780, PR owner: acinko-rh Signed-off-by: Adam Cinko <vsibirsk@redhat.com> Co-authored-by: Adam Cinko <vsibirsk@redhat.com> Co-authored-by: Meni Yakove <441263+myakove@users.noreply.github.com>
1 parent f65f55f commit d7b823d

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

tests/storage/snapshots/conftest.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99
from ocp_resources.datavolume import DataVolume
1010
from ocp_resources.role_binding import RoleBinding
11+
from ocp_resources.virtual_machine_restore import VirtualMachineRestore
1112
from ocp_resources.virtual_machine_snapshot import VirtualMachineSnapshot
1213
from pyhelper_utils.shell import run_ssh_commands
1314

@@ -104,3 +105,33 @@ def file_created_during_snapshot(windows_vm_for_snapshot, windows_snapshot):
104105
run_ssh_commands(host=windows_vm_for_snapshot.ssh_exec, commands=cmd, wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC)
105106
windows_snapshot.wait_snapshot_done(timeout=TIMEOUT_10MIN)
106107
windows_vm_for_snapshot.stop(wait=True)
108+
109+
110+
@pytest.fixture()
111+
def source_volume_name_for_predictable_name_restore(rhel_vm_for_snapshot):
112+
yield next(
113+
volume.name
114+
for volume in rhel_vm_for_snapshot.instance.spec.template.spec.volumes
115+
if getattr(volume, "dataVolume", None) or getattr(volume, "persistentVolumeClaim", None)
116+
)
117+
118+
119+
@pytest.fixture()
120+
def vm_restore_with_predictable_names(
121+
admin_client,
122+
rhel_vm_for_snapshot,
123+
snapshot_with_content,
124+
):
125+
if rhel_vm_for_snapshot.ready:
126+
rhel_vm_for_snapshot.stop(wait=True)
127+
128+
with VirtualMachineRestore(
129+
name=f"{rhel_vm_for_snapshot.name}-restored",
130+
namespace=rhel_vm_for_snapshot.namespace,
131+
vm_name=rhel_vm_for_snapshot.name,
132+
snapshot_name=snapshot_with_content[0].name,
133+
client=admin_client,
134+
volume_restore_policy="PrefixTargetName",
135+
) as vm_restore:
136+
vm_restore.wait_restore_done(timeout=TIMEOUT_10MIN)
137+
yield vm_restore

tests/storage/snapshots/test_snapshots.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,49 @@ def test_restore_same_snapshot_twice(
264264
expected_result=expected_output_after_restore(1),
265265
)
266266

267+
@pytest.mark.parametrize(
268+
"rhel_vm_name, snapshot_with_content",
269+
[
270+
pytest.param(
271+
{"vm_name": "vm-cnv-16212"},
272+
{"number_of_snapshots": 1, "online_vm": False},
273+
marks=pytest.mark.polarion("CNV-16212"),
274+
),
275+
],
276+
indirect=True,
277+
)
278+
def test_restore_snapshot_with_predictable_names(
279+
self,
280+
vm_restore_with_predictable_names,
281+
source_volume_name_for_predictable_name_restore,
282+
):
283+
"""
284+
Test restore snapshot where the DV/PVC restored has a predictable name derived from the source vm name and
285+
source volume name when `volumeRestorePolicy` is set to `PrefixTargetName`.
286+
287+
Preconditions:
288+
- A VM snapshot (any).
289+
- Volume restore policy is set to `PrefixTargetName`.
290+
291+
Steps:
292+
1. Restore the snapshot.
293+
294+
Expected Results:
295+
- The restored DV/PVC name matches the expected predictable name derived from the source vm name and source volume name.
296+
"""
297+
298+
restore_status = vm_restore_with_predictable_names.instance.status
299+
expected_name = (
300+
f"{vm_restore_with_predictable_names.vm_name}-{source_volume_name_for_predictable_name_restore}"[:63]
301+
)
302+
303+
assert restore_status.restores[0].dataVolumeName == expected_name, (
304+
f"Restored DV name is '{restore_status.restores[0].dataVolumeName}', expected '{expected_name}'"
305+
)
306+
assert restore_status.restores[0].persistentVolumeClaim == expected_name, (
307+
f"Restored PVC name is '{restore_status.restores[0].persistentVolumeClaim}', expected '{expected_name}'"
308+
)
309+
267310

268311
@pytest.mark.parametrize(
269312
"rhel_vm_name, snapshot_with_content",

0 commit comments

Comments
 (0)