@@ -171,150 +171,121 @@ function Test-GetRSVaultSettingsFile
171171
172172<#
173173. SYNOPSIS
174- Recovery Services Soft Deleted Vault Tests - Delete, Get, and Undo deletion
174+ Recovery Services Soft Deleted Vault Tests
175175#>
176176function Test-RecoveryServicesSoftDeletedVaultOperations
177177{
178- $location = " centraluseuap"
179- $resourceGroupName = Create- ResourceGroup $location
180- $name = " PSTestRSV" + @ (Get-RandomSuffix )
178+ $location = " westus"
179+ $resourceGroupName = " hiagawus-rg"
180+ $softDeletedVaultNamePattern = " hiagawus-rg_*" # Pattern to match the vault name
181+ $expectedBackupItems = @ (" hiagaCRRRVM23" , " hiagawusCRR-vm" )
182+ $subscriptionId = " f879818f-5b29-4a43-8961-34169783144f" # use this subscription for this test
183+ $originalRSVname = " hiagawusCRR-vault"
181184
182185 try
183186 {
184- # 1. Create a new Recovery Services Vault
185- $vault = New-AzRecoveryServicesVault - Name $name - ResourceGroupName $resourceGroupName - Location $location
186-
187- Assert-NotNull ($vault.Name )
188- Assert-NotNull ($vault.ID )
189- Assert-NotNull ($vault.Type )
190- Assert-AreEqual $name $vault.Name
191- Assert-AreEqual $location $vault.Location
192-
193- # 2. Delete the vault (this will soft delete it)
194- Remove-AzRecoveryServicesVault - Vault $vault
195-
196- # Verify vault is no longer in active vaults list
197- $activeVaults = Get-AzRecoveryServicesVault - Name $name - ResourceGroupName $resourceGroupName
198- Assert-True { $activeVaults.Count -eq 0 }
199-
200- # 3. Get soft deleted vault
187+ # 1. Get-AzRecoveryServicesSoftDeletedVault - Get all soft deleted vaults in location
201188 $softDeletedVaults = Get-AzRecoveryServicesSoftDeletedVault - Location $location
189+
202190 Assert-NotNull ($softDeletedVaults )
203191 Assert-True { $softDeletedVaults.Count -gt 0 }
204-
205- # Find our specific soft deleted vault
192+
193+ # Find the target soft deleted vault by resource group and name pattern
206194 $targetSoftDeletedVault = $softDeletedVaults | Where-Object {
207- $_.Name -eq $name -and $_.ResourceGroupName -eq $resourceGroupName
195+ $_.ResourceGroupName -eq $resourceGroupName -and $_.Name -like $softDeletedVaultNamePattern
208196 }
209-
210197 Assert-NotNull ($targetSoftDeletedVault )
211- Assert-AreEqual $name $targetSoftDeletedVault.Name
212- Assert-AreEqual $location $targetSoftDeletedVault.Location
213198 Assert-AreEqual $resourceGroupName $targetSoftDeletedVault.ResourceGroupName
214- Assert-NotNull ($targetSoftDeletedVault.Properties.VaultId )
215- Assert-NotNull ($targetSoftDeletedVault.Properties.VaultDeletionTime )
216- Assert-NotNull ($targetSoftDeletedVault.Properties.PurgeAt )
217-
218- # 4. Get soft deleted vault by name
219- $specificSoftDeletedVault = Get-AzRecoveryServicesSoftDeletedVault - Location $location - Name $name
220- Assert-NotNull ($specificSoftDeletedVault )
221- Assert-AreEqual $name $specificSoftDeletedVault.Name
222- Assert-AreEqual $location $specificSoftDeletedVault.Location
223-
224- # 5. Get soft deleted vault by resource group filter
225- $filteredSoftDeletedVaults = Get-AzRecoveryServicesSoftDeletedVault - Location $location - ResourceGroupName $resourceGroupName
226- Assert-NotNull ($filteredSoftDeletedVaults )
227- $filteredTargetVault = $filteredSoftDeletedVaults | Where-Object { $_.Name -eq $name }
228- Assert-NotNull ($filteredTargetVault )
229-
230- # 6. Undo vault deletion
231- $undoResult = Undo-AzRecoveryServicesVaultDeletion - ResourceGroupName $resourceGroupName - Name $name - Location $location
232-
233- Assert-NotNull ($undoResult )
234- Assert-AreEqual $name $undoResult.Name
235- Assert-AreEqual $location $undoResult.Location
199+ Assert-AreEqual $location $targetSoftDeletedVault.Location
200+ Assert-NotNull ($targetSoftDeletedVault.Properties )
236201
237- # 7. Verify vault is restored and back in active vaults list
238- # Note: The restoration might take some time, so we may need to wait or poll
239- Start-Sleep - Seconds 30 # Wait for restoration to complete
202+ # Get the actual vault name for subsequent operations
203+ $vaultName = $targetSoftDeletedVault .Name
204+ Write-Host " Found soft-deleted vault: $vaultName "
240205
241- $restoredVaults = Get-AzRecoveryServicesVault - Name $name - ResourceGroupName $resourceGroupName
242- Assert-NotNull ($restoredVaults )
243- Assert-True { $restoredVaults.Count -gt 0 }
206+ # 2. Get-AzRecoveryServicesSoftDeletedVault - Get specific soft deleted vault by name
207+ $specificSoftDeletedVault = Get-AzRecoveryServicesSoftDeletedVault - Location $location - Name $vaultName - ResourceGroupName $resourceGroupName
244208
245- $restoredVault = $restoredVaults [0 ]
246- Assert-AreEqual $name $restoredVault.Name
247- Assert-AreEqual $location $restoredVault.Location
248- Assert-AreEqual $resourceGroupName $restoredVault.ResourceGroupName
249-
250- # 8. Verify soft deleted vault is no longer in soft deleted list
251- $remainingSoftDeletedVaults = Get-AzRecoveryServicesSoftDeletedVault - Location $location - Name $name
252- # After successful restoration, the vault should not be in soft deleted state
253- # This assertion might need adjustment based on actual API behavior
254- # Assert-Null($remainingSoftDeletedVaults)
255-
256- # Clean up the restored vault
257- Remove-AzRecoveryServicesVault - Vault $restoredVault
258- }
259- finally
260- {
261- Cleanup- ResourceGroup $resourceGroupName
262- }
263- }
209+ Assert-NotNull ($specificSoftDeletedVault )
210+ Assert-AreEqual $vaultName $specificSoftDeletedVault.Name
211+ Assert-AreEqual $resourceGroupName $specificSoftDeletedVault.ResourceGroupName
264212
265- <#
266- . SYNOPSIS
267- Recovery Services Soft Deleted Vault Tests - Filter by existing vault pattern
268- #>
269- function Test-RecoveryServicesSoftDeletedVaultFiltering
270- {
271- $location = " centraluseuap"
272-
273- try
274- {
275- # 1. Get all soft deleted vaults in the location
276- $allSoftDeletedVaults = Get-AzRecoveryServicesSoftDeletedVault - Location $location
213+ # 3. Get-AzRecoveryServicesSoftDeletedVaultBackupItem - Using VaultId
214+ $backupItemsUsingVaultId = Get-AzRecoveryServicesSoftDeletedVaultBackupItem - VaultId $targetSoftDeletedVault.ID
277215
278- if ($allSoftDeletedVaults -ne $null -and $allSoftDeletedVaults.Count -gt 0 )
216+ Assert-NotNull ($backupItemsUsingVaultId )
217+ Assert-True { $backupItemsUsingVaultId.Count -ge 2 }
218+
219+ # Verify expected backup items exist
220+ $foundBackupItems = @ ()
221+ foreach ($item in $backupItemsUsingVaultId )
279222 {
280- Assert-True { $allSoftDeletedVaults.Count -gt 0 }
281-
282- foreach ($vault in $allSoftDeletedVaults )
223+ foreach ($expectedItem in $expectedBackupItems )
283224 {
284- Assert-NotNull ($vault.Name )
285- Assert-NotNull ($vault.Location )
286- Assert-AreEqual $location $vault.Location
287- Assert-NotNull ($vault.Properties )
288- Assert-NotNull ($vault.Properties.VaultId )
289- }
290-
291- # 2. Test filtering by vault name pattern (similar to your akkanaseTest23 example)
292- $filteredByPattern = $allSoftDeletedVaults | Where-Object { $_.Properties.VaultId -match " PSTestRSV" }
293-
294- # 3. Test resource group filtering
295- if ($allSoftDeletedVaults.Count -gt 0 )
296- {
297- $firstVault = $allSoftDeletedVaults [0 ]
298- if ($firstVault.ResourceGroupName -ne $null )
225+ if ($item.Name -like " *$expectedItem *" -or $item.SourceResourceId -like " *$expectedItem *" )
299226 {
300- $filteredByRG = Get-AzRecoveryServicesSoftDeletedVault - Location $location - ResourceGroupName $firstVault.ResourceGroupName
301- Assert-NotNull ($filteredByRG )
302-
303- foreach ($vault in $filteredByRG )
304- {
305- Assert-AreEqual $firstVault.ResourceGroupName $vault.ResourceGroupName
306- }
227+ $foundBackupItems += $expectedItem
228+ break
307229 }
308230 }
309231 }
310- else
311- {
312- Write-Warning " No soft deleted vaults found in location $location for filtering tests"
232+ Assert-True { $foundBackupItems.Count -eq $expectedBackupItems.Count }
233+
234+ # 4. Get-AzRecoveryServicesSoftDeletedVaultBackupItem - Using VaultName and ResourceGroupName
235+ $backupItemsUsingVaultName = Get-AzRecoveryServicesSoftDeletedVaultBackupItem - VaultName $vaultName - ResourceGroupName $resourceGroupName
236+
237+ Assert-NotNull ($backupItemsUsingVaultName )
238+ Assert-True { $backupItemsUsingVaultName.Count -ge 2 }
239+ Assert-AreEqual $backupItemsUsingVaultId.Count $backupItemsUsingVaultName.Count
240+
241+ # 5. Undo-AzRecoveryServicesVaultDeletion - Restore the soft deleted vault
242+ $undeleteResult = Undo-AzRecoveryServicesVaultDeletion - ResourceGroupName $resourceGroupName - Name $vaultName - Location $location - Force
243+
244+ Assert-NotNull ($undeleteResult )
245+
246+ # Wait a bit for the restoration to complete
247+ Start-Sleep - Seconds 30
248+
249+ # 6. Get-AzRecoveryServicesVault - Verify vault is now active again
250+ $activeVault = Get-AzRecoveryServicesVault - Name $originalRSVname - ResourceGroupName $resourceGroupName
251+
252+ Assert-NotNull ($activeVault )
253+ Assert-AreEqual $originalRSVname $activeVault.Name
254+ Assert-AreEqual $resourceGroupName $activeVault.ResourceGroupName
255+ Assert-AreEqual $location $activeVault.Location
256+ Assert-NotNull ($activeVault.ID )
257+ Assert-NotNull ($activeVault.Type )
258+
259+ # 7. Verify the vault is no longer in soft deleted state
260+ $softDeletedVaultsAfterRestore = Get-AzRecoveryServicesSoftDeletedVault - Location $location
261+ $softDeletedVaultAfterRestore = $softDeletedVaultsAfterRestore | Where-Object { $_.Properties.VaultId -match $originalRSVname }
262+ Assert-Null ($softDeletedVaultAfterRestore )
263+
264+ # 8. Remove-AzRecoveryServicesVault - Delete the vault again to maintain original test state
265+ Remove-AzRecoveryServicesVault - Vault $activeVault
266+
267+ # Wait a bit for the deletion to complete
268+ Start-Sleep - Seconds 30
269+
270+ # 9. Verify vault is back to soft deleted state (note: name may have changed due to new GUID)
271+ $softDeletedVaultsFinal = Get-AzRecoveryServicesSoftDeletedVault - Location $location
272+ $finalSoftDeletedVault = $softDeletedVaultsFinal | Where-Object {
273+ $_.ResourceGroupName -eq $resourceGroupName -and $_.Name -like $softDeletedVaultNamePattern
313274 }
275+ Assert-NotNull ($finalSoftDeletedVault )
276+ Write-Host " Vault back in soft-deleted state with name: $ ( $finalSoftDeletedVault.Name ) "
277+
278+ # 10. Verify active vault no longer exists (check by resource group and original pattern)
279+ $activeVaultsCheck = Get-AzRecoveryServicesVault - Name $originalRSVname - ResourceGroupName $resourceGroupName - ErrorAction SilentlyContinue
280+ Assert-Null ($activeVaultCheck )
314281 }
315- catch
282+ finally
316283 {
317- Write-Warning " Soft deleted vault filtering test encountered an issue: $ ( $_.Exception.Message ) "
318- # Don't fail the test if there are no existing soft deleted vaults
284+ # If test fails, attempt to clean up by ensuring vault is in soft deleted state
285+ $activeVaultsCleanup = Get-AzRecoveryServicesVault - Name $originalRSVname - ResourceGroupName $resourceGroupName - ErrorAction SilentlyContinue
286+ if ($activeVaultCleanup -ne $null )
287+ {
288+ Remove-AzRecoveryServicesVault - Vault $activeVaultCleanup
289+ }
319290 }
320291}
0 commit comments