-
Notifications
You must be signed in to change notification settings - Fork 36
Add volume snapshot test with vm part #2531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -597,3 +597,101 @@ def test_volume_snapshots_are_cleaned_up_after_source_volume_deleted(self, api_c | |
| sleep(1) | ||
| else: | ||
| raise AssertionError(f"timed out waiting for {volumesnapshotname} to be deleted") | ||
|
|
||
|
|
||
| @pytest.mark.p0 | ||
| @pytest.mark.virtualmachines | ||
| @pytest.mark.volumes | ||
| class TestVolumeSnapshotWithVM: | ||
| def test_snapshot_of_volume_on_vm(self, api_client, | ||
| source_vm, vm_checker, | ||
| volume_checker, wait_timeout, | ||
| host_shell, vm_shell, | ||
| ssh_keypair, polling_for): | ||
| """ | ||
| 1. Create a VM with volume | ||
| 2. Write some data on the VM | ||
| 3. Create snapshot of the volume of the VM | ||
| 4. Restore the snapshot to a new volume | ||
| 5. Create new VM and use the restored volume | ||
| 6. Check the data in the new VM | ||
| Ref. https://github.com/harvester/harvester/issues/2294 | ||
| """ | ||
| vm_name, ssh_user = source_vm | ||
|
|
||
| _, data = api_client.vms.get(vm_name) | ||
|
|
||
| vol_name = (data["spec"]["template"]["spec"]["volumes"][0] | ||
| ['persistentVolumeClaim']['claimName']) | ||
|
|
||
| vm_started, (code, vmi) = vm_checker.wait_started(vm_name) | ||
| assert vm_started, (code, vmi) | ||
|
|
||
| # Write 123 into test.txt | ||
| def action(sh): | ||
| _, _ = sh.exec_command("echo 123 > test.txt") # nosec B601 | ||
| _, _ = sh.exec_command("sync") # nosec B601 | ||
|
|
||
| vm_shell_do(vm_name, api_client, | ||
| host_shell, vm_shell, | ||
| ssh_user, ssh_keypair, | ||
| action, wait_timeout) | ||
|
|
||
| # Create a snapshot of the volume | ||
| snapshot_name = f"{vol_name[:60]}-snapshot" | ||
| code, _ = api_client.volumes.snapshot(vol_name, snapshot_name) | ||
| assert 204 == code, (f"Failed to create snapshot of volume {vol_name}") | ||
|
|
||
| # Wait the snapshot become ready | ||
| endtime = datetime.now() + timedelta(seconds=wait_timeout) | ||
| while endtime > datetime.now(): | ||
| code, data = api_client.vol_snapshots.get(snapshot_name) | ||
| if code == 200 and data.get('status', {}).get('readyToUse', False): | ||
| break | ||
| sleep(3) | ||
| else: | ||
| raise AssertionError( | ||
| f"Timeout in waiting the snapshot {snapshot_name} to be ready.\n" | ||
| f"Last API Status({code}): {data}" | ||
| ) | ||
|
|
||
| # Restore the snapshot | ||
| restore_vol_name = f"{snapshot_name[:60]}-restore" | ||
| code, _ = api_client.vol_snapshots.restore(snapshot_name, restore_vol_name) | ||
| assert 204 == code, ( | ||
| f"Failed to restore snapshot {snapshot_name} to volume {restore_vol_name}") | ||
|
|
||
| # Wait the restored volume become ready, check the data resource as expected | ||
| volumes_ready, (code, data) = volume_checker.wait_volumes_ready([restore_vol_name]) | ||
| assert volumes_ready, (code, data) | ||
| data_source = data.get('spec', {}).get('dataSource', {}).get('name') | ||
| assert snapshot_name == data_source, ( | ||
| f"Data source mismatch. Expected {snapshot_name}, got {data_source}") | ||
|
|
||
| # Create new VM and use the restored volume | ||
| vm_with_restore_vol_name = f"{restore_vol_name[:60]}-vm" | ||
| vm_spec = api_client.vms.Spec(1, 2) | ||
| vm_spec.add_existing_volume(vm_with_restore_vol_name, restore_vol_name) | ||
| code, data = api_client.vms.create(vm_with_restore_vol_name, vm_spec) | ||
| assert 201 == code, f"Fail to create VM\n{code}, {data}" | ||
| code, data = polling_for( | ||
| "VM do created", | ||
| lambda c, d: 200 == c and d.get('status', {}).get('printableStatus') == "Running", | ||
| api_client.vms.get, vm_with_restore_vol_name | ||
| ) | ||
|
|
||
| # Check the data in the new VM | ||
| def actassert(sh): | ||
| out, _ = sh.exec_command("cat test.txt") # nosec B601 | ||
| assert "123" in out | ||
|
Comment on lines
+683
to
+686
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| vm_shell_do(vm_with_restore_vol_name, api_client, | ||
| host_shell, vm_shell, | ||
| ssh_user, ssh_keypair, | ||
| actassert, wait_timeout) | ||
|
|
||
| # Teardown: delete the restore volume | ||
| vm_deleted, (code, data) = vm_checker.wait_deleted(vm_with_restore_vol_name) | ||
| assert vm_deleted, (code, data) | ||
| volumes_deleted, (code, data) = volume_checker.wait_volumes_deleted([restore_vol_name]) | ||
| assert volumes_deleted, (code, data) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nonblocking: this is great 😄 - perhaps in a future enhancement we can collect & audit an
md5sumof it, to validate that when it's restored the data's file integrity is the same.