Skip to content

Commit

Permalink
Update FinOps hubs tests (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
flanakin authored Dec 16, 2023
1 parent a45b050 commit 8040b27
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 151 deletions.
16 changes: 8 additions & 8 deletions src/powershell/Public/Deploy-FinOpsHub.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,30 @@ function Deploy-FinOpsHub
$resourceGroupObject = Get-AzResourceGroup -Name $ResourceGroupName -ErrorAction 'SilentlyContinue'
if (-not $resourceGroupObject)
{
if ($PSCmdlet.ShouldProcess($ResourceGroupName, 'CreateResourceGroup'))
if (Test-ShouldProcess $PSCmdlet $ResourceGroupName 'CreateResourceGroup')
{
$resourceGroupObject = New-AzResourceGroup -Name $ResourceGroupName -Location $Location
}
}

$toolkitPath = Join-Path $env:temp -ChildPath 'FinOpsToolkit'
if ($PSCmdlet.ShouldProcess($toolkitPath, 'CreateTempDirectory'))
if (Test-ShouldProcess $PSCmdlet $toolkitPath 'CreateTempDirectory')
{
New-Directory -Path $toolkitPath
}
Initialize-FinOpsHubDeployment -WhatIf:$WhatIfPreference

if ($PSCmdlet.ShouldProcess($Version, 'DownloadTemplate'))
if (Test-ShouldProcess $PSCmdlet $Version 'DownloadTemplate')
{
Save-FinOpsHubTemplate -Version $Version -Preview:$Preview -Destination $toolkitPath
$toolkitFile = Get-ChildItem -Path $toolkitPath -Include 'main.bicep' -Recurse | Where-Object -FilterScript { $_.FullName -like '*finops-hub-v*' }
if (-not $toolkitFile)
$bicepFile = Get-ChildItem -Path $toolkitPath -Include 'main.bicep' -Recurse | Where-Object -FilterScript { $_.FullName -like '*finops-hub-v*' }
if (-not $bicepFile)
{
throw ($LocalizedData.Hub_Deploy_TemplateNotFound -f $toolkitPath)
}

$parameterSplat = @{
TemplateFile = $toolkitFile.FullName
TemplateFile = $bicepFile.FullName
TemplateParameterObject = @{
hubName = $Name
storageSku = $StorageSku
Expand All @@ -121,9 +121,9 @@ function Deploy-FinOpsHub
}
}

if ($PSCmdlet.ShouldProcess($ResourceGroupName, 'DeployFinOpsHub'))
if (Test-ShouldProcess $PSCmdlet $ResourceGroupName 'DeployFinOpsHub')
{
Write-Verbose -Message ($LocalizedData.Hub_Deploy_Deploy -f $toolkitFile.FullName, $resourceGroupObject.ResourceGroupName)
Write-Verbose -Message ($LocalizedData.Hub_Deploy_Deploy -f $bicepFile.FullName, $resourceGroupObject.ResourceGroupName)
return New-AzResourceGroupDeployment @parameterSplat -ResourceGroupName $resourceGroupObject.ResourceGroupName
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/powershell/Public/Initialize-FinOpsHubDeployment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ function Initialize-FinOpsHubDeployment
[CmdletBinding(SupportsShouldProcess)]
param()

Register-FinOpsHubProviders -WhatIf:$WhatIfPreference
Register-FinOpsHubProviders -WhatIf:$WhatIfPreference | Out-Null
}
182 changes: 115 additions & 67 deletions src/powershell/Tests/Integration/Hubs.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,133 @@
# Licensed under the MIT License.

& "$PSScriptRoot/../Initialize-Tests.ps1"
$global:ftk_ResourceGroup = "ftk-test-integration"

Describe 'Hubs' {
# TODO: Make this more robust
It 'Should upgrade from 0.0.1 hub instance' {
# Arrange
$requiredRPs = @('Microsoft.CostManagementExports', 'Microsoft.EventGrid')
$name = "ftk-test-DeployHub_$($env:USERNAME)"
$rg = "ftk-test-integration"
$location = "eastus" # must be lowercase with no spaces
$versions = Get-FinOpsToolkitVersion | Select-Object -ExpandProperty Version -Unique | Sort-Object { $_ }
BeforeDiscovery {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$requiredRPs = $global:ftk_InitializeTests_Hubs_RequiredRPs

# Unregister RPs before deploying
$requiredRPs | ForEach-Object {
Write-Host " Unregistering $_..." -NoNewline
try
{
Unregister-AzResourceProvider -ProviderNamespace $_
Write-Host 'in progress...' -NoNewline
foreach ($x in 1..5)
{
Start-Sleep -Seconds 2
$rp = Get-AzResourceProvider -ProviderNamespace $_
if ($rp.RegistrationState -eq 'Unregistered')
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$versions = @('0.0.1', '0.1', '0.1.1') | Sort-Object { [version]$_ }
}

BeforeAll {
# Must be duplicated because pre-discovery vars aren't accessible
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$requiredRPs = $global:ftk_InitializeTests_Hubs_RequiredRPs
}

Context 'Register-FinOpsHubProviders' {
It 'Should register all required providers' {
# Arrange
# Act
Register-FinOpsHubProviders

# Assert
$requiredRPs | ForEach-Object {
# TODO: It's possible this could take some time; if this tests fails, add a wait, accept 'Registering', or remove it
Get-AzResourceProvider -ProviderNamespace $_ `
| Select-Object -ExpandProperty RegistrationState -First 1 `
| Should -Be 'Registered'
}
}
}

Context 'Deploy-FinOpsHub' {
BeforeAll {
# Arrange
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$ftk_ResourceGroup = "ftk-test-integration"
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$name = "ftk-test-DeployHub_$($env:USERNAME)"
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$rg = "ftk-test-integration"
# TODO: Confirm lowercase/space requirement and add handling to avoid the limitation
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$location = "eastus" # must be lowercase with no spaces
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$versions = Get-FinOpsToolkitVersion | Select-Object -ExpandProperty Version -Unique | Sort-Object { $_ }
}

Context 'Unregister resource providers to verify auto-registration' {
It "Should unregister the <_> RP" -ForEach $requiredRPs {
$rp = $_
Monitor "Unregistering $rp..." -Indent ' ' {
Unregister-AzResourceProvider -ProviderNamespace $rp -ErrorAction SilentlyContinue
if (-not $?)
{
break
Report 'Cannot unregister' -Exception $Error[0].Exception
Set-ItResult -Inconclusive -Because "the '$rp' cannot be unregistered"
return
}
else
{
Report 'Waiting for unregistration to complete...'
foreach ($x in 1..5)
{
Start-Sleep -Seconds 2
$state = Get-AzResourceProvider -ProviderNamespace $rp
if ($state.RegistrationState -eq 'Unregistered')
{
Report 'Done'
return
}
}
Report 'Not finished after 10s (continuing anyway)'
Set-ItResult -Inconclusive -Because "the '$rp' did not finish unregistering within 10s"
}
}
if ($rp.RegistrationState -eq 'Unregistered')
{
Write-Host 'done'
}
else
{
Write-Host 'not done (continuing anyway)'
}
}
catch
{
Write-Host 'cannot unregister'
}
}

# Loop thru each version
$versions | ForEach-Object {
# Act
[Diagnostics.CodeAnalysis.SuppressMessageAttribute ('PSUseDeclaredVarsMoreThanAssignments', Scope = 'Function', Target = '*')]
$result = Deploy-FinOpsHub `
-Name $name `
-Location $location `
-ResourceGroupName $rg `
-Version $_
$hub = Get-FinOpsHub -Name $name -ResourceGroupName $rg
Context 'Deploy and upgrade' -Skip {
It 'Should deploy FinOps hubs <_>' -ForEach $versions {
$ver = $_

# Act
Monitor "FinOps hubs $ver" -Indent ' ' {
Monitor "Deploying..." {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute ('PSUseDeclaredVarsMoreThanAssignments', Scope = 'Function', Target = '*')]
$script:deployResult = Deploy-FinOpsHub `
-Name $name `
-Location $location `
-ResourceGroupName $rg `
-Version $_
Report -Object ($script:deployResult ?? '(null)')
}

# Assert RP status
$requiredRPs | ForEach-Object {
$rp = Get-AzResourceProvider -ProviderNamespace $_
$rp.RegistrationState | Should -BeIn 'Registered', 'Registering' -Because "RP '$_' should be registered" -inc
}
Monitor 'Getting instance...' {
$script:getResult = Get-FinOpsHub -Name $name -ResourceGroupName $rg
Report -Object ($script:getResult ?? '(null)')
}
}

# Assert hub state
@($hub).Count | Should -Be 1 -Because "there should only be one hub with name '$name' in resource group '$rg' (v$_)"
$hub.Location.ToLower() -replace ' ', '' | Should -Be $location -Because "hub should be in location '$location' (v$_)"
$hub.Version | Should -Be $_
$hub.Status | Should -Be 'Deployed' -Because "hub should be in 'Deployed' status (v$_)"
$hub.Status | Should -Not -Be 'Unknown' -Because "hub should not be in 'Unknown' status (v$_)"
$hub.Resources.ResourceType.ToLower() | Sort-Object `
| Should -Be @(
'microsoft.datafactory/factories',
'microsoft.keyvault/vaults',
(if ([version]$_ -ge [version]'0.1') { 'microsoft.managedidentity/userassignedidentities', 'microsoft.managedidentity/userassignedidentities' }),
'microsoft.storage/storageaccounts'
) -Because "hub should have expected resources (v$_)"
# Assert RP status
$requiredRPs | ForEach-Object {
Get-AzResourceProvider -ProviderNamespace $_ `
| Select-Object -ExpandProperty RegistrationState -First 1 `
| Should -BeIn 'Registered', 'Registering' -ErrorAction Continue -Because "RP '$_' should be registered"
}

# TODO: Test 'StorageOnly' status
# TODO: Test 0.0.1 'DeployedWithExtraResources' status = storage + DF + KV + 1???
# TODO: Test 0.1 'DeployedWithExtraResources' status = storage + DF + KV + 3+???
}
# Assert hub state
@($script:getResult).Count | Should -Be 1 -ErrorAction Continue -Because "there should only be one hub with name '$name' in resource group '$rg' (v$ver)"
$script:getResult.Location.ToLower() -replace ' ', '' | Should -Be $location -ErrorAction Continue -Because "hub should be in location '$location' (v$ver)"
$script:getResult.Version | Should -Be $ver -ErrorAction Continue
$script:getResult.Status | Should -Be 'Deployed' -ErrorAction Continue -Because "hub should be in 'Deployed' status (v$ver)"
$script:getResult.Status | Should -Not -Be 'Unknown' -ErrorAction Continue -Because "hub should not be in 'Unknown' status (v$ver)"
$script:getResult.Resources.ResourceType.ToLower() | Sort-Object `
| Should -Be @(
'microsoft.datafactory/factories',
'microsoft.keyvault/vaults',
(if ([version]$ver -ge [version]'0.1') { 'microsoft.managedidentity/userassignedidentities', 'microsoft.managedidentity/userassignedidentities' }),
'microsoft.storage/storageaccounts'
) -ErrorAction Continue -Because "hub should have expected resources (v$ver)"

# TODO: Deploy local version to verify upgrade still works

# TODO: Test 'StorageOnly' status
# TODO: Test 0.0.1 'DeployedWithExtraResources' status = storage + DF + KV + 1???
# TODO: Test 0.1 'DeployedWithExtraResources' status = storage + DF + KV + 3+???
}
}
}
}
Loading

0 comments on commit 8040b27

Please sign in to comment.