Skip to content

Commit 6f47aeb

Browse files
authored
Refactor test scripts to use Start-TestSleep instead of Start-Sleep (#26429)
1 parent 5e39fe2 commit 6f47aeb

16 files changed

+655
-751
lines changed
Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
if(($null -eq $TestName) -or ($TestName -contains 'Approve-AzEventHubPrivateEndpointConnection'))
2-
{
3-
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
4-
if (-Not (Test-Path -Path $loadEnvPath)) {
5-
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
6-
}
7-
. ($loadEnvPath)
8-
$TestRecordingFile = Join-Path $PSScriptRoot 'Approve-AzEventHubPrivateEndpointConnection.Recording.json'
9-
$currentPath = $PSScriptRoot
10-
while(-not $mockingPath) {
11-
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
12-
$currentPath = Split-Path -Path $currentPath -Parent
13-
}
14-
. ($mockingPath | Select-Object -First 1).FullName
1+
if (($null -eq $TestName) -or ($TestName -contains 'Approve-AzEventHubPrivateEndpointConnection')) {
2+
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
3+
if (-Not (Test-Path -Path $loadEnvPath)) {
4+
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
5+
}
6+
. ($loadEnvPath)
7+
$TestRecordingFile = Join-Path $PSScriptRoot 'Approve-AzEventHubPrivateEndpointConnection.Recording.json'
8+
$currentPath = $PSScriptRoot
9+
while (-not $mockingPath) {
10+
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
11+
$currentPath = Split-Path -Path $currentPath -Parent
12+
}
13+
. ($mockingPath | Select-Object -First 1).FullName
1514
}
1615

1716
Describe 'Approve-AzEventHubPrivateEndpointConnection' {
1817
$privateEndpoint = Get-AzEventHubPrivateEndpointConnection -ResourceGroupName $env.resourceGroup -NamespaceName $env.namespace
19-
18+
2019
It 'SetExpanded' {
2120
$privateEndpoint[0].ConnectionState | Should -Be "Pending"
2221
$privateEndpoint[0].Description | Should -Be "Hello"
@@ -25,11 +24,9 @@ Describe 'Approve-AzEventHubPrivateEndpointConnection' {
2524
$firstPrivateEndpoint.ConnectionState | Should -Be "Approved"
2625
$firstPrivateEndpoint.Description | Should -Be ""
2726

28-
while($firstPrivateEndpoint.ProvisioningState -ne "Succeeded"){
27+
while ($firstPrivateEndpoint.ProvisioningState -ne "Succeeded") {
2928
$firstPrivateEndpoint = Get-AzEventHubPrivateEndpointConnection -Name $privateEndpoint[0].Name -ResourceGroupName $env.resourceGroup -NamespaceName $env.namespace
30-
if ($TestMode -ne 'playback') {
31-
Start-Sleep 10
32-
}
29+
Start-TestSleep 10
3330
}
3431
}
3532

@@ -40,11 +37,9 @@ Describe 'Approve-AzEventHubPrivateEndpointConnection' {
4037
$secondPrivateEndpoint.ConnectionState | Should -Be "Approved"
4138
$secondPrivateEndpoint.Description | Should -Be "Bye"
4239

43-
while($secondPrivateEndpoint.ProvisioningState -ne "Succeeded"){
40+
while ($secondPrivateEndpoint.ProvisioningState -ne "Succeeded") {
4441
$secondPrivateEndpoint = Get-AzEventHubPrivateEndpointConnection -Name $privateEndpoint[1].Name -ResourceGroupName $env.resourceGroup -NamespaceName $env.namespace
45-
if ($TestMode -ne 'playback') {
46-
Start-Sleep 10
47-
}
42+
Start-TestSleep 10
4843
}
4944
}
5045
}

src/EventHub/EventHub.Autorest/test/New-AzEventHubGeoDRConfiguration.Tests.ps1

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
if(($null -eq $TestName) -or ($TestName -contains 'New-AzEventHubGeoDRConfiguration'))
2-
{
3-
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
4-
if (-Not (Test-Path -Path $loadEnvPath)) {
5-
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
6-
}
7-
. ($loadEnvPath)
8-
$TestRecordingFile = Join-Path $PSScriptRoot 'New-AzEventHubGeoDRConfiguration.Recording.json'
9-
$currentPath = $PSScriptRoot
10-
while(-not $mockingPath) {
11-
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
12-
$currentPath = Split-Path -Path $currentPath -Parent
13-
}
14-
. ($mockingPath | Select-Object -First 1).FullName
1+
if (($null -eq $TestName) -or ($TestName -contains 'New-AzEventHubGeoDRConfiguration')) {
2+
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
3+
if (-Not (Test-Path -Path $loadEnvPath)) {
4+
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
5+
}
6+
. ($loadEnvPath)
7+
$TestRecordingFile = Join-Path $PSScriptRoot 'New-AzEventHubGeoDRConfiguration.Recording.json'
8+
$currentPath = $PSScriptRoot
9+
while (-not $mockingPath) {
10+
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
11+
$currentPath = Split-Path -Path $currentPath -Parent
12+
}
13+
. ($mockingPath | Select-Object -First 1).FullName
1514
}
1615

1716
Describe 'New-AzEventHubGeoDRConfiguration' {
@@ -22,11 +21,9 @@ Describe 'New-AzEventHubGeoDRConfiguration' {
2221
$drConfig.PartnerNamespace | Should -Be $env.secondaryNamespaceResourceId
2322
$drConfig.Role | Should -Be "Primary"
2423

25-
while($drConfig.ProvisioningState -ne "Succeeded"){
24+
while ($drConfig.ProvisioningState -ne "Succeeded") {
2625
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace
27-
if ($TestMode -ne 'playback') {
28-
Start-Sleep 10
29-
}
26+
Start-TestSleep 10
3027
}
3128

3229
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.secondaryNamespace
Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
if(($null -eq $TestName) -or ($TestName -contains 'Set-AzEventHubGeoDRConfigurationBreakPair'))
2-
{
3-
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
4-
if (-Not (Test-Path -Path $loadEnvPath)) {
5-
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
6-
}
7-
. ($loadEnvPath)
8-
$TestRecordingFile = Join-Path $PSScriptRoot 'Set-AzEventHubGeoDRConfigurationBreakPair.Recording.json'
9-
$currentPath = $PSScriptRoot
10-
while(-not $mockingPath) {
11-
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
12-
$currentPath = Split-Path -Path $currentPath -Parent
13-
}
14-
. ($mockingPath | Select-Object -First 1).FullName
1+
if (($null -eq $TestName) -or ($TestName -contains 'Set-AzEventHubGeoDRConfigurationBreakPair')) {
2+
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
3+
if (-Not (Test-Path -Path $loadEnvPath)) {
4+
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
5+
}
6+
. ($loadEnvPath)
7+
$TestRecordingFile = Join-Path $PSScriptRoot 'Set-AzEventHubGeoDRConfigurationBreakPair.Recording.json'
8+
$currentPath = $PSScriptRoot
9+
while (-not $mockingPath) {
10+
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
11+
$currentPath = Split-Path -Path $currentPath -Parent
12+
}
13+
. ($mockingPath | Select-Object -First 1).FullName
1514
}
1615

1716
Describe 'Set-AzEventHubGeoDRConfigurationBreakPair' {
1817
It 'Break' -skip:$($env.secondaryLocation -eq '') {
1918
Set-AzEventHubGeoDRConfigurationBreakPair -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace -Name $env.alias
20-
21-
while($drConfig.ProvisioningState -ne "Succeeded"){
19+
20+
while ($drConfig.ProvisioningState -ne "Succeeded") {
2221
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace
23-
if ($TestMode -ne 'playback') {
24-
Start-Sleep 10
25-
}
22+
Start-TestSleep 10
2623
}
2724

2825
$drConfig.Name | Should -Be $env.alias
@@ -32,38 +29,32 @@ Describe 'Set-AzEventHubGeoDRConfigurationBreakPair' {
3229

3330
$drConfig = New-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace -PartnerNamespace $env.secondaryNamespaceResourceId
3431

35-
while($drConfig.ProvisioningState -ne "Succeeded"){
32+
while ($drConfig.ProvisioningState -ne "Succeeded") {
3633
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace
37-
if ($TestMode -ne 'playback') {
38-
Start-Sleep 10
39-
}
34+
Start-TestSleep 10
4035
}
4136
}
4237

4338
It 'BreakViaIdentity' -skip:$($env.secondaryLocation -eq '') {
4439
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace
4540

4641
Set-AzEventHubGeoDRConfigurationBreakPair -InputObject $drConfig
47-
48-
do{
42+
43+
do {
4944
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace
50-
if ($TestMode -ne 'playback') {
51-
Start-Sleep 10
52-
}
53-
} while($drConfig.ProvisioningState -ne "Succeeded")
45+
Start-TestSleep 10
46+
} while ($drConfig.ProvisioningState -ne "Succeeded")
5447

5548
$drConfig.Name | Should -Be $env.alias
5649
$drConfig.ResourceGroupName | Should -Be $env.resourceGroup
5750
$drConfig.PartnerNamespace | Should -Be ""
5851
$drConfig.Role | Should -Be "PrimaryNotReplicating"
5952

6053
$drConfig = New-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace -PartnerNamespace $env.secondaryNamespaceResourceId
61-
62-
while($drConfig.ProvisioningState -ne "Succeeded"){
54+
55+
while ($drConfig.ProvisioningState -ne "Succeeded") {
6356
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace
64-
if ($TestMode -ne 'playback') {
65-
Start-Sleep 10
66-
}
57+
Start-TestSleep 10
6758
}
6859
}
6960
}

src/EventHub/EventHub.Autorest/test/Set-AzEventHubGeoDRConfigurationFailOver.Tests.ps1

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ if(($null -eq $TestName) -or ($TestName -contains 'Set-AzEventHubGeoDRConfigurat
1717
Describe 'Set-AzEventHubGeoDRConfigurationFailOver' {
1818
It 'Fail' -skip:$($env.secondaryLocation -eq '') {
1919
Set-AzEventHubGeoDRConfigurationFailOver -ResourceGroupName $env.resourceGroup -NamespaceName $env.secondaryNamespace -Name $env.alias
20-
20+
2121
while($drConfig.ProvisioningState -ne "Succeeded"){
2222
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.secondaryNamespace
23-
if ($TestMode -ne 'playback') {
24-
Start-Sleep 10
25-
}
23+
Start-TestSleep 10
2624
}
2725

2826
$drConfig.Name | Should -Be $env.alias
@@ -31,28 +29,22 @@ Describe 'Set-AzEventHubGeoDRConfigurationFailOver' {
3129
$drConfig.Role | Should -Be "PrimaryNotReplicating"
3230

3331
$drConfig = Remove-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.secondaryNamespace
34-
if ($TestMode -ne 'playback') {
35-
Start-Sleep 180
36-
}
32+
Start-TestSleep 180
3733
$drConfig = New-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace -PartnerNamespace $env.secondaryNamespaceResourceId
38-
34+
3935
while($drConfig.ProvisioningState -ne "Succeeded"){
4036
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace
41-
if ($TestMode -ne 'playback') {
42-
Start-Sleep 10
43-
}
37+
Start-TestSleep 10
4438
}
4539
}
4640
It 'FailViaIdentity' -skip:$($env.secondaryLocation -eq '') {
4741
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.secondaryNamespace
48-
42+
4943
Set-AzEventHubGeoDRConfigurationFailOver -InputObject $drConfig
50-
44+
5145
do {
5246
$drConfig = Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.secondaryNamespace
53-
if ($TestMode -ne 'playback') {
54-
Start-Sleep 10
55-
}
47+
Start-TestSleep 10
5648
} while($drConfig.ProvisioningState -ne "Succeeded")
5749

5850
$drConfig.Name | Should -Be $env.alias
@@ -61,10 +53,8 @@ Describe 'Set-AzEventHubGeoDRConfigurationFailOver' {
6153
$drConfig.Role | Should -Be "PrimaryNotReplicating"
6254

6355
$drConfig = Remove-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.secondaryNamespace
64-
65-
if ($TestMode -ne 'playback') {
66-
Start-Sleep 180
67-
}
56+
57+
Start-TestSleep 180
6858

6959
{ Get-AzEventHubGeoDRConfiguration -Name $env.alias -ResourceGroupName $env.resourceGroup -NamespaceName $env.primaryNamespace -ErrorAction Stop } | Should -Throw
7060
}

src/EventHub/EventHub.Autorest/test/utils.ps1

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
11
function RandomString([bool]$allChars, [int32]$len) {
22
if ($allChars) {
3-
return -join ((33..126) | Get-Random -Count $len | % {[char]$_})
4-
} else {
5-
return -join ((48..57) + (97..122) | Get-Random -Count $len | % {[char]$_})
3+
return -join ((33..126) | Get-Random -Count $len | % { [char]$_ })
4+
}
5+
else {
6+
return -join ((48..57) + (97..122) | Get-Random -Count $len | % { [char]$_ })
67
}
78
}
9+
function Start-TestSleep {
10+
[CmdletBinding(DefaultParameterSetName = 'SleepBySeconds')]
11+
param(
12+
[parameter(Mandatory = $true, Position = 0, ParameterSetName = 'SleepBySeconds')]
13+
[ValidateRange(0.0, 2147483.0)]
14+
[double] $Seconds,
15+
16+
[parameter(Mandatory = $true, ParameterSetName = 'SleepByMilliseconds')]
17+
[ValidateRange('NonNegative')]
18+
[Alias('ms')]
19+
[int] $Milliseconds
20+
)
21+
22+
if ($TestMode -ne 'playback') {
23+
switch ($PSCmdlet.ParameterSetName) {
24+
'SleepBySeconds' {
25+
Start-Sleep -Seconds $Seconds
26+
}
27+
'SleepByMilliseconds' {
28+
Start-Sleep -Milliseconds $Milliseconds
29+
}
30+
}
31+
}
32+
}
33+
834
$env = @{}
935
if ($UsePreviousConfigForRecord) {
1036
$previousEnv = Get-Content (Join-Path $PSScriptRoot 'env.json') | ConvertFrom-Json
1137
$previousEnv.psobject.properties | Foreach-Object { $env[$_.Name] = $_.Value }
1238
}
1339
# Add script method called AddWithCache to $env, when useCache is set true, it will try to get the value from the $env first.
1440
# example: $val = $env.AddWithCache('key', $val, $true)
15-
$env | Add-Member -Type ScriptMethod -Value { param( [string]$key, [object]$val, [bool]$useCache) if ($this.Contains($key) -and $useCache) { return $this[$key] } else { $this[$key] = $val; return $val } } -Name 'AddWithCache'
41+
$env | Add-Member -Type ScriptMethod -Value { param( [string]$key, [object]$val, [bool]$useCache) if ($this.Contains($key) -and $useCache) {
42+
return $this[$key]
43+
}
44+
else {
45+
$this[$key] = $val; return $val
46+
} } -Name 'AddWithCache'
1647
function setupEnv(
1748
$location = 'eastus',
1849
$secondaryLocation = 'southcentralus',
@@ -221,13 +252,13 @@ function setupEnv(
221252
}
222253

223254
function GenerateSASKey {
224-
[Reflection.Assembly]::LoadWithPartialName("System.Web")| out-null
225-
$URI="myNamespace.servicebus.windows.net/myEventHub"
226-
$Access_Policy_Name="RootManageSharedAccessKey"
227-
$Access_Policy_Key="myPrimaryKey"
255+
[Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null
256+
$URI = "myNamespace.servicebus.windows.net/myEventHub"
257+
$Access_Policy_Name = "RootManageSharedAccessKey"
258+
$Access_Policy_Key = "myPrimaryKey"
228259
#Token expires now+300
229-
$Expires=([DateTimeOffset]::Now.ToUnixTimeSeconds())+300
230-
$SignatureString=[System.Web.HttpUtility]::UrlEncode($URI)+ "`n" + [string]$Expires
260+
$Expires = ([DateTimeOffset]::Now.ToUnixTimeSeconds()) + 300
261+
$SignatureString = [System.Web.HttpUtility]::UrlEncode($URI) + "`n" + [string]$Expires
231262
$HMAC = New-Object System.Security.Cryptography.HMACSHA256
232263
$HMAC.key = [Text.Encoding]::ASCII.GetBytes($Access_Policy_Key)
233264
$Signature = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($SignatureString))

0 commit comments

Comments
 (0)