Skip to content

Commit

Permalink
Fix incorrect parameter mapping in Get-AzApplyUpdate (Azure#26869)
Browse files Browse the repository at this point in the history
* Fix incorrect parameter mapping in Get-AzApplyUpdate

* Update ChangeLog.md

* Add Az.Compute module for tests

* Add Get-AzApplyUpdate test for VM

* Fix Az.Compute and Az.Network module references

---------

Co-authored-by: Sethuram S <[email protected]>
Co-authored-by: Yabo Hu <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2024
1 parent e6dcb6f commit 2f087e4
Show file tree
Hide file tree
Showing 9 changed files with 5,250 additions and 7 deletions.
110 changes: 110 additions & 0 deletions src/Maintenance/Maintenance.Test/ScenarioTests/Common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ function Get-RandomVirtualMachineName
return getAssetName
}

<#
.SYNOPSIS
Gets dedicated host name
#>
function Get-RandomDedicatedHostGroupName
{
return getAssetName
}

<#
.SYNOPSIS
Gets dedicated host name
Expand Down Expand Up @@ -87,3 +96,104 @@ function Clean-ResourceGroup($rgname)
Remove-AzResourceGroup -Name $rgname -Force
}
}

<#
.SYNOPSIS
Creates a new virtual machine
#>
function New-VirtualMachine(
[string] $virtualMachineName,
[string] $resourceGroupName,
[string] $location)
{
$vmLocalAdminUser = "LocalAdminUser"
$vmLocalAdminSecurePassword = ConvertTo-SecureString -String ([guid]::NewGuid()) -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($vmLocalAdminUser, $vmLocalAdminSecurePassword);
$patchMode = "AutomaticByPlatform"
$patchSettings = New-Object `
-TypeName Microsoft.Azure.Management.Compute.Models.WindowsVMGuestPatchAutomaticByPlatformSettings `
-Property @{BypassPlatformSafetyChecksOnUserSchedule = $true}
$sku = "Standard_D2s_v3"
$computerName = $virtualMachineName
$networkName = "Net$virtualMachineName"
$nicName = "Nic$virtualMachineName"
$subnetName = "Subnet$virtualMachineName"
$subnetAddressPrefix = "10.0.0.0/24"
$vnetAddressPrefix = "10.0.0.0/16"
$securityType = "Standard"
$imagePublisher = "MicrosoftVisualStudio"
$imageOffer = "Windows"
$imageSku = "Windows-10-N-x64"
$imageVersion = "latest"

$subnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix $subnetAddressPrefix
$vnet = New-AzVirtualNetwork `
-Name $networkName `
-ResourceGroupName $resourceGroupName `
-Location $location `
-AddressPrefix $vnetAddressPrefix `
-Subnet $subnet
$nic = New-AzNetworkInterface `
-Name $nicName `
-ResourceGroupName $resourceGroupName `
-Location $location `
-SubnetId $vnet.Subnets[0].Id

$vmConfig = New-AzVMConfig `
-VMName $virtualMachineName `
-VMSize $sku `
-SecurityType $securityType
Set-AzVMOperatingSystem `
-VM $vmConfig `
-Windows `
-ComputerName $computerName `
-Credential $credential `
-ProvisionVMAgent `
-EnableAutoUpdate `
-PatchMode $patchMode `
| Out-Null
Set-AzVMSourceImage `
-VM $vmConfig `
-PublisherName $imagePublisher `
-Offer $imageOffer `
-Skus $imageSku `
-Version $imageVersion `
| Out-Null
Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id | Out-Null

New-AzVM -ResourceGroupName $resourceGroupName -Location $location -VM $vmConfig | Out-Null
$virtualMachine = Get-AzVM -ResourceGroupName $resourceGroupName -Name $virtualMachineName
$virtualMachine.OSProfile.WindowsConfiguration.PatchSettings.AutomaticByPlatformSettings = $patchSettings
Update-AzVM -VM $virtualMachine -ResourceGroupName $ResourceGroupName | Out-Null

return $virtualMachine.Id
}

