Skip to content

Commit

Permalink
Merge pull request #8 from BrianFarnhill/feature-SharePointTests
Browse files Browse the repository at this point in the history
Added tests for all DSC resource "test" methods
  • Loading branch information
BrianFarnhill committed Apr 26, 2015
2 parents 403636c + 1626435 commit 7f4f0d9
Show file tree
Hide file tree
Showing 28 changed files with 1,461 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function Test-TargetResource
if ($ErrorReportingAutomaticUploadEnabled -ne $null -and $ErrorReportingAutomaticUploadEnabled -ne $result.ErrorReportingAutomaticUploadEnabled) { return $false }
if ($ErrorReportingEnabled -ne $null -and $ErrorReportingEnabled -ne $result.ErrorReportingEnabled) { return $false }
if ($EventLogFloodProtectionEnabled -ne $null -and $EventLogFloodProtectionEnabled -ne $result.EventLogFloodProtectionEnabled) { return $false }
if ($EventLogFloodProtectionNotifyInterval -gt 0 -and $EventLogFloodProtectionNotifyInterval -ne $result.EventLogFloodProtectionNotifyInterval) {} return $false
if ($EventLogFloodProtectionNotifyInterval -gt 0 -and $EventLogFloodProtectionNotifyInterval -ne $result.EventLogFloodProtectionNotifyInterval) { return $false }
if ($EventLogFloodProtectionQuietPeriod -gt 0 -and $EventLogFloodProtectionQuietPeriod -ne $result.EventLogFloodProtectionQuietPeriod) { return $false }
if ($EventLogFloodProtectionThreshold -gt 0 -and $EventLogFloodProtectionThreshold -ne $result.EventLogFloodProtectionThreshold) { return $false }
if ($EventLogFloodProtectionTriggerPeriod -gt 0 -and $EventLogFloodProtectionTriggerPeriod -ne $result.EventLogFloodProtectionTriggerPeriod) { return $false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ function Get-TargetResource
Name = $params.Name
Id = $feature.Id
Version = $feature.Version
PathType = $path.Type
Enabled = $enabled
}
}
Expand Down Expand Up @@ -138,11 +137,12 @@ function Test-TargetResource
$Ensure
)

$result = Get-TargetResource -Name $Name -FeatureScope $FeatureScope -Url $Url -InstallAccount $InstallAccount
$result = Get-TargetResource -Name $Name -FeatureScope $FeatureScope -Url $Url -InstallAccount $InstallAccount -Ensure $Ensure
Write-Verbose "Testing for feature $Name at $FeatureScope scope"

