Skip to content

Commit

Permalink
fixed several tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandido committed Aug 20, 2024
1 parent c2e4e44 commit f3197ed
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 36 deletions.
20 changes: 6 additions & 14 deletions src/vm-repair/azext_vm_repair/repair_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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]
Expand All @@ -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',
Expand All @@ -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]
Expand Down
72 changes: 50 additions & 22 deletions src/vm-repair/azext_vm_repair/tests/latest/test_repair_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -574,19 +576,45 @@ 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

# Create key vault
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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit f3197ed

Please sign in to comment.