From f3197ed7459dba292c928dc67565f0d28cd5e69e Mon Sep 17 00:00:00 2001 From: Sandido Date: Tue, 20 Aug 2024 16:26:01 -0400 Subject: [PATCH] fixed several tests --- src/vm-repair/azext_vm_repair/repair_utils.py | 20 ++---- .../tests/latest/test_repair_commands.py | 72 +++++++++++++------ 2 files changed, 56 insertions(+), 36 deletions(-) diff --git a/src/vm-repair/azext_vm_repair/repair_utils.py b/src/vm-repair/azext_vm_repair/repair_utils.py index b6010cc57b4..f87677976f4 100644 --- a/src/vm-repair/azext_vm_repair/repair_utils.py +++ b/src/vm-repair/azext_vm_repair/repair_utils.py @@ -493,7 +493,6 @@ def _fetch_compatible_windows_os_urn(source_vm): def _select_distro_linux(distro): image_lookup = { - 'rhel6': 'RedHat:RHEL:6.10:latest', 'rhel7': 'RedHat:rhel-raw:7-raw:latest', 'rhel8': 'RedHat:rhel-raw:8-raw:latest', 'ubuntu18': 'Canonical:UbuntuServer:18.04-LTS:latest', @@ -502,10 +501,8 @@ def _select_distro_linux(distro): 'centos7': 'OpenLogic:CentOS:7_9:latest', 'centos8': 'OpenLogic:CentOS:8_4:latest', 'oracle6': 'Oracle:Oracle-Linux:6.10:latest', - 'oracle7': 'Oracle:Oracle-Linux:ol79:latest', - 'oracle8': 'Oracle:Oracle-Linux:ol82:latest', - 'sles12': 'SUSE:sles-12-sp5:gen1:latest', - 'sles15': 'SUSE:sles-15-sp3:gen1:latest', + 'sles12': 'SUSE:sles-12-sp5-hpc:gen1:latest', + 'sles15': 'SUSE:sles-15-sp3-sapcal:gen1:latest', } if distro in image_lookup: os_image_urn = image_lookup[distro] @@ -521,7 +518,7 @@ def _select_distro_linux(distro): def _select_distro_linux_Arm64(distro): image_lookup = { - 'rhel8': 'RedHat:rhel-arm64:8_8-arm64:latest', + 'rhel8': 'RedHat:rhel-arm64:8_8-arm64-gen2:latest', 'rhel9': 'RedHat:rhel-arm64:9_2-arm64:latest', 'ubuntu18': 'Canonical:UbuntuServer:18_04-lts-arm64:latest', 'ubuntu20': 'Canonical:0001-com-ubuntu-server-focal:20_04-lts-arm64:latest', @@ -542,19 +539,14 @@ def _select_distro_linux_Arm64(distro): def _select_distro_linux_gen2(distro): # base on the document : https://docs.microsoft.com/en-us/azure/virtual-machines/generation-2#generation-2-vm-images-in-azure-marketplace image_lookup = { - 'rhel6': 'RedHat:rhel-raw:7-raw-gen2:latest', - 'rhel7': 'RedHat:rhel-raw:7-raw-gen2:latest', - 'rhel8': 'RedHat:rhel-raw:8-raw-gen2:latest', + 'rhel8': 'RedHat:RHEL:8-lvm-gen2:latest', 'ubuntu18': 'Canonical:UbuntuServer:18_04-lts-gen2:latest', 'ubuntu20': 'Canonical:0001-com-ubuntu-server-focal:20_04-lts-gen2:latest', - 'centos6': 'OpenLogic:CentOS:7_9-gen2:latest', 'centos7': 'OpenLogic:CentOS:7_9-gen2:latest', 'centos8': 'OpenLogic:CentOS:8_4-gen2:latest', - 'oracle6': 'Oracle:Oracle-Linux:ol79-gen2:latest', - 'oracle7': 'Oracle:Oracle-Linux:ol79-gen2:latest', - 'oracle8': 'Oracle:Oracle-Linux:ol82-gen2:latest', + 'oracle8': 'Oracle:Oracle-Linux:o194-lvm-gen2:latest', 'sles12': 'SUSE:sles-12-sp5:gen2:latest', - 'sles15': 'SUSE:sles-15-sp3:gen2:latest', + 'sles15': 'SUSE:sles-15-sp3-sapcal:gen2:latest', } if distro in image_lookup: os_image_urn = image_lookup[distro] diff --git a/src/vm-repair/azext_vm_repair/tests/latest/test_repair_commands.py b/src/vm-repair/azext_vm_repair/tests/latest/test_repair_commands.py index b8ff0da28d7..00b37d5d204 100644 --- a/src/vm-repair/azext_vm_repair/tests/latest/test_repair_commands.py +++ b/src/vm-repair/azext_vm_repair/tests/latest/test_repair_commands.py @@ -7,10 +7,12 @@ import pytest from azure.cli.testsdk import LiveScenarioTest, ResourceGroupPreparer +import json +import re STATUS_SUCCESS = 'SUCCESS' - +@pytest.mark.WindowsManaged class WindowsManagedDiskCreateRestoreTest(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -44,7 +46,7 @@ def test_vmrepair_WinManagedCreateRestore(self, resource_group): source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] - +@pytest.mark.WindowsUnmanaged class WindowsUnmanagedDiskCreateRestoreTest(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -79,10 +81,10 @@ def test_vmrepair_WinUnmanagedCreateRestore(self, resource_group): assert source_vm['storageProfile']['osDisk']['vhd']['uri'] == result['copied_disk_uri'] -@pytest.mark.linux +@pytest.mark.linuxManaged class LinuxManagedDiskCreateRestoreTest(LiveScenarioTest): - @ResourceGroupPreparer(location='eastus') + @ResourceGroupPreparer(location='westus2') def test_vmrepair_LinuxManagedCreateRestore(self, resource_group): self.kwargs.update({ 'vm': 'vm1' @@ -114,10 +116,10 @@ def test_vmrepair_LinuxManagedCreateRestore(self, resource_group): assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] -@pytest.mark.linux +@pytest.mark.linuxUnmanaged class LinuxUnmanagedDiskCreateRestoreTest(LiveScenarioTest): - @ResourceGroupPreparer(location='eastus') + @ResourceGroupPreparer(location='westus2') def test_vmrepair_LinuxUnmanagedCreateRestore(self, resource_group): self.kwargs.update({ 'vm': 'vm1' @@ -148,7 +150,7 @@ def test_vmrepair_LinuxUnmanagedCreateRestore(self, resource_group): source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['vhd']['uri'] == result['copied_disk_uri'] - +@pytest.mark.WinManagedDiskPubIpRestore class WindowsManagedDiskCreateRestoreTestwithpublicip(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -182,7 +184,7 @@ def test_vmrepair_WinManagedCreateRestorePublicIp(self, resource_group): source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] - +@pytest.mark.WinUnmanagedDiskPubIpRestore class WindowsUnmanagedDiskCreateRestoreTestwithpublicip(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -216,7 +218,7 @@ def test_vmrepair_WinUnmanagedCreateRestorePublicIp(self, resource_group): source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['vhd']['uri'] == result['copied_disk_uri'] - +@pytest.mark.LinuxManagedDiskPubIpRestore class LinuxManagedDiskCreateRestoreTestwithpublicip(LiveScenarioTest): @ResourceGroupPreparer(location='eastus') @@ -250,7 +252,7 @@ def test_vmrepair_LinuxManagedCreateRestorePublicIp(self, resource_group): source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] - +@pytest.mark.LinuxUnmanagedDiskPubIpRestore class LinuxUnmanagedDiskCreateRestoreTestwithpublicip(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -394,7 +396,7 @@ def test_vmrepair_LinuxSinglepassKekEncryptedManagedDiskCreateRestore(self, reso source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] - +@pytest.mark.WindowsNoKekRestore class WindowsSinglepassNoKekEncryptedManagedDiskCreateRestoreTest(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -439,7 +441,7 @@ def test_vmrepair_WinSinglepassNoKekEncryptedManagedDiskCreateRestore(self, reso source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] - +@pytest.mark.LinuxNoKekRestore class LinuxSinglepassNoKekEncryptedManagedDiskCreateRestoreTest(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -486,7 +488,7 @@ def test_vmrepair_LinuxSinglepassNoKekEncryptedManagedDiskCreateRestoreTest(self source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] - +@pytest.mark.WindHelloWorld class WindowsRunHelloWorldTest(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -507,7 +509,7 @@ def test_vmrepair_WinRunHelloWorld(self, resource_group): # Check Output assert 'Hello World!' in result['output'] - +@pytest.mark.LinHelloWorld class LinuxRunHelloWorldTest(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -528,7 +530,7 @@ def test_vmrepair_LinuxRunHelloWorld(self, resource_group): # Check Output assert 'Hello World!' in result['output'] - +@pytest.mark.ManagedDiskGen2 class WindowsManagedDiskCreateRestoreGen2Test(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -562,7 +564,7 @@ def test_vmrepair_WinManagedCreateRestoreGen2(self, resource_group): source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] - +@pytest.mark.linuxKekRHEL class LinuxSinglepassKekEncryptedManagedDiskWithRHEL8DistroCreateRestoreTest(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -574,8 +576,27 @@ def test_vmrepair_LinuxSinglepassKekEncryptedManagedDiskCreateRestoreRHEL8(self, }) # Create test VM - self.cmd('vm create -g {rg} -n {vm} --image Canonical:UbuntuServer:18.04-LTS:latest --admin-username azureadmin --admin-password !Passw0rd2018 --size Standard_D2s_v3') - vms = self.cmd('vm list -g {rg} -o json').get_output_in_json() + resultVM = self.cmd('vm create -g {rg} -n {vm} --image Canonical:UbuntuServer:18.04-LTS:latest --admin-username azureadmin --admin-password !Passw0rd2018 --size Standard_D2s_v3').get_output_in_json() + vms = self.cmd('vm list -g {rg} -o json').get_output_in_json() + getVM = self.cmd('az vm show --name {vm} --resource-group {rg} --output json').get_output_in_json() + print("PRINTING VMS") + # print(vms['resourceGroup']) + # print(resultVM["name"]) + # print(json.dumps(vms, indent=4)) + # Define the regular expressions to match subscription ID, resource group, and VM name + vmId = getVM['id'] + sub_re = re.compile(r'/subscriptions/(.*?)/') + rg_re = re.compile(r'/resourceGroups/(.*?)/') + vm_re = re.compile(r'/virtualMachines/(.*)') + # Use the regular expressions to search the string + try: + subscription_id = sub_re.search(vmId).group(1) + resource_group = rg_re.search(vmId).group(1) + vm_name = vm_re.search(vmId).group(1) + print('successfully parsed id') + except AttributeError: + raise Exception("The VM Id did not match the expected format and could not be parsed!") + # Something wrong with vm create command if it fails here assert len(vms) == 1 @@ -583,10 +604,17 @@ def test_vmrepair_LinuxSinglepassKekEncryptedManagedDiskCreateRestoreRHEL8(self, self.cmd('keyvault create -n {kv} -g {rg} --enabled-for-disk-encryption True') # Check keyvault + subId = self.cmd('az account show --query id --output tsv') keyvault = self.cmd('keyvault list -g {rg} -o json').get_output_in_json() assert len(keyvault) == 1 # Create key + print('trying to create key') + roleAssignmentCmd = self.cmd('az role assignment create --assignee f90057dd-422f-49fc-a88f-91e965bc00c8 --role "Key Vault Contributor" --scope /subscriptions/{}/resourceGroups/{}/providers/Microsoft.Compute/virtualMachines/{}'.format(subscription_id, resource_group, vm_name)).get_output_in_json() + print(roleAssignmentCmd) + listRoles = self.cmd('az role assignment list --scope /subscriptions/{}/resourceGroups/{}/providers/Microsoft.Compute/virtualMachines/{} --query "[?principalId==\'f90057dd-422f-49fc-a88f-91e965bc00c8\']"'.format(subscription_id, resource_group, vm_name)).get_output_in_json() + print(listRoles) + print('listed out role assignments above') self.cmd('keyvault key create --vault-name {kv} --name {key} --protection software') # Check key @@ -617,7 +645,7 @@ def test_vmrepair_LinuxSinglepassKekEncryptedManagedDiskCreateRestoreRHEL8(self, source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] -@pytest.mark.linuxSles +@pytest.mark.linuxNoKekWithSles class LinuxSinglepassNoKekEncryptedManagedDiskWithSLES15CreateRestoreTest(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -646,7 +674,7 @@ def test_vmrepair_LinuxSinglepassNoKekEncryptedManagedDiskCreateRestoreTestSLES1 time.sleep(300) # Test create # SuseSles15SP3 - result = self.cmd('vm repair create -g {rg} -n {vm} --repair-username azureadmin --repair-password !Passw0rd2018 --distro ubuntu20 --unlock-encrypted-vm --yes -o json').get_output_in_json() + result = self.cmd('vm repair create -g {rg} -n {vm} --repair-username azureadmin --repair-password !Passw0rd2018 --distro sles15 --unlock-encrypted-vm --yes -o json').get_output_in_json() assert result['status'] == STATUS_SUCCESS, result['error_message'] # Check repair VM @@ -664,7 +692,7 @@ def test_vmrepair_LinuxSinglepassNoKekEncryptedManagedDiskCreateRestoreTestSLES1 source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] - +@pytest.mark.LinuxManagedPubIpOracle class LinuxManagedDiskCreateRestoreTestwithOracle8andpublicip(LiveScenarioTest): @ResourceGroupPreparer(location='westus2') @@ -698,7 +726,7 @@ def test_vmrepair_LinuxManagedCreateRestoreOracle8PublicIp(self, resource_group) source_vm = vms[0] assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name'] - +@pytest.mark.WindowsResetNic class ResetNICWindowsVM(LiveScenarioTest): @ResourceGroupPreparer(location='westus2')