@@ -7975,3 +7975,144 @@ function Test-VirtualMachineAddProxyAgentExtension
79757975 Clean - ResourceGroup $resourceGroupName ;
79767976 }
79777977}
7978+
7979+ <#
7980+ . SYNOPSIS
7981+ Test-VirtualMachineGalleryApplicationFlags tests GalleryApplication Creation and Addition of TreatFailureAsDeploymentFailure and EnableAutomaticUpgrade flags
7982+ #>
7983+ function Test-VirtualMachineGalleryApplicationFlags
7984+ {
7985+ if ((Get-ComputeTestMode ) -eq ' Playback' ) {
7986+ Write-Verbose " Skipping Test-VirtualMachineGalleryApplicationFlags in Playback (uses storage & gallery live operations)" ;
7987+ Assert-True { $true }
7988+ return
7989+ }
7990+
7991+ # Setup
7992+ $resourceGroupName = Get-ComputeTestResourceName ;
7993+ $adminUsername = Get-ComputeTestResourceName ;
7994+ $adminPassword = Get-PasswordForVM | ConvertTo-SecureString - AsPlainText - Force;
7995+ $cred = New-Object System.Management.Automation.PSCredential ($adminUsername , $adminPassword );
7996+ $vmName = ' VM1' ;
7997+ $imageName = " Canonical:0001-com-ubuntu-server-jammy:22_04-lts:latest" ;
7998+ $domainNameLabel = " d1" + $resourceGroupName ;
7999+ $loc = ' eastus2' ;
8000+
8001+ try {
8002+ # Create VM
8003+ New-AzVM - ResourceGroupName $resourceGroupName - Name $vmName - Credential $cred - Image $imageName - DomainNameLabel $domainNameLabel - Location $loc - EnableProxyAgent - AddProxyAgentExtension | Out-Null
8004+ $VM = Get-AzVM - ResourceGroupName $resourceGroupName - Name $vmName
8005+
8006+ # Names
8007+ $galleryName = " gal" + $resourceGroupName
8008+ $galleryAppName1 = " app" + $resourceGroupName
8009+ $galleryAppName2 = " app2" + $resourceGroupName
8010+ $galleryAppVersion1 = " 1.0.0"
8011+ $galleryAppVersion2 = " 1.0.0" # version string can be same; application name must differ
8012+
8013+ # Storage account + package blob (page blob with valid minimal ZIP)
8014+ $storageName = (" pkg" + ($resourceGroupName.ToLower ()))[0 .. ([Math ]::Min(23 , (" pkg" + ($resourceGroupName.ToLower ())).Length- 1 ))] -join ' '
8015+ $containerName = " packages"
8016+ $blobName = " apppkg.zip"
8017+
8018+ New-AzStorageAccount - ResourceGroupName $resourceGroupName - Name $storageName - Location $loc - Type Standard_LRS | Out-Null
8019+ $acctKeys = Get-AzStorageAccountKey - ResourceGroupName $resourceGroupName - Name $storageName
8020+ $ctx = New-AzStorageContext - StorageAccountName $storageName - StorageAccountKey $acctKeys [0 ].Value
8021+ New-AzStorageContainer - Name $containerName - Context $ctx - Permission Blob | Out-Null
8022+
8023+ $localPackage = Join-Path $TestOutputRoot $blobName
8024+ if (Test-Path $localPackage ) { Remove-Item $localPackage - Force }
8025+
8026+ # Create a minimal valid ZIP (empty directory) then pad to 512-byte multiple for page blob
8027+ $tmpDir = Join-Path $TestOutputRoot " pkgtmp"
8028+ if (Test-Path $tmpDir ) { Remove-Item $tmpDir - Recurse - Force }
8029+ New-Item - ItemType Directory - Path $tmpDir | Out-Null
8030+ Add-Type - AssemblyName System.IO.Compression.FileSystem
8031+ [System.IO.Compression.ZipFile ]::CreateFromDirectory($tmpDir , $localPackage )
8032+ Remove-Item $tmpDir - Force
8033+ $bytes = [IO.File ]::ReadAllBytes($localPackage )
8034+ $pad = 512 - ($bytes.Length % 512 )
8035+ if ($pad -ne 512 ) {
8036+ $bytes += (0 .. ($pad - 1 ) | ForEach-Object { 0 })
8037+ [IO.File ]::WriteAllBytes($localPackage , $bytes )
8038+ }
8039+
8040+ Set-AzStorageBlobContent - File $localPackage - Container $containerName - Blob $blobName - Context $ctx - BlobType Page | Out-Null
8041+ $packageUri = (Get-AzStorageBlob - Container $containerName - Blob $blobName - Context $ctx ).ICloudBlob.Uri.AbsoluteUri
8042+
8043+ # Gallery + application definitions
8044+ New-AzGallery - ResourceGroupName $resourceGroupName - Name $galleryName - Location $loc | Out-Null
8045+ New-AzGalleryApplication - ResourceGroupName $resourceGroupName - GalleryName $galleryName - Name $galleryAppName1 - Location $loc - SupportedOSType Linux - Description " Test gallery app flags - app1" | Out-Null
8046+ New-AzGalleryApplication - ResourceGroupName $resourceGroupName - GalleryName $galleryName - Name $galleryAppName2 - Location $loc - SupportedOSType Linux - Description " Test gallery app flags - app2" | Out-Null
8047+
8048+ $installCmd = " echo install"
8049+ $removeCmd = " echo remove"
8050+
8051+ function Wait-GalleryAppVersionSucceeded {
8052+ param (
8053+ [string ] $RG ,
8054+ [string ] $Gal ,
8055+ [string ] $App ,
8056+ [string ] $Ver ,
8057+ [int ] $TimeoutSeconds = 300
8058+ )
8059+ $start = Get-Date
8060+ do {
8061+ try {
8062+ $v = Get-AzGalleryApplicationVersion - ResourceGroupName $RG - GalleryName $Gal - GalleryApplicationName $App - Name $Ver - ErrorAction Stop
8063+ if ($v.ProvisioningState -eq ' Succeeded' ) { return $v }
8064+ }
8065+ catch {
8066+ # transient – ignore during creation
8067+ }
8068+ Start-Sleep - Seconds 5
8069+ } while ((Get-Date ) - $start -lt [TimeSpan ]::FromSeconds($TimeoutSeconds ))
8070+ throw " Gallery Application Version $App /$Ver did not reach Succeeded within timeout."
8071+ }
8072+
8073+ # Version for app1
8074+ New-AzGalleryApplicationVersion - ResourceGroupName $resourceGroupName - GalleryName $galleryName - GalleryApplicationName $galleryAppName1 - Name $galleryAppVersion1 - Location $loc - PackageFileLink $packageUri - Install $installCmd - Remove $removeCmd - TargetRegion @ (@ { Name = $loc ; ReplicaCount = 1 }) | Out-Null
8075+ $galVerApp1 = Wait-GalleryAppVersionSucceeded - RG $resourceGroupName - Gal $galleryName - App $galleryAppName1 - Ver $galleryAppVersion1
8076+ $pkgId1 = $galVerApp1.Id
8077+
8078+ # Version for app2
8079+ New-AzGalleryApplicationVersion - ResourceGroupName $resourceGroupName - GalleryName $galleryName - GalleryApplicationName $galleryAppName2 - Name $galleryAppVersion2 - Location $loc - PackageFileLink $packageUri - Install $installCmd - Remove $removeCmd - TargetRegion @ (@ { Name = $loc ; ReplicaCount = 1 }) | Out-Null
8080+ $galVerApp2 = Wait-GalleryAppVersionSucceeded - RG $resourceGroupName - Gal $galleryName - App $galleryAppName2 - Ver $galleryAppVersion2
8081+ $pkgId2 = $galVerApp2.Id
8082+
8083+ # Case 0: Add first gallery application with both flags false
8084+ $vmGalleryApplication0 = New-AzVmGalleryApplication - PackageReferenceId $pkgId1 - EnableAutomaticUpgrade:$false - TreatFailureAsDeploymentFailure:$false
8085+ $VM = Add-AzVmGalleryApplication - VM $VM - GalleryApplication $vmGalleryApplication0
8086+ $VM | Update-AzVM
8087+ $vmAfter0 = Get-AzVM - ResourceGroupName $resourceGroupName - Name $vmName
8088+ $gal0 = $vmAfter0.ApplicationProfile.GalleryApplications [0 ]
8089+ Assert-AreEqual $pkgId1 $gal0.PackageReferenceId
8090+ Assert-AreEqual $false $gal0.EnableAutomaticUpgrade
8091+ Assert-AreEqual $false $gal0.TreatFailureAsDeploymentFailure
8092+
8093+ # Case 1: Update existing application flags to true
8094+ $VM = Get-AzVM - ResourceGroupName $resourceGroupName - Name $vmName
8095+ $VM.ApplicationProfile.GalleryApplications [0 ].EnableAutomaticUpgrade = $true
8096+ $VM.ApplicationProfile.GalleryApplications [0 ].TreatFailureAsDeploymentFailure = $true
8097+ Update-AzVM - ResourceGroupName $resourceGroupName - VM $VM
8098+ $vmAfter1 = Get-AzVM - ResourceGroupName $resourceGroupName - Name $vmName
8099+ $gal1 = $vmAfter1.ApplicationProfile.GalleryApplications [0 ]
8100+ Assert-AreEqual $pkgId1 $gal1.PackageReferenceId
8101+ Assert-AreEqual $true $gal1.EnableAutomaticUpgrade
8102+ Assert-AreEqual $true $gal1.TreatFailureAsDeploymentFailure
8103+
8104+ # Case 2: Add second (distinct) application with preset true flags
8105+ $vmGalleryApplication2 = New-AzVmGalleryApplication - PackageReferenceId $pkgId2 - EnableAutomaticUpgrade:$true - TreatFailureAsDeploymentFailure:$true
8106+ $VM = Get-AzVM - ResourceGroupName $resourceGroupName - Name $vmName
8107+ $VM = Add-AzVmGalleryApplication - VM $VM - GalleryApplication $vmGalleryApplication2
8108+ $VM | Update-AzVM
8109+ $vmAfter2 = Get-AzVM - ResourceGroupName $resourceGroupName - Name $vmName
8110+ $gal2 = $vmAfter2.ApplicationProfile.GalleryApplications | Where-Object { $_.PackageReferenceId -eq $pkgId2 }
8111+ Assert-AreEqual $pkgId2 $gal2.PackageReferenceId
8112+ Assert-AreEqual $true $gal2.EnableAutomaticUpgrade
8113+ Assert-AreEqual $true $gal2.TreatFailureAsDeploymentFailure
8114+ }
8115+ finally {
8116+ Clean - ResourceGroup $resourceGroupName
8117+ }
8118+ }
0 commit comments