Skip to content

Commit 7be3fcf

Browse files
beaudryjcdhunt
authored andcommitted
* Added Features Requested in #5 resolves #5 * Broke Functions into individual Files #7 resolves #7 * Combined WebSiteState and WebSiteBinding and made better use of property * Forgot to update with new configuration * Made the Website a little less verbose * Broke Apart Website and Website Binding and added qualifiers to WebSiteBinding * Forgot to modify Help section * Modified Website Help * Added Tests for new function and added .trim() to get Get-PoshSpecParam
1 parent 2b7f270 commit 7be3fcf

27 files changed

Lines changed: 863 additions & 555 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Don't checkin this output dir
22
Release/
33

4-
.vscode/
4+
.vscode/
5+
.swp/

Private/Get-PoshspecParam.ps1

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function Get-PoshspecParam {
2+
[CmdletBinding()]
3+
param(
4+
[Parameter(Mandatory)]
5+
[string]
6+
$TestName,
7+
[Parameter(Mandatory)]
8+
[string]
9+
$TestExpression,
10+
[Parameter(Mandatory)]
11+
[string]
12+
$Target,
13+
[Parameter()]
14+
[string]
15+
$FriendlyName,
16+
[Parameter()]
17+
[string]
18+
$Property,
19+
[Parameter()]
20+
[string]
21+
$Qualifier,
22+
[Parameter(Mandatory)]
23+
[scriptblock]
24+
$Should
25+
)
26+
27+
$assertion = $Should.ToString().Trim()
28+
29+
if (-not $PSBoundParameters.ContainsKey("FriendlyName"))
30+
{
31+
$FriendlyName = $Target
32+
}
33+
34+
$expressionString = $TestExpression.ToString().Trim()
35+
36+
if ($PSBoundParameters.ContainsKey("Property"))
37+
{
38+
$expressionString += " | Select-Object -ExpandProperty '$Property'"
39+
40+
if ($PSBoundParameters.ContainsKey("Qualifier"))
41+
{
42+
$nameString = "{0} property '{1}' for '{2}' at '{3}' {4}" -f $TestName,$Property, $FriendlyName, $Qualifier, $assertion
43+
}
44+
else
45+
{
46+
$nameString = "{0} property '{1}' for '{2}' {3}" -f $TestName, $Property, $FriendlyName, $assertion
47+
}
48+
}
49+
else
50+
{
51+
$nameString = "{0} '{1}' {2}" -f $TestName, $FriendlyName, $assertion
52+
}
53+
54+
$expressionString = $ExecutionContext.InvokeCommand.ExpandString($expressionString)
55+
56+
$expressionString += " | $assertion"
57+
58+
Write-Output -InputObject ([PSCustomObject]@{Name = $nameString; Expression = $expressionString})
59+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Private - Build the It scriptblock based on parameters from Get-PoshspecParam
2+
function Invoke-PoshspecExpression {
3+
[CmdletBinding()]
4+
param(
5+
# Poshspec Param Object
6+
[Parameter(Mandatory, Position=0)]
7+
[PSCustomObject]
8+
$InputObject
9+
)
10+
11+
It $InputObject.Name {
12+
Invoke-Expression $InputObject.Expression
13+
}
14+
}

Public/AppPoolState.ps1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<#
2+
.SYNOPSIS
3+
Test State of Application Pool
4+
.DESCRIPTION
5+
Used To Determine if Application Pool is Running
6+
.PARAMETER Target
7+
The name of the App Pool to be Tested
8+
.PARAMETER Should
9+
A Script Block defining a Pester Assertion.
10+
.EXAMPLE
11+
AppPoolState TestSite { Should be Started }
12+
.NOTES
13+
Assertions: be
14+
#>
15+
function AppPoolState {
16+
[CmdletBinding()]
17+
param(
18+
[Parameter(Mandatory, Position=1)]
19+
[Alias('Name')]
20+
[string]$Target,
21+
22+
[Parameter(Mandatory, Position=2)]
23+
[scriptblock]$Should
24+
)
25+
26+
$expression = {Get-WebAppPoolState -Name '$Target' -ErrorAction SilentlyContinue}
27+
28+
$params = Get-PoshspecParam -TestName AppPoolState -Property "Value" -TestExpression $expression @PSBoundParameters
29+
30+
Invoke-PoshspecExpression @params
31+
}

Public/CheckAppPool.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<#
2+
.SYNOPSIS
3+
Check if AppPool Exists
4+
.DESCRIPTION
5+
Used To Determine if Website Exists
6+
.PARAMETER Target
7+
The name of the App Pool to be Tested
8+
.PARAMETER Should
9+
A Script Block defining a Pester Assertion.
10+
.EXAMPLE
11+
CheckAppPool TestSite { Should be $True}
12+
.NOTES
13+
#REQUIRES# webadministration module
14+
Assertions: be
15+
#>
16+
function CheckAppPool {
17+
[CmdletBinding()]
18+
param(
19+
[Parameter(Mandatory, Position=1)]
20+
[Alias('Name')]
21+
[string]$Target,
22+
23+
[Parameter(Mandatory, Position=2)]
24+
[scriptblock]$Should
25+
)
26+
27+
$expression = {Test-Path -Path "IIS:\AppPools\$Target" -ErrorAction SilentlyContinue}
28+
29+
$params = Get-PoshspecParam -TestName CheckAppPool -TestExpression $expression @PSBoundParameters
30+
31+
Invoke-PoshspecExpression @params
32+
}

Public/CheckSite.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<#
2+
.SYNOPSIS
3+
Check if Site Exists
4+
.DESCRIPTION
5+
Used To Determine if Website Exists
6+
.PARAMETER Target
7+
The name of the Web Site to be Tested
8+
.PARAMETER Should
9+
A Script Block defining a Pester Assertion.
10+
.EXAMPLE
11+
CheckSite TestSite { Should be $True}
12+
.NOTES
13+
#REQUIRES# webadministration module
14+
Assertions: be
15+
#>
16+
function CheckSite {
17+
[CmdletBinding()]
18+
param(
19+
[Parameter(Mandatory, Position=1)]
20+
[Alias('Name')]
21+
[string]$Target,
22+
23+
[Parameter(Mandatory, Position=2)]
24+
[scriptblock]$Should
25+
)
26+
27+
$expression = {Test-Path -Path "IIS:\Sites\$Target" -ErrorAction SilentlyContinue}
28+
29+
$params = Get-PoshspecParam -TestName CheckSite -TestExpression $expression @PSBoundParameters
30+
31+
Invoke-PoshspecExpression @params
32+
}

Public/CimObject.ps1

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<#
2+
.SYNOPSIS
3+
Test the value of a CimObject Property.
4+
.DESCRIPTION
5+
Test the value of a CimObject Property. The Class can be provided with the Namespace. See Example.
6+
.PARAMETER ClassName
7+
Specifies the name of the CIM class for which to retrieve the CIM instances. Can be just the ClassName
8+
in the default namespace or in the form of namespace/className to access other namespaces.
9+
.PARAMETER Property
10+
Specifies an instance property to retrieve.
11+
.PARAMETER Should
12+
A Script Block defining a Pester Assertion.
13+
.EXAMPLE
14+
CimObject Win32_OperatingSystem SystemDirectory { Should Be C:\WINDOWS\system32 }
15+
.EXAMPLE
16+
CimObject root/StandardCimv2/MSFT_NetOffloadGlobalSetting ReceiveSideScaling { Should Be Enabled }
17+
.NOTES
18+
Assertions: Be, BeExactly, Match, MatchExactly
19+
#>
20+
function CimObject {
21+
[CmdletBinding()]
22+
param(
23+
[Parameter(Mandatory, Position=1)]
24+
[Alias("ClassName")]
25+
[string]$Target,
26+
27+
[Parameter(Mandatory, Position=2)]
28+
[string]$Property,
29+
30+
[Parameter(Mandatory, Position=3)]
31+
[scriptblock]$Should
32+
)
33+
34+
35+
$expression = "Get-CimInstance"
36+
37+
if ($Target -match '/')
38+
{
39+
$lastIndexOfSlash = $Target.LastIndexOf('/')
40+
41+
$class = $Target.Substring($lastIndexOfSlash + 1)
42+
$namespace = $Target.Substring(0,$lastIndexOfSlash)
43+
44+
$PSBoundParameters["Target"] = $class
45+
$PSBoundParameters.Add("Qualifier", $namespace)
46+
47+
$expression = {Get-CimInstance -ClassName $Target -Namespace $Qualifier}
48+
}
49+
else
50+
{
51+
$expression = {Get-CimInstance -ClassName $Target}
52+
}
53+
54+
$params = Get-PoshspecParam -TestName CimObject -TestExpression $expression @PSBoundParameters
55+
56+
Invoke-PoshspecExpression @params
57+
}

Public/DnsHost.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<#
2+
.SYNOPSIS
3+
Test DNS resolution to a host.
4+
.DESCRIPTION
5+
Test DNS resolution to a host.
6+
.PARAMETER Target
7+
The hostname to resolve in DNS.
8+
.PARAMETER Should
9+
A Script Block defining a Pester Assertion.
10+
.EXAMPLE
11+
dnshost nonexistenthost.mymadeupdomain.tld { should be $null }
12+
.EXAMPLE
13+
dnshost www.google.com { should not be $null }
14+
.NOTES
15+
Assertions: be
16+
#>
17+
function DnsHost {
18+
[CmdletBinding()]
19+
param(
20+
[Parameter(Mandatory, Position=1)]
21+
[Alias('Name')]
22+
[string]$Target,
23+
24+
[Parameter(Mandatory, Position=2)]
25+
[scriptblock]$Should
26+
)
27+
28+
$expression = {Resolve-DnsName -Name $Target -DnsOnly -NoHostsFile -ErrorAction SilentlyContinue}
29+
30+
$params = Get-PoshspecParam -TestName DnsHost -TestExpression $expression @PSBoundParameters
31+
32+
Invoke-PoshspecExpression @params
33+
}

Public/File.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<#
2+
.SYNOPSIS
3+
Test a File.
4+
.DESCRIPTION
5+
Test the Existance or Contents of a File..
6+
.PARAMETER Path
7+
Specifies the path to an item.
8+
.PARAMETER Should
9+
A Script Block defining a Pester Assertion.
10+
.EXAMPLE
11+
File C:\inetpub\wwwroot\iisstart.htm { Should Exist }
12+
.EXAMPLE
13+
File C:\inetpub\wwwroot\iisstart.htm { Should Contain 'text-align:center' }
14+
.NOTES
15+
Assertions: Exist and Contain
16+
#>
17+
function File {
18+
[CmdletBinding()]
19+
param(
20+
[Parameter(Mandatory, Position=1)]
21+
[Alias("Path")]
22+
[string]$Target,
23+
24+
[Parameter(Mandatory, Position=2)]
25+
[scriptblock]$Should
26+
)
27+
28+
$name = Split-Path -Path $Target -Leaf
29+
$params = Get-PoshspecParam -TestName File -TestExpression {'$Target'} -FriendlyName $name @PSBoundParameters
30+
31+
Invoke-PoshspecExpression @params
32+
}

Public/Folder.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<#
2+
.SYNOPSIS
3+
Test if a folder exists.
4+
.DESCRIPTION
5+
Test if a folder exists.
6+
.PARAMETER Target
7+
The path of the folder to search for.
8+
.PARAMETER Should
9+
A Script Block defining a Pester Assertion.
10+
.EXAMPLE
11+
folder $env:ProgramData { should exist }
12+
.EXAMPLE
13+
folder C:\badfolder { should not exist }
14+
.NOTES
15+
Assertions: exist
16+
#>
17+
function Folder {
18+
[CmdletBinding()]
19+
param(
20+
[Parameter(Mandatory, Position=1)]
21+
[Alias('Path')]
22+
[string]$Target,
23+
24+
[Parameter(Mandatory, Position=2)]
25+
[scriptblock]$Should
26+
)
27+
28+
29+
$params = Get-PoshspecParam -TestName Folder -TestExpression {'$Target'} @PSBoundParameters
30+
31+
Invoke-PoshspecExpression @params
32+
}

0 commit comments

Comments
 (0)