<#
.SYNOPSIS
Creates a new dedicated host
#>
function New-DedicatedHost(
[string] $dedicatedHostName,
[string] $dedicatedHostGroupName,
[string] $resourceGroupName,
[string] $location)
{
$sku = "Dsv3-Type3"
$platformFaultDomain = 1

$dedicatedHostGroup = New-AzHostGroup `
-Name $dedicatedHostGroupName `
-ResourceGroupName $resourceGroupName `
-Location $location `
-PlatformFaultDomain $platformFaultDomain

$dedicatedHost = New-AzHost `
-HostGroupName $dedicatedHostGroup.Name `
-Location $location `
-Name $dedicatedHostName `
-ResourceGroupName $resourceGroupName `
-Sku $sku

return $dedicatedHost.Id
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ protected MaintenanceTestRunner(ITestOutputHelper output)
.WithNewRmModules(helper => new[]
{
helper.RMProfileModule,
helper.GetRMModulePath("Az.Maintenance.psd1")
helper.GetRMModulePath("Az.Maintenance.psd1"),
helper.GetRMModulePath("Az.Compute.psd1"),
helper.GetRMModulePath("Az.Network.psd1")
})
.WithNewRecordMatcherArguments(
userAgentsToIgnore: new Dictionary<string, string>
Expand Down
14 changes: 14 additions & 0 deletions src/Maintenance/Maintenance.Test/ScenarioTests/MaintenanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,19 @@ public void TestAzApplyUpdateCancelConfiguration()
{
TestRunner.RunTestScript("Test-AzApplyUpdateCancelConfiguration");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetAzApplyUpdateWithParentResource()
{
TestRunner.RunTestScript("Test-GetAzApplyUpdateWithParentResource");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetAzApplyUpdateWithoutParentResource()
{
TestRunner.RunTestScript("Test-GetAzApplyUpdateWithoutParentResource");
}
}
}
172 changes: 170 additions & 2 deletions src/Maintenance/Maintenance.Test/ScenarioTests/MaintenanceTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ function Test-AzMaintenanceUpdate

<#
.SYNOPSIS
Test New-AzMaintenanceConfiguration, Get-AzApplyUpdate, Remove-AzMaintenanceConfiguration
Test New-AzMaintenanceConfiguration, New-AzApplyUpdate, Remove-AzMaintenanceConfiguration
#>
function Test-AzApplyUpdateCancelConfiguration
{
Expand Down Expand Up @@ -660,7 +660,175 @@ function Test-AzApplyUpdateCancelConfiguration
# Cleanup
Clean-ResourceGroup $resourceGroupName
}
}
}

<#
.SYNOPSIS
Test New-AzMaintenanceConfiguration, New-AzConfigurationAssignment, Get-AzApplyUpdate
#>
function Test-GetAzApplyUpdateWithParentResource
{
$actualStartTime = (Get-Date -AsUTC).AddMinutes(12)
$resourceGroupName = Get-RandomResourceGroupName
$maintenanceConfigurationName = Get-RandomMaintenanceConfigurationName
$dedicatedHostGroupName = Get-RandomDedicatedHostGroupName
$dedicatedHostName = Get-RandomDedicatedHostName
$location = "eastus"
$maintenanceScope = "Host"
$duration = "02:00"
$actualStartDateTime = $actualStartTime.ToString("yyyy-MM-dd HH:mm")
$startDateTime = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::GetVariable("startDateTime", $actualStartDateTime)
$expirationDateTime = "9999-12-31 00:00"
$recurEvery = "Day"
$timezone = "UTC"
$providerName = "Microsoft.Compute"
$resourceType = "hosts"
$resourceParentType = "hostGroups"
$applyUpdateName = "default"

try
{
New-AzResourceGroup -Name $resourceGroupName -Location $location

$dedicatedHostId = New-DedicatedHost $dedicatedHostName $dedicatedHostGroupName $resourceGroupName $location

### Host maintenance config
$maintenanceConfiguration = New-AzMaintenanceConfiguration `
-ResourceGroupName $resourceGroupName `
-Name $maintenanceConfigurationName `
-MaintenanceScope $maintenanceScope `
-Location $location `
-Timezone $timezone `
-StartDateTime $startDateTime `
-ExpirationDateTime $expirationDateTime `
-Duration $duration `
-RecurEvery $recurEvery

Assert-AreEqual $maintenanceConfiguration.Name $maintenanceConfigurationName

### Wait few minutes so that the resource is available for configuration assignment
Start-TestSleep -Seconds (15 * 60)

### Create configuration assignment
$configurationAssignment = New-AzConfigurationAssignment `
-ResourceGroupName $resourceGroupName `
-Location $location `
-ResourceName $dedicatedHostName `
-ResourceType $resourceType `
-ResourceParentName $dedicatedHostGroupName `
-ResourceParentType $resourceParentType `
-ProviderName $providerName `
-ConfigurationAssignmentName $maintenanceConfigurationName `
-MaintenanceConfigurationId $maintenanceConfiguration.Id

Assert-AreEqual $configurationAssignment.Name $maintenanceConfigurationName

### Make Get-AzApplyUpdate call
$applyUpdateResponse = Get-AzApplyUpdate `
-ResourceGroupName $resourceGroupName `
-ProviderName $providerName `
-ResourceType $resourceType `
-ResourceName $dedicatedHostName `
-ResourceParentType $resourceParentType `
-ResourceParentName $dedicatedHostGroupName `
-ApplyUpdateName $applyUpdateName

Assert-AreEqual $applyUpdateResponse.ResourceId $dedicatedHostId
}
finally
{
Clean-ResourceGroup $resourceGroupName
}
}

