Skip to content

Commit 8960c04

Browse files
committed
Flag to enable no cleanup on failures
1 parent b4dfe50 commit 8960c04

2 files changed

Lines changed: 57 additions & 39 deletions

File tree

CanaryValidator/Canary.Tests.ps1

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ param (
8686
[Parameter(ParameterSetName="tenant", Mandatory=$false)]
8787
[ValidateNotNullOrEmpty()]
8888
[switch]$NoCleanup,
89+
[parameter(HelpMessage="Specifies whether Canary needs to clean up resources when a failure is encountered")]
90+
[Parameter(ParameterSetName="default", Mandatory=$false)]
91+
[Parameter(ParameterSetName="tenant", Mandatory=$false)]
92+
[ValidateNotNullOrEmpty()]
93+
[switch]$NoCleanupOnFailure,
8994
[parameter(HelpMessage="Specifies the path for log files")]
9095
[Parameter(ParameterSetName="default", Mandatory=$false)]
9196
[Parameter(ParameterSetName="tenant", Mandatory=$false)]
@@ -851,63 +856,66 @@ while ($runCount -le $NumberOfIterations)
851856

852857
if (-not $NoCleanup)
853858
{
854-
Invoke-Usecase -Name 'DeleteVMWithPrivateIP' -Description "Delete the VM with private IP" -UsecaseBlock `
859+
if (-not ($NoCleanupOnFailure -and (Get-CanaryFailureStatus)))
855860
{
856-
if ($vmObject = Get-AzureRmVM -ResourceGroupName $CanaryVMRG -Name $privateVMName -ErrorAction Stop)
861+
Invoke-Usecase -Name 'DeleteVMWithPrivateIP' -Description "Delete the VM with private IP" -UsecaseBlock `
857862
{
858-
$deleteVM = $vmObject | Remove-AzureRmVM -Force -ErrorAction Stop
859-
if (-not (($deleteVM.StatusCode -eq "OK") -and ($deleteVM.IsSuccessStatusCode)))
863+
if ($vmObject = Get-AzureRmVM -ResourceGroupName $CanaryVMRG -Name $privateVMName -ErrorAction Stop)
860864
{
861-
throw [System.Exception]"Failed to delete the VM $privateVMName"
865+
$deleteVM = $vmObject | Remove-AzureRmVM -Force -ErrorAction Stop
866+
if (-not (($deleteVM.StatusCode -eq "OK") -and ($deleteVM.IsSuccessStatusCode)))
867+
{
868+
throw [System.Exception]"Failed to delete the VM $privateVMName"
869+
}
862870
}
863871
}
864-
}
865872

866-
Invoke-Usecase -Name 'DeleteVMResourceGroup' -Description "Delete the resource group that contains all the VMs and corresponding resources" -UsecaseBlock `
867-
{
868-
if ($removeRG = Get-AzureRmResourceGroup -Name $CanaryVMRG -ErrorAction Stop)
873+
Invoke-Usecase -Name 'DeleteVMResourceGroup' -Description "Delete the resource group that contains all the VMs and corresponding resources" -UsecaseBlock `
869874
{
870-
$removeRG | Remove-AzureRmResourceGroup -Force -ErrorAction Stop
875+
if ($removeRG = Get-AzureRmResourceGroup -Name $CanaryVMRG -ErrorAction Stop)
876+
{
877+
$removeRG | Remove-AzureRmResourceGroup -Force -ErrorAction Stop
878+
}
871879
}
872-
}
873880

874-
Invoke-Usecase -Name 'DeleteUtilitiesResourceGroup' -Description "Delete the resource group that contains all the utilities and corresponding resources" -UsecaseBlock `
875-
{
876-
if ($removeRG = Get-AzureRmResourceGroup -Name $CanaryUtilitiesRG -ErrorAction Stop)
881+
Invoke-Usecase -Name 'DeleteUtilitiesResourceGroup' -Description "Delete the resource group that contains all the utilities and corresponding resources" -UsecaseBlock `
877882
{
878-
$removeRG | Remove-AzureRmResourceGroup -Force -ErrorAction Stop
883+
if ($removeRG = Get-AzureRmResourceGroup -Name $CanaryUtilitiesRG -ErrorAction Stop)
884+
{
885+
$removeRG | Remove-AzureRmResourceGroup -Force -ErrorAction Stop
886+
}
879887
}
880-
}
881888

