From b6eb944be6fb7f33b4adc6b36a8c6edf51a87e73 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Mon, 30 Aug 2021 10:41:08 +0200 Subject: [PATCH] Fix SqlRS and unit test --- CHANGELOG.md | 7 +- source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 | 4 +- .../DSC_SqlRS/en-US/DSC_SqlRS.strings.psd1 | 1 + tests/Unit/DSC_SqlRS.Tests.ps1 | 125 +++++++++++++++++- 4 files changed, 129 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9060009cd..cd4eea301 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,9 +63,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - SqlSetup - Fixed integration tests for SQL Server 2016 and SQL Server 2017. - SqlServerDsc.Common - - Fixed so that _CredScan_ no longer reports a password false-positive ([issue #1712](https://github.com/dsccommunity/SqlServerDsc/issues/1712)). + - Fixed so that _CredScan_ no longer reports a password false-positive + ([issue #1712](https://github.com/dsccommunity/SqlServerDsc/issues/1712)). - SqlRS - - Fixed SSRS 2019 initialisation [issue #1509](https://github.com/dsccommunity/SqlServerDsc/issues/1509). + - Fixed SSRS 2019 initialization ([issue #1509](https://github.com/dsccommunity/SqlServerDsc/issues/1509)). + - Fix a problem that did not correctly evaluate the `UseSSL` property against + the current state. ## [15.1.1] - 2021-02-12 diff --git a/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 b/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 index d0744df85..3eab32b9c 100644 --- a/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 +++ b/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 @@ -473,7 +473,7 @@ function Set-TargetResource #> if ($reportingServicesData.SqlVersion -ge 15) { - Write-Verbose -Message $script:localizedData.Restart + Write-Verbose -Message $script:localizedData.RestartToFinishInitialization Restart-ReportingServicesService -InstanceName $InstanceName -WaitTime 30 @@ -503,7 +503,7 @@ function Set-TargetResource Invoke-RsCimMethod @invokeRsCimMethodParameters } - if ( $PSBoundParameters.ContainsKey('UseSsl') -and $UseSsl -ne $currentConfig.UseSsl ) + if ( $PSBoundParameters.ContainsKey('UseSsl') -and $UseSsl -ne $reportingServicesData.Configuration.SecureConnectionLevel ) { Write-Verbose -Message "Changing value for using SSL to '$UseSsl'." diff --git a/source/DSCResources/DSC_SqlRS/en-US/DSC_SqlRS.strings.psd1 b/source/DSCResources/DSC_SqlRS/en-US/DSC_SqlRS.strings.psd1 index 2d0d26a5e..07dd9a79e 100644 --- a/source/DSCResources/DSC_SqlRS/en-US/DSC_SqlRS.strings.psd1 +++ b/source/DSCResources/DSC_SqlRS/en-US/DSC_SqlRS.strings.psd1 @@ -4,4 +4,5 @@ ConvertFrom-StringData @' TestFailedAfterSet = Test-TargetResource function returned false when Set-TargetResource function verified the desired state. This indicates that the Set-TargetResource did not correctly set set the desired state, or that the function Test-TargetResource does not correctly evaluate the desired state. ReportingServicesNotFound = SQL Reporting Services instance '{0}' does not exist. GetConfiguration = Get the current reporting services configuration for the instance '{0}'. + RestartToFinishInitialization = Restarting Reporting Services to finish initialization. '@ diff --git a/tests/Unit/DSC_SqlRS.Tests.ps1 b/tests/Unit/DSC_SqlRS.Tests.ps1 index f31ac4bcf..5392c8cf5 100644 --- a/tests/Unit/DSC_SqlRS.Tests.ps1 +++ b/tests/Unit/DSC_SqlRS.Tests.ps1 @@ -333,7 +333,8 @@ try Version = 15 } ) - foreach($sqlVersion in $sqlVersions) + + foreach ($sqlVersion in $sqlVersions) { Context "When the system is not in the desired state ($($sqlVersion.VersionName))" { Context "When configuring a named instance that are not initialized ($($sqlVersion.VersionName))" { @@ -411,7 +412,15 @@ try Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 1 -Scope It Assert-MockCalled -CommandName Invoke-Sqlcmd -Exactly -Times 2 -Scope It - Assert-MockCalled -CommandName Restart-ReportingServicesService -Exactly -Times 1 -Scope It + + if ($sqlVersion.Version -eq 15) + { + Assert-MockCalled -CommandName Restart-ReportingServicesService -Exactly -Times 2 -Scope It + } + else + { + Assert-MockCalled -CommandName Restart-ReportingServicesService -Exactly -Times 1 -Scope It + } } Context 'When there is no Reporting Services instance after Set-TargetResource has been called' { @@ -437,6 +446,107 @@ try } } + if ($sqlVersion.Version -eq 15) + { + Context "When configuring a named instance of $($sqlVersion.VersionName) that are not initialized" { + BeforeAll { + $script:alreadyCalledGetReportingServicesData = $false + $script:mockDynamicIsInitialized = $false + $script:mockDynamicSecureConnectionLevel = $false + + Mock -CommandName Get-ReportingServicesData -MockWith { + if ($script:alreadyCalledGetReportingServicesData) + { + $script:mockDynamicIsInitialized = $true + } + else + { + $script:alreadyCalledGetReportingServicesData = $true + } + + return @{ + Configuration = New-Object -TypeName Microsoft.Management.Infrastructure.CimInstance -ArgumentList @( + 'MSReportServer_ConfigurationSetting' + 'root/Microsoft/SQLServer/ReportServer/RS_SQL2019/v15/Admin' + ) | Add-Member -MemberType NoteProperty -Name 'DatabaseServerName' -Value "$mockReportingServicesDatabaseServerName\$mockReportingServicesDatabaseNamedInstanceName" -PassThru | + Add-Member -MemberType NoteProperty -Name 'IsInitialized' -Value $script:mockDynamicIsInitialized -PassThru | + Add-Member -MemberType NoteProperty -Name 'InstanceName' -Value $mockNamedInstanceName -PassThru | + Add-Member -MemberType NoteProperty -Name 'VirtualDirectoryReportServer' -Value $mockVirtualDirectoryReportServerName -PassThru | + Add-Member -MemberType NoteProperty -Name 'VirtualDirectoryReportManager' -Value $mockVirtualDirectoryReportManagerName -PassThru | + Add-Member -MemberType NoteProperty -Name 'SecureConnectionLevel' -Value $script:mockDynamicSecureConnectionLevel -PassThru -Force + ReportsApplicationName = 'ReportServerWebApp' + SqlVersion = $sqlVersion.Version + } + } + + Mock -CommandName Test-TargetResource -MockWith { + return $true + } + + $defaultParameters = @{ + InstanceName = $mockNamedInstanceName + DatabaseServerName = $mockReportingServicesDatabaseServerName + DatabaseInstanceName = $mockReportingServicesDatabaseNamedInstanceName + UseSsl = $false + } + } + + BeforeEach { + Mock -CommandName Get-CimInstance ` + -MockWith $mockGetCimInstance_Language ` + -ParameterFilter $mockGetCimInstance_OperatingSystem_ParameterFilter + } + + It 'Should configure Reporting Service without throwing an error' { + { Set-TargetResource @defaultParameters } | Should -Not -Throw + + Assert-MockCalled -CommandName Invoke-RsCimMethod -ParameterFilter { + $MethodName -eq 'SetSecureConnectionLevel' + } -Exactly -Times 0 -Scope It + + Assert-MockCalled -CommandName Invoke-RsCimMethod -ParameterFilter { + $MethodName -eq 'RemoveURL' + } -Exactly -Times 0 -Scope It + + Assert-MockCalled -CommandName Invoke-RsCimMethod -ParameterFilter { + $MethodName -eq 'InitializeReportServer' + } -Exactly -Times 0 -Scope It + + Assert-MockCalled -CommandName Invoke-RsCimMethod -ParameterFilter { + $MethodName -eq 'SetDatabaseConnection' + } -Exactly -Times 1 -Scope It + + Assert-MockCalled -CommandName Invoke-RsCimMethod -ParameterFilter { + $MethodName -eq 'GenerateDatabaseRightsScript' + } -Exactly -Times 1 -Scope It + + Assert-MockCalled -CommandName Invoke-RsCimMethod -ParameterFilter { + $MethodName -eq 'GenerateDatabaseCreationScript' + } -Exactly -Times 1 -Scope It + + Assert-MockCalled -CommandName Invoke-RsCimMethod -ParameterFilter { + $MethodName -eq 'SetVirtualDirectory' -and $Arguments.Application -eq $mockReportServerApplicationName + } -Exactly -Times 1 -Scope It + + Assert-MockCalled -CommandName Invoke-RsCimMethod -ParameterFilter { + $MethodName -eq 'SetVirtualDirectory' -and $Arguments.Application -eq $mockReportsApplicationName + } -Exactly -Times 1 -Scope It + + Assert-MockCalled -CommandName Invoke-RsCimMethod -ParameterFilter { + $MethodName -eq 'ReserveUrl' -and $Arguments.Application -eq $mockReportServerApplicationName + } -Exactly -Times 1 -Scope It + + Assert-MockCalled -CommandName Invoke-RsCimMethod -ParameterFilter { + $MethodName -eq 'ReserveUrl' -and $Arguments.Application -eq $mockReportsApplicationName + } -Exactly -Times 1 -Scope It + + Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 1 -Scope It + Assert-MockCalled -CommandName Invoke-Sqlcmd -Exactly -Times 2 -Scope It + Assert-MockCalled -CommandName Restart-ReportingServicesService -Exactly -Times 1 -Scope It + } + } + } + Context "When configuring a named instance that are already initialized ($($sqlVersion.VersionName))" { BeforeAll { $mockDynamicIsInitialized = $true @@ -697,8 +807,15 @@ try Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 1 -Scope It Assert-MockCalled -CommandName Invoke-Sqlcmd -Exactly -Times 2 -Scope It - Assert-MockCalled -CommandName Restart-ReportingServicesService -Exactly -Times 1 -Scope It - } + + if ($sqlVersion.Version -eq 15) + { + Assert-MockCalled -CommandName Restart-ReportingServicesService -Exactly -Times 2 -Scope It + } + else + { + Assert-MockCalled -CommandName Restart-ReportingServicesService -Exactly -Times 1 -Scope It + } } } } }