Skip to content

Commit

Permalink
Storage account refactor rename and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Hannebauer committed Nov 7, 2024
1 parent f870912 commit 81374dc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
7 changes: 4 additions & 3 deletions SCEPman/Private/storage-account.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

function GetStorageAccount ($ResourceGroup) {
function VerifyStorageAccountDoesNotExist ($ResourceGroup) {
$storageaccounts = Convert-LinesToObject -lines $(az graph query -q "Resources | where type == 'microsoft.storage/storageaccounts' and resourceGroup == '$ResourceGroup' | project name, resourceGroup, primaryEndpoints = properties.primaryEndpoints, subscriptionId, location")
if($storageaccounts.count -gt 0) {
$potentialStorageAccountName = Read-Host "We have found one or more existing storage accounts in the resource group $ResourceGroup. Please hit enter now if you still want to create a new storage account or enter the name of the storage account you would like to use, and then hit enter"
Expand All @@ -17,7 +17,7 @@ function GetStorageAccount ($ResourceGroup) {
}
}
else {
Write-Warning "Unable to determine the storage account"
Write-Information "Unable to find an existing storage account"
return $null
}
}
Expand Down Expand Up @@ -53,7 +53,7 @@ function SetStorageAccountPermissions ($SubscriptionId, $ScStorageAccount, $serv
}

function CreateScStorageAccount ($SubscriptionId, $ResourceGroup, $servicePrincipals) {
$ScStorageAccount = GetStorageAccount -ResourceGroup $ResourceGroup
$ScStorageAccount = VerifyStorageAccountDoesNotExist -ResourceGroup $ResourceGroup
if($null -eq $ScStorageAccount) {
Write-Information 'Storage account not found. We will create one now'
$storageAccountName = $ResourceGroup.ToLower() -replace '[^a-z0-9]',''
Expand Down Expand Up @@ -127,6 +127,7 @@ function Set-TableStorageEndpointsInScAndCmAppSettings {
}

if([string]::IsNullOrEmpty($storageAccountTableEndpoint)) {
Write-Warning "No storage account found. This is only expected if you upgrade from SCEPman 1.x"
Write-Information "Creating storage account"
if ($PSCmdlet.ShouldProcess($storageAccountTableEndpoint, "Create storage account")) {
$ScStorageAccount = CreateScStorageAccount -SubscriptionId $SubscriptionId -ResourceGroup $SCEPmanResourceGroup -servicePrincipals $servicePrincipals
Expand Down
42 changes: 38 additions & 4 deletions Tests/storage-account.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ BeforeAll {
. $PSScriptRoot/../SCEPman/Private/constants.ps1
. $PSScriptRoot/../SCEPman/Private/az-commands.ps1
. $PSScriptRoot/../SCEPman/Private/storage-account.ps1
. $PSScriptRoot/test-helpers.ps1
}

Describe 'Storage Account' {
Expand All @@ -27,10 +28,8 @@ Describe 'Storage Account' {
],
"skip_token": null,
"total_records": 1
}' } -ParameterFilter { $args[0] -eq 'graph' -and $args[1] -eq "query" }
Mock az {
throw "Unexpected parameter for az: $args (with array values $($args[0]), $($args[1]), ... -- #$($args.Count) in total)"
}
}' } -ParameterFilter { CheckAzParameters -argsFromCommand $args -azCommandPrefix 'graph query' }
EnsureNoAdditionalAzCalls
}

It 'Finds an existing Storage Account' {
Expand All @@ -52,6 +51,41 @@ Describe 'Storage Account' {
Assert-MockCalled az -Exactly 1 -Scope It
}

It 'Verification finds an existing Storage Account' {
# Arrange
mock Read-Host { return "stgxyztest" } -ParameterFilter { ($Prompt -join '').Contains("enter the name of the storage account") }

# Act
$staccount = VerifyStorageAccountDoesNotExist -dataTableEndpoint 'https://stgxyztest.table.core.windows.net/'

# Assert
$staccount.location | Should -Be "germanywestcentral"
$staccount.name | Should -Be "stgxyztest"
$staccount.primaryEndpoints.blob | Should -Be "https://stgxyztest.blob.core.windows.net/"
$staccount.primaryEndpoints.dfs | Should -Be "https://stgxyztest.dfs.core.windows.net/"
$staccount.primaryEndpoints.file | Should -Be "https://stgxyztest.file.core.windows.net/"
$staccount.primaryEndpoints.queue | Should -Be "https://stgxyztest.queue.core.windows.net/"
$staccount.primaryEndpoints.table | Should -Be "https://stgxyztest.table.core.windows.net/"
$staccount.primaryEndpoints.web | Should -Be "https://stgxyztest.z1.web.core.windows.net/"
$staccount.resourceGroup | Should -Be "rg-xyz-test"
$staccount.subscriptionId | Should -Be "63ee67fb-aad6-4711-82a9-ff838a489299"

Assert-MockCalled az -Exactly 1 -Scope It
}

It 'Verification allows creating a new Storage Account if the user wants it' {
# Arrange
mock Read-Host { return "" } -ParameterFilter { ($Prompt -join '').Contains("want to create a new storage account") }

# Act
$staccount = VerifyStorageAccountDoesNotExist -dataTableEndpoint 'https://stgxyztest.table.core.windows.net/'

# Assert
$staccount | Should -Be $null

Assert-MockCalled az -Exactly 1 -Scope It
}

Context "When SCEPman and CertMaster have a configured Storage Account" {
BeforeAll {
[System.Collections.IList]$script:servicePrincipals = @('12345678-aad6-4711-82a9-0123456789ab', '98765432-aad6-4711-82a9-9876543210ab')
Expand Down

0 comments on commit 81374dc

Please sign in to comment.