if ($result.Count -eq 0) { return $false }
else {
if ($result.Count -eq 0) {
throw "Unable to locate feature '$Name' in the current SharePoint farm, check that the name is correct and that the feature has been deployed to the file system."
} else {
if ($Ensure -eq "Present" -and $result.Enabled -eq $false) { return $false }
if ($Ensure -eq "Absent" -and $result.Enabled -eq $true) { return $false }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ function Get-TargetResource
}
else
{
$service = Get-SPUsageService
return @{
Name = $serviceApp.DisplayName
UsageLogCutTime = $service.UsageLogCutTime
UsageLogDir = $service.UsageLogDir
UsageLogMaxFileSize = $service.UsageLogMaxFileSize
UsageLogMaxSpaceGB = $service.UsageLogMaxSpaceGB
}
}
}
Expand Down Expand Up @@ -157,19 +162,12 @@ function Test-TargetResource
Write-Verbose "Testing for usage application '$Name'"
if ($result.Count -eq 0) { return $false }
else {
$session = Get-xSharePointAuthenticatedPSSession $InstallAccount
$returnVal = Invoke-Command -Session $session -ArgumentList $PSBoundParameters -ScriptBlock {
$params = $args[0]

$service = Get-SPUsageService
if ($params.ContainsKey("UsageLogCutTime") -and $service.UsageLogCutTime -ne $params.UsageLogCutTime) { return $false }
if ($params.ContainsKey("UsageLogLocation") -and $service.UsageLogDir -ne $params.UsageLogLocation) { return $false }
if ($params.ContainsKey("UsageLogMaxFileSizeKB") -and $service.UsageLogMaxFileSize -ne $params.UsageLogMaxFileSizeKB) { return $false }
if ($params.ContainsKey("UsageLogMaxSpaceGB") -and $service.UsageLogMaxSpaceGB -ne $params.UsageLogMaxSpaceGB) { return $false }
return $true
}
return $returnVal
if ($PSBoundParameters.ContainsKey("UsageLogCutTime") -and $result.UsageLogCutTime -ne $UsageLogCutTime) { return $false }
if ($PSBoundParameters.ContainsKey("UsageLogLocation") -and $result.UsageLogDir -ne $UsageLogLocation) { return $false }
if ($PSBoundParameters.ContainsKey("UsageLogMaxFileSizeKB") -and $result.UsageLogMaxFileSize -ne $UsageLogMaxFileSizeKB) { return $false }
if ($PSBoundParameters.ContainsKey("UsageLogMaxSpaceGB") -and $result.UsageLogMaxSpaceGB -ne $UsageLogMaxSpaceGB) { return $false }
}
return $true
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ function Test-TargetResource
Write-Verbose "Testing for User Profile Synchronization Service"
if ($result.Count -eq 0) { return $false }
else {
if (($syncService.Status -eq "Online") -and ($Ensure -ne "Present")) { return $false }
if (($syncService.Status -eq "Disabled") -and ($Ensure -ne "Absent")) { return $false }
if (($result.Status -eq "Online") -and ($Ensure -ne "Present")) { return $false }
if (($result.Status -eq "Disabled") -and ($Ensure -ne "Absent")) { return $false }
}
return $true
}
Expand Down
58 changes: 58 additions & 0 deletions Tests/Tests.pssproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{279294c4-e197-48cd-a1fb-263ce47dea4d}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>MyApplication</RootNamespace>
<AssemblyName>MyApplication</AssemblyName>
<Name>Tests</Name>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Folder Include="xSharePoint\" />
</ItemGroup>
<ItemGroup>
<Content Include="xSharePoint\xSharePoint.Global.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPWebApplication.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPUserProfileSyncService.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPUserProfileServiceApp.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPusageApplication.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPStateServiceApp.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPSite.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPServiceInstance.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPServiceAppPool.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPSearchServiceApp.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPManagedPath.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPManagedMetadataServiceApp.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPManagedAccount.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPJoinFarm.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPInstallPrereqs.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPInstall.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPFeature.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPDistributedCacheService.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPDiagnosticLoggingSettings.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPCreateFarm.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPCacheAccounts.Tests.ps1" />
<Content Include="xSharePoint\xSharePoint.xSPBCSServiceApp.Tests.ps1" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Target Name="Build" />
</Project>
39 changes: 39 additions & 0 deletions Tests/xSharePoint/xSharePoint.Global.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[CmdletBinding()]
param()

if (!$PSScriptRoot) # $PSScriptRoot is not defined in 2.0
{
$PSScriptRoot = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
}

$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest

$RepoRoot = (Resolve-Path $PSScriptRoot\..\..).Path

