Skip to content

Commit 355017b

Browse files
committed
Add Retry-AzCommand function in SmokeTest/RmCoreSmokeTests.ps1
1 parent 3ff6cc9 commit 355017b

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

tools/Test/SmokeTest/RmCoreSmokeTests.ps1

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,40 @@ for($i=0; $i -lt 9; $i++)
3131
{
3232
$randomValue += $strarray[(Get-Random -Maximum $strarray.Length)]
3333
}
34+
35+
# Retry azure powershell command.
36+
function Retry-AzCommand {
37+
[CmdletBinding()]
38+
param (
39+
[string]
40+
$Command,
41+
42+
[int]
43+
$RetryCount,
44+
45+
# Seconds between retries
46+
[int]
47+
$Sleep
48+
)
49+
$loopLimit = 0
50+
do {
51+
try {
52+
&([scriptblock]::Create($Command))
53+
break
54+
}
55+
catch {
56+
$commandName = ($Command -split ' ')[0]
57+
if (++$loopLimit -gt $RetryCount)
58+
{
59+
throw "Failed to invoke $commandName. $_"
60+
} else {
61+
Write-Warning "Retry $commandName after $Sleep seconds"
62+
Start-Sleep -Seconds $Sleep
63+
}
64+
}
65+
} while ($true)
66+
}
67+
3468
# The name of resource group is 1~90 charactors complying with ^[-\w\._\(\)]+$
3569
$resourceGroupName = "azpssmokerg$randomValue"
3670
# The name of storage account should be 3~24 lowercase letters and numbers.
@@ -39,27 +73,12 @@ $storageAccountName = "azpssmokesa$randomValue"
3973
$resourceSetUpCommands=@(
4074
@{Name = "Az.Resources"; Command = {New-AzResourceGroup -Name $resourceGroupName -Location westus -ErrorAction Stop}}
4175
)
76+
4277
$resourceCleanUpCommands = @(
43-
@{Name = "Az.Storage [Cleanup]"; Command = {
44-
$loopLimit = 0
45-
do {
46-
try {
47-
Remove-AzStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroupName -Force -ErrorAction Stop
48-
break
49-
}
50-
catch {
51-
if (++$loopLimit -gt 30)
52-
{
53-
throw "Failed to invoke Az.Storage [Cleanup]. $_"
54-
} else {
55-
Write-Warning "Retry Az.Storage [Cleanup] after 30 seconds"
56-
Start-Sleep -Seconds 30
57-
}
58-
}
59-
} while ($true)
60-
}},
78+
@{Name = "Az.Storage [Cleanup]"; Command = {Retry-AzCommand -Command "Remove-AzStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroupName -Force -ErrorAction Stop" -RetryCount 30 -Sleep 30}},
6179
@{Name = "Az.Resources [Cleanup]"; Command = {Remove-AzResourceGroup -Name $resourceGroupName -Force -ErrorAction Stop}}
6280
)
81+
6382
$resourceTestCommands = @(
6483
@{Name = "Az.Storage [Management]"; Command = {New-AzStorageAccount -Name $storageAccountName -SkuName Standard_LRS -Location westus -ResourceGroupName $resourceGroupName -ErrorAction Stop}},
6584
@{Name = "Az.Storage [Data]"; Command = {New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey 12345678 -ErrorAction Stop}},

0 commit comments

Comments
 (0)