Skip to content

Commit c752abc

Browse files
committed
Fix issues with running RunKeyVaultTests.ps1.
## Background When attempting to run the Azure Key Vault PowerShell tests via the `RunKeyVaultTests.ps1` script, the following 5 tests would fail: ``` Test_ImportByokWithDefaultAttributes Test_ImportByokWith1024BitKey Test_ImportByokWithCustomAttributes Test_GetAllKeys Test_GetKeyVersions ``` The first 3 BYOK tests failed because they test against dummy `.byok` files that were generated with a specific subscription ID that may not match the subscription ID of whomever is running the tests. The last 2 tests failed because they attempt to reference `[Microsoft.WindowsAzure.Testing.TestUtilities]`, which cannot be found. Moreover, this PowerShell script was difficult to use if we didn't want to provide our own vault to test against for both the data plane and the control plane, but we instead wanted the script to generate its own temporary vault. Also, we sometimes would like to programmatically skip Active Directory related tests for certain environments (instead of having it hardcoded to always skip for Fairfax environments). Finally, the PowerShell script could use some cleanup with respect to formatting whitespace, fleshing out docstrings, and general organization. ## The Change This change addresses the above issues by: - Guarding the 3 BYOK tests with an if condition that tests the subscription ID. - Replacing `Wait($timeout * 1000)` with `Start-Sleep -Seconds $timeout`. - Saving/restoring temporary vault state for both the control plane and data plane tests. - Exposing the `$NoADCmdLetMode` parameter. - Generally improving the coding style.
1 parent a1c152f commit c752abc

File tree

6 files changed

+671
-406
lines changed

6 files changed

+671
-406
lines changed

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,15 @@ function Get-ImportKeyFile1024([string]$filesuffix, [bool] $exists=$true)
109109

110110
<#
111111
.SYNOPSIS
112-
Remove log file under a folder
112+
Remove log files under the given folder.
113113
#>
114-
function Cleanup-Log([string]$rootfolder)
115-
{
116-
Get-ChildItem –Path $rootfolder -Include *.debug_log -Recurse | where {$_.mode -match "a"} | Remove-Item -Force
114+
function Cleanup-LogFiles([string]$rootfolder)
115+
{
116+
Write-Host "Cleaning up log files from $rootfolder..."
117+
118+
Get-ChildItem –Path $rootfolder -Include *.debug_log -Recurse |
119+
where {$_.mode -match "a"} |
120+
Remove-Item -Force
117121
}
118122

119123
<#
@@ -125,7 +129,7 @@ function Move-Log([string]$rootfolder)
125129
$logfolder = Join-Path $rootfolder ("$global:testEnv"+"$global:testns"+"log")
126130
if (Test-Path $logfolder)
127131
{
128-
Cleanup-Log $logfolder
132+
Cleanup-LogFiles $logfolder
129133
}
130134
else
131135
{
@@ -138,24 +142,32 @@ function Move-Log([string]$rootfolder)
138142

139143
<#
140144
.SYNOPSIS
141-
Removes all keys starting with the prefix
145+
Remove all old keys starting with the given prefix.
142146
#>
143-
function Initialize-KeyTest
147+
function Cleanup-OldKeys
144148
{
149+
Write-Host "Cleaning up old keys..."
150+
145151
$keyVault = Get-KeyVault
146152
$keyPattern = Get-KeyName '*'
147-
Get-AzureKeyVaultKey $keyVault | Where-Object {$_.KeyName -like $keyPattern} | Remove-AzureKeyVaultKey -Force -Confirm:$false
153+
Get-AzureKeyVaultKey $keyVault |
154+
Where-Object {$_.KeyName -like $keyPattern} |
155+
Remove-AzureKeyVaultKey -Force -Confirm:$false
148156
}
149157

150158
<#
151159
.SYNOPSIS
152-
Removes all secrets starting with the prefix
160+
Remove all old secrets starting with the given prefix.
153161
#>
154-
function Initialize-SecretTest
162+
function Cleanup-OldSecrets
155163
{
164+
Write-Host "Cleaning up old secrets..."
165+
156166
$keyVault = Get-KeyVault
157167
$secretPattern = Get-SecretName '*'
158-
Get-AzureKeyVaultSecret $keyVault | Where-Object {$_.SecretName -like $secretPattern} | Remove-AzureKeyVaultSecret -Force -Confirm:$false
168+
Get-AzureKeyVaultSecret $keyVault |
169+
Where-Object {$_.SecretName -like $secretPattern} |
170+
Remove-AzureKeyVaultSecret -Force -Confirm:$false
159171
}
160172

161173

src/ResourceManager/KeyVault/Commands.KeyVault.Test/Scripts/ControlPlane/KeyVaultManagementTests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Param($rgName, $location, $tagName, $tagValue)
3737
Assert-AreEqual "Standard" $actual.Sku
3838
Assert-AreEqual $false $actual.EnabledForDeployment
3939

40+
if ($global:noADCmdLetMode) {return;}
41+
4042
# Default Access Policy
4143
$upn = [Microsoft.WindowsAzure.Commands.Common.AzureRMProfileProvider]::Instance.Profile.Context.Account.Id
4244
$objectId = @(Get-AzureRmADUser -Mail $upn)[0].Id
@@ -50,8 +52,6 @@ Param($rgName, $location, $tagName, $tagValue)
5052
"restore")
5153
$expectedPermsToSecrets = @("all")
5254

53-
if ($global:noADCmdLetMode) {return;}
54-
5555
Assert-AreEqual 1 @($actual.AccessPolicies).Count
5656
Assert-AreEqual $objectId $actual.AccessPolicies[0].ObjectId
5757
$result = Compare-Object $expectedPermsToKeys $actual.AccessPolicies[0].PermissionsToKeys

0 commit comments

Comments
 (0)