From fe53215ba8a491aac15c842566b849acbe743fd3 Mon Sep 17 00:00:00 2001 From: Richard Crutchfield Date: Tue, 13 Feb 2024 09:18:50 -0500 Subject: [PATCH] OPA executable path default value inconsistent between invoke scuba with or without configfilepath (#894) * Add defaults * Back out unintended change * Update test * Rename test * Update tests * Fix module name in test * Update function scope * Update mock in test * Rework ScubaDefault values --- PowerShell/ScubaGear/Modules/Orchestrator.psm1 | 3 +-- .../ScubaGear/Modules/ScubaConfig/ScubaConfig.psm1 | 9 ++++++++- .../Unit/PowerShell/Orchestrator/Invoke-Scuba.Tests.ps1 | 3 +++ .../PowerShell/Orchestrator/Invoke-ScubaConfig.Tests.ps1 | 2 ++ .../PowerShell/ScubaConfig/ScubaConfigJson.Tests.ps1 | 7 +++++++ .../ScubaConfig/ScubaConfigJsonMissingDefaults.Tests.ps1 | 5 +++++ .../ScubaConfig/ScubaConfigLoadConfig.Tests.ps1 | 4 ++++ 7 files changed, 30 insertions(+), 3 deletions(-) diff --git a/PowerShell/ScubaGear/Modules/Orchestrator.psm1 b/PowerShell/ScubaGear/Modules/Orchestrator.psm1 index 2aa32b4713..82d1787cd1 100644 --- a/PowerShell/ScubaGear/Modules/Orchestrator.psm1 +++ b/PowerShell/ScubaGear/Modules/Orchestrator.psm1 @@ -1,6 +1,5 @@ using module 'ScubaConfig\ScubaConfig.psm1' - function Invoke-SCuBA { <# .SYNOPSIS @@ -128,7 +127,7 @@ function Invoke-SCuBA { [Parameter(Mandatory = $false, ParameterSetName = 'Report')] [ValidateScript({Test-Path -PathType Container $_})] [string] - $OPAPath = (Join-Path -Path $env:USERPROFILE -ChildPath ".scubagear\Tools"), + $OPAPath = [ScubaConfig]::ScubaDefault('DefaultOPAPath'), [Parameter(Mandatory = $false, ParameterSetName = 'Configuration')] [Parameter(Mandatory = $false, ParameterSetName = 'Report')] diff --git a/PowerShell/ScubaGear/Modules/ScubaConfig/ScubaConfig.psm1 b/PowerShell/ScubaGear/Modules/ScubaConfig/ScubaConfig.psm1 index bbc4626622..907f2f54fe 100644 --- a/PowerShell/ScubaGear/Modules/ScubaConfig/ScubaConfig.psm1 +++ b/PowerShell/ScubaGear/Modules/ScubaConfig/ScubaConfig.psm1 @@ -17,6 +17,13 @@ class ScubaConfig { #> hidden static [ScubaConfig]$_Instance = [ScubaConfig]::new() hidden static [Boolean]$_IsLoaded = $false + hidden static [hashtable]$ScubaDefaults = @{ + DefaultOPAPath = (Join-Path -Path $env:USERPROFILE -ChildPath ".scubagear\Tools") + } + + static [object]ScubaDefault ([string]$Name){ + return [ScubaConfig]::ScubaDefaults[$Name] + } [Boolean]LoadConfig([System.IO.FileInfo]$Path){ if (-Not (Test-Path -PathType Leaf $Path)){ @@ -59,7 +66,7 @@ class ScubaConfig { } if (-Not $this.Configuration.OPAPath){ - $this.Configuration.OPAPath = (Join-Path -Path $PSScriptRoot -ChildPath "..\..\..\..") + $this.Configuration.OPAPath = [ScubaConfig]::ScubaDefault('DefaultOPAPath') } if (-Not $this.Configuration.LogIn){ diff --git a/PowerShell/ScubaGear/Testing/Unit/PowerShell/Orchestrator/Invoke-Scuba.Tests.ps1 b/PowerShell/ScubaGear/Testing/Unit/PowerShell/Orchestrator/Invoke-Scuba.Tests.ps1 index fcfb45ade8..1f75853c9a 100644 --- a/PowerShell/ScubaGear/Testing/Unit/PowerShell/Orchestrator/Invoke-Scuba.Tests.ps1 +++ b/PowerShell/ScubaGear/Testing/Unit/PowerShell/Orchestrator/Invoke-Scuba.Tests.ps1 @@ -19,6 +19,9 @@ InModuleScope Orchestrator { function Disconnect-SCuBATenant {} Mock -ModuleName Orchestrator Disconnect-SCuBATenant {} + function Get-ScubaDefault {throw 'this will be mocked'} + Mock -ModuleName Orchestrator Get-ScubaDefault {"."} + Mock -CommandName New-Item {} Mock -CommandName Copy-Item {} } diff --git a/PowerShell/ScubaGear/Testing/Unit/PowerShell/Orchestrator/Invoke-ScubaConfig.Tests.ps1 b/PowerShell/ScubaGear/Testing/Unit/PowerShell/Orchestrator/Invoke-ScubaConfig.Tests.ps1 index f4fb1e601f..8e32dcf3d5 100644 --- a/PowerShell/ScubaGear/Testing/Unit/PowerShell/Orchestrator/Invoke-ScubaConfig.Tests.ps1 +++ b/PowerShell/ScubaGear/Testing/Unit/PowerShell/Orchestrator/Invoke-ScubaConfig.Tests.ps1 @@ -35,6 +35,8 @@ InModuleScope Orchestrator { Mock -ModuleName Orchestrator Disconnect-SCuBATenant { $script:TestSplat.Add('DisconnectOnExit', $DisconnectOnExit) } + function Get-ScubaDefault {throw 'this will be mocked'} + Mock -ModuleName Orchestrator Get-ScubaDefault {"."} Mock -CommandName New-Item {} Mock -CommandName Copy-Item {} } diff --git a/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigJson.Tests.ps1 b/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigJson.Tests.ps1 index ddfa235b5a..75f217838a 100644 --- a/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigJson.Tests.ps1 +++ b/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigJson.Tests.ps1 @@ -2,6 +2,9 @@ using module '..\..\..\..\Modules\ScubaConfig\ScubaConfig.psm1' InModuleScope ScubaConfig { Describe -tag "Utils" -name 'ScubaConfigJson' { + BeforeAll { + [ScubaConfig]::ResetInstance() + } Context 'General case'{ It 'Get Instance without loading'{ $Config1 = [ScubaConfig]::GetInstance() @@ -16,10 +19,14 @@ InModuleScope ScubaConfig { } context 'JSON Configuration' { BeforeAll { + function Get-ScubaDefault {throw 'this will be mocked'} + Mock -ModuleName ScubaConfig Get-ScubaDefault {"."} [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'ScubaConfigTestFile')] $ScubaConfigTestFile = Join-Path -Path $PSScriptRoot -ChildPath config_test.json [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'Result')] $Result = [ScubaConfig]::GetInstance().LoadConfig($ScubaConfigTestFile) + function Get-ScubaDefault {throw 'this will be mocked'} + Mock -ModuleName ScubaConfig Get-ScubaDefault {"."} } It 'Load valid config file'{ $Result | Should -Be $true diff --git a/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigJsonMissingDefaults.Tests.ps1 b/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigJsonMissingDefaults.Tests.ps1 index f089b145f6..e54f4abe21 100644 --- a/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigJsonMissingDefaults.Tests.ps1 +++ b/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigJsonMissingDefaults.Tests.ps1 @@ -2,6 +2,9 @@ using module '..\..\..\..\Modules\ScubaConfig\ScubaConfig.psm1' InModuleScope ScubaConfig { Describe -tag "Utils" -name 'ScubaConfigMissingDefaults' { + BeforeAll { + [ScubaConfig]::ResetInstance() + } Context 'General case'{ It 'Get Instance without loading'{ $Config1 = [ScubaConfig]::GetInstance() @@ -16,6 +19,8 @@ InModuleScope ScubaConfig { } context 'JSON Configuration' { BeforeAll { + function Get-ScubaDefault {throw 'this will be mocked'} + Mock -ModuleName ScubaConfig Get-ScubaDefault {"."} [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'ScubaConfigTestFile')] $ScubaConfigTestFile = Join-Path -Path $PSScriptRoot -ChildPath config_test.json [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'Result')] diff --git a/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigLoadConfig.Tests.ps1 b/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigLoadConfig.Tests.ps1 index 7364d44ec8..499e18cfbd 100644 --- a/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigLoadConfig.Tests.ps1 +++ b/PowerShell/ScubaGear/Testing/Unit/PowerShell/ScubaConfig/ScubaConfigLoadConfig.Tests.ps1 @@ -3,6 +3,10 @@ using module '..\..\..\..\Modules\ScubaConfig\ScubaConfig.psm1' InModuleScope ScubaConfig { Describe -tag "Utils" -name 'ScubaConfigLoadConfig' { context 'Handling repeated LoadConfig invocations' { + BeforeAll { + function Get-ScubaDefault {throw 'this will be mocked'} + Mock -ModuleName ScubaConfig Get-ScubaDefault {"."} + } It 'Load valid config file followed by another'{ $cfg = [ScubaConfig]::GetInstance() # Load the first file and check the ProductNames value.