diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 337ea35d0..adf71544d 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -1,17 +1,55 @@ -**Details of the scenario you tried and the problem that is occurring:** +#### Details of the scenario you tried and the problem that is occurring -**The DSC configuration that is using the resource (as detailed as possible):** +#### The DSC configuration that is using the resource (as detailed as possible) +``` + +``` + +#### Version of the operating system and PowerShell the target node is running + -**Version of the Operating System, SQL Server and PowerShell the DSC Target Node is running:** +#### SQL Server edition and version the target node is running + -**What module (SqlServer or SQLPS) and which version of the module the DSC Target Node is running:** +#### What SQL Server PowerShell modules, and which version, are present on the target node. + -**Version of the DSC module you're using, or 'dev' if you're using current dev branch:** +#### Version of the DSC module you're using, or write 'dev' if you're using current dev branch + diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 7b84ac775..5a677a5a6 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,30 +1,53 @@ +#### Pull Request (PR) description + -**Pull Request (PR) description** - -**This Pull Request (PR) fixes the following issues:** - +#### This Pull Request (PR) fixes the following issues + -**Task list:** +#### Task list -- [ ] Change details added to Unreleased section of CHANGELOG.md? -- [ ] Added/updated documentation, comment-based help and descriptions in .schema.mof files where appropriate? -- [ ] Examples appropriately updated? -- [ ] New/changed code adheres to [Style Guidelines](https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md)? -- [ ] [Unit and (optional) Integration tests](https://github.com/PowerShell/DscResources/blob/master/TestsGuidelines.md) created/updated where possible? +- [ ] Added an entry under the Unreleased section in the CHANGELOG.md? Entry + should say what was changed, and how that affects users (if applicable). +- [ ] Resource documentation added/updated in README.md? +- [ ] Resource parameter descriptions added/updated in README.md, schema.mof + and comment-based help? +- [ ] Comment-based help added/updated? +- [ ] Localization strings added/updated in all localization files as appropriate? +- [ ] Examples appropriately added/updated? +- [ ] Unit tests added/updated? + See [DSC Resource Testing Guidelines](https://github.com/PowerShell/DscResources/blob/master/TestsGuidelines.md). +- [ ] Integration tests added/updated (where possible)? + See [DSC Resource Testing Guidelines](https://github.com/PowerShell/DscResources/blob/master/TestsGuidelines.md). +- [ ] New/changed code adheres to + [DSC Resource Style Guidelines](https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md) + and [Best Practices](https://github.com/PowerShell/DscResources/blob/master/BestPractices.md)? diff --git a/CHANGELOG.md b/CHANGELOG.md index bc667e65b..490fd5dfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,35 @@ ## Unreleased +## 11.1.0.0 + +- Changes to SqlServerDsc + - Updated the PULL\_REQUEST\_TEMPLATE with an improved task list and modified + some text to be clearer ([issue #973](https://github.com/PowerShell/SqlServerDsc/issues/973)). + - Updated the ISSUE_TEMPLATE to hopefully be more intuitive and easier to use. + - Added information to ISSUE_TEMPLATE that issues must be reproducible in + SqlServerDsc resource module (if running the older xSQLServer resource module) + ([issue #1036](https://github.com/PowerShell/SqlServerDsc/issues/1036)). + - Updated ISSUE_TEMPLATE.md with a note about sensitive information ([issue #1092](https://github.com/PowerShell/SqlServerDsc/issues/1092)). +- Changes to SqlServerLogin + - [Claudio Spizzi (@claudiospizzi)](https://github.com/claudiospizzi): Fix password + test fails for nativ sql users ([issue #1048](https://github.com/PowerShell/SqlServerDsc/issues/1048)). +- Changes to SqlSetup + - [Michael Fyffe (@TraGicCode)](https://github.com/TraGicCode): Clarify usage + of 'SecurityMode' along with adding parameter validations for the only 2 + supported values ([issue #1010](https://github.com/PowerShell/SqlServerDsc/issues/1010)). + - Now accounts containing '$' will be able to be used for installing + SQL Server. Although, if the account ends with '$' it is considered a + Managed Service Account ([issue #1055](https://github.com/PowerShell/SqlServerDsc/issues/1055)). +- Changes to Integration Tests + - [Michael Fyffe (@TraGicCode)](https://github.com/TraGicCode): Replace xStorage + dsc resource module with StorageDsc ([issue #1038](https://github.com/PowerShell/SqlServerDsc/issues/1038)). +- Changes to Unit Tests + - [Michael Fyffe (@TraGicCode)](https://github.com/TraGicCode): Updated + the following resources unit test template to version 1.2.1 + - SqlAlias ([issue #999](https://github.com/PowerShell/SqlServerDsc/issues/999)). + - SqlWindowsFirewall ([issue #1089](https://github.com/PowerShell/SqlServerDsc/issues/1089)). + ## 11.0.0.0 - Changes to SqlServerDsc @@ -565,10 +594,10 @@ SHIFT+ALT+F, or press F1 and choose 'Format document' in the list. The PowerShell code will then be formatted according to the Style Guideline (although maybe not complete, but would help a long way). - - Removed powershell.codeFormatting.alignPropertyValuePairs setting since - it does not align with the style guideline. - - Added powershell.codeFormatting.preset with a value of 'Custom' so that - workspace formatting settings are honored (issue #665). + - Removed powershell.codeFormatting.alignPropertyValuePairs setting since + it does not align with the style guideline. + - Added powershell.codeFormatting.preset with a value of 'Custom' so that + workspace formatting settings are honored (issue #665). - Fixed lint error MD013 and MD036 in README.md. - Updated .markdownlint.json to enable rule MD013 and MD036 to enforce those lint markdown rules in the common tests. @@ -1033,11 +1062,11 @@ - BREAKING CHANGE: Removed default value "$PSScriptRoot\..\..\" from parameter SourcePath. - Old code, that no longer filled any function, has been replaced. - - Function `ResolvePath` has been replaced with + - Function `ResolvePath` has been replaced with `[Environment]::ExpandEnvironmentVariables($SourcePath)` so that environment variables still can be used in Source Path. - - Function `NetUse` has been replaced with `New-SmbMapping` and - `Remove-SmbMapping`. + - Function `NetUse` has been replaced with `New-SmbMapping` and + `Remove-SmbMapping`. - Renamed function `GetSQLVersion` to `Get-SqlMajorVersion`. - BREAKING CHANGE: Renamed parameter PID to ProductKey to avoid collision with automatic variable $PID @@ -1276,28 +1305,28 @@ - Grant-CNOPerms - New-ListenerADObject - xSQLDatabaseRecoveryModel - - Updated Verbose statements to use new function New-VerboseMessage + - Updated Verbose statements to use new function New-VerboseMessage - xSQLServerDatabase - - Updated Verbose statements to use new function New-VerboseMessage - - Removed ConnectSQL function and replaced with new Connect-SQL function + - Updated Verbose statements to use new function New-VerboseMessage + - Removed ConnectSQL function and replaced with new Connect-SQL function - xSQLServerDatabaseOwner - - Removed ConnectSQL function and replaced with new Connect-SQL function + - Removed ConnectSQL function and replaced with new Connect-SQL function - xSQLServerDatabasePermissions - - Removed ConnectSQL function and replaced with new Connect-SQL function + - Removed ConnectSQL function and replaced with new Connect-SQL function - xSQLServerDatabaseRole - - Removed ConnectSQL function and replaced with new Connect-SQL function + - Removed ConnectSQL function and replaced with new Connect-SQL function - xSQLServerLogin - - Removed ConnectSQL function and replaced with new Connect-SQL function + - Removed ConnectSQL function and replaced with new Connect-SQL function - xSQLServerMaxDop - - Updated Verbose statements to use new function New-VerboseMessage - - Removed ConnectSQL function and replaced with new Connect-SQL function + - Updated Verbose statements to use new function New-VerboseMessage + - Removed ConnectSQL function and replaced with new Connect-SQL function - xSQLServerMemory - - Updated Verbose statements to use new function New-VerboseMessage - - Removed ConnectSQL function and replaced with new Connect-SQL function + - Updated Verbose statements to use new function New-VerboseMessage + - Removed ConnectSQL function and replaced with new Connect-SQL function - xSQLServerPowerPlan - - Updated Verbose statements to use new function New-VerboseMessage + - Updated Verbose statements to use new function New-VerboseMessage - Examples - - Added xSQLServerConfiguration resource example + - Added xSQLServerConfiguration resource example ## 1.5.0.0 diff --git a/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 b/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 index fb91dfe5f..fd8f30d8f 100644 --- a/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 +++ b/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 @@ -416,7 +416,7 @@ function Test-TargetResource try { - Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName -SetupCredential $userCredential | Out-Null + Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName -SetupCredential $userCredential -LoginType 'SqlLogin' | Out-Null } catch { diff --git a/DSCResources/MSFT_SqlSetup/MSFT_SqlSetup.psm1 b/DSCResources/MSFT_SqlSetup/MSFT_SqlSetup.psm1 index d300acef1..257b4a2cd 100644 --- a/DSCResources/MSFT_SqlSetup/MSFT_SqlSetup.psm1 +++ b/DSCResources/MSFT_SqlSetup/MSFT_SqlSetup.psm1 @@ -578,7 +578,10 @@ function Get-TargetResource Array of accounts to be made SQL administrators. .PARAMETER SecurityMode - Security mode to apply to the SQL Server instance. + Security mode to apply to the + SQL Server instance. 'SQL' indicates mixed-mode authentication while + 'Windows' indicates Windows authentication. + Default is Windows. { *Windows* | SQL } .PARAMETER SAPwd SA password, if SecurityMode is set to 'SQL'. @@ -753,6 +756,7 @@ function Set-TargetResource $SQLSysAdminAccounts, [Parameter()] + [ValidateSet('SQL', 'Windows')] [System.String] $SecurityMode, @@ -1525,7 +1529,10 @@ function Set-TargetResource Array of accounts to be made SQL administrators. .PARAMETER SecurityMode - Security mode to apply to the SQL Server instance. + Security mode to apply to the + SQL Server instance. 'SQL' indicates mixed-mode authentication while + 'Windows' indicates Windows authentication. + Default is Windows. { *Windows* | SQL } .PARAMETER SAPwd SA password, if SecurityMode is set to 'SQL'. @@ -1691,6 +1698,7 @@ function Test-TargetResource $SQLSysAdminAccounts, [Parameter()] + [ValidateSet('SQL', 'Windows')] [System.String] $SecurityMode, @@ -2113,13 +2121,15 @@ function Get-ServiceAccountParameters } } - '.*\$' + # Testing if account is a Managed Service Account, which ends with '$'. + '\$$' { $parameters = @{ "$($ServiceType)SVCACCOUNT" = $ServiceAccount.UserName } } + # Normal local or domain service account. default { $parameters = @{ diff --git a/DSCResources/MSFT_SqlSetup/MSFT_SqlSetup.schema.mof b/DSCResources/MSFT_SqlSetup/MSFT_SqlSetup.schema.mof index 77f20276a..c486ac153 100644 --- a/DSCResources/MSFT_SqlSetup/MSFT_SqlSetup.schema.mof +++ b/DSCResources/MSFT_SqlSetup/MSFT_SqlSetup.schema.mof @@ -23,7 +23,7 @@ class MSFT_SqlSetup : OMI_BaseResource [Read, Description("Output username for the SQL Agent service.")] String AgtSvcAccountUsername; [Write, Description("Collation for SQL.")] String SQLCollation; [Write, Description("Array of accounts to be made SQL administrators.")] String SQLSysAdminAccounts[]; - [Write, Description("Security mode to apply to the SQL Server instance.")] String SecurityMode; + [Write, Description("Security mode to apply to the SQL Server instance. 'SQL' indicates mixed-mode authentication while 'Windows' indicates Windows authentication. Default is Windows."), ValueMap{"SQL", "Windows"}, Values{"SQL", "Windows"}] String SecurityMode; [Write, EmbeddedInstance("MSFT_Credential"), Description("SA password, if SecurityMode is set to 'SQL'.")] String SAPwd; [Write, Description("Root path for SQL database files.")] String InstallSQLDataDir; [Write, Description("Path for SQL database files.")] String SQLUserDBDir; diff --git a/Examples/Resources/SqlSetup/4-InstallNamedInstanceInFailoverClusterFirstNode.ps1 b/Examples/Resources/SqlSetup/4-InstallNamedInstanceInFailoverClusterFirstNode.ps1 index 4a4168fbd..598ce1f9b 100644 --- a/Examples/Resources/SqlSetup/4-InstallNamedInstanceInFailoverClusterFirstNode.ps1 +++ b/Examples/Resources/SqlSetup/4-InstallNamedInstanceInFailoverClusterFirstNode.ps1 @@ -11,7 +11,7 @@ (Windows Server 2012 R2 and earlier) or cluster role (Windows Server 2016 and later). Also so that the Virtual Computer Object (VCO) can be removed when the Failover CLuster instance is uninstalled. - See the DSC resources xFailoverCluster, xStorage and iSCSIDsc for information how to setup a failover cluster + See the DSC resources xFailoverCluster, StorageDsc and iSCSIDsc for information how to setup a failover cluster with DSC. The resource is run using the SYSTEM account, but the setup is run using impersonation, with the credentials in diff --git a/README.md b/README.md index d966afeb2..85983d644 100644 --- a/README.md +++ b/README.md @@ -1456,7 +1456,7 @@ cluster. This is a limitation of SQL Server. See article [You cannot add or remove features to a SQL Server 2008, SQL Server 2008 R2, or SQL Server 2012 failover cluster](https://support.microsoft.com/en-us/help/2547273/you-cannot-add-or-remove-features-to-a-sql-server-2008,-sql-server-2008-r2,-or-sql-server-2012-failover-cluster). -#### Credentials +#### Credentials for running the resource ##### PsDscRunAsCredential @@ -1470,6 +1470,21 @@ performed by the SYSTEM account. When installing as the SYSTEM account, then parameter SQLSysAdminAccounts and ASSysAdminAccounts must be specified when installing feature Database Engine and Analysis Services respectively. +#### Credentials for service accounts + +##### Service Accounts + +Service account username containing dollar sign ('$') is allowed, but if the +dollar sign is at the end of the username it will be considered a Managed Service +Account. + +##### Managed Service Accounts + +If a service account username has a dollar sign at the end of the name it will +be considered a Managed Service Account. Any password passed in +the credential object will be ignored, meaning the account is not expected to +need a '*SVCPASSWORD' argument in the setup arguments. + #### Parameters * **`[String]` Action** _(Write)_: The action to be performed. Defaults to 'Install'. @@ -1513,8 +1528,10 @@ installing feature Database Engine and Analysis Services respectively. * **`[String]` SQLCollation** _(Write)_: Collation for SQL. * **`[String[]]` SQLSysAdminAccounts** _(Write)_: Array of accounts to be made SQL administrators. -* **`[String]` SecurityMode** _(Write)_: Security mode to apply to the SQL Server - instance. +* **`[String]` SecurityMode** _(Write)_: Security mode to apply to the + SQL Server instance. 'SQL' indicates mixed-mode authentication while + 'Windows' indicates Windows authentication. + Default is Windows. { *Windows* | SQL } * **`[PSCredential]` SAPwd** _(Write)_: SA password, if SecurityMode is set to 'SQL'. * **`[String]` InstallSQLDataDir** _(Write)_: Root path for SQL database files. * **`[String]` SQLUserDBDir** _(Write)_: Path for SQL database files. diff --git a/SQLServerDsc.psd1 b/SQLServerDsc.psd1 index eb740c274..5f25c81c2 100644 --- a/SQLServerDsc.psd1 +++ b/SQLServerDsc.psd1 @@ -1,6 +1,6 @@ @{ # Version number of this module. - ModuleVersion = '11.0.0.0' + moduleVersion = '11.1.0.0' # ID used to uniquely identify this module GUID = '693ee082-ed36-45a7-b490-88b07c86b42f' @@ -50,168 +50,31 @@ # ReleaseNotes of this module ReleaseNotes = '- Changes to SqlServerDsc - - BREAKING CHANGE: Resource SqlRSSecureConnectionLevel was remove - ([issue 990](https://github.com/PowerShell/SqlServerDsc/issues/990)). - The parameter that was set using that resource has been merged into resource - SqlRS as the parameter UseSsl. The UseSsl parameter is of type boolean. This - change was made because from SQL Server 2008 R2 this value is made an on/off - switch. Read more in the article [ConfigurationSetting Method - SetSecureConnectionLevel](https://docs.microsoft.com/en-us/sql/reporting-services/wmi-provider-library-reference/configurationsetting-method-setsecureconnectionlevel). - - Updated so that named parameters are used for New-Object cmdlet. This was - done to follow the style guideline. - - Updated manifest and license to reflect the new year - ([issue 965](https://github.com/PowerShell/SqlServerDsc/issues/965)). - - Added a README.md under Tests\Integration to help contributors to write - integration tests. - - Added "Integration tests" section in the CONTRIBUTING.md. - - The complete examples were removed. They were no longer accurate and some - referenced resources that no longer exist. Accurate examples can be found - in each specific resource example folder. Examples for installing Failover Cluster - can be found in the resource examples folders in the xFailOverCluster - resource module ([issue 462](https://github.com/PowerShell/SqlServerDsc/issues/462)). - - A README.md was created under the Examples folder to be used as reference how - to install certain scenarios ([issue 462](https://github.com/PowerShell/SqlServerDsc/issues/462)). - - Removed the local specific common test for compiling examples in this repository - and instead opted-in for the common test in the "DscResource.Tests" repository - ([issue 669](https://github.com/PowerShell/SqlServerDsc/issues/669)). - - Added new resource SqlServerDatabaseMail for configuring SQL Server - Database Mail ([issue 155](https://github.com/PowerShell/SqlServerDsc/issues/155)). - - Updated the helper function Test-SQLDscParameterState to handle the - data type UInt16. - - Fixed typo in SqlServerDscCommon.Tests. - - Updated README.md with known issue section for each resource. - - Resources that did not have a description in the README.md now has one. - - Resources that missed links to the examples in the README.md now has those - links. - - Style changes in all examples, removing type [System.Management.Automation.Credential()] - from credential parameters ([issue 1003](https://github.com/PowerShell/SqlServerDsc/issues/1003)), - and renamed the credential parameter so it is not using abbreviation. - - Updated the security token for AppVeyor status badge in README.md. When we - renamed the repository the security token was changed - ([issue 1012](https://github.com/PowerShell/SqlServerDsc/issues/1012)). - - Now the helper function Restart-SqlService, after restarting the SQL Server - service, does not return until it can connect to the SQL Server instance, and - the instance returns status "Online" ([issue 1008](https://github.com/PowerShell/SqlServerDsc/issues/1008)). - If it fails to connect within the timeout period (defaults to 120 seconds) it - throws an error. - - Fixed typo in comment-base help for helper function Test-AvailabilityReplicaSeedingModeAutomatic. - - Style cleanup in helper functions and tests. -- Changes to SqlAG - - Fixed typos in tests. - - Style cleanup in code and tests. -- Changes to SqlAGDatabase - - Style cleanup in code and tests. -- Changes to SqlAGListener - - Fixed typo in comment-based help. - - Style cleanup in code and tests. -- Changes to SqlAGReplica - - Minor code style cleanup. Removed unused variable and instead piped the cmdlet - Join-SqlAvailabilityGroup to Out-Null. - - Fixed minor typos in comment-based help. - - Fixed minor typos in comment. - - Style cleanup in code and tests. - - Updated description for parameter Name in README.md and in comment-based help - ([issue 1034](https://github.com/PowerShell/SqlServerDsc/issues/1034)). -- Changes to SqlAlias - - Fixed issue where exception was thrown if reg keys did not exist - ([issue 949](https://github.com/PowerShell/SqlServerDsc/issues/949)). - - Style cleanup in tests. -- Changes to SqlAlwaysOnService - - Refactor integration tests slightly to improve run time performance - ([issue 1001](https://github.com/PowerShell/SqlServerDsc/issues/1001)). - - Style cleanup in code and tests. -- Changes to SqlDatabase - - Fix minor Script Analyzer warning. -- Changes to SqlDatabaseDefaultLocation - - Refactor integration tests slightly to improve run time performance - ([issue 1001](https://github.com/PowerShell/SqlServerDsc/issues/1001)). - - Minor style cleanup of code in tests. -- Changes to SqlDatabaseRole - - Style cleanup in tests. -- Changes to SqlRS - - Replaced Get-WmiObject with Get-CimInstance to fix Script Analyzer warnings - ([issue 264](https://github.com/PowerShell/SqlServerDsc/issues/264)). - - Refactored the resource to use Invoke-CimMethod. - - Added parameter UseSsl which when set to $true forces connections to the - Reporting Services to use SSL when connecting ([issue 990](https://github.com/PowerShell/SqlServerDsc/issues/990)). - - Added complete example for SqlRS (based on the integration tests) - ([issue 634](https://github.com/PowerShell/SqlServerDsc/issues/634)). - - Refactor integration tests slightly to improve run time performance - ([issue 1001](https://github.com/PowerShell/SqlServerDsc/issues/1001)). - - Style cleanup in code and tests. -- Changes to SqlScript - - Style cleanup in tests. - - Updated examples. - - Added integration tests. - - Fixed minor typos in comment-based help. - - Added new example based on integration test. -- Changes to SqlServerConfiguration - - Fixed minor typos in comment-based help. - - Now the verbose message say what option is changing and to what value - ([issue 1014](https://github.com/PowerShell/SqlServerDsc/issues/1014)). - - Changed the RestartTimeout parameter from type SInt32 to type UInt32. - - Added localization ([issue 605](https://github.com/PowerShell/SqlServerDsc/issues/605)). - - Style cleanup in code and tests. -- Changes to SqlServerEndpoint - - Updated README.md with links to the examples - ([issue 504](https://github.com/PowerShell/SqlServerDsc/issues/504)). - - Style cleanup in tests. + - Updated the PULL\_REQUEST\_TEMPLATE with an improved task list and modified + some text to be clearer ([issue 973](https://github.com/PowerShell/SqlServerDsc/issues/973)). + - Updated the ISSUE_TEMPLATE to hopefully be more intuitive and easier to use. + - Added information to ISSUE_TEMPLATE that issues must be reproducible in + SqlServerDsc resource module (if running the older xSQLServer resource module) + ([issue 1036](https://github.com/PowerShell/SqlServerDsc/issues/1036)). + - Updated ISSUE_TEMPLATE.md with a note about sensitive information ([issue 1092](https://github.com/PowerShell/SqlServerDsc/issues/1092)). - Changes to SqlServerLogin - - Added integration tests ([issue 748](https://github.com/PowerShell/SqlServerDsc/issues/748)). - - Minor code style cleanup. - - Removed unused variable and instead piped the helper function Connect-SQL to - Out-Null. - - Style cleanup in tests. -- Changes to SqlServerMaxDop - - Minor style changes in the helper function Get-SqlDscDynamicMaxDop. -- Changes to SqlServerMemory - - Style cleanup in code and tests. -- Changes to SqlServerPermission - - Fixed minor typos in comment-based help. - - Style cleanup in code. -- Changes to SqlServerReplication - - Fixed minor typos in verbose messages. - - Style cleanup in tests. -- Changes to SqlServerNetwork - - Added sysadmin account parameter usage to the examples. -- Changes to SqlServerReplication - - Fix Script Analyzer warning ([issue 263](https://github.com/PowerShell/SqlServerDsc/issues/263)). -- Changes to SqlServerRole - - Added localization ([issue 621](https://github.com/PowerShell/SqlServerDsc/issues/621)). - - Added integration tests ([issue 756](https://github.com/PowerShell/SqlServerDsc/issues/756)). - - Updated example to add two server roles in the same configuration. - - Style cleanup in tests. -- Changes to SqlServiceAccount - - Default services are now properly detected - ([issue 930](https://github.com/PowerShell/SqlServerDsc/issues/930)). - - Made the description of parameter RestartService more descriptive - ([issue 960](https://github.com/PowerShell/SqlServerDsc/issues/960)). - - Added a read-only parameter ServiceAccountName so that the service account - name is correctly returned as a string ([issue 982](https://github.com/PowerShell/SqlServerDsc/issues/982)). - - Added integration tests ([issue 980](https://github.com/PowerShell/SqlServerDsc/issues/980)). - - The timing issue that the resource returned before SQL Server service was - actually restarted has been solved by a change in the helper function - Restart-SqlService ([issue 1008](https://github.com/PowerShell/SqlServerDsc/issues/1008)). - Now Restart-SqlService waits for the instance to return status "Online" or - throws an error saying it failed to connect within the timeout period. - - Style cleanup in code and tests. + - [Claudio Spizzi (@claudiospizzi)](https://github.com/claudiospizzi): Fix password + test fails for nativ sql users ([issue 1048](https://github.com/PowerShell/SqlServerDsc/issues/1048)). - Changes to SqlSetup - - Added parameter `ASServerMode` to support installing Analysis Services in - Multidimensional mode, Tabular mode and PowerPivot mode - ([issue 388](https://github.com/PowerShell/SqlServerDsc/issues/388)). - - Added integration tests for testing Analysis Services Multidimensional mode - and Tabular mode. - - Cleaned up integration tests. - - Added integration tests for installing a default instance of Database Engine. - - Refactor integration tests slightly to improve run time performance - ([issue 1001](https://github.com/PowerShell/SqlServerDsc/issues/1001)). - - Added PSSA rule "PSUseDeclaredVarsMoreThanAssignments" override in the - function Set-TargetResource for the variable $global:DSCMachineStatus. - - Style cleanup in code and tests. -- Changes to SqlWaitForAG - - Style cleanup in code. -- Changes to SqlWindowsFirewall - - Fixed minor typos in comment-based help. - - Style cleanup in code. + - [Michael Fyffe (@TraGicCode)](https://github.com/TraGicCode): Clarify usage + of "SecurityMode" along with adding parameter validations for the only 2 + supported values ([issue 1010](https://github.com/PowerShell/SqlServerDsc/issues/1010)). + - Now accounts containing "$" will be able to be used for installing + SQL Server. Although, if the account ends with "$" it is considered a + Managed Service Account ([issue 1055](https://github.com/PowerShell/SqlServerDsc/issues/1055)). +- Changes to Integration Tests + - [Michael Fyffe (@TraGicCode)](https://github.com/TraGicCode): Replace xStorage + dsc resource module with StorageDsc ([issue 1038](https://github.com/PowerShell/SqlServerDsc/issues/1038)). +- Changes to Unit Tests + - [Michael Fyffe (@TraGicCode)](https://github.com/TraGicCode): Updated + the following resources unit test template to version 1.2.1 + - SqlAlias ([issue 999](https://github.com/PowerShell/SqlServerDsc/issues/999)). + - SqlWindowsFirewall ([issue 1089](https://github.com/PowerShell/SqlServerDsc/issues/1089)). ' @@ -231,3 +94,4 @@ + diff --git a/SqlServerDscHelper.psm1 b/SqlServerDscHelper.psm1 index e961ff804..84d7e2de0 100644 --- a/SqlServerDscHelper.psm1 +++ b/SqlServerDscHelper.psm1 @@ -18,6 +18,11 @@ $script:localizedData = Get-LocalizedData -ResourceName 'SqlServerDscHelper' -Sc .PARAMETER SetupCredential PSCredential object with the credentials to use to impersonate a user when connecting. If this is not provided then the current user will be used to connect to the SQL Server Database Engine instance. + + .PARAMETER LoginType + If the SetupCredential is set, specify with this parameter, which type + of credentials are set: Native SQL login or Windows user Login. Default + value is 'WindowsUser'. #> function Connect-SQL { @@ -34,7 +39,12 @@ function Connect-SQL [ValidateNotNull()] [System.Management.Automation.PSCredential] - $SetupCredential + $SetupCredential, + + [Parameter()] + [ValidateSet('WindowsUser', 'SqlLogin')] + [System.String] + $LoginType = 'WindowsUser' ) Import-SQLPSModule @@ -51,9 +61,21 @@ function Connect-SQL if ($SetupCredential) { $sql = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server - $sql.ConnectionContext.ConnectAsUser = $true - $sql.ConnectionContext.ConnectAsUserPassword = $SetupCredential.GetNetworkCredential().Password - $sql.ConnectionContext.ConnectAsUserName = $SetupCredential.GetNetworkCredential().UserName + + if ($LoginType -eq 'SqlLogin') + { + $sql.ConnectionContext.LoginSecure = $false + $sql.ConnectionContext.Login = $SetupCredential.Username + $sql.ConnectionContext.SecurePassword = $SetupCredential.Password + } + + if ($LoginType -eq 'WindowsUser') + { + $sql.ConnectionContext.ConnectAsUser = $true + $sql.ConnectionContext.ConnectAsUserPassword = $SetupCredential.GetNetworkCredential().Password + $sql.ConnectionContext.ConnectAsUserName = $SetupCredential.GetNetworkCredential().UserName + } + $sql.ConnectionContext.ServerInstance = $databaseEngineInstance $sql.ConnectionContext.Connect() } diff --git a/Tests/Integration/MSFT_SqlRS.config.ps1 b/Tests/Integration/MSFT_SqlRS.config.ps1 index 68485a069..4c47de379 100644 --- a/Tests/Integration/MSFT_SqlRS.config.ps1 +++ b/Tests/Integration/MSFT_SqlRS.config.ps1 @@ -46,18 +46,18 @@ Configuration MSFT_SqlRS_CreateDependencies_Config ) Import-DscResource -ModuleName 'PSDscResources' - Import-DscResource -ModuleName 'xStorage' + Import-DscResource -ModuleName 'StorageDsc' Import-DscResource -ModuleName 'SqlServerDsc' node localhost { - xMountImage 'MountIsoMedia' + MountImage 'MountIsoMedia' { ImagePath = $Node.ImagePath DriveLetter = $Node.DriveLetter Ensure = 'Present' } - xWaitForVolume 'WaitForMountOfIsoMedia' + WaitForVolume 'WaitForMountOfIsoMedia' { DriveLetter = $Node.DriveLetter RetryIntervalSec = 5 @@ -91,7 +91,7 @@ Configuration MSFT_SqlRS_CreateDependencies_Config ForceReboot = $Node.ForceReboot DependsOn = @( - '[xWaitForVolume]WaitForMountOfIsoMedia' + '[WaitForVolume]WaitForMountOfIsoMedia' '[User]CreateReportingServicesServiceAccount' '[WindowsFeature]NetFramework45' ) diff --git a/Tests/Integration/MSFT_SqlSetup.config.ps1 b/Tests/Integration/MSFT_SqlSetup.config.ps1 index 7facbb9e8..9f0b08d79 100644 --- a/Tests/Integration/MSFT_SqlSetup.config.ps1 +++ b/Tests/Integration/MSFT_SqlSetup.config.ps1 @@ -95,17 +95,17 @@ Configuration MSFT_SqlSetup_CreateDependencies_Config ) Import-DscResource -ModuleName 'PSDscResources' - Import-DscResource -ModuleName 'xStorage' + Import-DscResource -ModuleName 'StorageDsc' node localhost { - xMountImage 'MountIsoMedia' + MountImage 'MountIsoMedia' { ImagePath = $Node.ImagePath DriveLetter = $Node.DriveLetter Ensure = 'Present' } - xWaitForVolume WaitForMountOfIsoMedia + WaitForVolume WaitForMountOfIsoMedia { DriveLetter = $Node.DriveLetter RetryIntervalSec = 5 diff --git a/Tests/Unit/MSFT_SqlAlias.Tests.ps1 b/Tests/Unit/MSFT_SqlAlias.Tests.ps1 index a9c073d39..eee3a1096 100644 --- a/Tests/Unit/MSFT_SqlAlias.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlAlias.Tests.ps1 @@ -1,1228 +1,1227 @@ -$script:DSCModuleName = 'SqlServerDsc' -$script:DSCResourceName = 'MSFT_SqlAlias' - #region HEADER -# Unit Test Template Version: 1.1.0 -[System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) +# Unit Test Template Version: 1.2.1 +$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) { - & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) + & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests')) } -Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force -$TestEnvironment = Initialize-TestEnvironment -DSCModuleName $script:DSCModuleName ` - -DSCResourceName $script:DSCResourceName ` - -TestType Unit +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName 'SqlServerDsc' ` + -DSCResourceName 'MSFT_SqlAlias' ` + -TestType Unit #endregion HEADER +function Invoke-TestSetup { +} + +function Invoke-TestCleanup { + Restore-TestEnvironment -TestEnvironment $TestEnvironment +} + +# Begin Testing try { - #region Pester Test Initialization - $registryPath = 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' - $registryPathWow6432Node = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' - - $name = 'MyAlias' - $serverNameTcp = 'sqlnode.company.local' - $tcpPort = 1433 - $UseDynamicTcpPort = $false - $serverNameNamedPipes = 'sqlnode' - $pipeName = "\\$serverNameNamedPipes\PIPE\sql\query" - - $unknownName = 'UnknownAlias' - $unknownServerName = 'unknownserver' - - $nameDifferentTcpPort = 'DifferentTcpPort' - $nameDifferentServerNameTcp = 'DifferentServerNameTcp' - $nameDifferentPipeName = 'DifferentPipeName' - $differentTcpPort = 1500 - $differentServerNameTcp = "$unknownServerName.company.local" - $differentPipeName = "\\$unknownServerName\PIPE\sql\query" - - $nameWow6432NodeDifferFrom64BitOS = 'Wow6432NodeDifferFrom64BitOS' - - $defaultParameters = @{ - Name = $name - ServerName = $serverNameTcp - Protocol = 'TCP' - TcpPort = '1433' - Ensure = 'Present' - } - #endregion Pester Test Initialization - - #region Get-TargetResource - Describe "$($script:DSCResourceName)\Get-TargetResource" { - # Mocking for protocol TCP - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' - } - } -ModuleName $script:DSCResourceName -Verifiable + Invoke-TestSetup + + InModuleScope 'MSFT_SqlAlias' { + $registryPath = 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' + $registryPathWow6432Node = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' + + $name = 'MyAlias' + $serverNameTcp = 'sqlnode.company.local' + $tcpPort = 1433 + $UseDynamicTcpPort = $false + $serverNameNamedPipes = 'sqlnode' + $pipeName = "\\$serverNameNamedPipes\PIPE\sql\query" + + $unknownName = 'UnknownAlias' + $unknownServerName = 'unknownserver' + + $nameDifferentTcpPort = 'DifferentTcpPort' + $nameDifferentServerNameTcp = 'DifferentServerNameTcp' + $nameDifferentPipeName = 'DifferentPipeName' + $differentTcpPort = 1500 + $differentServerNameTcp = "$unknownServerName.company.local" + $differentPipeName = "\\$unknownServerName\PIPE\sql\query" + + $nameWow6432NodeDifferFrom64BitOS = 'Wow6432NodeDifferFrom64BitOS' + + $defaultParameters = @{ + Name = $name + ServerName = $serverNameTcp + Protocol = 'TCP' + TcpPort = '1433' + Ensure = 'Present' + } - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' - } - } -ModuleName $script:DSCResourceName -Verifiable + Describe 'SqlAlias\Get-TargetResource' { + # Mocking for protocol TCP + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + } + } -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $nameDifferentTcpPort } -MockWith { - return @{ - 'DifferentTcpPort' = 'DBMSSOCN,sqlnode.company.local,1500' - } - } -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + } + } -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $nameDifferentTcpPort } -MockWith { - return @{ - 'DifferentTcpPort' = 'DBMSSOCN,sqlnode.company.local,1500' - } - } -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $nameDifferentTcpPort } -MockWith { + return @{ + 'DifferentTcpPort' = 'DBMSSOCN,sqlnode.company.local,1500' + } + } -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $nameDifferentServerNameTcp } -MockWith { - return @{ - 'DifferentServerNameTcp' = 'DBMSSOCN,unknownserver.company.local,1433' - } - } -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $nameDifferentTcpPort } -MockWith { + return @{ + 'DifferentTcpPort' = 'DBMSSOCN,sqlnode.company.local,1500' + } + } -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $nameDifferentServerNameTcp } -MockWith { - return @{ - 'DifferentServerNameTcp' = 'DBMSSOCN,unknownserver.company.local,1433' - } - } -ModuleName $script:DSCResourceName -Verifiable - - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $unknownName } -MockWith { - return $null - } -ModuleName $script:DSCResourceName -Verifiable - - # Mocking 64-bit OS - Mock -CommandName Get-CimInstance -MockWith { - return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force - } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -ModuleName $script:DSCResourceName -Verifiable - - Context 'When the system is in the desired present state for 64-bit OS using TCP' { - $testParameters = @{ - Name = $name - ServerName = $serverNameTcp - } + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $nameDifferentServerNameTcp } -MockWith { + return @{ + 'DifferentServerNameTcp' = 'DBMSSOCN,unknownserver.company.local,1433' + } + } -Verifiable + + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $nameDifferentServerNameTcp } -MockWith { + return @{ + 'DifferentServerNameTcp' = 'DBMSSOCN,unknownserver.company.local,1433' + } + } -Verifiable - $result = Get-TargetResource @testParameters + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $unknownName } -MockWith { + return $null + } -Verifiable - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } + # Mocking 64-bit OS + Mock -CommandName Get-CimInstance -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force + } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - } + Context 'When the system is in the desired present state for 64-bit OS using TCP' { + $testParameters = @{ + Name = $name + ServerName = $serverNameTcp + } - It 'Should return TCP as the protocol used' { - $result.Protocol | Should -Be 'TCP' - } + $result = Get-TargetResource @testParameters - It "Should return $tcpPort as the port number used" { - $result.TcpPort | Should -Be $tcpPort - } - It 'Should not return any pipe name' { - $result.PipeName | Should -Be '' - } + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' + } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It 'Should return TCP as the protocol used' { + $result.Protocol | Should -Be 'TCP' + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It "Should return $tcpPort as the port number used" { + $result.TcpPort | Should -Be $tcpPort + } - Context 'When the system is in the desired absent state for 64-bit OS' { - $testParameters = @{ - Name = $unknownName - ServerName = $unknownServerName - } + It 'Should not return any pipe name' { + $result.PipeName | Should -Be '' + } - $result = Get-TargetResource @testParameters + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should return the state as absent' { - $result.Ensure | Should -Be 'Absent' - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should not return any protocol' { - $result.Protocol | Should -Be '' - } + Context 'When the system is in the desired absent state for 64-bit OS' { + $testParameters = @{ + Name = $unknownName + ServerName = $unknownServerName + } - It 'Should not return a port number' { - $result.TcpPort | Should -Be 0 - } + $result = Get-TargetResource @testParameters - It 'Should not return any pipe name' { - $result.PipeName | Should -Be '' - } + It 'Should return the state as absent' { + $result.Ensure | Should -Be 'Absent' + } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It 'Should not return any protocol' { + $result.Protocol | Should -Be '' + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It 'Should not return a port number' { + $result.TcpPort | Should -Be 0 + } - Context 'When the system is not in the desired state because TcpPort is different when desired protocol is TCP' { - $testParameters = @{ - Name = $nameDifferentTcpPort - ServerName = $serverNameTcp - } + It 'Should not return any pipe name' { + $result.PipeName | Should -Be '' + } - $result = Get-TargetResource @testParameters + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should return TCP as the protocol used' { - $result.Protocol | Should -Be 'TCP' - } + Context 'When the system is not in the desired state because TcpPort is different when desired protocol is TCP' { + $testParameters = @{ + Name = $nameDifferentTcpPort + ServerName = $serverNameTcp + } - It "Should return $differentTcpPort as the port number used" { - $result.TcpPort | Should -Be $differentTcpPort - } + $result = Get-TargetResource @testParameters - It 'Should not return any pipe name' { - $result.PipeName | Should -Be '' - } + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' + } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It 'Should return TCP as the protocol used' { + $result.Protocol | Should -Be 'TCP' + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It "Should return $differentTcpPort as the port number used" { + $result.TcpPort | Should -Be $differentTcpPort + } - Context 'When the system is not in the desired state because ServerName is different when desired protocol is TCP' { - $testParameters = @{ - Name = $nameDifferentServerNameTcp - ServerName = $serverNameTcp - } + It 'Should not return any pipe name' { + $result.PipeName | Should -Be '' + } - $result = Get-TargetResource @testParameters + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should return different server name than the one passed as parameter' { - $result.ServerName | Should -Be $differentServerNameTcp - } + Context 'When the system is not in the desired state because ServerName is different when desired protocol is TCP' { + $testParameters = @{ + Name = $nameDifferentServerNameTcp + ServerName = $serverNameTcp + } - It 'Should return TCP as the protocol used' { - $result.Protocol | Should -Be 'TCP' - } + $result = Get-TargetResource @testParameters - It "Should return $tcpPort as the port number used" { - $result.TcpPort | Should -Be $tcpPort - } + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' + } - It 'Should not return any pipe name' { - $result.PipeName | Should -Be '' - } + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It 'Should return different server name than the one passed as parameter' { + $result.ServerName | Should -Be $differentServerNameTcp + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It 'Should return TCP as the protocol used' { + $result.Protocol | Should -Be 'TCP' + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It "Should return $tcpPort as the port number used" { + $result.TcpPort | Should -Be $tcpPort + } + + It 'Should not return any pipe name' { + $result.PipeName | Should -Be '' + } - Context 'When the system is not in the desired present state for 64-bit OS using UseDynamicTcpPort' { - $testParameters = @{ - Name = $name - ServerName = $serverNameTcp + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context + + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context + + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - # Testing protocol TCP "With Dynamically determine port" - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + Context 'When the system is not in the desired present state for 64-bit OS using UseDynamicTcpPort' { + $testParameters = @{ + Name = $name + ServerName = $serverNameTcp } - } -ModuleName $script:DSCResourceName -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + # Testing protocol TCP "With Dynamically determine port" + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + } + } -Verifiable + + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + } + } -Verifiable + + # Mocking 64-bit OS + Mock -CommandName Get-CimInstance -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force + } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable + + $result = Get-TargetResource @testParameters + + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' } - } -ModuleName $script:DSCResourceName -Verifiable - # Mocking 64-bit OS - Mock -CommandName Get-CimInstance -MockWith { - return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force - } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -ModuleName $script:DSCResourceName -Verifiable + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName + } + + It 'Should return TCP as the protocol used' { + $result.Protocol | Should -Be 'TCP' + } - $result = Get-TargetResource @testParameters + It 'Should return the UseDynamicTcpPort parameter as false' { + $result.UseDynamicTcpPort | Should -Be $false + } - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } + It "Should not return any pipe name" { + $result.PipeName | Should -Be '' + } - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - } + It 'Should not return any TCP Port' { + $result.TcpPort | Should -Be 1433 + } - It 'Should return TCP as the protocol used' { - $result.Protocol | Should -Be 'TCP' - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should return the UseDynamicTcpPort parameter as false' { - $result.UseDynamicTcpPort | Should -Be $false - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - It "Should not return any pipe name" { - $result.PipeName | Should -Be '' + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should not return any TCP Port' { - $result.TcpPort | Should -Be 1433 - } + # Mocking 32-bit OS + Mock -CommandName Get-CimInstance -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '32-bit' -PassThru -Force + } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the system is in the desired present state for 32-bit OS using TCP' { + $testParameters = @{ + Name = $name + ServerName = $serverNameTcp + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + $result = Get-TargetResource @testParameters - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' + } - # Mocking 32-bit OS - Mock -CommandName Get-CimInstance -MockWith { - return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '32-bit' -PassThru -Force - } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -ModuleName $script:DSCResourceName -Verifiable + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName + $result.Protocol | Should -Be $defaultParameters.Protocol + $result.TcpPort | Should -Be $defaultParameters.TcpPort + } - Context 'When the system is in the desired present state for 32-bit OS using TCP' { - $testParameters = @{ - Name = $name - ServerName = $serverNameTcp - } + It 'Should not return any pipe name' { + $result.PipeName | Should -Be '' + } - $result = Get-TargetResource @testParameters + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context + } - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - $result.Protocol | Should -Be $defaultParameters.Protocol - $result.TcpPort | Should -Be $defaultParameters.TcpPort + It 'Should not call the Get-ItemProperty for the Wow6432Node-path' { + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 0 -Scope Context + } } - It 'Should not return any pipe name' { - $result.PipeName | Should -Be '' - } + Context 'When the system is in the desired present state for 32-bit OS using UseDynamicTcpPort' { + $testParameters = @{ + Name = $name + ServerName = $serverNameTcp + } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + # Testing protocol TCP "With Dynamically determine port" + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + } + } -Verifiable - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + } + } -Verifiable - It 'Should not call the Get-ItemProperty for the Wow6432Node-path' { - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 0 -ModuleName $script:DSCResourceName -Scope Context - } - } + $result = Get-TargetResource @testParameters - Context 'When the system is in the desired present state for 32-bit OS using UseDynamicTcpPort' { - $testParameters = @{ - Name = $name - ServerName = $serverNameTcp - } + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' + } - # Testing protocol TCP "With Dynamically determine port" - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName } - } -ModuleName $script:DSCResourceName -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + It 'Should return TCP as the protocol used' { + $result.Protocol | Should -Be 'TCP' } - } -ModuleName $script:DSCResourceName -Verifiable - $result = Get-TargetResource @testParameters + It 'Should return the UseDynamicTcpPort parameter as false' { + $result.UseDynamicTcpPort | Should -Be $false + } - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } + It "Should not return any pipe name" { + $result.PipeName | Should -Be '' + } - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - } + It 'Should not return any TCP Port' { + $result.TcpPort | Should -Be 1433 + } - It 'Should return TCP as the protocol used' { - $result.Protocol | Should -Be 'TCP' - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should return the UseDynamicTcpPort parameter as false' { - $result.UseDynamicTcpPort | Should -Be $false - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context + } - It "Should not return any pipe name" { - $result.PipeName | Should -Be '' + It 'Should not call the Get-ItemProperty for the Wow6432Node-path' { + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 0 -Scope Context + } } - It 'Should not return any TCP Port' { - $result.TcpPort | Should -Be 1433 - } + Context 'When the system is in the desired absent state for 32-bit OS' { + $testParameters = @{ + Name = $unknownName + ServerName = $unknownServerName + } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + $result = Get-TargetResource @testParameters - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } + It 'Should return the state as absent' { + $result.Ensure | Should -Be 'Absent' + } - It 'Should not call the Get-ItemProperty for the Wow6432Node-path' { - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 0 -ModuleName $script:DSCResourceName -Scope Context - } - } + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName + } - Context 'When the system is in the desired absent state for 32-bit OS' { - $testParameters = @{ - Name = $unknownName - ServerName = $unknownServerName - } + It 'Should not return any protocol' { + $result.Protocol | Should -Be '' + } - $result = Get-TargetResource @testParameters + It 'Should not return a port number' { + $result.TcpPort | Should -Be 0 + } - It 'Should return the state as absent' { - $result.Ensure | Should -Be 'Absent' - } + It 'Should not return any pipe name' { + $result.PipeName | Should -Be '' + } - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should not return any protocol' { - $result.Protocol | Should -Be '' - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context + } - It 'Should not return a port number' { - $result.TcpPort | Should -Be 0 + It 'Should not call the Get-ItemProperty for the Wow6432Node-path' { + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 0 -Scope Context + } } - It 'Should not return any pipe name' { - $result.PipeName | Should -Be '' - } + # Testing protocol NP (Named Pipes) + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBNMPNTW,\\sqlnode\PIPE\sql\query' + } + } -Verifiable - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBNMPNTW,\\sqlnode\PIPE\sql\query' + } + } -Verifiable - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $nameDifferentPipeName } -MockWith { + return @{ + 'DifferentPipeName' = 'DBNMPNTW,\\unknownserver\PIPE\sql\query' + } + } -Verifiable - It 'Should not call the Get-ItemProperty for the Wow6432Node-path' { - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 0 -ModuleName $script:DSCResourceName -Scope Context - } - } + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $nameDifferentPipeName } -MockWith { + return @{ + 'DifferentPipeName' = 'DBNMPNTW,\\unknownserver\PIPE\sql\query' + } + } -Verifiable - # Testing protocol NP (Named Pipes) - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBNMPNTW,\\sqlnode\PIPE\sql\query' - } - } -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $nameWow6432NodeDifferFrom64BitOS } -MockWith { + return @{ + 'Wow6432NodeDifferFrom64BitOS' = 'DBNMPNTW,\\firstserver\PIPE\sql\query' + } + } -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBNMPNTW,\\sqlnode\PIPE\sql\query' - } - } -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $nameWow6432NodeDifferFrom64BitOS } -MockWith { + return @{ + 'Wow6432NodeDifferFrom64BitOS' = 'DBNMPNTW,\\secondserver\PIPE\sql\query' + } + } -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $nameDifferentPipeName } -MockWith { - return @{ - 'DifferentPipeName' = 'DBNMPNTW,\\unknownserver\PIPE\sql\query' - } - } -ModuleName $script:DSCResourceName -Verifiable + # Mocking 64-bit OS + Mock -CommandName Get-CimInstance -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force + } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $nameDifferentPipeName } -MockWith { - return @{ - 'DifferentPipeName' = 'DBNMPNTW,\\unknownserver\PIPE\sql\query' - } - } -ModuleName $script:DSCResourceName -Verifiable + Context 'When the system is in the desired present state for 64-bit OS using Named Pipes' { + $testParameters = @{ + Name = $name + ServerName = $serverNameNamedPipes + } - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $nameWow6432NodeDifferFrom64BitOS } -MockWith { - return @{ - 'Wow6432NodeDifferFrom64BitOS' = 'DBNMPNTW,\\firstserver\PIPE\sql\query' - } - } -ModuleName $script:DSCResourceName -Verifiable + $result = Get-TargetResource @testParameters - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $nameWow6432NodeDifferFrom64BitOS } -MockWith { - return @{ - 'Wow6432NodeDifferFrom64BitOS' = 'DBNMPNTW,\\secondserver\PIPE\sql\query' - } - } -ModuleName $script:DSCResourceName -Verifiable - - # Mocking 64-bit OS - Mock -CommandName Get-CimInstance -MockWith { - return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force - } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -ModuleName $script:DSCResourceName -Verifiable - - Context 'When the system is in the desired present state for 64-bit OS using Named Pipes' { - $testParameters = @{ - Name = $name - ServerName = $serverNameNamedPipes - } + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' + } - $result = Get-TargetResource @testParameters + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName + } - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } + It 'Should return NP as the protocol used' { + $result.Protocol | Should -Be 'NP' + } - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - } + It 'Should not return a port number' { + $result.TcpPort | Should -Be 0 + } - It 'Should return NP as the protocol used' { - $result.Protocol | Should -Be 'NP' - } + It 'Should return the correct pipe name based on the passed ServerName parameter' { + $result.PipeName | Should -Be "\\$serverNameNamedPipes\PIPE\sql\query" + } - It 'Should not return a port number' { - $result.TcpPort | Should -Be 0 - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should return the correct pipe name based on the passed ServerName parameter' { - $result.PipeName | Should -Be "\\$serverNameNamedPipes\PIPE\sql\query" + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context + + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the system is not in the desired state because ServerName is different when desired protocol is Named Pipes' { + $testParameters = @{ + Name = $nameDifferentPipeName + ServerName = $serverNameNamedPipes + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + $result = Get-TargetResource @testParameters - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' + } - Context 'When the system is not in the desired state because ServerName is different when desired protocol is Named Pipes' { - $testParameters = @{ - Name = $nameDifferentPipeName - ServerName = $serverNameNamedPipes - } + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName + } - $result = Get-TargetResource @testParameters + It 'Should return NP as the protocol used' { + $result.Protocol | Should -Be 'NP' + } - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } + It 'Should not return a port number' { + $result.TcpPort | Should -Be 0 + } - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - } + It 'Should return the correct pipe name based on the passed ServerName parameter' { + $result.PipeName | Should -Be $differentPipeName + } - It 'Should return NP as the protocol used' { - $result.Protocol | Should -Be 'NP' - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should not return a port number' { - $result.TcpPort | Should -Be 0 - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - It 'Should return the correct pipe name based on the passed ServerName parameter' { - $result.PipeName | Should -Be $differentPipeName + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the state differ between 32-bit OS and 64-bit OS registry keys' { + $testParameters = @{ + Name = $nameWow6432NodeDifferFrom64BitOS + ServerName = $serverNameNamedPipes + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + $result = Get-TargetResource @testParameters - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It 'Should return the state as absent' { + $result.Ensure | Should -Be 'Absent' + } - Context 'When the state differ between 32-bit OS and 64-bit OS registry keys' { - $testParameters = @{ - Name = $nameWow6432NodeDifferFrom64BitOS - ServerName = $serverNameNamedPipes - } + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName + } - $result = Get-TargetResource @testParameters + It 'Should not return any protocol' { + $result.Protocol | Should -Be '' + } - It 'Should return the state as absent' { - $result.Ensure | Should -Be 'Absent' - } + It 'Should not return a port number' { + $result.TcpPort | Should -Be 0 + } - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - } + It 'Should not return any pipe name' { + $result.PipeName | Should -Be '' + } - It 'Should not return any protocol' { - $result.Protocol | Should -Be '' - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should not return a port number' { - $result.TcpPort | Should -Be 0 - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - It 'Should not return any pipe name' { - $result.PipeName | Should -Be '' + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + # Mocking 32-bit OS + Mock -CommandName Get-CimInstance -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '32-bit' -PassThru -Force + } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the system is in the desired present state for 32-bit OS using Named Pipes' { + $testParameters = @{ + Name = $name + ServerName = $serverNameNamedPipes + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + $result = Get-TargetResource @testParameters - # Mocking 32-bit OS - Mock -CommandName Get-CimInstance -MockWith { - return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '32-bit' -PassThru -Force - } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -ModuleName $script:DSCResourceName -Verifiable + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' + } - Context 'When the system is in the desired present state for 32-bit OS using Named Pipes' { - $testParameters = @{ - Name = $name - ServerName = $serverNameNamedPipes - } + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName + } - $result = Get-TargetResource @testParameters + It 'Should return NP as the protocol used' { + $result.Protocol | Should -Be 'NP' + } - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } + It 'Should not return a port number' { + $result.TcpPort | Should -Be 0 + } - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - } + It 'Should return the correct pipe name based on the passed ServerName parameter' { + $result.PipeName | Should -Be $pipeName + } - It 'Should return NP as the protocol used' { - $result.Protocol | Should -Be 'NP' - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should not return a port number' { - $result.TcpPort | Should -Be 0 - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context + } - It 'Should return the correct pipe name based on the passed ServerName parameter' { - $result.PipeName | Should -Be $pipeName + It 'Should not call the Get-ItemProperty for the Wow6432Node-path' { + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 0 -Scope Context + } } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + # Mocking 64-bit OS + Mock -CommandName Get-CimInstance -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force + } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } + Context 'When the system is in the desired present state for 64-bit OS using UseDynamicTcpPort' { + $testParameters = @{ + Name = $name + ServerName = $serverNameTcp + } - It 'Should not call the Get-ItemProperty for the Wow6432Node-path' { - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 0 -ModuleName $script:DSCResourceName -Scope Context - } - } + # Testing protocol TCP "With Dynamically determine port" + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local' + } + } -Verifiable - # Mocking 64-bit OS - Mock -CommandName Get-CimInstance -MockWith { - return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force - } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local' + } + } -Verifiable - Context 'When the system is in the desired present state for 64-bit OS using UseDynamicTcpPort' { - $testParameters = @{ - Name = $name - ServerName = $serverNameTcp - } + $result = Get-TargetResource @testParameters - # Testing protocol TCP "With Dynamically determine port" - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local' + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' } - } -ModuleName $script:DSCResourceName -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local' + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName } - } -ModuleName $script:DSCResourceName -Verifiable - $result = Get-TargetResource @testParameters + It 'Should return TCP as the protocol used' { + $result.Protocol | Should -Be 'TCP' + } - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } + It 'Should return the UseDynamicTcpPort parameter as true' { + $result.UseDynamicTcpPort | Should -Be $true + } - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - } + It "Should not return any pipe name" { + $result.PipeName | Should -Be '' + } - It 'Should return TCP as the protocol used' { - $result.Protocol | Should -Be 'TCP' - } + It 'Should not return any TCP Port' { + $result.TcpPort | Should -Be 0 + } - It 'Should return the UseDynamicTcpPort parameter as true' { - $result.UseDynamicTcpPort | Should -Be $true - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It "Should not return any pipe name" { - $result.PipeName | Should -Be '' - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - It 'Should not return any TCP Port' { - $result.TcpPort | Should -Be 0 + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + # Mocking 32-bit OS + Mock -CommandName Get-CimInstance -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '32-bit' -PassThru -Force + } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the system is in the desired present state for 32-bit OS using UseDynamicTcpPort' { + $testParameters = @{ + Name = $name + ServerName = $serverNameTcp + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + # Testing protocol TCP "With Dynamically determine port" + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local' + } + } -Verifiable - # Mocking 32-bit OS - Mock -CommandName Get-CimInstance -MockWith { - return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '32-bit' -PassThru -Force - } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local' + } + } -Verifiable - Context 'When the system is in the desired present state for 32-bit OS using UseDynamicTcpPort' { - $testParameters = @{ - Name = $name - ServerName = $serverNameTcp - } + $result = Get-TargetResource @testParameters - # Testing protocol TCP "With Dynamically determine port" - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local' + It 'Should return the state as present' { + $result.Ensure | Should -Be 'Present' } - } -ModuleName $script:DSCResourceName -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local' + It 'Should return the same values as passed as parameters' { + $result.Name | Should -Be $testParameters.Name + $result.ServerName | Should -Be $testParameters.ServerName } - } -ModuleName $script:DSCResourceName -Verifiable - - $result = Get-TargetResource @testParameters - It 'Should return the state as present' { - $result.Ensure | Should -Be 'Present' - } - - It 'Should return the same values as passed as parameters' { - $result.Name | Should -Be $testParameters.Name - $result.ServerName | Should -Be $testParameters.ServerName - } + It 'Should return TCP as the protocol used' { + $result.Protocol | Should -Be 'TCP' + } - It 'Should return TCP as the protocol used' { - $result.Protocol | Should -Be 'TCP' - } + It 'Should return the UseDynamicTcpPort parameter as true' { + $result.UseDynamicTcpPort | Should -Be $true + } - It 'Should return the UseDynamicTcpPort parameter as true' { - $result.UseDynamicTcpPort | Should -Be $true - } + It "Should not return any pipe name" { + $result.PipeName | Should -Be '' + } - It "Should not return any pipe name" { - $result.PipeName | Should -Be '' - } + It 'Should not return any TCP Port' { + $result.TcpPort | Should -Be 0 + } - It 'Should not return any TCP Port' { - $result.TcpPort | Should -Be 0 - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It 'Should not call the Get-ItemProperty for the Wow6432Node-path' { + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 0 -Scope Context + } } - It 'Should not call the Get-ItemProperty for the Wow6432Node-path' { - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 0 -ModuleName $script:DSCResourceName -Scope Context - } + Assert-VerifiableMock } - Assert-VerifiableMock - } - #endregion Get-TargetResource - - #region Set-TargetResource - Describe "$($script:DSCResourceName)\Set-TargetResource" { - Mock -CommandName New-Item -ModuleName $script:DSCResourceName -Verifiable - Mock -CommandName Set-ItemProperty -ModuleName $script:DSCResourceName -Verifiable - Mock -CommandName Remove-ItemProperty -ModuleName $script:DSCResourceName -Verifiable - Mock -CommandName Test-Path -MockWith { - return $false - } -ModuleName $script:DSCResourceName -Verifiable - - # Mocking 64-bit OS - Mock -CommandName Get-CimInstance -MockWith { - return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force - } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -ModuleName $script:DSCResourceName -Verifiable - - Context 'When the system is not in the desired state for 64-bit OS using TCP' { - It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty twice each when desired state should be present for protocol TCP' { - $testParameters = @{ - Name = $name - Protocol = 'TCP' - ServerName = $serverNameTcp - TcpPort = $tcpPort + Describe 'SqlAlias\Set-TargetResource' { + Mock -CommandName New-Item -Verifiable + Mock -CommandName Set-ItemProperty -Verifiable + Mock -CommandName Remove-ItemProperty -Verifiable + Mock -CommandName Test-Path -MockWith { + return $false + } -Verifiable + + # Mocking 64-bit OS + Mock -CommandName Get-CimInstance -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force + } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable + + Context 'When the system is not in the desired state for 64-bit OS using TCP' { + It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty twice each when desired state should be present for protocol TCP' { + $testParameters = @{ + Name = $name + Protocol = 'TCP' + ServerName = $serverNameTcp + TcpPort = $tcpPort + } + + Set-TargetResource @testParameters + + Assert-MockCalled -CommandName Test-Path -Exactly 2 -Scope It + Assert-MockCalled -CommandName New-Item -Exactly 2 -Scope It + Assert-MockCalled -CommandName Set-ItemProperty -Exactly 2 -Scope It } - Set-TargetResource @testParameters + It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty twice each when desired state should be present for protocol Named Pipes' { + $testParameters = @{ + Name = $name + Protocol = 'NP' + ServerName = $serverNameNamedPipes + } - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 2 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-Item -Exactly 2 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 2 -Scope It - } + Set-TargetResource @testParameters - It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty twice each when desired state should be present for protocol Named Pipes' { - $testParameters = @{ - Name = $name - Protocol = 'NP' - ServerName = $serverNameNamedPipes + Assert-MockCalled -CommandName Test-Path -Exactly 2 -Scope It + Assert-MockCalled -CommandName New-Item -Exactly 2 -Scope It + Assert-MockCalled -CommandName Set-ItemProperty -Exactly 2 -Scope It } - Set-TargetResource @testParameters + It 'Should call mocked functions Test-Path and Remove-ItemProperty twice each when desired state should be absent for 64-bit OS' { + Mock -CommandName Test-Path -MockWith { + return $true + } -Verifiable - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 2 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-Item -Exactly 2 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 2 -Scope It - } + $testParameters = @{ + Ensure = 'Absent' + Name = $name + ServerName = $serverNameTcp + } - It 'Should call mocked functions Test-Path and Remove-ItemProperty twice each when desired state should be absent for 64-bit OS' { - Mock -CommandName Test-Path -MockWith { - return $true - } -ModuleName $script:DSCResourceName -Verifiable + Set-TargetResource @testParameters - $testParameters = @{ - Ensure = 'Absent' - Name = $name - ServerName = $serverNameTcp + Assert-MockCalled -CommandName Test-Path -Exactly 2 -Scope It + Assert-MockCalled -CommandName Remove-ItemProperty -Exactly 2 -Scope It } + } - Set-TargetResource @testParameters + Context 'When the system is not in the desired state for 64-bit OS using UseDynamicTcpPort' { + It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty twice each when desired state should be present for protocol TCP' { + $testParameters = @{ + Name = $name + Protocol = 'TCP' + ServerName = $serverNameTcp + UseDynamicTcpPort = $true + } - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 2 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Remove-ItemProperty -Exactly 2 -Scope It - } - } + Set-TargetResource @testParameters - Context 'When the system is not in the desired state for 64-bit OS using UseDynamicTcpPort' { - It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty twice each when desired state should be present for protocol TCP' { - $testParameters = @{ - Name = $name - Protocol = 'TCP' - ServerName = $serverNameTcp - UseDynamicTcpPort = $true + Assert-MockCalled -CommandName Test-Path -Exactly 2 -Scope It + Assert-MockCalled -CommandName New-Item -Exactly 2 -Scope It + Assert-MockCalled -CommandName Set-ItemProperty -Exactly 2 -Scope It } - Set-TargetResource @testParameters + It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty twice each when desired state should be present for protocol Named Pipes' { + $testParameters = @{ + Name = $name + Protocol = 'NP' + ServerName = $serverNameNamedPipes + } - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 2 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-Item -Exactly 2 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 2 -Scope It - } + Set-TargetResource @testParameters - It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty twice each when desired state should be present for protocol Named Pipes' { - $testParameters = @{ - Name = $name - Protocol = 'NP' - ServerName = $serverNameNamedPipes + Assert-MockCalled -CommandName Test-Path -Exactly 2 -Scope It + Assert-MockCalled -CommandName New-Item -Exactly 2 -Scope It + Assert-MockCalled -CommandName Set-ItemProperty -Exactly 2 -Scope It } - Set-TargetResource @testParameters + It 'Should call mocked functions Test-Path and Remove-ItemProperty twice each when desired state should be absent for 64-bit OS' { + Mock -CommandName Test-Path -MockWith { + return $true + } -Verifiable - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 2 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-Item -Exactly 2 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 2 -Scope It - } + $testParameters = @{ + Ensure = 'Absent' + Name = $name + ServerName = $serverNameTcp + } - It 'Should call mocked functions Test-Path and Remove-ItemProperty twice each when desired state should be absent for 64-bit OS' { - Mock -CommandName Test-Path -MockWith { - return $true - } -ModuleName $script:DSCResourceName -Verifiable + Set-TargetResource @testParameters - $testParameters = @{ - Ensure = 'Absent' - Name = $name - ServerName = $serverNameTcp + Assert-MockCalled -CommandName Test-Path -Exactly 2 -Scope It + Assert-MockCalled -CommandName Remove-ItemProperty -Exactly 2 -Scope It } - - Set-TargetResource @testParameters - - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 2 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Remove-ItemProperty -Exactly 2 -Scope It } - } - - # Mocking 32-bit OS - Mock -CommandName Get-CimInstance -MockWith { - return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '32-bit' -PassThru -Force - } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -ModuleName $script:DSCResourceName -Verifiable - Context 'When the system is not in the desired state for 32-bit OS using TCP' { - It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty once each when desired state should be present for protocol TCP' { - $testParameters = @{ - Name = $name - Protocol = 'TCP' - ServerName = $serverNameTcp - TcpPort = $tcpPort + # Mocking 32-bit OS + Mock -CommandName Get-CimInstance -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '32-bit' -PassThru -Force + } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable + + Context 'When the system is not in the desired state for 32-bit OS using TCP' { + It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty once each when desired state should be present for protocol TCP' { + $testParameters = @{ + Name = $name + Protocol = 'TCP' + ServerName = $serverNameTcp + TcpPort = $tcpPort + } + + Set-TargetResource @testParameters + + Assert-MockCalled -CommandName Test-Path -Exactly 1 -Scope It + Assert-MockCalled -CommandName New-Item -Exactly 1 -Scope It + Assert-MockCalled -CommandName Set-ItemProperty -Exactly 1 -Scope It } - Set-TargetResource @testParameters + It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty once each when desired state should be present for protocol Named Pipes' { + $testParameters = @{ + Name = $name + Protocol = 'NP' + ServerName = $serverNameNamedPipes + } - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 1 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-Item -Exactly 1 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 1 -Scope It - } + Set-TargetResource @testParameters - It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty once each when desired state should be present for protocol Named Pipes' { - $testParameters = @{ - Name = $name - Protocol = 'NP' - ServerName = $serverNameNamedPipes + Assert-MockCalled -CommandName Test-Path -Exactly 1 -Scope It + Assert-MockCalled -CommandName New-Item -Exactly 1 -Scope It + Assert-MockCalled -CommandName Set-ItemProperty -Exactly 1 -Scope It } - Set-TargetResource @testParameters + It 'Should call mocked functions Test-Path and Remove-ItemProperty once each when desired state should be absent for 32-bit OS' { + Mock -CommandName Test-Path -MockWith { + return $true + } -Verifiable - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 1 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-Item -Exactly 1 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 1 -Scope It - } + $testParameters = @{ + Ensure = 'Absent' + Name = $name + ServerName = $serverNameNamedPipes + } - It 'Should call mocked functions Test-Path and Remove-ItemProperty once each when desired state should be absent for 32-bit OS' { - Mock -CommandName Test-Path -MockWith { - return $true - } -ModuleName $script:DSCResourceName -Verifiable + Set-TargetResource @testParameters - $testParameters = @{ - Ensure = 'Absent' - Name = $name - ServerName = $serverNameNamedPipes + Assert-MockCalled -CommandName Test-Path -Exactly 1 -Scope It + Assert-MockCalled -CommandName Remove-ItemProperty -Exactly 1 -Scope It } + } - Set-TargetResource @testParameters + Context 'When the system is not in the desired state for 32-bit OS using UseDynamicTcpPort' { + It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty once each when desired state should be present for protocol TCP' { + $testParameters = @{ + Name = $name + Protocol = 'TCP' + ServerName = $serverNameTcp + UseDynamicTcpPort = $true + } - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 1 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Remove-ItemProperty -Exactly 1 -Scope It - } - } + Set-TargetResource @testParameters - Context 'When the system is not in the desired state for 32-bit OS using UseDynamicTcpPort' { - It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty once each when desired state should be present for protocol TCP' { - $testParameters = @{ - Name = $name - Protocol = 'TCP' - ServerName = $serverNameTcp - UseDynamicTcpPort = $true + Assert-MockCalled -CommandName Test-Path -Exactly 1 -Scope It + Assert-MockCalled -CommandName New-Item -Exactly 1 -Scope It + Assert-MockCalled -CommandName Set-ItemProperty -Exactly 1 -Scope It } - Set-TargetResource @testParameters + It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty once each when desired state should be present for protocol Named Pipes' { + $testParameters = @{ + Name = $name + Protocol = 'NP' + ServerName = $serverNameNamedPipes + } - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 1 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-Item -Exactly 1 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 1 -Scope It - } + Set-TargetResource @testParameters - It 'Should call mocked functions Test-Path, New-Item and Set-ItemProperty once each when desired state should be present for protocol Named Pipes' { - $testParameters = @{ - Name = $name - Protocol = 'NP' - ServerName = $serverNameNamedPipes + Assert-MockCalled -CommandName Test-Path -Exactly 1 -Scope It + Assert-MockCalled -CommandName New-Item -Exactly 1 -Scope It + Assert-MockCalled -CommandName Set-ItemProperty -Exactly 1 -Scope It } - Set-TargetResource @testParameters + It 'Should call mocked functions Test-Path and Remove-ItemProperty once each when desired state should be absent for 32-bit OS' { + Mock -CommandName Test-Path -MockWith { + return $true + } -Verifiable - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 1 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-Item -Exactly 1 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 1 -Scope It - } + $testParameters = @{ + Ensure = 'Absent' + Name = $name + ServerName = $serverNameNamedPipes + } - It 'Should call mocked functions Test-Path and Remove-ItemProperty once each when desired state should be absent for 32-bit OS' { - Mock -CommandName Test-Path -MockWith { - return $true - } -ModuleName $script:DSCResourceName -Verifiable + Set-TargetResource @testParameters - $testParameters = @{ - Ensure = 'Absent' - Name = $name - ServerName = $serverNameNamedPipes + Assert-MockCalled -CommandName Test-Path -Exactly 1 -Scope It + Assert-MockCalled -CommandName Remove-ItemProperty -Exactly 1 -Scope It } - - Set-TargetResource @testParameters - - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Test-Path -Exactly 1 -Scope It - Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Remove-ItemProperty -Exactly 1 -Scope It } } - } - #endregion Set-TargetResource - #region Test-TargetResource - Describe "$($script:DSCResourceName)\Test-TargetResource" { - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' - } - } -ModuleName $script:DSCResourceName -Verifiable + Describe 'SqlAlias\Test-TargetResource' { + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + } + } -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' - } - } -ModuleName $script:DSCResourceName -Verifiable - - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $unknownName } -MockWith { - return $null - } -ModuleName $script:DSCResourceName -Verifiable - - # Mocking 64-bit OS - Mock -CommandName Get-CimInstance -MockWith { - return New-Object -TypeName Object | - Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force - } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -ModuleName $script:DSCResourceName -Verifiable - - Context 'When the system is in the desired state (when using TCP)' { - $testParameters = @{ - Name = $name - ServerName = $serverNameTcp - } + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBMSSOCN,sqlnode.company.local,1433' + } + } -Verifiable - It "Should return true from the test method" { - Test-TargetResource @testParameters | Should -Be $true - } + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $unknownName } -MockWith { + return $null + } -Verifiable - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + # Mocking 64-bit OS + Mock -CommandName Get-CimInstance -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force + } -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the system is in the desired state (when using TCP)' { + $testParameters = @{ + Name = $name + ServerName = $serverNameTcp + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It "Should return true from the test method" { + Test-TargetResource @testParameters | Should -Be $true + } - Context 'When the system is in the desired state (when using UseDynamicTcpPort)' { - $testParameters = @{ - Name = $name - ServerName = $serverNameTcp - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - It "Should return true from the test method" { - Test-TargetResource @testParameters | Should -Be $true + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context + + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the system is in the desired state (when using UseDynamicTcpPort)' { + $testParameters = @{ + Name = $name + ServerName = $serverNameTcp + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It "Should return true from the test method" { + Test-TargetResource @testParameters | Should -Be $true + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - Context 'When the system is not in the desired state (when using TCP)' { - $testParameters = @{ - Name = $unknownName - ServerName = $serverNameTcp - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - It "Should return false from the test method" { - Test-TargetResource @testParameters | Should -Be $false + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the system is not in the desired state (when using TCP)' { + $testParameters = @{ + Name = $unknownName + ServerName = $serverNameTcp + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It "Should return false from the test method" { + Test-TargetResource @testParameters | Should -Be $false + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - Context 'When the system is not in the desired state (when using UseDynamicTcpPort)' { - $testParameters = @{ - Name = $unknownName - ServerName = $serverNameTcp - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - It "Should return false from the test method" { - Test-TargetResource @testParameters | Should -Be $false + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the system is not in the desired state (when using UseDynamicTcpPort)' { + $testParameters = @{ + Name = $unknownName + ServerName = $serverNameTcp + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It "Should return false from the test method" { + Test-TargetResource @testParameters | Should -Be $false + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBNMPNTW,\\sqlnode\PIPE\sql\query' - } - } -ModuleName $script:DSCResourceName -Verifiable + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { - return @{ - 'MyAlias' = 'DBNMPNTW,\\sqlnode\PIPE\sql\query' + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - } -ModuleName $script:DSCResourceName -Verifiable - Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $unknownName } -MockWith { - return $null - } -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBNMPNTW,\\sqlnode\PIPE\sql\query' + } + } -Verifiable - Context 'When the system is in the desired state (when using Named Pipes)' { - $testParameters = @{ - Name = $name - ServerName = $serverNameNamedPipes - } + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node -and $Name -eq $name } -MockWith { + return @{ + 'MyAlias' = 'DBNMPNTW,\\sqlnode\PIPE\sql\query' + } + } -Verifiable - It "Should return true from the test method" { - $testParameters.Add('Protocol','NP') - Test-TargetResource @testParameters | Should -Be $true - } + Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq $registryPath -and $Name -eq $unknownName } -MockWith { + return $null + } -Verifiable - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the system is in the desired state (when using Named Pipes)' { + $testParameters = @{ + Name = $name + ServerName = $serverNameNamedPipes + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + It "Should return true from the test method" { + $testParameters.Add('Protocol','NP') + Test-TargetResource @testParameters | Should -Be $true + } - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context - } - } + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - Context 'When the system is not in the desired state (when using Named Pipes)' { - $testParameters = @{ - Name = $unknownName - ServerName = $unknownServerName - } + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - It "Should return false from the test method" { - Test-TargetResource @testParameters | Should -Be $false + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } - It 'Should call the mocked functions exactly 1 time each' { - Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Context 'When the system is not in the desired state (when using Named Pipes)' { + $testParameters = @{ + Name = $unknownName + ServerName = $unknownServerName + } + + It "Should return false from the test method" { + Test-TargetResource @testParameters | Should -Be $false + } + + It 'Should call the mocked functions exactly 1 time each' { + Assert-MockCalled Get-CimInstance -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } ` + -Exactly -Times 1 -Scope Context - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPath } ` + -Exactly -Times 1 -Scope Context - Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` - -Exactly -Times 1 -ModuleName $script:DSCResourceName -Scope Context + Assert-MockCalled Get-ItemProperty -ParameterFilter { $Path -eq $registryPathWow6432Node } ` + -Exactly -Times 1 -Scope Context + } } } } - #endregion Test-TargetResource } finally { - #region FOOTER - - Restore-TestEnvironment -TestEnvironment $TestEnvironment - - #endregion + Invoke-TestCleanup } diff --git a/Tests/Unit/MSFT_SqlSetup.Tests.ps1 b/Tests/Unit/MSFT_SqlSetup.Tests.ps1 index da094a1f3..fbee7c0af 100644 --- a/Tests/Unit/MSFT_SqlSetup.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlSetup.Tests.ps1 @@ -4683,56 +4683,65 @@ try BeforeAll { $mockServiceAccountPassword = ConvertTo-SecureString 'Password' -AsPlainText -Force - $mockSystemServiceAccount = ( + $mockSystemServiceAccount = ` New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'NT AUTHORITY\SYSTEM', $mockServiceAccountPassword - ) - $mockVirtualServiceAccount = ( + $mockVirtualServiceAccount = ` New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'NT SERVICE\MSSQLSERVER', $mockServiceAccountPassword - ) - $mockManagedServiceAccount = ( + $mockManagedServiceAccount = ` New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'COMPANY\ManagedAccount$', $mockServiceAccountPassword - ) - $mockDomainServiceAccount = ( + $mockDomainServiceAccount = ` New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'COMPANY\sql.service', $mockServiceAccountPassword - ) + + $mockDomainServiceAccountContainingDollarSign = ` + New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'COMPANY\$sql.service', $mockServiceAccountPassword } $serviceTypes | ForEach-Object { - $serviceType = $_ Context "When service type is $serviceType" { + $mockAccountArgumentName = ('{0}SVCACCOUNT' -f $serviceType) + $mockPasswordArgumentName = ('{0}SVCPASSWORD' -f $serviceType) - It "Should return the correct parameters when the account is a system account." { + It 'Should return the correct parameters when the account is a system account.' { $result = Get-ServiceAccountParameters -ServiceAccount $mockSystemServiceAccount -ServiceType $serviceType - $result.$("$($serviceType)SVCACCOUNT") | Should -BeExactly $mockSystemServiceAccount.UserName - $result.ContainsKey("$($serviceType)SVCPASSWORD") | Should -Be $false + $result.$mockAccountArgumentName | Should -BeExactly $mockSystemServiceAccount.UserName + $result.ContainsKey($mockPasswordArgumentName) | Should -Be $false } - It "Should return the correct parameters when the account is a virtual service account" { + It 'Should return the correct parameters when the account is a virtual service account' { $result = Get-ServiceAccountParameters -ServiceAccount $mockVirtualServiceAccount -ServiceType $serviceType - $result.$("$($serviceType)SVCACCOUNT") | Should -BeExactly $mockVirtualServiceAccount.UserName - $result.ContainsKey("$($serviceType)SVCPASSWORD") | Should -Be $false + $result.$mockAccountArgumentName | Should -BeExactly $mockVirtualServiceAccount.UserName + $result.ContainsKey($mockPasswordArgumentName) | Should -Be $false } - It "Should return the correct parameters when the account is a managed service account" { + It 'Should return the correct parameters when the account is a managed service account' { $result = Get-ServiceAccountParameters -ServiceAccount $mockManagedServiceAccount -ServiceType $serviceType - $result.$("$($serviceType)SVCACCOUNT") | Should -BeExactly $mockManagedServiceAccount.UserName - $result.ContainsKey("$($serviceType)SVCPASSWORD") | Should -Be $false + $result.$mockAccountArgumentName | Should -BeExactly $mockManagedServiceAccount.UserName + $result.ContainsKey($mockPasswordArgumentName) | Should -Be $false } - It "Should return the correct parameters when the account is a domain account" { + It 'Should return the correct parameters when the account is a domain account' { $result = Get-ServiceAccountParameters -ServiceAccount $mockDomainServiceAccount -ServiceType $serviceType - $result.$("$($serviceType)SVCACCOUNT") | Should -BeExactly $mockDomainServiceAccount.UserName - $result.$("$($serviceType)SVCPASSWORD") | Should -BeExactly $mockDomainServiceAccount.GetNetworkCredential().Password + $result.$mockAccountArgumentName | Should -BeExactly $mockDomainServiceAccount.UserName + $result.$mockPasswordArgumentName | Should -BeExactly $mockDomainServiceAccount.GetNetworkCredential().Password } + + # Regression test for issue #1055 + It 'Should return the correct parameters when the account is a domain account containing a dollar sign ($)' { + $result = Get-ServiceAccountParameters -ServiceAccount $mockDomainServiceAccountContainingDollarSign -ServiceType $serviceType + + $result.$mockAccountArgumentName | Should -BeExactly $mockDomainServiceAccountContainingDollarSign.UserName + $result.$mockPasswordArgumentName | Should -BeExactly $mockDomainServiceAccountContainingDollarSign.GetNetworkCredential().Password + } + } } } diff --git a/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 b/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 index eff5bcf40..fff7115c9 100644 --- a/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 @@ -1,25 +1,18 @@ -# Suppressing this rule because PlainText is required for one of the functions used in this test -[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] -param() - -$script:DSCModuleName = 'SqlServerDsc' -$script:DSCResourceName = 'MSFT_SqlWindowsFirewall' - #region HEADER -# Unit Test Template Version: 1.2.0 +# Unit Test Template Version: 1.2.1 $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) { - & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) + & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests')) } -Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName $script:DSCModuleName ` - -DSCResourceName $script:DSCResourceName ` + -DSCModuleName 'SqlServerDsc' ` + -DSCResourceName 'MSFT_SqlWindowsFirewall' ` -TestType Unit #endregion HEADER @@ -36,7 +29,7 @@ try { Invoke-TestSetup - InModuleScope $script:DSCResourceName { + InModuleScope 'MSFT_SqlWindowsFirewall' { <# Testing two major versions to verify Integration Services differences (i.e service name). No point in testing each supported SQL Server version, since there are no difference diff --git a/Tests/Unit/SqlServerDSCHelper.Tests.ps1 b/Tests/Unit/SqlServerDSCHelper.Tests.ps1 index cb7ac21e4..abaee5164 100644 --- a/Tests/Unit/SqlServerDSCHelper.Tests.ps1 +++ b/Tests/Unit/SqlServerDSCHelper.Tests.ps1 @@ -81,6 +81,10 @@ InModuleScope $script:moduleName { Add-Member -MemberType NoteProperty -Name ConnectionContext -Value ( New-Object -TypeName Object | Add-Member -MemberType NoteProperty -Name ServerInstance -Value $serverInstance -PassThru | + #Add-Member -MemberType ScriptProperty -Name LoginSecure -Value { [System.Boolean] $mockExpectedDatabaseEngineLoginSecure } -PassThru -Force | + Add-Member -MemberType NoteProperty -Name LoginSecure -Value $true -PassThru | + Add-Member -MemberType NoteProperty -Name Login -Value '' -PassThru | + Add-Member -MemberType NoteProperty -Name SecurePassword -Value $null -PassThru | Add-Member -MemberType NoteProperty -Name ConnectAsUser -Value $false -PassThru | Add-Member -MemberType NoteProperty -Name ConnectAsUserPassword -Value '' -PassThru | Add-Member -MemberType NoteProperty -Name ConnectAsUserName -Value '' -PassThru | @@ -1169,6 +1173,23 @@ InModuleScope $script:moduleName { } } + Context 'When connecting to the default instance using SQL Server Authentication' { + It 'Should return the correct service instance' { + $mockExpectedDatabaseEngineServer = 'TestServer' + $mockExpectedDatabaseEngineInstance = 'MSSQLSERVER' + $mockExpectedDatabaseEngineLoginSecure = $false + + $databaseEngineServerObject = Connect-SQL -SQLServer $mockExpectedDatabaseEngineServer -SetupCredential $mockSetupCredential -LoginType 'SqlLogin' + $databaseEngineServerObject.ConnectionContext.LoginSecure | Should -Be $false + $databaseEngineServerObject.ConnectionContext.Login | Should -Be $mockSetupCredentialUserName + $databaseEngineServerObject.ConnectionContext.SecurePassword | Should -Be $mockSetupCredentialSecurePassword + $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly $mockExpectedDatabaseEngineServer + + Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + } + } + Context 'When connecting to the named instance using Windows Authentication' { It 'Should return the correct service instance' { $mockExpectedDatabaseEngineServer = $env:COMPUTERNAME @@ -1182,6 +1203,23 @@ InModuleScope $script:moduleName { } } + Context 'When connecting to the named instance using SQL Server Authentication' { + It 'Should return the correct service instance' { + $mockExpectedDatabaseEngineServer = $env:COMPUTERNAME + $mockExpectedDatabaseEngineInstance = $mockInstanceName + $mockExpectedDatabaseEngineLoginSecure = $false + + $databaseEngineServerObject = Connect-SQL -SQLInstanceName $mockExpectedDatabaseEngineInstance -SetupCredential $mockSetupCredential -LoginType 'SqlLogin' + $databaseEngineServerObject.ConnectionContext.LoginSecure | Should -Be $false + $databaseEngineServerObject.ConnectionContext.Login | Should -Be $mockSetupCredentialUserName + $databaseEngineServerObject.ConnectionContext.SecurePassword | Should -Be $mockSetupCredentialSecurePassword + $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" + + Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + } + } + Context 'When connecting to the named instance using Windows Authentication and different server name' { It 'Should return the correct service instance' { $mockExpectedDatabaseEngineServer = 'SERVER'