From 0ced9f538b827ea177050259746bea370e2554ac Mon Sep 17 00:00:00 2001 From: Brian Farnhill Date: Sun, 27 Sep 2015 15:00:50 +1000 Subject: [PATCH 1/8] Bug fix in get method --- .../MSFT_xSPUserProfileServiceApp.psm1 | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 index 4e5698f54..7c82c4dfe 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 @@ -35,7 +35,18 @@ function Get-TargetResource } else { - $databases = Get-UserProfileServiceProperties $serviceApp + $databases = @{} + $propData = $serviceApp.GetType().GetProperties([System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic) + + $socialProp = $propData | Where-Object {$_.Name -eq "SocialDatabase"} + $databases.Add("SocialDatabase", $socialProp.GetValue($serviceApp)) + + $profileProp = $propData | Where-Object {$_.Name -eq "ProfileDatabase"} + $databases.Add("ProfileDatabase", $profileProp.GetValue($serviceApp)) + + $syncProp = $propData | Where-Object {$_.Name -eq "SynchronizationDatabase"} + $databases.Add("SynchronizationDatabase", $syncProp.GetValue($serviceApp)) + $spFarm = Get-SPFarm if ($params.FarmAccount.UserName -eq $spFarm.DefaultServiceAccount.Name) { @@ -62,28 +73,6 @@ function Get-TargetResource return $result } -function Get-UserProfileServiceProperties() { - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param - ( - [parameter(Mandatory = $true)] [System.String] $serviceApp - ) - $results = @{} - $propData = $serviceApp.GetType().GetProperties([System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic) - - $socialProp = $propData | Where-Object {$_.Name -eq "SocialDatabase"} - $results.Add("SocialDatabase", $socialProp.GetValue($serviceApp)) - - $profileProp = $propData | Where-Object {$_.Name -eq "ProfileDatabase"} - $results.Add("ProfileDatabase", $profileProp.GetValue($serviceApp)) - - $syncProp = $propData | Where-Object {$_.Name -eq "SynchronizationDatabase"} - $results.Add("SynchronizationDatabase", $syncProp.GetValue($serviceApp)) - - return $results -} - function Set-TargetResource { [CmdletBinding()] From 195a316b46e3749507e2f554fa66ba8e1de13dd9 Mon Sep 17 00:00:00 2001 From: Brian Farnhill Date: Sun, 27 Sep 2015 15:54:24 +1000 Subject: [PATCH 2/8] Refactored calls to reflect profile DBs --- .../MSFT_xSPUserProfileServiceApp.psm1 | 14 ++--- ...rePoint.xSPUserProfileServiceApp.Tests.ps1 | 61 +++++++++++++------ 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 index 7c82c4dfe..1c7f12ca4 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 @@ -36,16 +36,16 @@ function Get-TargetResource else { $databases = @{} - $propData = $serviceApp.GetType().GetProperties([System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic) + $propData = $serviceApp.GetType().GetProperties([System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic) - $socialProp = $propData | Where-Object {$_.Name -eq "SocialDatabase"} - $databases.Add("SocialDatabase", $socialProp.GetValue($serviceApp)) + $socialProp = $propData | Where-Object {$_.Name -eq "SocialDatabase"} + $databases.Add("SocialDatabase", $socialProp.GetValue($serviceApp)) - $profileProp = $propData | Where-Object {$_.Name -eq "ProfileDatabase"} - $databases.Add("ProfileDatabase", $profileProp.GetValue($serviceApp)) + $profileProp = $propData | Where-Object {$_.Name -eq "ProfileDatabase"} + $databases.Add("ProfileDatabase", $profileProp.GetValue($serviceApp)) - $syncProp = $propData | Where-Object {$_.Name -eq "SynchronizationDatabase"} - $databases.Add("SynchronizationDatabase", $syncProp.GetValue($serviceApp)) + $syncProp = $propData | Where-Object {$_.Name -eq "SynchronizationDatabase"} + $databases.Add("SynchronizationDatabase", $syncProp.GetValue($serviceApp)) $spFarm = Get-SPFarm diff --git a/Tests/xSharePoint/xSharePoint.xSPUserProfileServiceApp.Tests.ps1 b/Tests/xSharePoint/xSharePoint.xSPUserProfileServiceApp.Tests.ps1 index 329478f9d..8d1b09f64 100644 --- a/Tests/xSharePoint/xSharePoint.xSPUserProfileServiceApp.Tests.ps1 +++ b/Tests/xSharePoint/xSharePoint.xSPUserProfileServiceApp.Tests.ps1 @@ -27,20 +27,6 @@ Describe "xSPUserProfileServiceApp" { Import-Module $Global:CurrentSharePointStubModule -WarningAction SilentlyContinue - Mock Get-UserProfileServiceProperties { return @{ - ProfileDatabase = @{ - Name = "SP_ProfileDB" - Server = @{ Name = "SQL.domain.local" } - } - SocialDatabase = @{ - Name = "SP_SocialDB" - Server = @{ Name = "SQL.domain.local" } - } - SynchronizationDatabase = @{ - Name = "SP_SyncDB" - Server = @{ Name = "SQL.domain.local" } - } - }} Mock Get-SPFarm { return @{ DefaultServiceAccount = @{ Name = $testParams.FarmAccount.Username } }} @@ -72,11 +58,48 @@ Describe "xSPUserProfileServiceApp" { Context "When a service application exists and is configured correctly" { Mock Get-SPServiceApplication { - return @(@{ - TypeName = "User Profile Service Application" - DisplayName = $testParams.Name - ApplicationPool = @{ Name = $testParams.ApplicationPool } - }) + return @( + New-Object Object | + Add-Member NoteProperty TypeName "User Profile Service Application" -PassThru | + Add-Member NoteProperty DisplayName $testParams.Name -PassThru | + Add-Member NoteProperty ApplicationPool @{ Name = $testParams.ApplicationPool } -PassThru | + Add-Member ScriptMethod GetType { + New-Object Object | + Add-Member ScriptMethod GetProperties { + param($x) + return @( + (New-Object Object | + Add-Member NoteProperty Name "SocialDatabase" -PassThru | + Add-Member ScriptMethod GetValue { + param($x) + return @{ + Name = "SP_SocialDB" + Server = @{ Name = "SQL.domain.local" } + } + } -PassThru + ), + (New-Object Object | + Add-Member NoteProperty Name "ProfileDatabase" -PassThru | + Add-Member ScriptMethod GetValue { + return @{ + Name = "SP_ProfileDB" + Server = @{ Name = "SQL.domain.local" } + } + } -PassThru + ), + (New-Object Object | + Add-Member NoteProperty Name "SynchronizationDatabase" -PassThru | + Add-Member ScriptMethod GetValue { + return @{ + Name = "SP_ProfileSyncDB" + Server = @{ Name = "SQL.domain.local" } + } + } -PassThru + ) + ) + } -PassThru + } -PassThru -Force + ) } It "returns values from the get method" { From b375468072876b4ca1e96595546cb8a25a1cbab5 Mon Sep 17 00:00:00 2001 From: Brian Farnhill Date: Sun, 27 Sep 2015 16:22:41 +1000 Subject: [PATCH 3/8] Refactor to take get call out of invoke block --- .../MSFT_xSPManagedAccount.psm1 | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 index 015e3fc95..304dc902f 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 @@ -51,15 +51,16 @@ function Set-TargetResource Write-Verbose -Message "Setting managed account $AccountName" + if ($null -eq (Get-TargetResource @params)) { + Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { + $params = $args[0] + New-SPManagedAccount -Credential $params.Account + } + } + Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] - - $current = Get-TargetResource @params - if ($current.Count -eq 0) { - New-SPManagedAccount -Credential $params.Account - } - $updateParams = @{ Identity = $params.Account.UserName } From 04e851397f05ea05e30f3a9702a9ffa545635c1a Mon Sep 17 00:00:00 2001 From: Brian Farnhill Date: Sun, 27 Sep 2015 17:45:57 +1000 Subject: [PATCH 4/8] Fixed issue with the get call in the set method --- .../MSFT_xSPSecureStoreServiceApp.psm1 | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 index 3fad07354..33a1693e5 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 @@ -70,7 +70,7 @@ function Set-TargetResource $result = Get-TargetResource @PSBoundParameters - if ($result.Count -eq 0) { + if ($null -eq $result) { Write-Verbose -Message "Creating Secure Store Service Application $Name" Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] @@ -80,18 +80,15 @@ function Set-TargetResource switch((Get-xSharePointInstalledProductVersion).FileMajorPart) { 15 { - $app = New-SPSecureStoreServiceApplication @params + New-SPSecureStoreServiceApplication @params | New-SPSecureStoreServiceApplicationProxy -Name "$($params.Name) Proxy" } 16 { - $app = New-SPSecureStoreServiceApplication @params -EnableMinDB:$false + $app = New-SPSecureStoreServiceApplication @params -EnableMinDB:$false | New-SPSecureStoreServiceApplicationProxy -Name "$($params.Name) Proxy" } Default { throw [Exception] "An unknown version of SharePoint (Major version $_) was detected. Only versions 15 (SharePoint 2013) or 16 (SharePoint 2016) are supported." } } - if ($app) { - New-SPSecureStoreServiceApplicationProxy -Name "$($params.Name) Proxy" -ServiceApplication $app - } } } else { if ([string]::IsNullOrEmpty($ApplicationPool) -eq $false -and $ApplicationPool -ne $result.ApplicationPool) { From 6159c631ac58b7070b1f5a508202734e54caa7b1 Mon Sep 17 00:00:00 2001 From: Brian Farnhill Date: Sun, 27 Sep 2015 18:04:34 +1000 Subject: [PATCH 5/8] Fixed variable name in set method --- .../MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 index 304dc902f..acfda0b27 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 @@ -48,16 +48,16 @@ function Set-TargetResource [parameter(Mandatory = $false)] [System.String] $Schedule, [parameter(Mandatory = $true)] [System.String] $AccountName ) - - Write-Verbose -Message "Setting managed account $AccountName" - if ($null -eq (Get-TargetResource @params)) { + if ($null -eq (Get-TargetResource @PSBoundParameters)) { + Write-Verbose "Creating a new managed account" Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] New-SPManagedAccount -Credential $params.Account } } + Write-Verbose -Message "Updating settings for managed account" Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] From 4a9cb6f90b1b81c2ac7d56251a84fb523e7cd1fb Mon Sep 17 00:00:00 2001 From: Brian Farnhill Date: Sun, 27 Sep 2015 18:06:17 +1000 Subject: [PATCH 6/8] Formatting corrections --- .../MSFT_xSPManagedAccount.psm1 | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 index acfda0b27..6b85880f4 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 @@ -49,15 +49,15 @@ function Set-TargetResource [parameter(Mandatory = $true)] [System.String] $AccountName ) - if ($null -eq (Get-TargetResource @PSBoundParameters)) { - Write-Verbose "Creating a new managed account" - Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { - $params = $args[0] - New-SPManagedAccount -Credential $params.Account - } - } + if ($null -eq (Get-TargetResource @PSBoundParameters)) { + Write-Verbose "Creating a new managed account" + Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { + $params = $args[0] + New-SPManagedAccount -Credential $params.Account + } + } - Write-Verbose -Message "Updating settings for managed account" + Write-Verbose -Message "Updating settings for managed account" Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] From beeaf958b39d4fe153609753ecdcb04c5d7c45d9 Mon Sep 17 00:00:00 2001 From: Brian Farnhill Date: Sun, 27 Sep 2015 18:27:10 +1000 Subject: [PATCH 7/8] Increase code coverage of BCS service app resource tests --- .../xSharePoint.xSPBCSServiceApp.Tests.ps1 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Tests/xSharePoint/xSharePoint.xSPBCSServiceApp.Tests.ps1 b/Tests/xSharePoint/xSharePoint.xSPBCSServiceApp.Tests.ps1 index bd4d25a3b..4f8a2023e 100644 --- a/Tests/xSharePoint/xSharePoint.xSPBCSServiceApp.Tests.ps1 +++ b/Tests/xSharePoint/xSharePoint.xSPBCSServiceApp.Tests.ps1 @@ -34,7 +34,7 @@ Describe "xSPBCSServiceApp" { Import-Module $Global:CurrentSharePointStubModule -WarningAction SilentlyContinue - Context "When no service application exists in the current farm" { + Context "When no service applications exist in the current farm" { Mock Get-SPServiceApplication { return $null } Mock New-SPBusinessDataCatalogServiceApplication { } @@ -54,6 +54,19 @@ Describe "xSPBCSServiceApp" { } } + Context "When service applications exist in the current farm but the specific BCS app does not" { + + Mock Get-SPServiceApplication { return @(@{ + TypeName = "Some other service app type" + }) } + + It "returns null from the Get method" { + Get-TargetResource @testParams | Should BeNullOrEmpty + Assert-MockCalled Get-SPServiceApplication -ParameterFilter { $Name -eq $testParams.Name } + } + + } + Context "When a service application exists and is configured correctly" { Mock Get-SPServiceApplication { return @(@{ From 98d527f1bec87fb78afceee15a72db9403b0f09b Mon Sep 17 00:00:00 2001 From: Brian Farnhill Date: Sun, 27 Sep 2015 21:38:23 +1000 Subject: [PATCH 8/8] Fixed issue with 2016 calls to secure store cmdlets --- .../MSFT_xSPSecureStoreServiceApp.psm1 | 31 +-- .../MSFT_xSPUserProfileSyncService.psm1 | 13 + ...ePoint.xSPUserProfileSyncService.Tests.ps1 | 228 ++++++++++-------- 3 files changed, 159 insertions(+), 113 deletions(-) diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 index 33a1693e5..a27731b3c 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 @@ -69,33 +69,36 @@ function Set-TargetResource ) $result = Get-TargetResource @PSBoundParameters + $params = $PSBoundParameters + + switch((Get-xSharePointInstalledProductVersion).FileMajorPart) { + 16 { + $hasOptionalParams = $false + @("AuditlogMaxSize","DatabaseName","DatabasePassword","DatabaseServer","DatabaseUsername", ` + "FailoverDatabaseServer","PartitionMode","Sharing","DatabaseCredentials") | ForEach-Object { + if ($PSBoundParameters.ContainsKey($_) -eq $true) { $hasOptionalParams = $true } + } + if ($hasOptionalParams -eq $false) { + # Add the MinDB param to ensure that the cmdlet call gets differentiated without the optional params being set + $params.Add("EnableMinDB", $false) + } + } + } if ($null -eq $result) { Write-Verbose -Message "Creating Secure Store Service Application $Name" - Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { + Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $params -ScriptBlock { $params = $args[0] - if ($params.ContainsKey("InstallAccount")) { $params.Remove("InstallAccount") | Out-Null } - switch((Get-xSharePointInstalledProductVersion).FileMajorPart) { - 15 { - New-SPSecureStoreServiceApplication @params | New-SPSecureStoreServiceApplicationProxy -Name "$($params.Name) Proxy" - } - 16 { - $app = New-SPSecureStoreServiceApplication @params -EnableMinDB:$false | New-SPSecureStoreServiceApplicationProxy -Name "$($params.Name) Proxy" - } - Default { - throw [Exception] "An unknown version of SharePoint (Major version $_) was detected. Only versions 15 (SharePoint 2013) or 16 (SharePoint 2016) are supported." - } - } + New-SPSecureStoreServiceApplication @params | New-SPSecureStoreServiceApplicationProxy -Name "$($params.Name) Proxy" } } else { if ([string]::IsNullOrEmpty($ApplicationPool) -eq $false -and $ApplicationPool -ne $result.ApplicationPool) { Write-Verbose -Message "Updating Secure Store Service Application $Name" Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] - $serviceApp = Get-SPServiceApplication -Name $params.Name | Where-Object { $_.TypeName -eq "Secure Store Service Application" } $appPool = Get-SPServiceApplicationPool -Identity $params.ApplicationPool diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileSyncService/MSFT_xSPUserProfileSyncService.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileSyncService/MSFT_xSPUserProfileSyncService.psm1 index 01d422ee3..bfa80d63d 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileSyncService/MSFT_xSPUserProfileSyncService.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileSyncService/MSFT_xSPUserProfileSyncService.psm1 @@ -9,6 +9,11 @@ function Get-TargetResource [parameter(Mandatory = $true)] [System.Management.Automation.PSCredential] $FarmAccount, [parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount ) + + if ((Get-xSharePointInstalledProductVersion).FileMajorPart -ne 15) { + throw [Exception] "Only SharePoint 2013 is supported to deploy the user profile sync service via DSC, as 2016 does not use the FIM based sync service." + } + Write-Verbose -Message "Getting the local user profile sync service instance" $result = Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { @@ -59,6 +64,10 @@ function Set-TargetResource [parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount ) + if ((Get-xSharePointInstalledProductVersion).FileMajorPart -ne 15) { + throw [Exception] "Only SharePoint 2013 is supported to deploy the user profile sync service via DSC, as 2016 does not use the FIM based sync service." + } + Write-Verbose -Message "Setting User Profile Synchronization Service" # Add the FarmAccount to the local Admins group, if it's not already there @@ -129,6 +138,10 @@ function Test-TargetResource [parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount ) + if ((Get-xSharePointInstalledProductVersion).FileMajorPart -ne 15) { + throw [Exception] "Only SharePoint 2013 is supported to deploy the user profile sync service via DSC, as 2016 does not use the FIM based sync service." + } + $CurrentValues = Get-TargetResource @PSBoundParameters Write-Verbose -Message "Testing for User Profile Synchronization Service" return Test-xSharePointSpecificParameters -CurrentValues $CurrentValues -DesiredValues $PSBoundParameters -ValuesToCheck @("Ensure") diff --git a/Tests/xSharePoint/xSharePoint.xSPUserProfileSyncService.Tests.ps1 b/Tests/xSharePoint/xSharePoint.xSPUserProfileSyncService.Tests.ps1 index 322e0e04a..088953a3b 100644 --- a/Tests/xSharePoint/xSharePoint.xSPUserProfileSyncService.Tests.ps1 +++ b/Tests/xSharePoint/xSharePoint.xSPUserProfileSyncService.Tests.ps1 @@ -21,6 +21,11 @@ Describe "xSPUserProfileSyncService" { } Import-Module (Join-Path ((Resolve-Path $PSScriptRoot\..\..).Path) "Modules\xSharePoint") + Import-Module $Global:CurrentSharePointStubModule -WarningAction SilentlyContinue + $versionBeingTested = (Get-Item $Global:CurrentSharePointStubModule).Directory.BaseName + $majorBuildNumber = $versionBeingTested.Substring(0, $versionBeingTested.IndexOf(".")) + Mock Get-xSharePointInstalledProductVersion { return @{ FileMajorPart = $majorBuildNumber } } + Mock Invoke-xSharePointCommand { return Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments -NoNewScope } @@ -38,124 +43,149 @@ Describe "xSPUserProfileSyncService" { Mock Remove-xSharePointUserToLocalAdmin { } Mock New-PSSession { return $null } -ModuleName "xSharePoint.Util" - Context "User profile sync service is not running and should be" { - Mock Get-SPServiceInstance { if ($Global:xSharePointUPACheck -eq $false) { - $Global:xSharePointUPACheck = $true - return @( @{ - Status = "Disabled" - ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") - UserProfileApplicationGuid = [Guid]::Empty - TypeName = "User Profile Synchronization Service" - }) - } else { - return @( @{ - Status = "Online" - ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") - UserProfileApplicationGuid = [Guid]::NewGuid() - TypeName = "User Profile Synchronization Service" - }) + switch ($majorBuildNumber) { + 15 { + Context "User profile sync service is not running and should be" { + Mock Get-SPServiceInstance { if ($Global:xSharePointUPACheck -eq $false) { + $Global:xSharePointUPACheck = $true + return @( @{ + Status = "Disabled" + ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") + UserProfileApplicationGuid = [Guid]::Empty + TypeName = "User Profile Synchronization Service" + }) + } else { + return @( @{ + Status = "Online" + ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") + UserProfileApplicationGuid = [Guid]::NewGuid() + TypeName = "User Profile Synchronization Service" + }) + } + } + Mock Get-SPServiceApplication { return @( + New-Object Object | + Add-Member NoteProperty ID ([Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f")) -PassThru | + Add-Member NoteProperty TypeName "User Profile Service Application" -PassThru | + Add-Member ScriptMethod SetSynchronizationMachine { + param($computerName, $syncServiceID, $FarmUserName, $FarmPassword) + } -PassThru + )} + + It "returns absent from the get method" { + $Global:xSharePointUPACheck = $false + (Get-TargetResource @testParams).Ensure | Should Be "Absent" } - } - Mock Get-SPServiceApplication { return @( - New-Object Object | - Add-Member NoteProperty ID ([Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f")) -PassThru | - Add-Member NoteProperty TypeName "User Profile Service Application" -PassThru | - Add-Member ScriptMethod SetSynchronizationMachine { - param($computerName, $syncServiceID, $FarmUserName, $FarmPassword) - } -PassThru - )} - - It "returns absent from the get method" { - $Global:xSharePointUPACheck = $false - (Get-TargetResource @testParams).Ensure | Should Be "Absent" - } - It "returns false from the test method" { - $Global:xSharePointUPACheck = $false - Test-TargetResource @testParams | Should Be $false - } + It "returns false from the test method" { + $Global:xSharePointUPACheck = $false + Test-TargetResource @testParams | Should Be $false + } - It "calls the start service cmdlet from the set method" { - $Global:xSharePointUPACheck = $false - Set-TargetResource @testParams + It "calls the start service cmdlet from the set method" { + $Global:xSharePointUPACheck = $false + Set-TargetResource @testParams - Assert-MockCalled Start-SPServiceInstance + Assert-MockCalled Start-SPServiceInstance + } } - } - Context "User profile sync service is running and should be" { - Mock Get-SPServiceInstance { return @( @{ - Status = "Online" - ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") - UserProfileApplicationGuid = [Guid]::NewGuid() - TypeName = "User Profile Synchronization Service" - }) - } + Context "User profile sync service is running and should be" { + Mock Get-SPServiceInstance { return @( @{ + Status = "Online" + ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") + UserProfileApplicationGuid = [Guid]::NewGuid() + TypeName = "User Profile Synchronization Service" + }) + } - It "returns present from the get method" { - (Get-TargetResource @testParams).Ensure | Should Be "Present" - } + It "returns present from the get method" { + (Get-TargetResource @testParams).Ensure | Should Be "Present" + } - It "returns true from the test method" { - Test-TargetResource @testParams | Should Be $true + It "returns true from the test method" { + Test-TargetResource @testParams | Should Be $true + } } - } - $testParams.Ensure = "Absent" - - Context "User profile sync service is running and shouldn't be" { - Mock Get-SPServiceInstance { if ($Global:xSharePointUPACheck -eq $false) { - $Global:xSharePointUPACheck = $true - return @( @{ - Status = "Online" - ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") - UserProfileApplicationGuid = [Guid]::NewGuid() - TypeName = "User Profile Synchronization Service" - }) - } else { - return @( @{ - Status = "Disabled" - ID = [Guid]::Empty - UserProfileApplicationGuid = [Guid]::Empty - TypeName = "User Profile Synchronization Service" - }) + $testParams.Ensure = "Absent" + + Context "User profile sync service is running and shouldn't be" { + Mock Get-SPServiceInstance { if ($Global:xSharePointUPACheck -eq $false) { + $Global:xSharePointUPACheck = $true + return @( @{ + Status = "Online" + ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") + UserProfileApplicationGuid = [Guid]::NewGuid() + TypeName = "User Profile Synchronization Service" + }) + } else { + return @( @{ + Status = "Disabled" + ID = [Guid]::Empty + UserProfileApplicationGuid = [Guid]::Empty + TypeName = "User Profile Synchronization Service" + }) + } + } + + It "returns present from the get method" { + $Global:xSharePointUPACheck = $false + (Get-TargetResource @testParams).Ensure | Should Be "Present" } - } - It "returns present from the get method" { - $Global:xSharePointUPACheck = $false - (Get-TargetResource @testParams).Ensure | Should Be "Present" - } + It "returns false from the test method" { + $Global:xSharePointUPACheck = $false + Test-TargetResource @testParams | Should Be $false + } - It "returns false from the test method" { - $Global:xSharePointUPACheck = $false - Test-TargetResource @testParams | Should Be $false + It "calls the start service cmdlet from the set method" { + $Global:xSharePointUPACheck = $false + Set-TargetResource @testParams + + Assert-MockCalled Stop-SPServiceInstance + } } - It "calls the start service cmdlet from the set method" { - $Global:xSharePointUPACheck = $false - Set-TargetResource @testParams + Context "User profile sync service is not running and shouldn't be" { + Mock Get-SPServiceInstance { return @( @{ + Status = "Disabled" + ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") + UserProfileApplicationGuid = [Guid]::Empty + TypeName = "User Profile Synchronization Service" + }) + } + + It "returns absent from the get method" { + (Get-TargetResource @testParams).Ensure | Should Be "Absent" + } - Assert-MockCalled Stop-SPServiceInstance + It "returns true from the test method" { + Test-TargetResource @testParams | Should Be $true + } } - } - - Context "User profile sync service is not running and shouldn't be" { - Mock Get-SPServiceInstance { return @( @{ - Status = "Disabled" - ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") - UserProfileApplicationGuid = [Guid]::Empty - TypeName = "User Profile Synchronization Service" - }) - } - - It "returns absent from the get method" { - (Get-TargetResource @testParams).Ensure | Should Be "Absent" } - - It "returns true from the test method" { - Test-TargetResource @testParams | Should Be $true + 16 { + Context "All methods throw exceptions as user profile sync doesn't exist in 2016" { + It "throws on the get method" { + { Get-TargetResource @testParams } | Should Throw + } + + It "throws on the test method" { + { Test-TargetResource @testParams } | Should Throw + } + + It "throws on the set method" { + { Set-TargetResource @testParams } | Should Throw + } + } } } + + + + + + } } \ No newline at end of file