Skip to content

Commit ad43429

Browse files
authored
Merge pull request Azure#4760 from vladimir-shcherbakov/AutomationTestFramework
Automation test framework
2 parents ecbcc0e + 4351547 commit ad43429

File tree

28 files changed

+1637
-52
lines changed

28 files changed

+1637
-52
lines changed

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

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,18 @@ Waits for specified duration if not-mocked, otherwise skips wait.
304304
.PARAMETER timeout
305305
Timeout in seconds
306306
#>
307-
function Wait-Seconds
308-
{
307+
function Wait-Seconds {
309308
param([int] $timeout)
310-
311-
[Microsoft.Azure.Test.TestUtilities]::Wait($timeout * 1000)
309+
310+
try {
311+
[Microsoft.Azure.Test.TestUtilities]::Wait($timeout * 1000);
312+
} catch {
313+
if ($PSItem.Exception.Message -like '*Unable to find type*') {
314+
Start-Sleep -Seconds $timeout;
315+
} else {
316+
throw;
317+
}
318+
}
312319
}
313320

314321

@@ -346,12 +353,39 @@ function Retry-Function
346353
return $result;
347354
}
348355

349-
function getAssetName
350-
{
351-
$stack = Get-PSCallStack
356+
<#
357+
.SYNOPSIS
358+
Gets random resource name
359+
#>
360+
function getRandomItemName {
361+
param([string] $prefix)
362+
363+
if ($prefix -eq $null -or $prefix -eq '') {
364+
$prefix = "ps";
365+
}
366+
367+
$str = $prefix + ((Get-Random) % 10000);
368+
return $str;
369+
}
370+
371+
function getAssetName {
372+
param([string] $prefix)
373+
374+
if ($prefix -eq $null -or $prefix -eq '') {
375+
$prefix = "ps";
376+
}
377+
352378
$testName = getTestName
353379

354-
$assetName = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::GetAssetName($testName, "onesdk")
380+
try {
381+
$assetName = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::GetAssetName($testName, $prefix);
382+
} catch {
383+
if ($PSItem.Exception.Message -like '*Unable to find type*') {
384+
$assetName = getRandomItemName $prefix;
385+
} else {
386+
throw;
387+
}
388+
}
355389

356390
return $assetName
357391
}

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/ComputeTestCommon.ps1

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@ function Get-ComputeTestResourceName
3030
}
3131
}
3232

33-
$oldErrorActionPreferenceValue = $ErrorActionPreference;
34-
$ErrorActionPreference = "SilentlyContinue";
35-
3633
try
3734
{
3835
$assetName = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::GetAssetName($testName, "crptestps");
3936
}
4037
catch
4138
{
42-
if (($Error.Count -gt 0) -and ($Error[0].Exception.Message -like '*Unable to find type*'))
39+
if ($PSItem.Exception.Message -like '*Unable to find type*')
4340
{
4441
$assetName = Get-RandomItemName;
4542
}
@@ -48,10 +45,6 @@ function Get-ComputeTestResourceName
4845
throw;
4946
}
5047
}
51-
finally
52-
{
53-
$ErrorActionPreference = $oldErrorActionPreferenceValue;
54-
}
5548

5649
return $assetName
5750
}
@@ -63,17 +56,14 @@ Gets test mode - 'Record' or 'Playback'
6356
#>
6457
function Get-ComputeTestMode
6558
{
66-
$oldErrorActionPreferenceValue = $ErrorActionPreference;
67-
$ErrorActionPreference = "SilentlyContinue";
68-
6959
try
7060
{
7161
$testMode = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode;
7262
$testMode = $testMode.ToString();
7363
}
7464
catch
7565
{
76-
if (($Error.Count -gt 0) -and ($Error[0].Exception.Message -like '*Unable to find type*'))
66+
if ($PSItem.Exception.Message -like '*Unable to find type*')
7767
{
7868
$testMode = 'Record';
7969
}
@@ -82,10 +72,6 @@ function Get-ComputeTestMode
8272
throw;
8373
}
8474
}
85-
finally
86-
{
87-
$ErrorActionPreference = $oldErrorActionPreferenceValue;
88-
}
8975

