Skip to content

Commit

Permalink
Create new class TestBackupRestoreWithSnapshot to place new test
Browse files Browse the repository at this point in the history
scripts
  • Loading branch information
TachunLin committed Aug 9, 2024
1 parent 831369d commit 1e6d8b8
Showing 1 changed file with 101 additions and 26 deletions.
127 changes: 101 additions & 26 deletions harvester_e2e_tests/integrations/test_4_vm_backup_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,32 +555,124 @@ def test_restore_with_invalid_name(self, api_client, backup_config, base_vm_with
code, data = api_client.backups.restore(unique_vm_name, spec)
assert 422 == code, (code, data)

@pytest.mark.skip_version_if('< v1.2.2')
@pytest.mark.dependency(depends=["TestBackupRestore::tests_backup_vm"], param=True)
def test_with_snapshot_restore_with_new_vm(
self, api_client, vm_shell_from_host, vm_checker, ssh_keypair, wait_timeout,
def test_restore_replace_with_vm_shutdown_command(
self, api_client, vm_shell_from_host, ssh_keypair, wait_timeout, vm_checker,
backup_config, base_vm_with_data
):
''' ref: https://github.com/harvester/tests/issues/943
1. Create VM and write some data
2. Take backup for the VM
3. Mess up existing data
3. Shutdown the VM by executing `shutdown` command in OS
4. Restore backup to replace existing VM
5. VM should be restored successfully
6. Data in VM should be the same as backed up
'''

unique_vm_name, backup_data = base_vm_with_data['name'], base_vm_with_data['data']
pub_key, pri_key = ssh_keypair

vm_backup_name = unique_vm_name + '-backup-new'
# mess up the existing data then shutdown it
with vm_shell_from_host(
base_vm_with_data['host_ip'], base_vm_with_data['vm_ip'],
base_vm_with_data['ssh_user'], pkey=pri_key
) as sh:
out, err = sh.exec_command(f"echo {pub_key!r} > {base_vm_with_data['data']['path']}")
assert not err, (out, err)
sh.exec_command('sync')
sh.exec_command('sudo shutdown now')

endtime = datetime.now() + timedelta(seconds=wait_timeout)
while endtime > datetime.now():
code, data = api_client.vms.get(unique_vm_name)
if 200 == code and "Stopped" == data.get('status', {}).get('printableStatus'):
break
sleep(5)
else:
raise AssertionError(
f"Failed to shut down VM({unique_vm_name}) with errors:\n"
f"Status({code}): {data}"
)

# restore VM to existing
spec = api_client.backups.RestoreSpec.for_existing(delete_volumes=True)
code, data = api_client.backups.restore(unique_vm_name, spec)
assert 201 == code, f'Failed to restore backup with current VM replaced, {data}'

# Check VM Started then get IPs (vm and host)
vm_got_ips, (code, data) = vm_checker.wait_ip_addresses(unique_vm_name, ['default'])
assert vm_got_ips, (
f"Failed to Start VM({unique_vm_name}) with errors:\n"
f"Status: {data.get('status')}\n"
f"API Status({code}): {data}"
)
vm_ip = next(iface['ipAddress'] for iface in data['status']['interfaces']
if iface['name'] == 'default')
code, data = api_client.hosts.get(data['status']['nodeName'])
host_ip = next(addr['address'] for addr in data['status']['addresses']
if addr['type'] == 'InternalIP')

# Login to the new VM and check data is existing
with vm_shell_from_host(host_ip, vm_ip, base_vm_with_data['ssh_user'], pkey=pri_key) as sh:
cloud_inited, (out, err) = vm_checker.wait_cloudinit_done(sh)
assert cloud_inited, (
f"VM {unique_vm_name} Started {wait_timeout} seconds"
f", but cloud-init still in {out}"
)
out, err = sh.exec_command(f"cat {backup_data['path']}")

assert backup_data['content'] in out, (
f"cloud-init writefile failed\n"
f"Executed stdout: {out}\n"
f"Executed stderr: {err}"
)


@pytest.mark.p0
@pytest.mark.backup_target
@pytest.mark.parametrize(
"backup_config", [
pytest.param("S3", marks=pytest.mark.S3),
pytest.param("NFS", marks=pytest.mark.NFS)
],
indirect=True)
class TestBackupRestoreWithSnapshot:

@pytest.mark.dependency()
def test_connection(self, api_client, backup_config, config_backup_target):
code, data = api_client.settings.backup_target_test_connection()
assert 200 == code, f'Failed to test backup target connection: {data}'

@pytest.mark.dependency(depends=["TestBackupRestoreWithSnapshot::test_connection"], param=True)
def tests_backup_vm(self, api_client, wait_timeout, backup_config, base_vm_with_data):
unique_vm_name = base_vm_with_data['name']

# Create backup with the name as VM's name
code, data = api_client.vms.backup(unique_vm_name, vm_backup_name)
code, data = api_client.vms.backup(unique_vm_name, unique_vm_name)
assert 204 == code, (code, data)
# Check backup is ready
endtime = datetime.now() + timedelta(seconds=wait_timeout)
while endtime > datetime.now():
code, backup = api_client.backups.get(vm_backup_name)
code, backup = api_client.backups.get(unique_vm_name)
if 200 == code and backup.get('status', {}).get('readyToUse'):
break
sleep(3)
else:
raise AssertionError(
f'Timed-out waiting for the backup \'{vm_backup_name}\' to be ready.'
f'Timed-out waiting for the backup \'{unique_vm_name}\' to be ready.'
)

vm_snapshot_name = unique_vm_name + '-snapshot-new'
@pytest.mark.dependency(depends=["TestBackupRestoreWithSnapshot::tests_backup_vm"], param=True)
def test_with_snapshot_restore_with_new_vm(
self, api_client, vm_shell_from_host, vm_checker, ssh_keypair, wait_timeout,
backup_config, base_vm_with_data
):
unique_vm_name, backup_data = base_vm_with_data['name'], base_vm_with_data['data']
pub_key, pri_key = ssh_keypair

vm_snapshot_name = unique_vm_name + '-snapshot'
# take vm snapshot
code, data = api_client.vm_snapshots.create(unique_vm_name, vm_snapshot_name)
assert 201 == code
Expand Down Expand Up @@ -667,32 +759,15 @@ def test_with_snapshot_restore_with_new_vm(
vol_name = vol['volume']['persistentVolumeClaim']['claimName']
api_client.volumes.delete(vol_name)

@pytest.mark.dependency(depends=["TestBackupRestore::tests_backup_vm"], param=True)
@pytest.mark.dependency(depends=["TestBackupRestoreWithSnapshot::tests_backup_vm"], param=True)
def test_with_snapshot_restore_replace_retain_vols(
self, api_client, vm_shell_from_host, ssh_keypair, wait_timeout, vm_checker,
backup_config, base_vm_with_data
):
unique_vm_name, backup_data = base_vm_with_data['name'], base_vm_with_data['data']
pub_key, pri_key = ssh_keypair

vm_backup_name = unique_vm_name + '-backup-replace'

# Create backup with the name as VM's name
code, data = api_client.vms.backup(unique_vm_name, vm_backup_name)
assert 204 == code, (code, data)
# Check backup is ready
endtime = datetime.now() + timedelta(seconds=wait_timeout)
while endtime > datetime.now():
code, backup = api_client.backups.get(vm_backup_name)
if 200 == code and backup.get('status', {}).get('readyToUse'):
break
sleep(3)
else:
raise AssertionError(
f'Timed-out waiting for the backup \'{vm_backup_name}\' to be ready.'
)

vm_snapshot_name = unique_vm_name + '-snapshot-replace'
vm_snapshot_name = unique_vm_name + '-snapshot-retain'
# take vm snapshot
code, data = api_client.vm_snapshots.create(unique_vm_name, vm_snapshot_name)
assert 201 == code
Expand Down

0 comments on commit 1e6d8b8

Please sign in to comment.