Skip to content

Commit 8055ded

Browse files
authored
SqlRS: Validate Configuration.ServiceName for SQL Server 14+ (#2340)
1 parent e9268cc commit 8055ded

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
127127
- `SqlRS`
128128
- Obtain the Reporting service name from WMI for version 14 and higher.
129129
[issue #2313](https://github.com/dsccommunity/SqlServerDsc/issues/2313)
130+
- Added validation to ensure Configuration.ServiceName is not null or empty
131+
for SQL Server version 14 and higher [issue #2342](https://github.com/dsccommunity/SqlServerDsc/issues/2342).
130132
- `Repair-SqlDscServer`
131133
- Removed the `Features` parameter from the command as SQL Server Repair action
132134
does not accept the `/FEATURES` parameter. SQL Server automatically repairs

source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,14 @@ function Set-TargetResource
296296
}
297297

298298
$reportingServicesServiceName = $reportingServicesData.Configuration.ServiceName
299+
300+
if ( [System.String]::IsNullOrEmpty($reportingServicesServiceName) )
301+
{
302+
$errorMessage = $script:localizedData.ServiceNameIsNullOrEmpty -f $InstanceName
303+
304+
New-InvalidOperationException -Message $errorMessage
305+
}
306+
299307
$reportingServicesDatabaseName = 'ReportServer'
300308
}
301309
elseif ( $InstanceName -eq 'MSSQLSERVER' )

source/DSCResources/DSC_SqlRS/en-US/DSC_SqlRS.strings.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ ConvertFrom-StringData @'
66
GetConfiguration = Get the current reporting services configuration for the instance '{0}'.
77
RestartToFinishInitialization = Restarting Reporting Services to finish initialization.
88
WaitingForServiceReady = Waiting {0} seconds for Reporting Services to be fully ready after restart. (DSC_SQLRS0001)
9+
ServiceNameIsNullOrEmpty = The Configuration.ServiceName property is null or empty for SQL Server Reporting Services instance '{0}'. This property is required to determine the service name for SQL Server version 14 and higher. (DSC_SQLRS0002)
910
'@

tests/Unit/DSC_SqlRS.Tests.ps1

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,80 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' {
693693
}
694694
}
695695
}
696+
697+
Context 'When Configuration.ServiceName is null or empty for SQL Server version 14 and higher' {
698+
BeforeAll {
699+
$mockDynamicIsInitialized = $false
700+
701+
$mockGetCimInstance_ConfigurationSetting_ServiceNameNull = {
702+
return @(
703+
(
704+
New-Object -TypeName Microsoft.Management.Infrastructure.CimInstance -ArgumentList @(
705+
'MSReportServer_ConfigurationSetting'
706+
'root/Microsoft/SQLServer/ReportServer/RS_SQL2016/v14/Admin'
707+
) | Add-Member -MemberType NoteProperty -Name 'DatabaseServerName' -Value "$mockReportingServicesDatabaseServerName\$mockReportingServicesDatabaseNamedInstanceName" -PassThru |
708+
Add-Member -MemberType NoteProperty -Name 'IsInitialized' -Value $false -PassThru |
709+
Add-Member -MemberType NoteProperty -Name 'InstanceName' -Value $mockNamedInstanceName -PassThru |
710+
Add-Member -MemberType NoteProperty -Name 'VirtualDirectoryReportServer' -Value '' -PassThru |
711+
Add-Member -MemberType NoteProperty -Name 'VirtualDirectoryReportManager' -Value '' -PassThru |
712+
Add-Member -MemberType NoteProperty -Name 'SecureConnectionLevel' -Value $mockDynamicSecureConnectionLevel -PassThru -Force |
713+
Add-Member -MemberType NoteProperty -Name 'ServiceName' -Value $null -PassThru -Force
714+
)
715+
)
716+
}
717+
718+
Mock -CommandName Get-ReportingServicesData -MockWith {
719+
return @{
720+
Configuration = (& $mockGetCimInstance_ConfigurationSetting_ServiceNameNull)[0]
721+
ReportsApplicationName = 'ReportServerWebApp'
722+
SqlVersion = 14
723+
}
724+
}
725+
726+
Mock -CommandName Get-CimInstance `
727+
-MockWith $mockGetCimInstance_Language `
728+
-ParameterFilter $mockGetCimInstance_OperatingSystem_ParameterFilter
729+
}
730+
731+
It 'Should throw the correct error message when ServiceName is null' {
732+
InModuleScope -ScriptBlock {
733+
Set-StrictMode -Version 1.0
734+
735+
$mockDefaultParameters = @{
736+
InstanceName = $mockNamedInstanceName
737+
DatabaseServerName = $mockReportingServicesDatabaseServerName
738+
DatabaseInstanceName = $mockReportingServicesDatabaseNamedInstanceName
739+
}
740+
741+
{ Set-TargetResource @mockDefaultParameters } | Should -Throw -ExpectedMessage ('*' + ($script:localizedData.ServiceNameIsNullOrEmpty -f $mockNamedInstanceName))
742+
}
743+
}
744+
745+
It 'Should throw the correct error message when ServiceName is empty' {
746+
Mock -CommandName Get-ReportingServicesData -MockWith {
747+
return @{
748+
Configuration = New-Object -TypeName Microsoft.Management.Infrastructure.CimInstance -ArgumentList @(
749+
'MSReportServer_ConfigurationSetting'
750+
'root/Microsoft/SQLServer/ReportServer/RS_SQL2016/v14/Admin'
751+
) | Add-Member -MemberType NoteProperty -Name 'ServiceName' -Value '' -PassThru -Force
752+
ReportsApplicationName = 'ReportServerWebApp'
753+
SqlVersion = 14
754+
}
755+
}
756+
757+
InModuleScope -ScriptBlock {
758+
Set-StrictMode -Version 1.0
759+
760+
$mockDefaultParameters = @{
761+
InstanceName = $mockNamedInstanceName
762+
DatabaseServerName = $mockReportingServicesDatabaseServerName
763+
DatabaseInstanceName = $mockReportingServicesDatabaseNamedInstanceName
764+
}
765+
766+
{ Set-TargetResource @mockDefaultParameters } | Should -Throw -ExpectedMessage ('*' + ($script:localizedData.ServiceNameIsNullOrEmpty -f $mockNamedInstanceName))
767+
}
768+
}
769+
}
696770
}
697771

698772
Context "When configuring a named instance that are already initialized (<TestCaseVersionName>)" {

0 commit comments

Comments
 (0)