9076
return $testMode;
9177
}

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/DscExtensionTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function Test-GetAzureRmVMDscExtension
113113
#helper methods for ARM
114114
function Get-DefaultResourceGroupLocation
115115
{
116-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
116+
if ((Get-ComputeTestMode) -ne 'Playback')
117117
{
118118
$namespace = "Microsoft.Resources"
119119
$type = "resourceGroups"

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/SqlIaaSExtensionTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ function Test-SetAzureRmVMSqlServerExtensionWith2016Image
361361
#helper methods for ARM
362362
function Get-DefaultResourceGroupLocation
363363
{
364-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
364+
if ((Get-ComputeTestMode) -ne 'Playback')
365365
{
366366
$namespace = "Microsoft.Resources"
367367
$type = "resourceGroups"

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VMDynamicTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
function get_all_vm_locations
1616
{
17-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
17+
if ((Get-ComputeTestMode) -ne 'Playback')
1818
{
1919
$namespace = "Microsoft.Compute"
2020
$type = "virtualMachines"

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineBootDiagnosticsTests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<#
1616
.SYNOPSIS
1717
Test Virtual Machines
18+
.Description
19+
AzureAutomationTest
1820
#>
1921
function Test-VirtualMachineBootDiagnostics
2022
{
@@ -293,6 +295,8 @@ function Test-VirtualMachineBootDiagnosticsPremium
293295
<#
294296
.SYNOPSIS
295297
Test Virtual Machine Boot Diagnostics with Linux VM
298+
.Description
299+
AzureAutomationTest
296300
#>
297301
function Test-LinuxVirtualMachineBootDiagnostics
298302
{

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineRunCommandTests.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<#
1616
.SYNOPSIS
1717
Test Virtual Machine Run Command Get
18+
.Description
19+
AzureAutomationTest
1820
#>
1921
function Test-VirtualMachineGetRunCommand
2022
{

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,8 @@ function Test-VirtualMachinePlan
15411541
<#
15421542
.SYNOPSIS
15431543
Test Virtual Machines Plan 2
1544+
.Description
1545+
AzureAutomationTest
15441546
#>
15451547
function Test-VirtualMachinePlan2
15461548
{
@@ -1720,6 +1722,8 @@ function Test-VirtualMachineTags
17201722
<#
17211723
.SYNOPSIS
17221724
Test Virtual Machines with VMAgent and AutoUpdate
1725+
.Description
1726+
AzureAutomationTest
17231727
#>
17241728
function Test-VirtualMachineWithVMAgentAutoUpdate
17251729
{
@@ -1824,6 +1828,8 @@ function Test-VirtualMachineWithVMAgentAutoUpdate
18241828
<#
18251829
.SYNOPSIS
18261830
Test Virtual Machines with VMAgent and AutoUpdate
1831+
.Description
1832+
AzureAutomationTest
18271833
#>
18281834
function Test-LinuxVirtualMachine
18291835
{
@@ -1925,6 +1931,10 @@ function Test-LinuxVirtualMachine
19251931
}
19261932

19271933
# Test Image Cmdlet Output Format
1934+
<#
1935+
.Description
1936+
AzureAutomationTest
1937+
#>
19281938
function Test-VMImageCmdletOutputFormat
19291939
{
19301940
$locStr = Get-ComputeVMLocation;
@@ -1965,7 +1975,7 @@ function Test-GetVMSizeFromAllLocations
19651975

19661976
function get_all_vm_locations
19671977
{
1968-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
1978+
if ((Get-ComputeTestMode) -ne 'Playback')
19691979
{
19701980
$namespace = "Microsoft.Compute"
19711981
$type = "virtualMachines"

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,32 @@ function Get-VaultName
3030
return getAssetName
3131
}
3232

33+
<#
34+
.SYNOPSIS
35+
Gets test mode - 'Record' or 'Playback'
36+
#>
37+
function Get-KeyVaultTestMode {
38+
try {
39+
$testMode = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode;
40+
$testMode = $testMode.ToString();
41+
} catch {
42+
if ($PSItem.Exception.Message -like '*Unable to find type*') {
43+
$testMode = 'Record';
44+
} else {
45+
throw;
46+
}
47+
}
48+
49+
return $testMode
50+
}
51+
3352
<#
3453
.SYNOPSIS
3554
Gets the location for the Vault. Default to West US if none found.
3655
#>
3756
function Get-Location
3857
{
39-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::GetCurrentMode() -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
58+
if ((Get-KeyVaultTestMode) -ne 'Playback')
4059
{
4160
$namespace = "Microsoft.KeyVault"
4261
$type = "vaults"
@@ -45,7 +64,8 @@ function Get-Location
4564
if ($location -eq $null)
4665
{
4766
return "East US"
48-
} else
67+
}
68+
else
4969
{
5070
return $location.Locations[0]
5171
}
@@ -60,7 +80,7 @@ Gets the default location for a provider
6080
#>
6181
function Get-ProviderLocation($provider)
6282
{
63-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::GetCurrentMode() -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
83+
if ((Get-KeyVaultTestMode) -ne 'Playback')
6484
{
6585
$namespace = $provider.Split("/")[0]
6686
if($provider.Contains("/"))
@@ -71,7 +91,8 @@ function Get-ProviderLocation($provider)
7191
if ($location -eq $null)
7292
{
7393
return "East US"
74-
} else
94+
}
95+
else
7596
{
7697
return $location.Locations[0]
7798
}
@@ -89,7 +110,7 @@ Cleans the created resource groups
89110
#>
90111
function Clean-ResourceGroup($rgname)
91112
{
92-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::GetCurrentMode() -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback) {
113+
if ((Get-KeyVaultTestMode) -ne 'Playback') {
93114
Remove-AzureRmResourceGroup -Name $rgname -Force
94115
}
95116
}

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,32 @@ function Get-ResourceName
3030
return getAssetName
3131
}
3232

33+
<#
34+
.SYNOPSIS
35+
Gets test mode - 'Record' or 'Playback'
36+
#>
37+
function Get-NetworkTestMode {
38+
try {
39+
$testMode = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode;
40+
$testMode = $testMode.ToString();
41+
} catch {
42+
if ($PSItem.Exception.Message -like '*Unable to find type*') {
43+
$testMode = 'Record';
44+
} else {
45+
throw;
46+
}
47+
}
48+
49+
return $testMode
50+
}
51+
3352
<#
3453
.SYNOPSIS
3554
Gets the default location for a provider
3655
#>
3756
function Get-ProviderLocation($provider)
3857
{
39-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
58+
if ((Get-NetworkTestMode) -ne 'Playback')
4059
{
4160
$namespace = $provider.Split("/")[0]
4261
if($provider.Contains("/"))
@@ -47,7 +66,8 @@ function Get-ProviderLocation($provider)
4766
if ($location -eq $null)
4867
{
4968
return "West US"
50-
} else
69+
}
70+
else
5171
{
5272
if($location.Locations[0] -eq "West US")
5373
{
@@ -72,7 +92,7 @@ Cleans the created resource groups
7292
#>
7393
function Clean-ResourceGroup($rgname)
7494
{
75-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback) {
95+
if ((Get-NetworkTestMode) -ne 'Playback') {
7696
Remove-AzureRmResourceGroup -Name $rgname -Force
7797
}
7898
}
@@ -83,7 +103,7 @@ Sleeps but only during recording.
83103
#>
84104
function Start-TestSleep($milliseconds)
85105
{
86-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
106+
if ((Get-NetworkTestMode) -ne 'Playback')
87107
{
88108
Start-Sleep -Milliseconds $milliseconds
89109
}

0 commit comments

Comments
 (0)