Skip to content

Commit

Permalink
Added new functions to FunctionsToExport
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhunt committed May 15, 2016
2 parents 099005a + a3a48b9 commit 8b2a6fe
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 4 deletions.
91 changes: 89 additions & 2 deletions Tests/poshspec.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,95 @@ Describe 'Test Functions' {
It "Should return the correct test Expression" {
$results.Expression | Should Be "Get-CimInstance -ClassName 'MSFT_DSCConfigurationStatus' -Namespace 'root/Microsoft/Windows/DesiredStateConfiguration' | Select-Object -ExpandProperty 'Error' | Should BeNullOrEmpty"
}
}
}

Context 'Package' {

$results = Package 'Microsoft Visual Studio Code' { Should Not BeNullOrEmpty }

It 'Should return a correct test name' {
$results.Name | Should Be "Package 'Microsoft Visual Studio Code' Should Not BeNullOrEmpty"
}

It 'Should return a correct text expression' {
$results.Expression | Should Be "Get-Package -Name 'Microsoft Visual Studio Code' -ErrorAction SilentlyContinue | Should Not BeNullOrEmpty"
}
}

Context 'Package w/ properties' {

$results = Package 'Microsoft Visual Studio Code' version { Should be '1.1.0' }

It 'Should return a correct test name' {
$results.Name | Should Be "Package property 'version' for 'Microsoft Visual Studio Code' Should be '1.1.0'"
}

It 'Should return a correct text expression' {
$results.Expression | Should Be "Get-Package -Name 'Microsoft Visual Studio Code' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'version' | Should be '1.1.0'"
}
}

Context 'LocalGroup' {

$results = LocalGroup 'Administrators' { Should Not BeNullOrEmpty }

It 'Should return a correct test name' {
$results.Name | Should Be "LocalGroup 'Administrators' Should Not BeNullOrEmpty"
}

It 'Should return a correct text expression' {
$results.Expression | Should Be 'Get-CimInstance -ClassName Win32_Group -Filter "Name = ''$Target''" | Should Not BeNullOrEmpty'
}
}

Context 'Interface' {
$results = Interface ethernet0 { Should Not BeNullOrEmpty }

It 'Should return a correct test name' {
$results.Name | Should Be "Interface 'ethernet0' Should Not BeNullOrEmpty"
}

It 'Should return a correct text expression' {
$results.Expression | Should Be "Get-NetAdapter -Name 'ethernet0' -ErrorAction SilentlyContinue | Should Not BeNullOrEmpty"
}
}

Context 'Interface w/ properties' {
$results = interface ethernet0 status { should be 'up' }

It 'Should return a correct test name' {
$results.Name | Should Be "Interface property 'status' for 'ethernet0' should be 'up'"
}

It 'Should return a correct text expression' {
$results.Expression | Should Be "Get-NetAdapter -Name 'ethernet0' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'status' | Should Be 'up'"
}
}

Context 'Folder' {
$results = Folder $env:ProgramData { should exist }

It "Should return the correct test Name" {
$results.Name | Should Be "Folder 'C:\ProgramData' Should Exist"
}

It "Should return the correct test Expression" {
$results.Expression | Should Be "'C:\ProgramData' | Should Exist"
}
}

Context 'Dnshost' {
$results = DnsHost www.google.com { should not BeNullOrEmpty }

It "Should return the correct test Name" {
$results.Name | Should Be "DnsHost 'www.google.com' Should Not BeNullOrEmpty"
}

It "Should return the correct test Expression" {
$results.Expression | Should Be "Resolve-DnsName -Name 'www.google.com' -DnsOnly -NoHostsFile -ErrorAction SilentlyContinue | Should Not BeNullOrEmpty"
}
}
}
}

Remove-Module Poshspec
Remove-Module Poshspec
Binary file modified poshspec.psd1
Binary file not shown.
198 changes: 196 additions & 2 deletions poshspec.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ function Get-PoshspecParam {
}
else
{
Write-Output -InputObject $token.Content
if ($token.Type -eq 'String') {
Write-Output -InputObject ('"' + $token.Content + '"')
} else {
if ($token.Type -ne 'NewLine') {
Write-Output -InputObject $token.Content
}
}
}
}

Expand Down Expand Up @@ -403,4 +409,192 @@ function CimObject {
$params = Get-PoshspecParam -TestName CimObject -TestExpression $expression @PSBoundParameters

Invoke-PoshspecExpression @params
}
}