<#
.SYNOPSIS
Test New-AzMaintenanceConfiguration, New-AzConfigurationAssignment, Get-AzApplyUpdate
#>
function Test-GetAzApplyUpdateWithoutParentResource
{
$actualStartTime = (Get-Date -AsUTC).AddMinutes(12)
$resourceGroupName = Get-RandomResourceGroupName
$maintenanceConfigurationName = Get-RandomMaintenanceConfigurationName
$virtualMachineName = Get-RandomVirtualMachineName
$location = "westus"
$maintenanceScope = "InGuestPatch"
$duration = "02:00"
$actualStartDateTime = $actualStartTime.ToString("yyyy-MM-dd HH:mm")
$startDateTime = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::GetVariable("startDateTime", $actualStartDateTime)
$expirationDateTime = "9999-12-31 00:00"
$recurEvery = "Day"
$timezone = "UTC"
$extensionProperty = @{"inGuestPatchMode"="User"}
$rebootOption = "IfRequired";
$windowsParameterClassificationToInclude = "FeaturePack","ServicePack";
$windowsParameterKbNumberToInclude = "KB123456","KB123466";
$windowsParameterKbNumberToExclude = "KB123456","KB123466";
$linuxParameterClassificationToInclude = "Other";
$linuxParameterPackageNameMaskToInclude = "apt","httpd";
$linuxParameterPackageNameMaskToExclude = "ppt","userpk";
$providerName = "Microsoft.Compute"
$resourceType = "virtualmachines"
$applyUpdateName = "default"

try
{
New-AzResourceGroup -Name $resourceGroupName -Location $location

$virtualMachineId = New-VirtualMachine $virtualMachineName $resourceGroupName $location

### InGuestPatch maintenance config
$maintenanceConfiguration = New-AzMaintenanceConfiguration `
-ResourceGroupName $resourceGroupName `
-Name $maintenanceConfigurationName `
-MaintenanceScope $maintenanceScope `
-Location $location `
-Timezone $timezone `
-StartDateTime $startDateTime `
-ExpirationDateTime $expirationDateTime `
-Duration $duration `
-RecurEvery $recurEvery `
-ExtensionProperty $extensionProperty `
-InstallPatchRebootSetting $rebootOption `
-WindowParameterClassificationToInclude $windowsParameterClassificationToInclude `
-WindowParameterKbNumberToInclude $windowsParameterKbNumberToInclude `
-WindowParameterKbNumberToExclude $windowsParameterKbNumberToExclude `
-LinuxParameterPackageNameMaskToInclude $linuxParameterPackageNameMaskToInclude `
-LinuxParameterClassificationToInclude $linuxParameterClassificationToInclude `
-LinuxParameterPackageNameMaskToExclude $linuxParameterPackageNameMaskToExclude

Assert-AreEqual $maintenanceConfiguration.Name $maintenanceConfigurationName

### Wait few minutes so that the resource is available for configuration assignment
Start-TestSleep -Seconds (15 * 60)

### Create configuration assignment
$configurationAssignment = New-AzConfigurationAssignment `
-ResourceGroupName $resourceGroupName `
-Location $location `
-ResourceName $virtualMachineName `
-ResourceType $resourceType `
-ProviderName $providerName `
-ConfigurationAssignmentName $maintenanceConfigurationName `
-MaintenanceConfigurationId $maintenanceConfiguration.Id

Assert-AreEqual $configurationAssignment.Name $maintenanceConfigurationName

### Make Get-AzApplyUpdate call
$applyUpdateResponse = Get-AzApplyUpdate `
-ResourceGroupName $resourceGroupName `
-ProviderName $providerName `
-ResourceType $resourceType `
-ResourceName $virtualMachineName `
-ApplyUpdateName $applyUpdateName

Assert-AreEqual $applyUpdateResponse.ResourceId $virtualMachineId
}
finally
{
Clean-ResourceGroup $resourceGroupName
}
}

<#
.SYNOPSIS
Expand Down
Loading

0 comments on commit 2f087e4

Please sign in to comment.