|
| 1 | +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] |
| 2 | +param () |
| 3 | + |
| 4 | +BeforeDiscovery { |
| 5 | + try |
| 6 | + { |
| 7 | + if (-not (Get-Module -Name 'DscResource.Test')) |
| 8 | + { |
| 9 | + # Assumes dependencies has been resolved, so if this module is not available, run 'noop' task. |
| 10 | + if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) |
| 11 | + { |
| 12 | + # Redirect all streams to $null, except the error stream (stream 2) |
| 13 | + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null |
| 14 | + } |
| 15 | + |
| 16 | + # If the dependencies has not been resolved, this will throw an error. |
| 17 | + Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' |
| 18 | + } |
| 19 | + } |
| 20 | + catch [System.IO.FileNotFoundException] |
| 21 | + { |
| 22 | + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.' |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +BeforeAll { |
| 27 | + $script:dscModuleName = 'SqlServerDsc' |
| 28 | + |
| 29 | + $env:SqlServerDscCI = $true |
| 30 | + |
| 31 | + Import-Module -Name $script:dscModuleName |
| 32 | + |
| 33 | + # Loading mocked classes |
| 34 | + Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../Stubs') -ChildPath 'SMO.cs') |
| 35 | + |
| 36 | + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName |
| 37 | + $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName |
| 38 | + $PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName |
| 39 | +} |
| 40 | + |
| 41 | +AfterAll { |
| 42 | + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') |
| 43 | + $PSDefaultParameterValues.Remove('Mock:ModuleName') |
| 44 | + $PSDefaultParameterValues.Remove('Should:ModuleName') |
| 45 | + |
| 46 | + # Unload the module being tested so that it doesn't impact any other tests. |
| 47 | + Get-Module -Name $script:dscModuleName -All | Remove-Module -Force |
| 48 | + |
| 49 | + Remove-Item -Path 'env:SqlServerDscCI' |
| 50 | +} |
| 51 | + |
| 52 | +Describe 'Get-SqlDscConfigurationOption' -Tag 'Public' { |
| 53 | + It 'Should have the correct parameters in parameter set <MockParameterSetName>' -ForEach @( |
| 54 | + @{ |
| 55 | + MockParameterSetName = '__AllParameterSets' |
| 56 | + MockExpectedParameters = '[-ServerObject] <Server> [[-Name] <string>] [-Refresh] [<CommonParameters>]' |
| 57 | + } |
| 58 | + ) { |
| 59 | + $result = (Get-Command -Name 'Get-SqlDscConfigurationOption').ParameterSets | |
| 60 | + Where-Object -FilterScript { |
| 61 | + $_.Name -eq $mockParameterSetName |
| 62 | + } | |
| 63 | + Select-Object -Property @( |
| 64 | + @{ |
| 65 | + Name = 'ParameterSetName' |
| 66 | + Expression = { $_.Name } |
| 67 | + }, |
| 68 | + @{ |
| 69 | + Name = 'ParameterListAsString' |
| 70 | + Expression = { $_.ToString() } |
| 71 | + } |
| 72 | + ) |
| 73 | + |
| 74 | + $result.ParameterSetName | Should -Be $MockParameterSetName |
| 75 | + $result.ParameterListAsString | Should -Be $MockExpectedParameters |
| 76 | + } |
| 77 | + |
| 78 | + Context 'When the specified configuration option exist' { |
| 79 | + BeforeAll { |
| 80 | + $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | |
| 81 | + Add-Member -MemberType 'ScriptProperty' -Name 'Configuration' -Value { |
| 82 | + return @{ |
| 83 | + Properties = @() |
| 84 | + } |
| 85 | + } -PassThru -Force |
| 86 | + |
| 87 | + $mockDefaultParameters = @{ |
| 88 | + ServerObject = $mockServerObject |
| 89 | + Name = 'Unknown Option Name' |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Context 'When specifying to throw on error' { |
| 94 | + BeforeAll { |
| 95 | + $mockErrorMessage = InModuleScope -ScriptBlock { |
| 96 | + $script:localizedData.ConfigurationOption_Get_Missing |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + It 'Should throw the correct error' { |
| 101 | + { Get-SqlDscConfigurationOption @mockDefaultParameters -ErrorAction 'Stop' } | |
| 102 | + Should -Throw -ExpectedMessage ($mockErrorMessage -f 'Unknown Option Name') |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Context 'When ignoring the error' { |
| 107 | + It 'Should not throw an exception and return $null' { |
| 108 | + Get-SqlDscConfigurationOption @mockDefaultParameters -ErrorAction 'SilentlyContinue' | |
| 109 | + Should -BeNullOrEmpty |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + Context 'When getting a specific configuration option' { |
| 115 | + BeforeAll { |
| 116 | + $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | |
| 117 | + Add-Member -MemberType 'ScriptProperty' -Name 'Configuration' -Value { |
| 118 | + $configOption1 = [Microsoft.SqlServer.Management.Smo.ConfigProperty]::CreateTypeInstance() |
| 119 | + $configOption1.DisplayName = 'blocked process threshold (s)' |
| 120 | + |
| 121 | + return @{ |
| 122 | + Properties = @($configOption1) |
| 123 | + } |
| 124 | + } -PassThru -Force |
| 125 | + } |
| 126 | + |
| 127 | + It 'Should return the correct values' { |
| 128 | + $mockDefaultParameters = @{ |
| 129 | + ServerObject = $mockServerObject |
| 130 | + Name = 'blocked process threshold (s)' |
| 131 | + } |
| 132 | + |
| 133 | + $result = Get-SqlDscConfigurationOption @mockDefaultParameters |
| 134 | + |
| 135 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty' |
| 136 | + |
| 137 | + $result.DisplayName | Should -Be 'blocked process threshold (s)' |
| 138 | + } |
| 139 | + |
| 140 | + Context 'When passing parameter ServerObject over the pipeline' { |
| 141 | + It 'Should return the correct values' { |
| 142 | + $result = $mockServerObject | Get-SqlDscConfigurationOption -Name 'blocked process threshold (s)' |
| 143 | + |
| 144 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty' |
| 145 | + |
| 146 | + $result.DisplayName | Should -Be 'blocked process threshold (s)' |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + Context 'When getting all available configuration options' { |
| 152 | + BeforeAll { |
| 153 | + $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | |
| 154 | + Add-Member -MemberType 'ScriptProperty' -Name 'Configuration' -Value { |
| 155 | + $configOption1 = [Microsoft.SqlServer.Management.Smo.ConfigProperty]::CreateTypeInstance() |
| 156 | + $configOption1.DisplayName = 'blocked process threshold (s)' |
| 157 | + |
| 158 | + $configOption2 = [Microsoft.SqlServer.Management.Smo.ConfigProperty]::CreateTypeInstance() |
| 159 | + $configOption2.DisplayName = 'show advanced options' |
| 160 | + |
| 161 | + return @{ |
| 162 | + Properties = @($configOption1, $configOption2) |
| 163 | + } |
| 164 | + } -PassThru -Force |
| 165 | + } |
| 166 | + |
| 167 | + It 'Should return the correct values' { |
| 168 | + $result = Get-SqlDscConfigurationOption -ServerObject $mockServerObject |
| 169 | + |
| 170 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty' |
| 171 | + |
| 172 | + $result.DisplayName | Should -Contain 'show advanced options' |
| 173 | + $result.DisplayName | Should -Contain 'blocked process threshold (s)' |
| 174 | + } |
| 175 | + |
| 176 | + Context 'When passing parameter ServerObject over the pipeline' { |
| 177 | + It 'Should return the correct values' { |
| 178 | + $result = $mockServerObject | Get-SqlDscConfigurationOption |
| 179 | + |
| 180 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty' |
| 181 | + |
| 182 | + $result.DisplayName | Should -Contain 'show advanced options' |
| 183 | + $result.DisplayName | Should -Contain 'blocked process threshold (s)' |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments