From 81374dc33eb0ce9d34a0bd6bc1663b82a1d601f3 Mon Sep 17 00:00:00 2001 From: Christoph Hannebauer Date: Thu, 7 Nov 2024 14:39:29 +0100 Subject: [PATCH] Storage account refactor rename and test --- SCEPman/Private/storage-account.ps1 | 7 ++--- Tests/storage-account.Tests.ps1 | 42 ++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/SCEPman/Private/storage-account.ps1 b/SCEPman/Private/storage-account.ps1 index e117edd..f0a4f14 100644 --- a/SCEPman/Private/storage-account.ps1 +++ b/SCEPman/Private/storage-account.ps1 @@ -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" @@ -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 } } @@ -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]','' @@ -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 diff --git a/Tests/storage-account.Tests.ps1 b/Tests/storage-account.Tests.ps1 index 3631149..c631e11 100644 --- a/Tests/storage-account.Tests.ps1 +++ b/Tests/storage-account.Tests.ps1 @@ -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' { @@ -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' { @@ -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')