Skip to content

Commit

Permalink
Fixed failing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ykuijs committed Mar 25, 2022
1 parent 33d8355 commit 059ca7e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,9 @@ function Set-TargetResource
# Only check and correct when Ensure=Present, FixFarmAccountPermissions=True and the permissions are incorrect
if ($Ensure -eq "Present")
{
if (($FixFarmAccountPermissions -eq $true -and `
$result.FixFarmAccountPermissions -eq $true) -or `
$newServiceApp -eq $true)
if ($FixFarmAccountPermissions -eq $true -and `
($result.FixFarmAccountPermissions -eq $true -or `
$newServiceApp -eq $true))
{
Write-Verbose -Message "Fixing database permissions for Search Service Application $Name"
Invoke-SPDscCommand -Arguments @($PSBoundParameters, $PSScriptRoot) `
Expand Down
90 changes: 79 additions & 11 deletions tests/Unit/SharePointDsc/SharePointDsc.SPSearchServiceApp.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ try
InModuleScope -ModuleName $script:DSCResourceFullName -ScriptBlock {
Describe -Name $Global:SPDscHelper.DescribeHeader -Fixture {
BeforeAll {
Invoke-Command -ScriptBlock $Global:SPDscHelper.InitializeScript -NoNewScope
Invoke-Command -Scriptblock $Global:SPDscHelper.InitializeScript -NoNewScope

# Initialize tests
Import-Module -Name (Join-Path -Path (Get-Module SharePointDsc -ListAvailable).ModuleBase `
Expand Down Expand Up @@ -267,32 +267,67 @@ try
Mock Import-Module -MockWith { } -ParameterFilter { $_.Name -eq $ModuleName }

Mock -CommandName Get-SPServiceApplication -MockWith {
$spServiceApp = [PSCustomObject]@{
DisplayName = $testParams.Name
Name = $testParams.Name
if ($global:SPDscGetServiceApplicationCount -eq 0)
{
$spServiceApp = [PSCustomObject]@{
DisplayName = $testParams.Name
Name = $testParams.Name
}
$spServiceApp | Add-Member -MemberType ScriptMethod `
-Name GetType `
-Value {
return @{
FullName = "Microsoft.Office.UnKnownWebServiceApplication"
}
} -PassThru -Force
}
$spServiceApp | Add-Member -MemberType ScriptMethod `
-Name GetType `
-Value {
return @{
FullName = "Microsoft.Office.UnKnownWebServiceApplication"
else
{
$spServiceApp = [PSCustomObject]@{
TypeName = "Search Service Application"
DisplayName = $testParams.Name
Name = $testParams.Name
ApplicationPool = @{ Name = $testParams.ApplicationPool }
Database = @{
Name = "SP_Search"
NormalizedDataSource = 'SQL01'
}
SearchAdminDatabase = @{
Name = "SP_Search" + '_Admin'
NormalizedDataSource = 'SQL01'
}
AnalyticsReportingDatabases = @{
Name = "SP_Search" + '_AnalyticsReportingStore'
NormalizedDataSource = 'SQL01'
}
}
} -PassThru -Force
$spServiceApp = $spServiceApp | Add-Member -MemberType ScriptMethod -Name GetType -Value {
return @{ FullName = $getTypeFullName }
} -PassThru -Force
$spServiceApp = $spServiceApp | Add-Member -MemberType ScriptMethod -Name GetProperty -Value {
return 0
} -PassThru -Force
}
$global:SPDscGetServiceApplicationCount++
return $spServiceApp
}
}

$global:SPDscGetServiceApplicationCount = 0
It "Should return absent from the Get method" {
(Get-TargetResource @testParams).Ensure | Should -Be "Absent"
}

$global:SPDscGetServiceApplicationCount = 0
It "Should return false when the Test method is called" {
Test-TargetResource @testParams | Should -Be $false
}

$global:SPDscGetServiceApplicationCount = 0
It "Should create a new service application in the set method" {
Set-TargetResource @testParams
Assert-MockCalled New-SPEnterpriseSearchServiceApplication
$global:SPDscGetServiceApplicationCount | Should -Be 3
}
}

Expand Down Expand Up @@ -1163,7 +1198,39 @@ try
Mock Import-Module -MockWith { } -ParameterFilter { $_.Name -eq $ModuleName }

Mock -CommandName Get-SPServiceApplication -MockWith {
return $null
if ($global:SPDscGetServiceApplicationCount -eq 0)
{
$spServiceApp = $null
}
else
{
$spServiceApp = [PSCustomObject]@{
TypeName = "Search Service Application"
DisplayName = $testParams.Name
Name = $testParams.Name
ApplicationPool = @{ Name = $testParams.ApplicationPool }
Database = @{
Name = "SP_Search"
NormalizedDataSource = 'SQL01'
}
SearchAdminDatabase = @{
Name = "SP_Search" + '_Admin'
NormalizedDataSource = 'SQL01'
}
AnalyticsReportingDatabases = @{
Name = "SP_Search" + '_AnalyticsReportingStore'
NormalizedDataSource = 'SQL01'
}
}
$spServiceApp = $spServiceApp | Add-Member -MemberType ScriptMethod -Name GetType -Value {
return @{ FullName = $getTypeFullName }
} -PassThru -Force
$spServiceApp = $spServiceApp | Add-Member -MemberType ScriptMethod -Name GetProperty -Value {
return 0
} -PassThru -Force
}
$global:SPDscGetServiceApplicationCount++
return $spServiceApp
}

Mock -CommandName Get-SPDscInstalledProductVersion -MockWith {
Expand All @@ -1178,6 +1245,7 @@ try
Set-TargetResource @testParams
}

$global:SPDscGetServiceApplicationCount = 0
It "Should throw an error in the set method if the version of SharePoint isn't high enough" {
Mock -CommandName Get-SPDscInstalledProductVersion -MockWith {
return @{
Expand Down

0 comments on commit 059ca7e

Please sign in to comment.