Skip to content

Commit a34ca2b

Browse files
committed
Changes made based on markcowl comments.
1 parent 893395e commit a34ca2b

File tree

5 files changed

+45
-94
lines changed

5 files changed

+45
-94
lines changed

src/ResourceManager/Common/Commands.ScenarioTests.ResourceManager.Common/Common.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,15 @@ function getRandomItemName {
364364
$prefix = "ps";
365365
}
366366

367-
$str = $prefix + ((Get-Random) % 10000);
367+
$str = $prefix + (([guid]::NewGuid().ToString() -replace '-','')[0..9] -join '');
368368
return $str;
369369
}
370370

371371
function getAssetName {
372372
param([string] $prefix)
373373

374374
if ($prefix -eq $null -or $prefix -eq '') {
375-
$prefix = "ps$(([guid]::NewGuid().ToString() -replace '-','')[0..9] -join '')";
375+
$prefix = "ps";
376376
}
377377

378378
$testName = getTestName

tools/AutomationTestFramework/Management/RunbookProcessor.ps1

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -91,51 +91,25 @@ function Create-Runbooks (
9191
}
9292
}
9393

94-
# https://www.codeisahighway.com/how-to-easily-and-silently-obtain-accesstoken-bearer-from-an-existing-azure-powershell-session/
95-
function Get-AzureRmAccessToken() {
96-
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
97-
if(-not $azureRmProfile.Accounts.Count) {
98-
Write-Error "Ensure you have logged in before calling this function."
99-
return
100-
}
101-
102-
$currentAzureContext = Get-AzureRmContext
103-
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile)
104-
Write-Debug ("Getting access token for tenant" + $currentAzureContext.Subscription.TenantId)
105-
$token = $profileClient.AcquireAccessToken($currentAzureContext.Subscription.TenantId)
106-
$token.AccessToken
107-
}
108-
10994
function Start-Runbooks ([hashtable] $automation, [string] $runbooksPath) {
110-
# Remove any previous jobs from this PS session
111-
Get-Job | Remove-Job
112-
11395
foreach ($runbook in Get-ChildItem $runbooksPath) {
11496
$bookName = $runbook.BaseName
11597
Write-Verbose "Uploading '$bookName' runbook..."
11698
$null = Import-AzureRmAutomationRunbook -Path $runbook.FullName -Name $bookName -type PowerShell -AutomationAccountName $automation.AccountName -ResourceGroupName $automation.ResourceGroupName -LogVerbose $true -Force -ErrorAction Stop
11799
Write-Verbose "Publishing '$bookName' runbook..."
118100
$null = Publish-AzureRmAutomationRunbook -Name $bookName -AutomationAccountName $automation.AccountName -ResourceGroupName $automation.ResourceGroupName -ErrorAction Stop
119101

120-
$accessToken = Get-AzureRmAccessToken
121-
$accountId = $(Get-AzureRmContext).Account.Id
122-
$subscriptionName = $(Get-AzureRmContext).Subscription.Name
123-
Write-Verbose "Starting '$bookName' runbook..."
124-
$null = Start-Job `
125-
-Name $bookName `
126-
-ArgumentList $accessToken,$accountId,$subscriptionName,$bookName,$automation `
127-
-ScriptBlock {
128-
param($accessToken,$accountId,$subscriptionName,$bookName,$automation)
129-
Login-AzureRmAccount -AccessToken $accessToken -AccountId $accountId -Subscription $subscriptionName
130-
Start-AzureRmAutomationRunbook -Name $bookName -AutomationAccountName $automation.AccountName -ResourceGroupName $automation.ResourceGroupName -ErrorAction Stop -Wait -MaxWaitSeconds 3600
102+
Start-Job -Name $bookName -ArgumentList (Get-AzureRmContext),$bookName,$automation -ScriptBlock {
103+
param ($context,$bookName,$automation)
104+
Start-AzureRmAutomationRunbook -DefaultProfile $context -Name $bookName -AutomationAccountName $automation.AccountName -ResourceGroupName $automation.ResourceGroupName -ErrorAction Stop -Wait -MaxWaitSeconds 3600
131105
}
132106
Write-Verbose "$bookName started."
133107
}
134108
}
135109

136-
function Wait-RunbookResults ([hashtable] $automation) {
110+
function Wait-RunbookResults ([hashtable] $automation, $jobs) {
137111
Write-Verbose 'Waiting for runbooks to complete...'
138-
$failedJobs = Get-Job | Wait-Job | ForEach-Object {
112+
$failedJobs = $jobs | Wait-Job | ForEach-Object {
139113
$name = $_.Name
140114
# https://stackoverflow.com/a/8751271/294804
141115
$state = $_.State

tools/AutomationTestFramework/RunAutomationTests.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ param (
2626

2727
try {
2828
$srcPath = "$PSScriptRoot\..\..\src"
29-
$projectList = @('Compute', 'Network', 'Profile', 'Resources', 'Sql', 'Storage', 'Websites')
29+
$projectList = @('Profile', 'Compute', 'Resources', 'Storage', 'Websites', 'Network', 'Sql')
3030
$testResourcesDir = "$PSScriptRoot\TestResources"
3131
$packagingDir = "$PSScriptRoot\Package"
3232
$helperModuleName = 'Smoke.Helper'
@@ -51,7 +51,7 @@ try {
5151
Path = "$testResourcesDir\RunbookTemplate.ps1"
5252
}
5353

54-
$signedModuleList = @('AzureRM.Resources', 'AzureRM.Compute', 'AzureRM.Automation', 'AzureRM.Network', 'AzureRM.Storage', 'AzureRM.Websites', 'AzureRM.KeyVault', 'AzureRM.Sql')
54+
$signedModuleList = @('AzureRM.Automation', 'AzureRM.Compute','AzureRM.Resources', 'AzureRM.Storage', 'AzureRM.Websites', 'AzureRM.Network', 'AzureRM.Sql')
5555
# Profile is required to be uploaded first. Storage is required second (for AzureRm.Storage). Then, the rest of the order doesn't matter.
5656
# Note: These are lists so that the addition operation is handled properly.
5757
$signedModules = @{
@@ -105,11 +105,13 @@ try {
105105
-srcPath $srcPath `
106106
-projectList $projectList `
107107
-outputPath $runbooksPath
108-
Start-Runbooks `
108+
$jobs = Start-Runbooks `
109109
-automation $automation `
110110
-runbooksPath $runbooksPath
111111
if ($waitForResults) {
112-
$success = Wait-RunbookResults -automation $automation
112+
$success = Wait-RunbookResults `
113+
-automation $automation `
114+
-jobs $jobs
113115
}
114116
Write-Verbose '============================================='
115117
}
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
115
Write-Output "JobId:$($PsPrivateMetaData.JobId.Guid)"
216
$VerbosePreference = 'Continue'
3-
LoginWithConnection %LOGIN-PARAMS%
17+
Login-AutomationConnection %LOGIN-PARAMS%
418

519
%TEST-LIST%
6-
TestRunner $testList %LOGIN-PARAMS%
20+
Run-Test $testList %LOGIN-PARAMS%
721

822
Write-Verbose 'Resolve-AzureRmError Information'
923
Write-Verbose '--------------------------------'
10-
Resolve-AzureRmError | Out-String | Write-Verbose
24+
Resolve-AzureRmError | ConvertTo-Json | Write-Verbose

tools/AutomationTestFramework/TestResources/Smoke.Helper.psm1

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# limitations under the License.
1313
# ----------------------------------------------------------------------------------
1414

15-
function LoginWithConnection([string] $connectionName, [string] $subscriptionName) {
15+
function Login-AutomationConnection([string] $connectionName, [string] $subscriptionName) {
1616

1717
if ([string]::IsNullOrEmpty($connectionName)) {
1818
$connectionName = 'AzureRunAsConnection';
@@ -40,71 +40,32 @@ function LoginWithConnection([string] $connectionName, [string] $subscriptionNam
4040
}
4141
}
4242

43-
# https://blogs.endjin.com/2014/07/how-to-retry-commands-in-powershell/
44-
function Retry-Command
45-
{
46-
param (
47-
[ScriptBlock] $script,
48-
[string] $connectionName,
49-
[string] $subscriptionName,
50-
[int] $retyCount = 3,
51-
[int] $secondsDelay = 5
52-
)
53-
$currentRetryCount = 0
54-
$succeeded = $false
55-
while (-not $succeeded) {
56-
try {
57-
# Setting ErrorAction to Stop is important. This ensures any errors that occur in the command are
58-
# treated as terminating errors, and will be caught by the catch block.
59-
& $script -ErrorAction Stop
60-
$succeeded = $true
61-
} catch {
62-
if ($currentRetryCount -ge $retyCount) {
63-
Write-Output "Maximum retry count ($currentRetryCount) exceeded!"
64-
throw $_
65-
} else {
66-
$currentRetryCount++
67-
Write-Output "Attempt #$currentRetryCount failed (Max $retyCount). Retrying after $secondsDelay seconds..."
68-
Start-Sleep $secondsDelay
69-
LoginWithConnection $connectionName $subscriptionName
70-
}
71-
}
72-
}
73-
}
74-
75-
function TestRunner([string[]]$tests, [string] $connectionName, [string] $subscriptionName) {
43+
function Run-Test([string[]] $tests, [string] $connectionName, [string] $subscriptionName) {
7644

77-
if (!$tests -or $tests.Count -eq 0) {
78-
$msg = "No tests found to execute"
79-
throw $msg
45+
if (-not $tests -or $tests.Count -eq 0) {
46+
throw "No tests found to execute"
8047
}
81-
82-
$pass = 0;
83-
$fail = 0;
84-
$run = 0;
85-
$total = $tests.Count;
8648

8749
"==> Starting test execution..."
50+
$pass = 0
51+
$fail = 0
52+
$run = 0
8853
$startTime = (Get-Date)
8954
foreach ($test in $tests) {
9055
try {
91-
$run++;
56+
$run++
9257
"--> Running $test..."
93-
# Retry-Command -connectionName $connectionName -subscriptionName $subscriptionName { $null = & $test }
9458
$null = & $test
9559
"+++ Test PASSED"
96-
$pass++;
60+
$pass++
9761
} catch {
98-
$fail++;
99-
"!!! Test FAILED: $PSItem"
100-
$scriptStackTrace = $PSItem.ScriptStackTrace
101-
$newLine = [Environment]::NewLine
102-
$scriptStackTrace.Split($newLine) | Where-Object {$_.Length -gt 0} | ForEach-Object { Write-Output "`t$_" }
62+
$fail++
63+
"!!! Test FAILED: $_"
64+
$_.ScriptStackTrace.Split([Environment]::NewLine) | Where-Object {$_.Length -gt 0} | ForEach-Object { Write-Output "`t$_" }
10365
}
10466
}
105-
10667
$endTime = (Get-Date)
107-
$elapsedTime = $endTime-$startTime
108-
$duration = '{0:hh} h {0:mm} min {0:ss} sec' -f $elapsedTime
109-
"==> Done: PASSED $pass FAILED $fail EXECUTED $run($total) DURATION $duration";
68+
69+
$duration = '{0:hh} h {0:mm} min {0:ss} sec' -f ($endTime - $startTime)
70+
"==> Done: PASSED $pass | FAILED $fail | EXECUTED $run($($tests.Count)) | DURATION $duration"
11071
}

0 commit comments

Comments
 (0)