From e235fe2cb87dad9c9fe6052dbd4a6ff1138bf825 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 20 Aug 2016 16:22:57 +0200 Subject: [PATCH] Changes to xSQLAlias Fixed a bug if the alias did not exists then Get-ItemProperty would throw an error. Fixed a bug so that the logic to create a new alias is triggered. If the alias did not exist it was the change-logic that created the alias, not the create-logic. Fixed a bug in the create-logic that throw an error if the key ConnectTo already existed. Removed that New-Item since we already test for the existence of the key. Also some style changes. Changes to xSQLAlias Improved testing and added a test for removal of alias Bug found with the test, create-logic was called when it shouldn't Aligned schemas with README.md Also added add text that explains the use of the credential parameter (issue from #58) --- .../MSFT_xSQLAOGroupEnsure.psm1 | 2 +- .../MSFT_xSQLAOGroupEnsure.schema.mof | 28 +++-- .../MSFT_xSQLAlias/MSFT_xSQLAlias.psm1 | 103 ++++++++++-------- .../MSFT_xSQLAlias/MSFT_xSQLAlias.schema.mof | 2 +- README.md | 28 ++--- Tests/Unit/MSFT_xSqlAlias.Tests.ps1 | 78 ++++++++----- 6 files changed, 141 insertions(+), 100 deletions(-) diff --git a/DSCResources/MSFT_xSQLAOGroupEnsure/MSFT_xSQLAOGroupEnsure.psm1 b/DSCResources/MSFT_xSQLAOGroupEnsure/MSFT_xSQLAOGroupEnsure.psm1 index 84f59d35c3..f9c07c7f00 100644 --- a/DSCResources/MSFT_xSQLAOGroupEnsure/MSFT_xSQLAOGroupEnsure.psm1 +++ b/DSCResources/MSFT_xSQLAOGroupEnsure/MSFT_xSQLAOGroupEnsure.psm1 @@ -149,7 +149,7 @@ function Set-TargetResource # First two nodes will account for Syncronous Automatic Failover, Any additional will be Asyncronous try { - $nodes = Get-ClusterNode -cluster $sql.ClusterName -Verbose:$false | elect-Object -ExpandProperty name + $nodes = Get-ClusterNode -cluster $sql.ClusterName -Verbose:$false | Select-Object -ExpandProperty name $syncNodes = $nodes | Select-Object -First 2 $asyncNodes = $nodes | Select-Object -Skip 2 $availabilityGroup = New-Object -typename Microsoft.SqlServer.Management.Smo.AvailabilityGroup -ArgumentList $SQL, $AvailabilityGroupName diff --git a/DSCResources/MSFT_xSQLAOGroupEnsure/MSFT_xSQLAOGroupEnsure.schema.mof b/DSCResources/MSFT_xSQLAOGroupEnsure/MSFT_xSQLAOGroupEnsure.schema.mof index 2803e48544..0d722dd6b2 100644 --- a/DSCResources/MSFT_xSQLAOGroupEnsure/MSFT_xSQLAOGroupEnsure.schema.mof +++ b/DSCResources/MSFT_xSQLAOGroupEnsure/MSFT_xSQLAOGroupEnsure.schema.mof @@ -1,19 +1,17 @@ - [ClassVersion("1.0.0.0"), FriendlyName("xSQLAOGroupEnsure")] class MSFT_xSQLAOGroupEnsure : OMI_BaseResource { - [Key, ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; - [Key] String AvailabilityGroupName; - [Write] String AvailabilityGroupNameListener; - [Write] String AvailabilityGroupNameIP[]; - [Write] String AvailabilityGroupSubMask[]; - [Write] Uint32 AvailabilityGroupPort; - [Write, ValueMap{"None","ReadOnly","ReadIntent"}, Values{"None","ReadOnly","ReadIntent"}] String ReadableSecondary; - [Write, ValueMap{"Primary","Secondary"}, Values{"Primary","Secondary"}] String AutoBackupPreference; - [Write] Uint32 BackupPriority; - [Write] Uint32 EndPointPort; - [Write] String SQLServer; - [Write] String SQLInstanceName; - [Required, EmbeddedInstance("MSFT_Credential"), Description("Credential to be used to Grant Permissions in SQL.")] String SetupCredential; + [Key, Description("Determines whether the availability group should be added or removed."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; + [Key, Description("Name for availability group.")] String AvailabilityGroupName; + [Write, Description("Listener name for availability group.")] String AvailabilityGroupNameListener; + [Write, Description("List of IP addresses associated with listener.")] String AvailabilityGroupNameIP[]; + [Write, Description("Network subnetmask for listener.")] String AvailabilityGroupSubMask[]; + [Write, Description("Port availability group should listen on.")] Uint32 AvailabilityGroupPort; + [Write, Description("Mode secondaries should operate under (None, ReadOnly, ReadIntent)."), ValueMap{"None","ReadOnly","ReadIntent"}, Values{"None","ReadOnly","ReadIntent"}] String ReadableSecondary; + [Write, Description("Where backups should be backed up from (Primary, Secondary)."), ValueMap{"Primary","Secondary"}, Values{"Primary","Secondary"}] String AutoBackupPreference; + [Write, Description("The percentage weight for backup prority (default 50).")] Uint32 BackupPriority; + [Write, Description("he TCP port for the SQL AG Endpoint (default 5022).")] Uint32 EndPointPort; + [Write, Description("The SQL Server for the database.")] String SQLServer; + [Write, Description("The SQL instance for the database.")] String SQLInstanceName; + [Required, EmbeddedInstance("MSFT_Credential"), Description("Credential to be used to Grant Permissions on SQL Server, set this to $null to use Windows Authentication.")] String SetupCredential; }; - diff --git a/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.psm1 b/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.psm1 index e214db3f8b..ace2f0ecc2 100644 --- a/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.psm1 +++ b/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.psm1 @@ -32,7 +32,7 @@ function Get-TargetResource if ($null -ne (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue)) { - $itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name + $itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue $returnValue.Name = $Name $itemConfig = $itemValue."$Name" -split ',' @@ -97,49 +97,59 @@ function Set-TargetResource # Logic based on ensure value Present if ($Ensure -eq 'Present') { - if ($PSCmdlet.ShouldProcess("'$Name'","Replace the Client Alias")) + Write-Debug -Message 'Check if value requires changing' + + $currentValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue + if ($null -ne $currentValue -and $itemValue -ne $currentValue) { - - # Update the registry - if (Test-Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo') + if ($PSCmdlet.ShouldProcess($Name,"Changing the client alias (64-bit)")) { - Write-Debug -Message 'Check if value requires changing' - $currentValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" - if ($itemValue -ne $currentValue) - { - Write-Debug -Message 'Set-ItemProperty' - Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" -Value $itemValue - } + Write-Debug -Message 'Set-ItemProperty' + Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue } - else + } + elseif ($null -eq $currentValue) + { + if ($PSCmdlet.ShouldProcess($Name,"Create client alias (64-bit)")) { - Write-Debug -Message 'New-Item' - New-Item -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' | Out-Null + if (!(Test-Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo')) + { + Write-Debug -Message 'New-Item' + New-Item -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' | Out-Null + } + Write-Debug -Message 'New-ItemProperty' - New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" -Value $itemValue | Out-Null + New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue | Out-Null } + } - Write-Debug -Message 'Check OSArchitecture' - # If this is a 64 bit machine also update Wow6432Node - if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit') + Write-Debug -Message 'Check OSArchitecture' + # If this is a 64 bit machine also update Wow6432Node + if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit') + { + Write-Debug -Message 'Is 64Bit' + Write-Debug -Message 'Check if value requires changing' + $currentValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue + if ($null -ne $currentValue -and $itemValue -ne $currentValue) { - Write-Debug -Message 'Is 64Bit' - if (Test-Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo') + if ($PSCmdlet.ShouldProcess($Name,"Changing the client alias (32-bit)")) { - Write-Debug -Message 'Check if value requires changing' - $currentValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" - if ($itemValue -ne $currentValue) - { - Write-Debug -Message 'Set-ItemProperty' - Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" -Value $itemValue - } + Write-Debug -Message 'Set-ItemProperty' + Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue } - else + } + elseif ($null -eq $currentValue) + { + if ($PSCmdlet.ShouldProcess($Name,"Create client alias (32-bit)")) { - Write-Debug -Message 'New-Item' - New-Item -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' + if (!(Test-Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo')) + { + Write-Debug -Message 'New-Item' + New-Item -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' | Out-Null + } + Write-Debug -Message 'New-ItemProperty' - New-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" -Value $itemValue + New-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue } } } @@ -148,20 +158,25 @@ function Set-TargetResource # Logic based on ensure value Absent if ($Ensure -eq 'Absent') { - if ($PSCmdlet.ShouldProcess("'$Name'","Remove the Client Alias (if exists)")) + # If the base path doesn't exist then we don't need to do anything + if (Test-Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo') { - # If the base path doesn't exist then we don't need to do anything - if (Test-Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo') + + if ($PSCmdlet.ShouldProcess($Name,"Remove the client alias (64-bit)")) { Write-Debug -Message 'Remove-ItemProperty' - Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" + Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name + } + + Write-Debug -Message 'Check OSArchitecture' - Write-Debug -Message 'Check OSArchitecture' - # If this is a 64 bit machine also update Wow6432Node - if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit' -and (Test-Path -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo')) + # If this is a 64 bit machine also update Wow6432Node + if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit' -and (Test-Path -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo')) + { + if ($PSCmdlet.ShouldProcess($Name,"Remove the client alias (34-bit)")) { Write-Debug -Message 'Remove-ItemProperty Wow6432Node' - Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" + Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name } } } @@ -203,12 +218,12 @@ function Test-TargetResource if (Test-Path -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo') { Write-Debug -Message 'Alias registry container exists' - if ($null -ne (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" -ErrorAction SilentlyContinue)) + if ($null -ne (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue)) { Write-Debug -Message 'Existing alias found' if ($Ensure -eq 'Present') { - $itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" + $itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue $itemConfig = $itemValue."$Name" -split ',' @@ -250,10 +265,10 @@ function Test-TargetResource if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit') { Write-Debug -Message 'Wow6432Node' - if ($null -ne (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" -ErrorAction SilentlyContinue)) + if ($null -ne (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue)) { Write-Debug -Message 'Existing alias found' - $itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" + $itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue $itemConfig = $itemValue."$Name" -split ',' diff --git a/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.schema.mof b/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.schema.mof index dc20e5ccb2..b62ca6dfac 100644 --- a/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.schema.mof +++ b/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.schema.mof @@ -6,5 +6,5 @@ class MSFT_xSQLAlias : OMI_BaseResource [Key, Description("The SQL Server you are aliasing (the real SQL Server name).")] String ServerName; [Write, Description("The TCP port SQL is listening on.")] Sint32 TCPPort; [Read, Description("Named Pipes name from the Get-TargetResource method.")] String PipeName; - [Write, Description("Determines if Alias is to be Present or Absent."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; + [Write, Description("Determines whether the alias should be added or removed."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; }; diff --git a/README.md b/README.md index 8476ff6721..9d05efcb26 100644 --- a/README.md +++ b/README.md @@ -233,19 +233,19 @@ Please check out common DSC Resources [contributing guidelines](https://github.c * **SQLInstance**: The SQL instance for the database ###xSQLAOGroupEnsure -* **Ensure**: (key) An enumerated value that describes if Availability Group is to be present or absent. -* **AvailabilityGroupName** (key) Name for availability group -* **AvailabilityGroupNameListener** Listener name for availability group -* **AvailabilityGroupNameIP** List of IP addresses associated with listener -* **AvailabilityGroupSubMask** Network subnetmask for listener -* **AvailabilityGroupPort** Port availability group should listen on -* **ReadableSecondary** Mode secondaries should operate under (None, ReadOnly, ReadIntent) -* **AutoBackupPreference** Where backups should be backed up from (Primary,Secondary) -* **BackupPriority** The percentage weight for backup prority (default 50) -* **EndPointPort** The TCP port for the SQL AG Endpoint (default 5022) -* **SQLServer**: The SQL Server for the database -* **SQLInstance**: The SQL instance for the database -* **SetupCredential**: (Required) Credential to be used to Grant Permissions on SQL Server +* **Ensure**: (Key) Determines whether the availability group should be added or removed. +* **AvailabilityGroupName** (Key) Name for availability group. +* **AvailabilityGroupNameListener** Listener name for availability group. +* **AvailabilityGroupNameIP** List of IP addresses associated with listener. +* **AvailabilityGroupSubMask** Network subnetmask for listener. +* **AvailabilityGroupPort** Port availability group should listen on. +* **ReadableSecondary** Mode secondaries should operate under (None, ReadOnly, ReadIntent). +* **AutoBackupPreference** Where backups should be backed up from (Primary, Secondary). +* **BackupPriority** The percentage weight for backup prority (default 50). +* **EndPointPort** The TCP port for the SQL AG Endpoint (default 5022). +* **SQLServer**: The SQL Server for the database. +* **SQLInstance**: The SQL instance for the database. +* **SetupCredential**: (Required) Credential to be used to Grant Permissions on SQL Server, set this to $null to use Windows Authentication. ###xSQLServerAOJoin * **Ensure**: (key) An enumerated value that describes if Replica is to be present or absent from availability group @@ -330,7 +330,7 @@ Please check out common DSC Resources [contributing guidelines](https://github.c * **Variable**: Creates a sqlcmd scripting variable for use in the sqlcmd script, and sets a value for the variable. ### xSqlAlias - * **Ensure**: Determines if Alias is to be Present or Absent. + * **Ensure**: Determines whether the alias should be added or removed. * **Name**: (Key) The name of Alias (e.g. svr01\inst01). * **ServerName**: (Key) The SQL Server you are aliasing (the real SQL Server name). * **Protocol**: Protocol to use when connecting. Valid values are TCP or NP. NP is Named Pipes. diff --git a/Tests/Unit/MSFT_xSqlAlias.Tests.ps1 b/Tests/Unit/MSFT_xSqlAlias.Tests.ps1 index 48278a0f04..7a5d39d213 100644 --- a/Tests/Unit/MSFT_xSqlAlias.Tests.ps1 +++ b/Tests/Unit/MSFT_xSqlAlias.Tests.ps1 @@ -27,9 +27,9 @@ try #region Get-TargetResource Describe 'Get-TargetResource' { - Mock -ModuleName MSFT_xSqlAlias -CommandName Get-ItemProperty -MockWith { + Mock -CommandName Get-ItemProperty -MockWith { return 'DBMSSOCN,localhost,1433' - } + } -ModuleName $script:DSCResourceName $SqlAlias = Get-TargetResource -Name 'localhost' -Servername 'localhost' @@ -45,57 +45,85 @@ try #region Set-TargetResource Describe 'Set-TargetResource' { - Mock -ModuleName MSFT_xSqlAlias -CommandName Test-Path -MockWith { - return $true - } - - Mock -ModuleName MSFT_xSqlAlias -CommandName Get-ItemProperty -MockWith { - return 'DBMSSOCN,localhost,52002' - } - - Mock -ModuleName MSFT_xSqlAlias -CommandName Set-ItemProperty -MockWith { + Mock -CommandName New-ItemProperty -MockWith {} -ModuleName $script:DSCResourceName + Mock -CommandName Set-ItemProperty -MockWith {} -ModuleName $script:DSCResourceName + Mock -CommandName Remove-ItemProperty -MockWith {} -ModuleName $script:DSCResourceName + + Mock -CommandName Test-Path -MockWith { return $true - } + } -ModuleName $script:DSCResourceName - Mock -ModuleName MSFT_xSqlAlias -CommandName Get-WmiObject -MockWith { + Mock -CommandName Get-WmiObject -MockWith { return @{ Class = 'win32_OperatingSystem' OSArchitecture = '64-bit' } + } -ModuleName $script:DSCResourceName + + It 'Should call New-ItemProperty when value is not set' { + Mock -CommandName Get-ItemProperty -MockWith { + return $null + } -ModuleName $script:DSCResourceName + + Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'localhost' -TCPPort 52002 -Ensure 'Present' + + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-ItemProperty -Exactly 2 -Scope It + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 0 -Scope It } - It 'Should not call Set-ItemProperty with value already set' { - Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'localhost' -TCPPort 52002 -Ensure 'Present' + Mock -CommandName Get-ItemProperty -MockWith { + return 'DBMSSOCN,localhost,52002' + } -ModuleName $script:DSCResourceName + + It 'Should not call any *-ItemProperty when value is already set' { + Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'localhost' -TCPPort 52002 -Ensure 'Present' + + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 0 -Scope It + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-ItemProperty -Exactly 0 -Scope It + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Remove-ItemProperty -Exactly 0 -Scope It + } + + It 'Should call Set-ItemProperty exactly 2 times (1 for 32bit and 1 for 64 bit reg keys) when server name is different' { + Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'newserver' -TCPPort 52002 -Ensure 'Present' - Assert-MockCalled -ModuleName MSFT_xSqlAlias -CommandName Set-ItemProperty -Exactly 0 + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 2 -Scope It + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-ItemProperty -Exactly 0 -Scope It } - It 'Should call Set-ItemProperty exactly 2 times (1 for 32bit and 1 for 64 bit reg keys)' { - Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'localhost' -TCPPort 1433 -Ensure 'Present' + It 'Should call Set-ItemProperty exactly 2 times (1 for 32bit and 1 for 64 bit reg keys) when TCP port is different' { + Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'localhost' -TCPPort 1433 -Ensure 'Present' - Assert-MockCalled -ModuleName MSFT_xSqlAlias -CommandName Set-ItemProperty -Exactly 2 + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 2 -Scope It + } + + It 'Should call any Remove-ItemProperty exactly 2 times (1 for 32bit and 1 for 64 bit reg keys) when alias should be absent' { + Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'localhost' -TCPPort 52002 -Ensure 'Absent' + + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 0 -Scope It + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-ItemProperty -Exactly 0 -Scope It + Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Remove-ItemProperty -Exactly 2 -Scope It } } #end region Set-TargetResource #region Test-TargetResource Describe 'Test-TargetResource' { - Mock -ModuleName MSFT_xSqlAlias -CommandName Test-Path -MockWith { + Mock -CommandName Test-Path -MockWith { return $true - } + } -ModuleName $script:DSCResourceName - Mock -ModuleName MSFT_xSqlAlias -CommandName Get-ItemProperty -MockWith { + Mock -CommandName Get-ItemProperty -MockWith { return @{ myServerAlias = 'DBMSSOCN,localhost,1433' } - } + } -ModuleName $script:DSCResourceName - Mock -ModuleName MSFT_xSqlAlias -CommandName Get-WmiObject -MockWith { + Mock -CommandName Get-WmiObject -MockWith { return @{ Class = 'win32_OperatingSystem' OSArchitecture = '64-bit' } - } + } -ModuleName $script:DSCResourceName It 'Should return $true when Test is passed as Alias thats already set'{ Test-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName localhost -TCPPort 1433 -Ensure 'Present' | Should Be $true