From 1e6d8b8a0f68939d6bffa1658196ef1579e1fddd Mon Sep 17 00:00:00 2001 From: davidtclin Date: Sat, 13 Jul 2024 14:31:40 +0800 Subject: [PATCH] Create new class TestBackupRestoreWithSnapshot to place new test scripts --- .../integrations/test_4_vm_backup_restore.py | 127 ++++++++++++++---- 1 file changed, 101 insertions(+), 26 deletions(-) diff --git a/harvester_e2e_tests/integrations/test_4_vm_backup_restore.py b/harvester_e2e_tests/integrations/test_4_vm_backup_restore.py index 3e75366d3..21e2fe313 100644 --- a/harvester_e2e_tests/integrations/test_4_vm_backup_restore.py +++ b/harvester_e2e_tests/integrations/test_4_vm_backup_restore.py @@ -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 @@ -667,7 +759,7 @@ 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 @@ -675,24 +767,7 @@ def test_with_snapshot_restore_replace_retain_vols( 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