882-
if (($TenantAdminCredentials) -or ($listAvl))
883-
{
884-
Invoke-Usecase -Name 'TenantRelatedcleanup' -Description "Remove all the tenant related stuff" -UsecaseBlock `
889+
if (($TenantAdminCredentials) -or ($listAvl))
885890
{
886-
Invoke-Usecase -Name 'DeleteTenantSubscriptions' -Description "Remove all the tenant related subscriptions" -UsecaseBlock `
891+
Invoke-Usecase -Name 'TenantRelatedcleanup' -Description "Remove all the tenant related stuff" -UsecaseBlock `
887892
{
888-
if ($subs = Get-AzureRmTenantSubscription -ErrorAction Stop | Where-Object DisplayName -eq $tenantSubscriptionName)
889-
{
890-
Remove-AzureRmTenantSubscription -TargetSubscriptionId $subs.SubscriptionId -ErrorAction Stop
891-
}
892-
if ($subs = Get-AzureRmTenantSubscription -ErrorAction Stop | Where-Object DisplayName -eq $canaryDefaultTenantSubscription)
893+
Invoke-Usecase -Name 'DeleteTenantSubscriptions' -Description "Remove all the tenant related subscriptions" -UsecaseBlock `
893894
{
894-
Remove-AzureRmTenantSubscription -TargetSubscriptionId $subs.SubscriptionId -ErrorAction Stop
895-
}
896-
}
895+
if ($subs = Get-AzureRmTenantSubscription -ErrorAction Stop | Where-Object DisplayName -eq $tenantSubscriptionName)
896+
{
897+
Remove-AzureRmTenantSubscription -TargetSubscriptionId $subs.SubscriptionId -ErrorAction Stop
898+
}
899+
if ($subs = Get-AzureRmTenantSubscription -ErrorAction Stop | Where-Object DisplayName -eq $canaryDefaultTenantSubscription)
900+
{
901+
Remove-AzureRmTenantSubscription -TargetSubscriptionId $subs.SubscriptionId -ErrorAction Stop
902+
}
903+
}
897904

898-
Invoke-Usecase -Name 'LoginToAzureStackEnvAsSvcAdminForCleanup' -Description "Login to $SvcAdminEnvironmentName as service administrator to remove the subscription resource group" -UsecaseBlock `
899-
{
900-
Add-AzureRmAccount -EnvironmentName $SvcAdminEnvironmentName -Credential $ServiceAdminCredentials -TenantId $TenantID -ErrorAction Stop
901-
}
905+
Invoke-Usecase -Name 'LoginToAzureStackEnvAsSvcAdminForCleanup' -Description "Login to $SvcAdminEnvironmentName as service administrator to remove the subscription resource group" -UsecaseBlock `
906+
{
907+
Add-AzureRmAccount -EnvironmentName $SvcAdminEnvironmentName -Credential $ServiceAdminCredentials -TenantId $TenantID -ErrorAction Stop
908+
}
902909

903-
Invoke-Usecase -Name 'DeleteSubscriptionResourceGroup' -Description "Delete the resource group that contains subscription resources" -UsecaseBlock `
904-
{
905-
if ($removeRG = Get-AzureRmResourceGroup -Name $subscriptionRGName -ErrorAction Stop)
910+
Invoke-Usecase -Name 'DeleteSubscriptionResourceGroup' -Description "Delete the resource group that contains subscription resources" -UsecaseBlock `
906911
{
907-
$removeRG | Remove-AzureRmResourceGroup -Force -ErrorAction Stop
908-
}
909-
}
910-
}
912+
if ($removeRG = Get-AzureRmResourceGroup -Name $subscriptionRGName -ErrorAction Stop)
913+
{
914+
$removeRG | Remove-AzureRmResourceGroup -Force -ErrorAction Stop
915+
}
916+
}
917+
}
918+
}
911919
}
912920
}
913921

CanaryValidator/Canary.Utilities.psm1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ function Get-CanaryLonghaulResult
151151
@{Expression={$pCount = ($_.Group | Where-Object Result -eq "PASS").Count; $times = ($_.Group | Where-Object Result -eq "PASS" | ForEach-Object {((Get-Date $_.EndTime) - (Get-Date $_.StartTime)).TotalMilliseconds}); $avgTime = ($times | Measure-Object -Average).Average; $sd = 0; foreach ($time in $times){$sd += [math]::Pow(($time - $avgTime), 2)}; [math]::Round(([math]::Round([math]::Sqrt($sd/$pCount), 0)/$avgTime), 0) * 100};Label="RelativeStdDev`n[Goal: <50%]"; Align = "Left"}
152152
}
153153

154+
function Get-CanaryFailureStatus
155+
{
156+
$logContent = Get-Content -Raw -Path $Global:JSONLogFile | ConvertFrom-Json
157+
if ($logContent.Usecases.Result -contains "FAIL")
158+
{
159+
return $true
160+
}
161+
return $false
162+
}
163+
154164
function Start-Scenario
155165
{
156166
[CmdletBinding()]

0 commit comments

Comments
 (0)