Skip to content

Commit

Permalink
Set-AzVMOperatingSystem fix null reference when credential is not pro…
Browse files Browse the repository at this point in the history
…vided (#25624)

* dev and test and changelog and help doc

* Update VirtualMachineTests.ps1, remove passwords

* Update VirtualMachineTests.ps1

* test and simplify code

* Update VirtualMachineTests.ps1

* rerecord old test for password removal

* Update TestVMSetAzOSCredentialNullRef.json, remove auto password
  • Loading branch information
Sandido authored Aug 8, 2024
1 parent fa80569 commit 6b5a52f
Show file tree
Hide file tree
Showing 7 changed files with 3,731 additions and 178 deletions.
7 changes: 7 additions & 0 deletions src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,5 +661,12 @@ public void TestAddVMDataDisk()
{
TestRunner.RunTestScript("Test-AddVMDataDisk");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestVMSetAzOSCredentialNullRef()
{
TestRunner.RunTestScript("Test-VMSetAzOSCredentialNullRef");
}
}
}
75 changes: 71 additions & 4 deletions src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5295,8 +5295,8 @@ function Test-VirtualMachineEnableAutoUpdate
$computerName = "v" + $rgname;

# VM Credential
$user = "usertest";
$password = "*****";
$user = Get-ComputeTestResourceName;
$password = Get-PasswordForVM;
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);

Expand Down Expand Up @@ -6255,7 +6255,7 @@ function Test-ManualConfidentialVMSetAzVmOsDiskDesIdDiskWithVMGuest
$secureEncryptGuestState = 'DiskWithVMGuestState';
$vmSecurityType = "ConfidentialVM";
$user = "admin01";
$password;
$password = $PLACEHOLDER;
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force;
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
Expand Down Expand Up @@ -6583,7 +6583,8 @@ function Test-ConfVMSetAzDiskEncryptionSetConfig
$desName= "des" + $rgname;
# Creating a VM using simple parameterset
$securePassword | ConvertTo-SecureString -AsPlainText -Force;
$password = "*****";
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force;
$user = "admin01";
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
Expand Down Expand Up @@ -7542,3 +7543,69 @@ function Test-AddVMDataDisk
# Validate
Assert-AreEqual $vmConfig.StorageProfile.DataDisks[0].SourceResource.id "testSourceResourceId"
}

<#
.SYNOPSIS
Test Set-AzVMOperatingSystem does not have a null ref exception.
The ComputerName is an expected required parameter.
#>
function Test-VMSetAzOSCredentialNullRef
{
# Setup
$rgname = Get-ComputeTestResourceName;
$loc = "westus2";#Get-ComputeVMLocation;

try
{
New-AzResourceGroup -Name $rgname -Location $loc -Force;
# SimpleParameterSet, no config, scenario.
# create credential
$password = Get-PasswordForVM;
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force;
$user = Get-ComputeTestResourceName;
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);

# DefaultParameterSet with VMConfig scenario
$domainNameLabel = "d2" + $rgname;
$vmsize = 'Standard_D4s_v3';
$vmname = 'v' + $rgname;
$vnetname = "vn" + $rgname;
$vnetAddress = "10.0.0.0/16";
$subnetname = "slb" + $rgname;
$subnetAddress = "10.0.2.0/24";
$OSDiskName = $vmname + "d";
$NICName = $vmname+ "n";
$NSGName = $vmname + "nsg";

# Creating a VM using Default parameterset
$frontendSubnet = New-AzVirtualNetworkSubnetConfig -Name $subnetname -AddressPrefix $subnetAddress;

$vnet = New-AzVirtualNetwork -Name $vnetname -ResourceGroupName $rgname -Location $loc -AddressPrefix $vnetAddress -Subnet $frontendSubnet;

$nsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name RDP -Protocol Tcp -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow;
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $rgname -Location $loc -Name $NSGName -SecurityRules $nsgRuleRDP;
$nic = New-AzNetworkInterface -Name $NICName -ResourceGroupName $rgname -Location $loc -SubnetId $vnet.Subnets[0].Id -NetworkSecurityGroupId $nsg.Id -EnableAcceleratedNetworking;

# VM
$vmConfig = New-AzVMConfig -VMName $vmname -VMSize $vmsize;
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmname;
$vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id;

# Verify a VM needs the ComputerName.
Assert-ThrowsContains {New-AzVM -ResourceGroupName $rgname -Location $loc -VM $vmConfig; } "Required parameter"

# Verify the VM is created successfully.
$vmConfig = New-AzVMConfig -VMName $vmname -VMSize $vmsize;
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmname -Credential $cred;
$vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id;
New-AzVM -ResourceGroupName $rgname -Location $loc -VM $vmconfig;

$vm = Get-AzVM -ResourceGroupName $rgname -Name $vmname;
Assert-NotNull $vm;
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname;
}
}
Loading

0 comments on commit 6b5a52f

Please sign in to comment.