Skip to content

Commit

Permalink
Implement new pester test under Orchestrator section to tests these c…
Browse files Browse the repository at this point in the history
…hanges
  • Loading branch information
kennethpalmer committed Dec 29, 2023
1 parent 25df0e6 commit 4a23e63
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Testing/Unit/PowerShell/Orchestrator/Invoke-ScubaConfig.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
$OrchestratorPath = '../../../../PowerShell/ScubaGear/Modules/Orchestrator.psm1'
$ScubaConfigPath = '../../../../PowerShell/ScubaGear/Modules/ScubaConfig/ScubaConfig.psm1'
$TestUtilsPath = "../PsTestUtils.psm1"
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath $OrchestratorPath) -Function 'Invoke-SCuBA' -Force
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath $ScubaConfigPath)
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath $TestUtilsPath)

InModuleScope Orchestrator {
Describe -Tag 'Orchestrator' -Name 'Invoke-Scuba with Config' {
BeforeAll {
function Remove-Resources {}
Mock -ModuleName Orchestrator Remove-Resources {}
function Import-Resources {}
Mock -ModuleName Orchestrator Import-Resources {}
function Invoke-Connection {}
Mock -ModuleName Orchestrator Invoke-Connection { @() }
function Get-TenantDetail {}
Mock -ModuleName Orchestrator Get-TenantDetail { '{"DisplayName": "displayName"}' }
function Invoke-ProviderList {}
Mock -ModuleName Orchestrator Invoke-ProviderList {}
function Invoke-RunRego {}
Mock -ModuleName Orchestrator Invoke-RunRego {}

Mock -ModuleName Orchestrator Invoke-ReportCreation {}
function Disconnect-SCuBATenant {}
Mock -ModuleName Orchestrator Disconnect-SCuBATenant {}

Mock -CommandName New-Item {}
Mock -CommandName Copy-Item {}
}
Context 'Testing Invoke-Scuba with -ConfigFilePath arg and parameter override' {
BeforeAll {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'SplatParams')]
$ConfigFile = ( Join-Path -Path $PSScriptRoot -ChildPath "orchestrator_config_test.yaml" )
$SplatParams = @{
ConfigFilePath = $ConfigFile
M365Environment = 'gcc'
}
}
It 'Verify override: Compare-Hash should fail' {
{Invoke-Scuba @SplatParams} | Should -Not -Throw
$ScubaConfTest = [ScubaConfig]::GetInstance().Configuration.Clone()

[ScubaConfig]::GetInstance().LoadConfig($ConfigFile)
$ScubaConfRef = [ScubaConfig]::GetInstance().Configuration

$difference_test_pass = ( -Not ( Compare-Hashes $ScubaConfRef $ScubaConfTest ))
$difference_test_pass

}
It 'Verify modified config matches expected value: Compare-Hash should pass ' {
{Invoke-Scuba @SplatParams} | Should -Not -Throw
$ScubaConfTest = [ScubaConfig]::GetInstance().Configuration.Clone()

[ScubaConfig]::GetInstance().LoadConfig($ConfigFile)
$ScubaConfRef = [ScubaConfig]::GetInstance().Configuration

# Modifiy the expected value to reflect override
$ScubaConfRef.M365Enviroment = 'gcc'

$other_properties_pass = ( Compare-Hashes $ScubaConfRef $ScubaConfTest )
$other_properties_pass

}
}
}
}
AfterAll {
Remove-Module Orchestrator -ErrorAction SilentlyContinue
}
23 changes: 23 additions & 0 deletions Testing/Unit/PowerShell/Orchestrator/orchestrator_config_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Description: YAML Configuration file for unit test of ConfigFilePath overrides
ProductNames:
- teams
- exo
- defender
- aad
- sharepoint
M365Environment: commercial
OPAPath: .
LogIn: true
DisconnectOnExit: false
OutPath: .
OutFolderName: M365BaselineConformance
OutProviderFileName: ProviderSettingsExport
OutRegoFileName: TestResults
OutReportName: BaselineReports
AnObject:
name: MyObjectName

# Dummy parameters for credentials
Organization: sub.domain.com
AppID: 7892dfe467aef9023bead7812fe56763
CertificateThumbprint: 8A673F1087453ABC89423B2DA9387CEFA9BE3452
55 changes: 55 additions & 0 deletions Testing/Unit/PowerShell/PsTestUtils.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# $hash1 = @{
# "color" = "blue"
# "city" = "boston"
# "team" = "redsox"
# }

# $hash2 = $hash1.clone()

# $hash3 = $hash1.clone()

# $hash2.color = "red"

# $hash1keys=$hash1.keys

# foreach ($h in $hash1keys ) {
# Write-Host "${h}: $($hash1.$h)"
# }
function Compare-Hashes{
param (
[hashtable] $hash1,
[hashtable] $hash2
)

$compare = $true

$arr1 = @()
$arr2 = @()

foreach ($key in $hash1.keys){ $arr1 += $key }
foreach ($key in $hash2.keys){ $arr2 += $key }

foreach ($key in $arr1) {
if ( -Not ( $hash2.ContainsKey($key ))) {
Write-Output("1st hash key {0} not in 2nd hash" -f $key )
$compare = $false
}
}
foreach ($key in $arr2) {
if ( -Not ( $hash1.ContainsKey($key ))) {
Write-Output("2nd hash key {0} not in 1st hash" -f $key )
$compare = $false
}
}
if ($compare) {
foreach ($key in $arr1 )
{
if ( -Not ( $hash1.$key -eq $hash2.$key )) {
Write-Output("key {0} values differ {1} and {2}" -f $key, $hash1.$key, $hash2.$key )
$compare = $false
}
}
}
$compare
}

55 changes: 55 additions & 0 deletions Testing/Unit/PowerShell/PsTestUtils.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# $hash1 = @{
# "color" = "blue"
# "city" = "boston"
# "team" = "redsox"
# }

# $hash2 = $hash1.clone()

# $hash3 = $hash1.clone()

# $hash2.color = "red"

# $hash1keys=$hash1.keys

# foreach ($h in $hash1keys ) {
# Write-Host "${h}: $($hash1.$h)"
# }
function Compare-Hashes{
param (
[hashtable] $hash1,
[hashtable] $hash2
)

$compare = $true

$arr1 = @()
$arr2 = @()

foreach ($key in $hash1.keys){ $arr1 += $key }
foreach ($key in $hash2.keys){ $arr2 += $key }

foreach ($key in $arr1) {
if ( -Not ( $hash2.ContainsKey($key ))) {
Write-Output("1st hash key {0} not in 2nd hash" -f $key )
$compare = $false
}
}
foreach ($key in $arr2) {
if ( -Not ( $hash1.ContainsKey($key ))) {
Write-Output("2nd hash key {0} not in 1st hash" -f $key )
$compare = $false
}
}
if ($compare) {
foreach ($key in $arr1 )
{
if ( -Not ( $hash1.$key -eq $hash2.$key )) {
Write-Output("key {0} values differ {1} and {2}" -f $key, $hash1.$key, $hash2.$key )
$compare = $false
}
}
}
$compare
}

0 comments on commit 4a23e63

Please sign in to comment.