Describe 'xSharePoint Global Tests' {

$mofFiles = @(Get-ChildItem $RepoRoot -Recurse -Filter "*.schema.mof" -File | ? {
($_.FullName -like "*\DscResources\*")
})

Context 'MOF schemas use InstallAccount' {

It "Doesn't have an InstallAccount required parameter" {
$mofFilesWithNoInstallAccount = 0
$mofFiles | % {
$fileHasInstallAccount = $false
Get-Content $_.FullName | % {
if ($_.IndexOf("[Required, EmbeddedInstance(`"MSFT_Credential`")] String InstallAccount;") -gt 0) { $fileHasInstallAccount = $true }
}
if (-not $fileHasInstallAccount -and $_.Name -ne "MSFT_xSPInstall.schema.mof" `
-and $_.Name -ne "MSFT_xSPClearRemoteSessions.schema.mof" `
-and $_.Name -ne "MSFT_xSPInstallPrereqs.schema.mof") {
$mofFilesWithNoInstallAccount += 1
Write-Warning "File $($_.FullName) does not contain an InstallAccount parameter. All SharePoint specific resources should use this to impersonate as and access SharePoint resources"
}
}
$mofFilesWithNoInstallAccount | Should Be 0
}
}
}
52 changes: 52 additions & 0 deletions Tests/xSharePoint/xSharePoint.xSPBCSServiceApp.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[CmdletBinding()]
param()

if (!$PSScriptRoot) # $PSScriptRoot is not defined in 2.0
{
$PSScriptRoot = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
}

$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest

$RepoRoot = (Resolve-Path $PSScriptRoot\..\..).Path

$ModuleName = "MSFT_xSPBCSServiceApp"
Import-Module (Join-Path $RepoRoot "Modules\xSharePoint\DSCResources\$ModuleName\$ModuleName.psm1")

Describe "xSPBCSServiceApp" {
InModuleScope $ModuleName {
$testParams = @{
Name = "Test App"
ApplicationPool = "Test App Pool"
DatabaseName = "Test_DB"
DatabaseServer = "TestServer\Instance"
InstallAccount = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "password" -AsPlainText -Force))
}

Context "Validate test method" {
It "Fails when no service app exists" {
Mock Get-TargetResource { return @{} }
Test-TargetResource @testParams | Should Be $false
}
It "Passes when the service app exists" {
Mock Get-TargetResource {
return @{
Name = $testParams.Name
ApplicationPool = $testParams.ApplicationPool
}
}
Test-TargetResource @testParams | Should Be $true
}
It "Fails when the service app exists but has the wrong app pool" {
Mock Get-TargetResource {
return @{
Name = $testParams.Name
ApplicationPool = "Wrong app pool"
}
}
Test-TargetResource @testParams | Should Be $false
}
}
}
}
60 changes: 60 additions & 0 deletions Tests/xSharePoint/xSharePoint.xSPCacheAccounts.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[CmdletBinding()]
param()

if (!$PSScriptRoot) # $PSScriptRoot is not defined in 2.0
{
$PSScriptRoot = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
}

$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest

$RepoRoot = (Resolve-Path $PSScriptRoot\..\..).Path

$ModuleName = "MSFT_xSPCacheAccounts"
Import-Module (Join-Path $RepoRoot "Modules\xSharePoint\DSCResources\$ModuleName\$ModuleName.psm1")

Describe "xSPCacheAccounts" {
InModuleScope $ModuleName {
$testParams = @{
WebAppUrl = "http://test.sharepoint.com"
SuperUserAlias = "DEMO\SuperUser"
SuperReaderAlias = "DEMO\SuperReader"
InstallAccount = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "password" -AsPlainText -Force))
}

Context "Validate test method" {
It "Fails when no cache accounts exist" {
Mock -ModuleName $ModuleName Get-TargetResource { return @{} }
Test-TargetResource @testParams | Should Be $false
}
It "Passes when the correct accounts are assigned" {
Mock -ModuleName $ModuleName Get-TargetResource {
return @{
portalsuperuseraccount = $testParams.SuperUserAlias
portalsuperreaderaccount = $testParams.SuperReaderAlias
}
}
Test-TargetResource @testParams | Should Be $true
}
It "Fails when the wrong super reader is defined" {
Mock -ModuleName $ModuleName Get-TargetResource {
return @{
portalsuperuseraccount = $testParams.SuperUserAlias
portalsuperreaderaccount = "DEMO\WrongUser"
}
}
Test-TargetResource @testParams | Should Be $false
}
It "Fails when the wrong super user is defined" {
Mock -ModuleName $ModuleName Get-TargetResource {
return @{
portalsuperuseraccount = "DEMO\WrongUser"
portalsuperreaderaccount = $testParams.SuperReaderAlias
}
}
Test-TargetResource @testParams | Should Be $false
}
}
}
}
43 changes: 43 additions & 0 deletions Tests/xSharePoint/xSharePoint.xSPCreateFarm.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[CmdletBinding()]
param()

if (!$PSScriptRoot) # $PSScriptRoot is not defined in 2.0
{
$PSScriptRoot = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
}

$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest

$RepoRoot = (Resolve-Path $PSScriptRoot\..\..).Path

$ModuleName = "MSFT_xSPCreateFarm"
Import-Module (Join-Path $RepoRoot "Modules\xSharePoint\DSCResources\$ModuleName\$ModuleName.psm1")

Describe "xSPCreateFarm" {
InModuleScope $ModuleName {
$testParams = @{
FarmConfigDatabaseName = "SP_Config"
DatabaseServer = "DatabaseServer\Instance"
FarmAccount = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "password" -AsPlainText -Force))
InstallAccount = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "password" -AsPlainText -Force))
Passphrase = "passphrase"
AdminContentDatabaseName = "Admin_Content"
}

Context "Validate test method" {
It "Fails when local server is not in a farm" {
Mock -ModuleName $ModuleName Get-TargetResource { return @{} }
Test-TargetResource @testParams | Should Be $false
}
It "Passes when local server is in a farm" {
Mock -ModuleName $ModuleName Get-TargetResource {
return @{
FarmName = "SP_Config"
}
}
Test-TargetResource @testParams | Should Be $true
}
}
}
}

0 comments on commit 7f4f0d9

Please sign in to comment.