ps/Modules/Alkami.PowerShell.Configuration/Public/Get-HelmDeploymentMetadata.tests.ps1
2023-05-30 22:51:22 -07:00

92 lines
4.0 KiB
PowerShell

. $PSScriptRoot\..\..\Load-PesterModules.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.'
$functionPath = Join-Path -Path $here -ChildPath $sut
Write-Host "Overriding SUT: $functionPath"
Import-Module $functionPath -Force
$moduleForMock = ""
Describe "Get-HelmDeploymentMetadata" {
# Mock Helm Yaml values file with some globals and three services defined.
$global:testHelmValuesFileContent = @'
# Globals
global:
someBoolean: true
# Config Map
configMap:
environmentType: Development
alk-svc-rpc-iaccountservicecontract-v2:
serviceName: "alk-svc-rpc-iaccountservicecontract-v2"
image: alkami.microservices.iaccountservicecontract.netcore
tag: 1.0.0
alk-svc-rpc-isettingsservicecontract-v4:
serviceName: "alk-svc-rpc-isettingsservicecontract-v4"
image: alkami.microservices.isettingsservicecontract.netcore
tag: 2.0.0
alk-svc-rpc-itransactionservicecontract-v1:
serviceName: "alk-svc-rpc-isettingsservicecontract-v1"
image: alkami.microservices.itransactionservicecontract.netcore
tag: 3.0.0
firstVariable:
secondVariable: testValue
'@
Context "Get-HelmDeploymentMetadata Contains Expected Data" {
Mock -ModuleName $moduleForMock -CommandName Test-Path -MockWith { return $true }
Mock -ModuleName $moduleForMock -CommandName Get-Content -MockWith { return $testHelmValuesFileContent }
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
$result = Get-HelmDeploymentMetadata -RepoPath "C:/does/not/matter/its/mocked"
It "should contain all expected services" {
$result.count | Should -Be 3
$result | Where-Object { $_.serviceName -eq "alk-svc-rpc-iaccountservicecontract-v2" } | Should -Not -BeNullOrEmpty
$result | Where-Object { $_.serviceName -eq "alk-svc-rpc-isettingsservicecontract-v4" } | Should -Not -BeNullOrEmpty
$result | Where-Object { $_.serviceName -eq "alk-svc-rpc-isettingsservicecontract-v1" } | Should -Not -BeNullOrEmpty
}
It "should not contain globals or configmaps" {
$result | Where-Object { $_.someBoolean -eq $true } | Should -BeNull
$result | Where-Object { $_.environmentType -eq "Development" } | Should -BeNull
}
It "contains arbitrary multi-level data" {
$settingsService = $result | Where-Object { $_.serviceName -eq "alk-svc-rpc-isettingsservicecontract-v1" }
$settingsService | Should -Not -BeNullOrEmpty
$settingsService.firstVariable.secondVariable | Should -Be "testValue"
}
It "did not write an error" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Times 0 -Scope Context
}
}
Context "Get-HelmDeploymentMetadata Expected Failures" {
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
It "should fail if the repository path is bad" {
Mock -ModuleName $moduleForMock -CommandName Test-Path -ParameterFilter { $Path -eq "./BadRepoPath" } -MockWith { return $false }
$result = Get-HelmDeploymentMetadata -RepoPath "./BadRepoPath"
$result | Should -BeNullOrEmpty
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Times 1 -Scope Context
}
It "should fail if the values file is missing" {
Mock -ModuleName $moduleForMock -CommandName Test-Path -ParameterFilter { $Path -eq "./GoodRepositoryPath" } -MockWith { return $true }
Mock -ModuleName $moduleForMock -CommandName Test-Path -ParameterFilter { $Path -eq "./GoodRepositoryPath/alkami-services/values.yaml" } -MockWith { return $false }
$result = Get-HelmDeploymentMetadata -RepoPath "./GoodRepositoryPath"
$result | Should -BeNullOrEmpty
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Times 1 -Scope Context
}
}
}