Skip to content

Commit a3d9ab3

Browse files
authored
Test-SqlDscRSInstalled: Add command (#2079)
- Public commands: - `Test-SqlDscRSInstalled` to test whether an instance is installed or not (issue #2078).
1 parent a8befef commit a3d9ab3

File tree

7 files changed

+214
-1
lines changed

7 files changed

+214
-1
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@
7272
"creplace",
7373
"dbatools",
7474
"db_datareader",
75-
"fastbuild"
75+
"fastbuild",
76+
"CODEOWNERS",
77+
"analyzersettings",
78+
"sqlcmd"
7679
],
7780
"cSpell.ignorePaths": [
7881
".git"

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
Reporting Services ([issue #2064](https://github.com/dsccommunity/SqlServerDsc/issues/2064)).
2626
- `Repair-SqlDscBIReportServer` to repair an already installed SQL Server
2727
BI Report Server ([issue #2064](https://github.com/dsccommunity/SqlServerDsc/issues/2064)).
28+
- `Test-SqlDscRSInstalled` to test whether an instance is installed or not
29+
([issue #2078](https://github.com/dsccommunity/SqlServerDsc/issues/2078)).
2830
- `Uninstall-SqlDscReportingService` to uninstall SQL Server Reporting
2931
Services ([issue #2065](https://github.com/dsccommunity/SqlServerDsc/issues/2065)).
3032
- `Uninstall-SqlDscBIReportServer` to uninstall SQL Server BI Report Server

azure-pipelines.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ stages:
328328
# Group 2
329329
'tests/Integration/Commands/Get-SqlDscInstalledInstance.Integration.Tests.ps1'
330330
'tests/Integration/Commands/Get-SqlDscRSSetupConfiguration.Integration.Tests.ps1'
331+
'tests/Integration/Commands/Test-SqlDscRSInstalled.Integration.Tests.ps1'
331332
# Group 8
332333
'tests/Integration/Commands/Repair-SqlDscReportingService.Integration.Tests.ps1'
333334
# Group 9
@@ -390,6 +391,7 @@ stages:
390391
# Group 2
391392
'tests/Integration/Commands/Get-SqlDscInstalledInstance.Integration.Tests.ps1'
392393
'tests/Integration/Commands/Get-SqlDscRSSetupConfiguration.Integration.Tests.ps1'
394+
'tests/Integration/Commands/Test-SqlDscRSInstalled.Integration.Tests.ps1'
393395
# Group 8
394396
'tests/Integration/Commands/Repair-SqlDscBIReportServer.Integration.Tests.ps1'
395397
# Group 9
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<#
2+
.SYNOPSIS
3+
Tests if a SQL Server Reporting Services or Power BI Report Server instance
4+
is installed.
5+
6+
.DESCRIPTION
7+
Tests if a SQL Server Reporting Services or Power BI Report Server instance
8+
is installed on the local server. The command returns $true if the specified
9+
instance exists and $false if it does not.
10+
11+
.PARAMETER InstanceName
12+
Specifies the name of the SQL Server Reporting Services or Power BI Report Server
13+
instance to check for. This parameter is required.
14+
15+
.EXAMPLE
16+
Test-SqlDscRSInstalled -InstanceName 'SSRS'
17+
18+
Tests if a SQL Server Reporting Services instance named 'SSRS' is installed.
19+
Returns $true if the instance exists and $false if it does not.
20+
21+
.EXAMPLE
22+
Test-SqlDscRSInstalled -InstanceName 'PBIRS'
23+
24+
Tests if a Power BI Report Server instance named 'PBIRS' is installed.
25+
Returns $true if the instance exists and $false if it does not.
26+
27+
.OUTPUTS
28+
System.Boolean
29+
30+
Returns $true if the specified instance exists and $false if it does not.
31+
#>
32+
function Test-SqlDscRSInstalled
33+
{
34+
# cSpell: ignore PBIRS
35+
[CmdletBinding()]
36+
[OutputType([System.Boolean])]
37+
param
38+
(
39+
[Parameter(Mandatory = $true)]
40+
[System.String]
41+
$InstanceName
42+
)
43+
44+
Write-Verbose -Message ($script:localizedData.Test_SqlDscRSInstalled_Checking -f $InstanceName)
45+
46+
$reportingServices = Get-SqlDscRSSetupConfiguration -InstanceName $InstanceName -ErrorAction 'SilentlyContinue'
47+
48+
if ($reportingServices)
49+
{
50+
Write-Verbose -Message ($script:localizedData.Test_SqlDscRSInstalled_Found -f $InstanceName)
51+
52+
return $true
53+
}
54+
else
55+
{
56+
Write-Verbose -Message ($script:localizedData.Test_SqlDscRSInstalled_NotFound -f $InstanceName)
57+
58+
return $false
59+
}
60+
}

source/en-US/SqlServerDsc.strings.psd1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,9 @@ ConvertFrom-StringData @'
213213
Get_SqlDscRSSetupConfiguration_ProcessingInstance = Processing configuration for instance '{0}'.
214214
Get_SqlDscRSSetupConfiguration_InstanceNotFound = Could not find a Microsoft SQL Server Reporting Services instance with the name '{0}'.
215215
Get_SqlDscRSSetupConfiguration_NoInstancesFound = No SQL Server Reporting Services instances were found.
216+
217+
## Test-SqlDscRSInstalled
218+
Test_SqlDscRSInstalled_Checking = Checking if Reporting Services instance '{0}' is installed.
219+
Test_SqlDscRSInstalled_Found = Reporting Services instance '{0}' was found.
220+
Test_SqlDscRSInstalled_NotFound = Reporting Services instance '{0}' was not found.
216221
'@
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
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+
Describe 'Test-SqlDscRSInstalled' {
27+
Context 'When testing if a specific Reporting Services instance exists' -Tag @('Integration_SQL2017_RS', 'Integration_SQL2019_RS', 'Integration_SQL2022_RS', 'Integration_PowerBI') {
28+
It 'Should return $false for a non-existing instance' {
29+
# We'll test with a fake instance name that we know doesn't exist.
30+
$result = Test-SqlDscRSInstalled -InstanceName 'FAKE_RS_INSTANCE'
31+
32+
$result | Should -BeFalse
33+
}
34+
}
35+
36+
Context 'When testing if a specific Reporting Services instance exists' -Tag @('Integration_SQL2017_RS', 'Integration_SQL2019_RS', 'Integration_SQL2022_RS') {
37+
It 'Should return $true for an existing instance' {
38+
# We'll test with a real instance name that we know exists.
39+
$result = Test-SqlDscRSInstalled -InstanceName 'SSRS'
40+
41+
$result | Should -BeTrue
42+
}
43+
}
44+
45+
Context 'When testing if a specific Reporting Services instance exists' -Tag @('Integration_PowerBI') {
46+
It 'Should return $true for an existing instance' {
47+
# We'll test with a real instance name that we know exists. cSpell:ignore PBIRS
48+
$result = Test-SqlDscRSInstalled -InstanceName 'PBIRS'
49+
50+
$result | Should -BeTrue
51+
}
52+
}
53+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName
34+
$PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName
35+
$PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName
36+
}
37+
38+
AfterAll {
39+
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
40+
$PSDefaultParameterValues.Remove('Mock:ModuleName')
41+
$PSDefaultParameterValues.Remove('Should:ModuleName')
42+
43+
# Unload the module being tested so that it doesn't impact any other tests.
44+
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force
45+
46+
Remove-Item -Path 'env:SqlServerDscCI'
47+
}
48+
49+
Describe 'Test-SqlDscRSInstalled' {
50+
Context 'When the instance is found' {
51+
BeforeAll {
52+
Mock -CommandName Get-SqlDscRSSetupConfiguration -MockWith {
53+
return @{
54+
InstanceName = 'SSRS'
55+
InstallFolder = 'C:\Program Files\Microsoft SQL Server Reporting Services'
56+
}
57+
}
58+
}
59+
60+
It 'Should return $true when the instance exists' {
61+
$result = Test-SqlDscRSInstalled -InstanceName 'SSRS'
62+
63+
$result | Should -BeTrue
64+
65+
Should -Invoke -CommandName Get-SqlDscRSSetupConfiguration -ParameterFilter {
66+
$InstanceName -eq 'SSRS'
67+
} -Exactly -Times 1 -Scope It
68+
}
69+
}
70+
71+
Context 'When the instance is not found' {
72+
BeforeAll {
73+
Mock -CommandName Get-SqlDscRSSetupConfiguration -MockWith {
74+
return $null
75+
}
76+
}
77+
78+
It 'Should return $false when the instance does not exist' {
79+
$result = Test-SqlDscRSInstalled -InstanceName 'SSRS'
80+
81+
$result | Should -BeFalse
82+
83+
Should -Invoke -CommandName Get-SqlDscRSSetupConfiguration -ParameterFilter {
84+
$InstanceName -eq 'SSRS'
85+
} -Exactly -Times 1 -Scope It
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)