diff --git a/CHANGELOG.md b/CHANGELOG.md index 108ee175b..91e3337cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 for a file. - `Assert-Feature` - Throws an exception if a feature is not supported for a specific Microsoft SQL Server major version. +- SqlServerDsc.Common + - `Connect-SQL`. + - Add new parameter `Encrypt`. +- `Connect-SqlDscDatabaseEngine` + - Add new parameter `Encrypt`. +- `Invoke-SqlDscQuery` + - Add new parameter `Encrypt`. ### Changed @@ -40,6 +47,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - SqlSetup - Update to support checking non-supported features using the command `SqlDscIsSupportedFeature` ([issue #1872](https://github.com/dsccommunity/SqlServerDsc/issues/1872)). +- SqlRS + - Now uses the command `Invoke-SqlDscQuery` instead of `Invoke-SqlCmd` + ([issue #1917](https://github.com/dsccommunity/SqlServerDsc/issues/1917)). + - The parameter `Encrypt` has changed so that `Mandatory` or `Strict` + will turn on encryption when connecting to the database instance. +- `Invoke-SqlDscQuery` + - Now shows the correct instance name when called using a server object + ([issue #1918](https://github.com/dsccommunity/SqlServerDsc/issues/1918)). + - Correctly outputs query in verbose message when parameter `RedactText` + is not passed. ## [16.2.0] - 2023-04-10 diff --git a/RequiredModules.psd1 b/RequiredModules.psd1 index 730f13427..46c4c4d29 100644 --- a/RequiredModules.psd1 +++ b/RequiredModules.psd1 @@ -48,4 +48,5 @@ PSDscResources = '2.12.0.0' StorageDsc = '4.9.0.0' NetworkingDsc = '7.4.0.0' + WSManDsc = '3.1.1' } diff --git a/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 b/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 index 6022f6461..78a6bf2a7 100644 --- a/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 +++ b/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 @@ -20,13 +20,8 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' Name of the SQL Server instance to host the Reporting Service database. .PARAMETER Encrypt - Specifies how encryption should be enforced when using command `Invoke-SqlCmd`. - When not specified, the default value is `Mandatory`. - - This value maps to the Encrypt property SqlConnectionEncryptOption - on the SqlConnection object of the Microsoft.Data.SqlClient driver. - - This parameter can only be used when the module SqlServer v22.x.x is installed. + Specifies how encryption should be enforced. There are currently no + difference between using `Mandatory` or `Strict`. #> function Get-TargetResource { @@ -186,13 +181,8 @@ function Get-TargetResource will not be restarted, even after initialization. .PARAMETER Encrypt - Specifies how encryption should be enforced when using command `Invoke-SqlCmd`. - When not specified, the default value is `Mandatory`. - - This value maps to the Encrypt property SqlConnectionEncryptOption - on the SqlConnection object of the Microsoft.Data.SqlClient driver. - - This parameter can only be used when the module SqlServer v22.x.x is installed. + Specifies how encryption should be enforced. There are currently no + difference between using `Mandatory` or `Strict`. .NOTES To find out the parameter names for the methods in the class @@ -329,15 +319,6 @@ function Set-TargetResource $reportingServicesDatabaseName = "ReportServer`$$InstanceName" } - if ( $DatabaseInstanceName -eq 'MSSQLSERVER' ) - { - $reportingServicesConnection = $DatabaseServerName - } - else - { - $reportingServicesConnection = "$DatabaseServerName\$DatabaseInstanceName" - } - $wmiOperatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem -Namespace 'root/cimv2' -ErrorAction SilentlyContinue if ( $null -eq $wmiOperatingSystem ) { @@ -466,32 +447,36 @@ function Set-TargetResource $reportingServicesDatabaseRightsScript = Invoke-RsCimMethod @invokeRsCimMethodParameters - <# - Import-SqlDscPreferredModule cmdlet will import SQLPS (SQL 2012/14) or SqlServer module (SQL 2016), - and if importing SQLPS, change directory back to the original one, since SQLPS changes the - current directory to SQLSERVER:\ on import. - #> Import-SqlDscPreferredModule - $invokeSqlCmdParameters = @{ - ServerInstance = $reportingServicesConnection + $invokeSqlDscQueryParameters = @{ + ServerName = $DatabaseServerName + InstanceName = $DatabaseInstanceName + DatabaseName = 'master' + Force = $true + Verbose = $VerbosePreference + ErrorAction = 'Stop' } - if ($PSBoundParameters.ContainsKey('Encrypt')) + if ($PSBoundParameters.ContainsKey('Encrypt') -and $Encrypt -ne 'Optional') { - $commandInvokeSqlCmd = Get-Command -Name 'Invoke-SqlCmd' - - if ($null -ne $commandInvokeSqlCmd -and $commandInvokeSqlCmd.Parameters.Keys -contains 'Encrypt') - { - $invokeSqlCmdParameters.Encrypt = $Encrypt - } + $invokeSqlDscQueryParameters.Encrypt = $true } - Invoke-SqlCmd @invokeSqlCmdParameters -Query $reportingServicesDatabaseScript.Script - Invoke-SqlCmd @invokeSqlCmdParameters -Query $reportingServicesDatabaseRightsScript.Script + Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query $reportingServicesDatabaseScript.Script + Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query $reportingServicesDatabaseRightsScript.Script Write-Verbose -Message "Set database connection on $DatabaseServerName\$DatabaseInstanceName to database '$reportingServicesDatabaseName'." + if ( $DatabaseInstanceName -eq 'MSSQLSERVER' ) + { + $reportingServicesConnection = $DatabaseServerName + } + else + { + $reportingServicesConnection = "$DatabaseServerName\$DatabaseInstanceName" + } + $invokeRsCimMethodParameters = @{ CimInstance = $reportingServicesData.Configuration MethodName = 'SetDatabaseConnection' @@ -863,13 +848,8 @@ function Set-TargetResource will not be restarted, even after initialization. .PARAMETER Encrypt - Specifies how encryption should be enforced when using command `Invoke-SqlCmd`. - When not specified, the default value is `Mandatory`. - - This value maps to the Encrypt property SqlConnectionEncryptOption - on the SqlConnection object of the Microsoft.Data.SqlClient driver. - - This parameter can only be used when the module SqlServer v22.x.x is installed. + Specifies how encryption should be enforced. There are currently no + difference between using `Mandatory` or `Strict`. #> function Test-TargetResource { diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 index da0397d8e..492d1f70a 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 @@ -447,6 +447,9 @@ function Start-SqlSetupProcess .PARAMETER StatementTimeout Set the query StatementTimeout in seconds. Default 600 seconds (10 minutes). + .PARAMETER Encrypt + Specifies if encryption should be used. + .EXAMPLE Connect-SQL @@ -493,7 +496,11 @@ function Connect-SQL [Parameter()] [ValidateNotNull()] [System.Int32] - $StatementTimeout = 600 + $StatementTimeout = 600, + + [Parameter()] + [System.Management.Automation.SwitchParameter] + $Encrypt ) Import-SqlDscPreferredModule @@ -513,6 +520,11 @@ function Connect-SQL $sqlConnectionContext.StatementTimeout = $StatementTimeout $sqlConnectionContext.ApplicationName = 'SqlServerDsc' + if ($Encrypt.IsPresent) + { + $sqlConnectionContext.EncryptConnection = $true + } + if ($PSCmdlet.ParameterSetName -eq 'SqlServer') { <# diff --git a/source/Public/Connect-SqlDscDatabaseEngine.ps1 b/source/Public/Connect-SqlDscDatabaseEngine.ps1 index 14eabf576..d4071296e 100644 --- a/source/Public/Connect-SqlDscDatabaseEngine.ps1 +++ b/source/Public/Connect-SqlDscDatabaseEngine.ps1 @@ -31,6 +31,9 @@ .PARAMETER StatementTimeout Set the query StatementTimeout in seconds. Default 600 seconds (10 minutes). + .PARAMETER Encrypt + Specifies if encryption should be used. + .EXAMPLE Connect-SqlDscDatabaseEngine @@ -80,7 +83,11 @@ function Connect-SqlDscDatabaseEngine [Parameter()] [ValidateNotNull()] [System.Int32] - $StatementTimeout = 600 + $StatementTimeout = 600, + + [Parameter()] + [System.Management.Automation.SwitchParameter] + $Encrypt ) # Call the private function. diff --git a/source/Public/Invoke-SqlDscQuery.ps1 b/source/Public/Invoke-SqlDscQuery.ps1 index 929af7c95..03db39897 100644 --- a/source/Public/Invoke-SqlDscQuery.ps1 +++ b/source/Public/Invoke-SqlDscQuery.ps1 @@ -42,6 +42,9 @@ are written to the console. Strings will be escaped so they will not be interpreted as regular expressions (RegEx). + .PARAMETER Encrypt + Specifies if encryption should be used. + .PARAMETER Force Specifies that the query should be executed without any confirmation. @@ -116,6 +119,10 @@ function Invoke-SqlDscQuery [System.String] $LoginType = 'Integrated', + [Parameter(ParameterSetName = 'ByServerName')] + [System.Management.Automation.SwitchParameter] + $Encrypt, + [Parameter(Mandatory = $true)] [System.String] $DatabaseName, @@ -161,6 +168,11 @@ function Invoke-SqlDscQuery Verbose = $VerbosePreference } + if ($Encrypt.IsPresent) + { + $connectSqlDscDatabaseEngineParameters.Encrypt = $true + } + if ($LoginType -ne 'Integrated') { $connectSqlDscDatabaseEngineParameters['LoginType'] = $LoginType @@ -174,6 +186,13 @@ function Invoke-SqlDscQuery $ServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters } + if ($PSCmdlet.ParameterSetName -eq 'ByServerObject') + { + $InstanceName = $ServerObject.InstanceName + } + + $redactedQuery = $Query + if ($PSBoundParameters.ContainsKey('RedactText')) { $redactedQuery = ConvertTo-RedactedText -Text $Query -RedactPhrase $RedactText diff --git a/tests/Integration/DSC_SqlRS.config.ps1 b/tests/Integration/DSC_SqlRS.config.ps1 index f8b0cbfa9..04aa7072c 100644 --- a/tests/Integration/DSC_SqlRS.config.ps1 +++ b/tests/Integration/DSC_SqlRS.config.ps1 @@ -78,10 +78,17 @@ Configuration DSC_SqlRS_CreateDependencies_Config { Import-DscResource -ModuleName 'PSDscResources' -ModuleVersion '2.12.0.0' Import-DscResource -ModuleName 'StorageDsc' -ModuleVersion '4.9.0.0' + Import-DscResource -ModuleName 'WSManDsc' -ModuleVersion '3.1.1' Import-DscResource -ModuleName 'SqlServerDsc' node $AllNodes.NodeName { + WSManConfig Config + { + IsSingleInstance = 'Yes' + MaxEnvelopeSizeKb = 600 + } + User 'CreateReportingServicesServiceAccount' { Ensure = 'Present' diff --git a/tests/Integration/DSC_SqlRS_Default.config.ps1 b/tests/Integration/DSC_SqlRS_Default.config.ps1 index 3e602c6e4..d31a72fc3 100644 --- a/tests/Integration/DSC_SqlRS_Default.config.ps1 +++ b/tests/Integration/DSC_SqlRS_Default.config.ps1 @@ -78,10 +78,17 @@ Configuration DSC_SqlRS_CreateDependencies_Config { Import-DscResource -ModuleName 'PSDscResources' -ModuleVersion '2.12.0.0' Import-DscResource -ModuleName 'StorageDsc' -ModuleVersion '4.9.0.0' + Import-DscResource -ModuleName 'WSManDsc' -ModuleVersion '3.1.1' Import-DscResource -ModuleName 'SqlServerDsc' node $AllNodes.NodeName { + WSManConfig Config + { + IsSingleInstance = 'Yes' + MaxEnvelopeSizeKb = 600 + } + User 'CreateReportingServicesServiceAccount' { Ensure = 'Present' diff --git a/tests/Unit/DSC_SqlRS.Tests.ps1 b/tests/Unit/DSC_SqlRS.Tests.ps1 index 81c270cdd..151b53c1c 100644 --- a/tests/Unit/DSC_SqlRS.Tests.ps1 +++ b/tests/Unit/DSC_SqlRS.Tests.ps1 @@ -43,6 +43,9 @@ BeforeAll { Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') + # Loading mocked classes + Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs') -ChildPath 'SMO.cs') + # Load the correct SQL Module stub $script:stubModuleName = Import-SQLModuleStub -PassThru @@ -508,7 +511,7 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' { Mock -CommandName Invoke-CimMethod -MockWith $mockInvokeCimMethod Mock -CommandName Import-SqlDscPreferredModule - Mock -CommandName Invoke-SqlCmd + Mock -CommandName Invoke-SqlDscQuery Mock -CommandName Restart-ReportingServicesService Mock -CommandName Invoke-RsCimMethod Mock -CommandName Invoke-RsCimMethod -MockWith $mockInvokeRsCimMethod_GenerateDatabaseCreationScript -ParameterFilter { @@ -566,18 +569,6 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' { Mock -CommandName Test-TargetResource -MockWith { return $true } - - BeforeAll { - Mock -CommandName Get-Command -ParameterFilter { - $Name -eq 'Invoke-SqlCmd' - } -MockWith { - return @{ - Parameters = @{ - Keys = @() - } - } - } - } } BeforeEach { @@ -641,7 +632,7 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' { } -Exactly -Times 1 -Scope It Should -Invoke -CommandName Get-CimInstance -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-SqlCmd -Exactly -Times 2 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 2 -Scope It Should -Invoke -CommandName Restart-ReportingServicesService -Exactly -Times 2 -Scope It } @@ -661,12 +652,13 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' { DatabaseServerName = $mockReportingServicesDatabaseServerName DatabaseInstanceName = $mockReportingServicesDatabaseNamedInstanceName UseSsl = $true + Encrypt = 'Mandatory' } { Set-TargetResource @mockDefaultParameters } | Should -Throw -ExpectedMessage ('*' + $script:localizedData.TestFailedAfterSet) - Should -Invoke -CommandName Invoke-SqlCmd -ParameterFilter { - $PesterBoundParameters.Keys -notcontains 'Encrypt' + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { + $PesterBoundParameters.Keys -contains 'Encrypt' } -Times 2 -Exactly -Scope It } } @@ -785,7 +777,7 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' { } -Exactly -Times 2 -Scope It Should -Invoke -CommandName Get-CimInstance -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-SqlCmd -Exactly -Times 0 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 0 -Scope It Should -Invoke -CommandName Restart-ReportingServicesService -Exactly -Times 1 -Scope It } } @@ -880,7 +872,7 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' { } -Exactly -Times 2 -Scope It Should -Invoke -CommandName Get-CimInstance -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-SqlCmd -Exactly -Times 0 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 0 -Scope It Should -Invoke -CommandName Restart-ReportingServicesService -Exactly -Times 0 -Scope It } } @@ -893,16 +885,6 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' { return $true } - Mock -CommandName Get-Command -ParameterFilter { - $Name -eq 'Invoke-SqlCmd' - } -MockWith { - return @{ - Parameters = @{ - Keys = @('Encrypt') - } - } - } - $defaultParameters = @{ InstanceName = $mockDefaultInstanceName DatabaseServerName = $mockReportingServicesDatabaseServerName @@ -967,11 +949,11 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' { } -Exactly -Times 1 -Scope It Should -Invoke -CommandName Get-CimInstance -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-SqlCmd -Exactly -Times 2 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 2 -Scope It Should -Invoke -CommandName Restart-ReportingServicesService -Exactly -Times 2 -Scope It - Should -Invoke -CommandName Invoke-SqlCmd -ParameterFilter { - $Encrypt -eq 'Optional' + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { + $PesterBoundParameters.Keys -notcontains 'Encrypt' } -Times 2 -Exactly -Scope It } } @@ -1070,7 +1052,7 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' { } -Exactly -Times 1 -Scope It Should -Invoke -CommandName Get-CimInstance -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-SqlCmd -Exactly -Times 2 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 2 -Scope It Should -Invoke -CommandName Restart-ReportingServicesService -Exactly -Times 1 -Scope It } } @@ -1093,8 +1075,8 @@ Describe 'SqlRS\Test-TargetResource' -Tag 'Test' { $testParameters = @{ InstanceName = 'INSTANCE' - DatabaseServerName = 'DBSERVER' - DatabaseInstanceName = 'DBINSTANCE' + DatabaseServerName = 'MockDatabaseServer' + DatabaseInstanceName = 'MockDatabaseInstance' Encrypt = 'Optional' } @@ -1121,8 +1103,8 @@ Describe 'SqlRS\Test-TargetResource' -Tag 'Test' { $testParameters = @{ InstanceName = 'INSTANCE' - DatabaseServerName = 'DBSERVER' - DatabaseInstanceName = 'DBINSTANCE' + DatabaseServerName = 'MockDatabaseServer' + DatabaseInstanceName = 'MockDatabaseInstance' ReportServerReservedUrl = 'ReportServer_SQL2016' } @@ -1149,8 +1131,8 @@ Describe 'SqlRS\Test-TargetResource' -Tag 'Test' { $testParameters = @{ InstanceName = 'INSTANCE' - DatabaseServerName = 'DBSERVER' - DatabaseInstanceName = 'DBINSTANCE' + DatabaseServerName = 'MockDatabaseServer' + DatabaseInstanceName = 'MockDatabaseInstance' ReportsReservedUrl = 'Reports_SQL2016' } @@ -1178,8 +1160,8 @@ Describe 'SqlRS\Test-TargetResource' -Tag 'Test' { $testParameters = @{ InstanceName = 'INSTANCE' - DatabaseServerName = 'DBSERVER' - DatabaseInstanceName = 'DBINSTANCE' + DatabaseServerName = 'MockDatabaseServer' + DatabaseInstanceName = 'MockDatabaseInstance' ReportsVirtualDirectory = 'Reports_SQL2016' ReportServerVirtualDirectory = 'ReportServer_NewName' } @@ -1207,8 +1189,8 @@ Describe 'SqlRS\Test-TargetResource' -Tag 'Test' { $testParameters = @{ InstanceName = 'INSTANCE' - DatabaseServerName = 'DBSERVER' - DatabaseInstanceName = 'DBINSTANCE' + DatabaseServerName = 'MockDatabaseServer' + DatabaseInstanceName = 'MockDatabaseInstance' ReportServerVirtualDirectory = 'ReportServer_SQL2016' ReportsVirtualDirectory = 'Reports_NewName' } @@ -1236,8 +1218,8 @@ Describe 'SqlRS\Test-TargetResource' -Tag 'Test' { $testParameters = @{ InstanceName = 'INSTANCE' - DatabaseServerName = 'DBSERVER' - DatabaseInstanceName = 'DBINSTANCE' + DatabaseServerName = 'MockDatabaseServer' + DatabaseInstanceName = 'MockDatabaseInstance' ReportServerReservedUrl = 'https://+:443' } @@ -1264,8 +1246,8 @@ Describe 'SqlRS\Test-TargetResource' -Tag 'Test' { $testParameters = @{ InstanceName = 'INSTANCE' - DatabaseServerName = 'DBSERVER' - DatabaseInstanceName = 'DBINSTANCE' + DatabaseServerName = 'MockDatabaseServer' + DatabaseInstanceName = 'MockDatabaseInstance' ReportsReservedUrl = 'https://+:443' } @@ -1292,8 +1274,8 @@ Describe 'SqlRS\Test-TargetResource' -Tag 'Test' { $testParameters = @{ InstanceName = 'INSTANCE' - DatabaseServerName = 'DBSERVER' - DatabaseInstanceName = 'DBINSTANCE' + DatabaseServerName = 'MockDatabaseServer' + DatabaseInstanceName = 'MockDatabaseInstance' UseSsl = $true } @@ -1320,8 +1302,8 @@ Describe 'SqlRS\Test-TargetResource' -Tag 'Test' { $defaultParameters = @{ InstanceName = 'INSTANCE' - DatabaseServerName = 'DBSERVER' - DatabaseInstanceName = 'DBINSTANCE' + DatabaseServerName = 'MockDatabaseServer' + DatabaseInstanceName = 'MockDatabaseInstance' } $resultTestTargetResource = Test-TargetResource @defaultParameters @@ -1359,7 +1341,7 @@ Describe 'SqlRS\Invoke-RsCimMethod' -Tag 'Helper' { BeforeAll { Mock -CommandName Invoke-CimMethod -MockWith { return @{ - HRESULT = 0 + HRESULT = 0 # cSpell: disable-line } } } @@ -1375,7 +1357,7 @@ Describe 'SqlRS\Invoke-RsCimMethod' -Tag 'Helper' { } $resultTestTargetResource = Invoke-RsCimMethod @invokeRsCimMethodParameters - $resultTestTargetResource.HRESULT | Should -Be 0 + $resultTestTargetResource.HRESULT | Should -Be 0 # cSpell: disable-line } Should -Invoke -CommandName Invoke-CimMethod -ParameterFilter { @@ -1398,7 +1380,7 @@ Describe 'SqlRS\Invoke-RsCimMethod' -Tag 'Helper' { } $resultTestTargetResource = Invoke-RsCimMethod @invokeRsCimMethodParameters - $resultTestTargetResource.HRESULT | Should -Be 0 + $resultTestTargetResource.HRESULT | Should -Be 0 # cSpell: disable-line } Should -Invoke -CommandName Invoke-CimMethod -ParameterFilter { @@ -1413,7 +1395,7 @@ Describe 'SqlRS\Invoke-RsCimMethod' -Tag 'Helper' { BeforeAll { Mock -CommandName Invoke-CimMethod -MockWith { return @{ - HRESULT = 1 + HRESULT = 1 # cSpell: disable-line Error = 'Something went wrong' } } @@ -1428,6 +1410,7 @@ Describe 'SqlRS\Invoke-RsCimMethod' -Tag 'Helper' { MethodName = 'AnyMethod' } + # cSpell: disable-next { Invoke-RsCimMethod @invokeRsCimMethodParameters } | Should -Throw 'Method AnyMethod() failed with an error. Error: Something went wrong (HRESULT:1)' } @@ -1441,7 +1424,7 @@ Describe 'SqlRS\Invoke-RsCimMethod' -Tag 'Helper' { BeforeAll { Mock -CommandName Invoke-CimMethod -MockWith { return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name 'HRESULT' -Value 1 -PassThru | + Add-Member -MemberType NoteProperty -Name 'HRESULT' -Value 1 -PassThru | # cSpell: disable-line Add-Member -MemberType NoteProperty -Name 'ExtendedErrors' -Value @('Something went wrong', 'Another thing went wrong') -PassThru -Force } } @@ -1455,6 +1438,7 @@ Describe 'SqlRS\Invoke-RsCimMethod' -Tag 'Helper' { MethodName = 'AnyMethod' } + # cSpell: disable-next { Invoke-RsCimMethod @invokeRsCimMethodParameters } | Should -Throw 'Method AnyMethod() failed with an error. Error: Something went wrong;Another thing went wrong (HRESULT:1)' } @@ -1468,7 +1452,7 @@ Describe 'SqlRS\Invoke-RsCimMethod' -Tag 'Helper' { Describe 'SqlRS\Get-ReportingServicesData' -Tag 'Helper' { BeforeAll { - $mockInstanceId = 'MSRS13.INSTANCE' + $mockInstanceId = 'MSRS13.INSTANCE' # cSpell: disable-line $mockGetItemProperty_Sql2014 = { return @{ @@ -1535,7 +1519,7 @@ Describe 'SqlRS\Get-ReportingServicesData' -Tag 'Helper' { New-Object -TypeName Microsoft.Management.Infrastructure.CimInstance -ArgumentList @( 'MSReportServer_ConfigurationSetting' 'root/Microsoft/SQLServer/ReportServer/RS_SQL2016/v13/Admin' - ) | Add-Member -MemberType NoteProperty -Name 'DatabaseServerName' -Value 'DBSERVER\DBINSTANCE' -PassThru | + ) | Add-Member -MemberType NoteProperty -Name 'DatabaseServerName' -Value 'MockDatabaseServer\MockDatabaseInstance' -PassThru | Add-Member -MemberType NoteProperty -Name 'IsInitialized' -Value $false -PassThru | Add-Member -MemberType NoteProperty -Name 'InstanceName' -Value 'INSTANCE' -PassThru | Add-Member -MemberType NoteProperty -Name 'VirtualDirectoryReportServer' -Value 'ReportServer' -PassThru | @@ -1545,7 +1529,7 @@ Describe 'SqlRS\Get-ReportingServicesData' -Tag 'Helper' { ( # Array is a regression test for issue #819. New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name 'DatabaseServerName' -Value 'DBSERVER\DBINSTANCE' -PassThru | + Add-Member -MemberType NoteProperty -Name 'DatabaseServerName' -Value 'MockDatabaseServer\MockDatabaseInstance' -PassThru | Add-Member -MemberType NoteProperty -Name 'IsInitialized' -Value $true -PassThru | Add-Member -MemberType NoteProperty -Name 'InstanceName' -Value 'DummyInstance' -PassThru -Force ) @@ -1574,7 +1558,7 @@ Describe 'SqlRS\Get-ReportingServicesData' -Tag 'Helper' { $getReportingServicesDataResult.Configuration | Should -BeOfType [Microsoft.Management.Infrastructure.CimInstance] $getReportingServicesDataResult.Configuration.InstanceName | Should -Be 'INSTANCE' - $getReportingServicesDataResult.Configuration.DatabaseServerName | Should -Be 'DBSERVER\DBINSTANCE' + $getReportingServicesDataResult.Configuration.DatabaseServerName | Should -Be 'MockDatabaseServer\MockDatabaseInstance' $getReportingServicesDataResult.Configuration.IsInitialized | Should -BeFalse $getReportingServicesDataResult.Configuration.VirtualDirectoryReportServer | Should -Be 'ReportServer' $getReportingServicesDataResult.Configuration.VirtualDirectoryReportManager | Should -Be 'Reports' @@ -1614,7 +1598,7 @@ Describe 'SqlRS\Get-ReportingServicesData' -Tag 'Helper' { $getReportingServicesDataResult.Configuration | Should -BeOfType [Microsoft.Management.Infrastructure.CimInstance] $getReportingServicesDataResult.Configuration.InstanceName | Should -Be 'INSTANCE' - $getReportingServicesDataResult.Configuration.DatabaseServerName | Should -Be 'DBSERVER\DBINSTANCE' + $getReportingServicesDataResult.Configuration.DatabaseServerName | Should -Be 'MockDatabaseServer\MockDatabaseInstance' $getReportingServicesDataResult.Configuration.IsInitialized | Should -BeFalse $getReportingServicesDataResult.Configuration.VirtualDirectoryReportServer | Should -Be 'ReportServer' $getReportingServicesDataResult.Configuration.VirtualDirectoryReportManager | Should -Be 'Reports' @@ -1654,7 +1638,7 @@ Describe 'SqlRS\Get-ReportingServicesData' -Tag 'Helper' { $getReportingServicesDataResult.Configuration | Should -BeOfType [Microsoft.Management.Infrastructure.CimInstance] $getReportingServicesDataResult.Configuration.InstanceName | Should -Be 'INSTANCE' - $getReportingServicesDataResult.Configuration.DatabaseServerName | Should -Be 'DBSERVER\DBINSTANCE' + $getReportingServicesDataResult.Configuration.DatabaseServerName | Should -Be 'MockDatabaseServer\MockDatabaseInstance' $getReportingServicesDataResult.Configuration.IsInitialized | Should -BeFalse $getReportingServicesDataResult.Configuration.VirtualDirectoryReportServer | Should -Be 'ReportServer' $getReportingServicesDataResult.Configuration.VirtualDirectoryReportManager | Should -Be 'Reports' @@ -1694,7 +1678,7 @@ Describe 'SqlRS\Get-ReportingServicesData' -Tag 'Helper' { $getReportingServicesDataResult.Configuration | Should -BeOfType [Microsoft.Management.Infrastructure.CimInstance] $getReportingServicesDataResult.Configuration.InstanceName | Should -Be 'INSTANCE' - $getReportingServicesDataResult.Configuration.DatabaseServerName | Should -Be 'DBSERVER\DBINSTANCE' + $getReportingServicesDataResult.Configuration.DatabaseServerName | Should -Be 'MockDatabaseServer\MockDatabaseInstance' $getReportingServicesDataResult.Configuration.IsInitialized | Should -BeFalse $getReportingServicesDataResult.Configuration.VirtualDirectoryReportServer | Should -Be 'ReportServer' $getReportingServicesDataResult.Configuration.VirtualDirectoryReportManager | Should -Be 'Reports' diff --git a/tests/Unit/Public/Connect-SqlDscDatabaseEngine.Tests.ps1 b/tests/Unit/Public/Connect-SqlDscDatabaseEngine.Tests.ps1 index 95cef49fa..9c43c9094 100644 --- a/tests/Unit/Public/Connect-SqlDscDatabaseEngine.Tests.ps1 +++ b/tests/Unit/Public/Connect-SqlDscDatabaseEngine.Tests.ps1 @@ -51,33 +51,65 @@ AfterAll { } Describe 'Connect-SqlDscDatabaseEngine' -Tag 'Public' { - BeforeAll { - Mock -CommandName Connect-Sql - - $mockCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @( - 'COMPANY\SqlAdmin', - ('dummyPassW0rd' | ConvertTo-SecureString -AsPlainText -Force) - ) - + It 'Should have the correct parameters in parameter set ' -ForEach @( + @{ + MockParameterSetName = 'SqlServer' + # cSpell: disable-next + MockExpectedParameters = '[-ServerName ] [-InstanceName ] [-StatementTimeout ] [-Encrypt] []' + } + @{ + MockParameterSetName = 'SqlServerWithCredential' + # cSpell: disable-next + MockExpectedParameters = '-Credential [-ServerName ] [-InstanceName ] [-LoginType ] [-StatementTimeout ] [-Encrypt] []' + } + ) { + $result = (Get-Command -Name 'Connect-SqlDscDatabaseEngine').ParameterSets | + Where-Object -FilterScript { + $_.Name -eq $mockParameterSetName + } | + Select-Object -Property @( + @{ + Name = 'ParameterSetName' + Expression = { $_.Name } + }, + @{ + Name = 'ParameterListAsString' + Expression = { $_.ToString() } + } + ) + + $result.ParameterSetName | Should -Be $MockParameterSetName + $result.ParameterListAsString | Should -Be $MockExpectedParameters } - It 'Should call the correct mock with the expected parameters' { - $mockConnectSqlDscDatabaseEngineParameters = @{ - ServerName = 'MyServer' - InstanceName = 'MyInstance' - Credential = $mockCredentials - LoginType = 'WindowsUser' - StatementTimeout = 800 + Context 'When connecting to an instance' { + BeforeAll { + Mock -CommandName Connect-Sql + + $mockCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @( + 'COMPANY\SqlAdmin', + ('dummyPassW0rd' | ConvertTo-SecureString -AsPlainText -Force) + ) } - Connect-SqlDscDatabaseEngine @mockConnectSqlDscDatabaseEngineParameters + It 'Should call the correct mock with the expected parameters' { + $mockConnectSqlDscDatabaseEngineParameters = @{ + ServerName = 'MyServer' + InstanceName = 'MyInstance' + Credential = $mockCredentials + LoginType = 'WindowsUser' + StatementTimeout = 800 + } + + Connect-SqlDscDatabaseEngine @mockConnectSqlDscDatabaseEngineParameters - Should -Invoke -CommandName Connect-Sql -ParameterFilter { - $ServerName -eq 'MyServer' -and - $InstanceName -eq 'MyInstance' -and - $Credential -eq $mockCredentials -and - $LoginType -eq 'WindowsUser' -and - $StatementTimeout -eq 800 + Should -Invoke -CommandName Connect-Sql -ParameterFilter { + $ServerName -eq 'MyServer' -and + $InstanceName -eq 'MyInstance' -and + $Credential -eq $mockCredentials -and + $LoginType -eq 'WindowsUser' -and + $StatementTimeout -eq 800 + } } } } diff --git a/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 b/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 index 8638211b7..e0c103f76 100644 --- a/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 +++ b/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 @@ -55,7 +55,7 @@ Describe 'Invoke-SqlDscQuery' -Tag 'Public' { @{ MockParameterSetName = 'ByServerName' # cSpell: disable-next - MockExpectedParameters = '-DatabaseName -Query [-ServerName ] [-InstanceName ] [-Credential ] [-LoginType ] [-PassThru] [-StatementTimeout ] [-RedactText ] [-Force] [-WhatIf] [-Confirm] []' + MockExpectedParameters = '-DatabaseName -Query [-ServerName ] [-InstanceName ] [-Credential ] [-LoginType ] [-Encrypt] [-PassThru] [-StatementTimeout ] [-RedactText ] [-Force] [-WhatIf] [-Confirm] []' } @{ MockParameterSetName = 'ByServerObject' @@ -106,6 +106,7 @@ Describe 'Invoke-SqlDscQuery' -Tag 'Public' { Add-Member -MemberType 'NoteProperty' -Name 'StatementTimeout' -Value 100 -PassThru -Force $mockServerObject.ConnectionContext = $mockConnectionContext + $mockServerObject.InstanceName = 'MockInstance' } BeforeEach { @@ -200,6 +201,19 @@ Describe 'Invoke-SqlDscQuery' -Tag 'Public' { Should -Invoke -CommandName Connect-SqlDscDatabaseEngine -Exactly -Times 1 -Scope It Should -Invoke -CommandName Disconnect-SqlDscDatabaseEngine -Exactly -Times 1 -Scope It } + + Context 'When calling the command with optional parameter Encrypt' { + It 'Should execute the query without throwing and without returning any result' { + $result = Invoke-SqlDscQuery -Encrypt -ServerName 'localhost' -InstanceName 'INSTANCE' -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -Force + + $result | Should -BeNullOrEmpty + + $mockMethodExecuteNonQueryCallCount | Should -Be 1 + + Should -Invoke -CommandName Connect-SqlDscDatabaseEngine -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Disconnect-SqlDscDatabaseEngine -Exactly -Times 1 -Scope It + } + } } } diff --git a/tests/Unit/SqlServerDsc.Common.Tests.ps1 b/tests/Unit/SqlServerDsc.Common.Tests.ps1 index 9da5692b7..8853ee58a 100644 --- a/tests/Unit/SqlServerDsc.Common.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.Common.Tests.ps1 @@ -2364,6 +2364,7 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { Add-Member -MemberType NoteProperty -Name ConnectAsUserPassword -Value '' -PassThru | Add-Member -MemberType NoteProperty -Name ConnectAsUserName -Value '' -PassThru | Add-Member -MemberType NoteProperty -Name StatementTimeout -Value 600 -PassThru | + Add-Member -MemberType NoteProperty -Name EncryptConnection -Value $false -PassThru | Add-Member -MemberType NoteProperty -Name ApplicationName -Value 'SqlServerDsc' -PassThru | Add-Member -MemberType ScriptMethod -Name Disconnect -Value { return $true @@ -2602,6 +2603,25 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { } } + Context 'When using encryption' { + BeforeAll { + Mock -CommandName New-Object ` + -MockWith $mockNewObject_MicrosoftDatabaseEngine ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + } + + It 'Should return the correct service instance' { + $mockExpectedDatabaseEngineServer = 'SERVER' + $mockExpectedDatabaseEngineInstance = 'SqlInstance' + + $databaseEngineServerObject = Connect-SQL -Encrypt -ServerName $mockExpectedDatabaseEngineServer -InstanceName $mockExpectedDatabaseEngineInstance -ErrorAction 'Stop' + $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" + + Should -Invoke -CommandName New-Object -Exactly -Times 1 -Scope It ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + } + } + Context 'When connecting to the default instance using the correct service instance but does not return a correct Database Engine object' { Context 'When using ErrorAction set to Stop' { BeforeAll {