<#
.SYNOPSIS
Test for installed package.
.DESCRIPTION
Test that a specified package is installed.
.PARAMETER Target
Specifies the Display Name of the package to search for.
.PARAMETER Property
Specifies an optional property to test for on the package.
.PARAMETER Should
A Script Block defining a Pester Assertion.
.EXAMPLE
package 'Microsoft Visual Studio Code' { should not BeNullOrEmpty }
.EXAMPLE
package 'Microsoft Visual Studio Code' version { should be '1.1.0' }
.EXAMPLE
package 'NonExistentPackage' { should BeNullOrEmpty }
.NOTES
Assertions: Be, BeNullOrEmpty
#>
function Package {
[CmdletBinding(DefaultParameterSetName="Default")]
param(
[Parameter(Mandatory, Position=1,ParameterSetName="Default")]
[Parameter(Mandatory, Position=1,ParameterSetName="Property")]
[Alias('Name')]
[string]$Target,

[Parameter(Position=2,ParameterSetName="Property")]
[string]$Property,

[Parameter(Mandatory, Position=2,ParameterSetName="Default")]
[Parameter(Mandatory, Position=3,ParameterSetName="Property")]
[scriptblock]$Should
)

$expression = {Get-Package -Name $Target -ErrorAction SilentlyContinue}

$params = Get-PoshspecParam -TestName Package -TestExpression $expression @PSBoundParameters

Invoke-PoshspecExpression @params
}

<#
.SYNOPSIS
Test if a local group exists.
.DESCRIPTION
Test if a local group exists.
.PARAMETER Id
The local group name to test for. Eg 'Administrators'
.PARAMETER Should
A Script Block defining a Pester Assertion.
.EXAMPLE
LocalGroup 'Administrators' { should not BeNullOrEmpty }
.EXAMPLE
LocalGroup 'BadGroup' { should BeNullOrEmpty }
.NOTES
Assertions: BeNullOrEmpty
#>
function LocalGroup {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position=1)]
[Alias('Name')]
[string]$Target,

[Parameter(Mandatory, Position=2)]
[scriptblock]$Should
)

$expression = {Get-CimInstance -ClassName Win32_Group -Filter "Name = '$Target'"}

$params = Get-PoshspecParam -TestName LocalGroup -TestExpression $expression @PSBoundParameters

Invoke-PoshspecExpression @params
}

<#
.SYNOPSIS
Test a local network interface.
.DESCRIPTION
Test a local network interface and optionally and specific property.
.PARAMETER Target
Specifies the name of the network adapter to search for.
.PARAMETER Property
Specifies an optional property to test for on the adapter.
.PARAMETER Should
A Script Block defining a Pester Assertion.
.EXAMPLE
interface ethernet0 { should not BeNullOrEmpty }
.EXAMPLE
interface ethernet0 status { should be 'up' }
.EXAMPLE
Interface Ethernet0 linkspeed { should be '1 gbps' }
.EXAMPLE
Interface Ethernet0 macaddress { should be '00-0C-29-F2-69-DD' }
.NOTES
Assertions: Be, BeNullOrEmpty
#>
function Interface {
[CmdletBinding(DefaultParameterSetName="Default")]
param(
[Parameter(Mandatory, Position=1,ParameterSetName="Default")]
[Parameter(Mandatory, Position=1,ParameterSetName="Property")]
[Alias('Name')]
[string]$Target,

[Parameter(Position=2,ParameterSetName="Property")]
[string]$Property,

[Parameter(Mandatory, Position=2,ParameterSetName="Default")]
[Parameter(Mandatory, Position=3,ParameterSetName="Property")]
[scriptblock]$Should
)

$expression = {Get-NetAdapter -Name $Target -ErrorAction SilentlyContinue}

$params = Get-PoshspecParam -TestName Interface -TestExpression $expression @PSBoundParameters

Invoke-PoshspecExpression @params
}

<#
.SYNOPSIS
Test if a folder exists.
.DESCRIPTION
Test if a folder exists.
.PARAMETER Target
The path of the folder to search for.
.PARAMETER Should
A Script Block defining a Pester Assertion.
.EXAMPLE
folder $env:ProgramData { should exist }
.EXAMPLE
folder C:\badfolder { should not exist }
.NOTES
Assertions: exist
#>
function Folder {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position=1)]
[Alias('Path')]
[string]$Target,

[Parameter(Mandatory, Position=2)]
[scriptblock]$Should
)

$params = Get-PoshspecParam -TestName Folder -TestExpression {$Target} @PSBoundParameters

Invoke-PoshspecExpression @params
}

<#
.SYNOPSIS
Test DNS resolution to a host.
.DESCRIPTION
Test DNS resolution to a host.
.PARAMETER Target
The hostname to resolve in DNS.
.PARAMETER Should
A Script Block defining a Pester Assertion.
.EXAMPLE
dnshost nonexistenthost.mymadeupdomain.tld { should be $null }
.EXAMPLE
dnshost www.google.com { should not be $null }
.NOTES
Assertions: be
#>
function DnsHost {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position=1)]
[Alias('Name')]
[string]$Target,

[Parameter(Mandatory, Position=2)]
[scriptblock]$Should
)

$expression = {Resolve-DnsName -Name $Target -DnsOnly -NoHostsFile -ErrorAction SilentlyContinue}

$params = Get-PoshspecParam -TestName DnsHost -TestExpression $expression @PSBoundParameters

Invoke-PoshspecExpression @params
}

0 comments on commit 8b2a6fe

Please sign in to comment.