Code Monkey home page Code Monkey logo

poshspec's Introduction

PoshSpec

An infrastructure testing DSL running in Pester. The goal is to expand the Pester DSL to assist in the development of infrastructure validation scripts.

Install from the Gallery

PS> Install-Module -Name poshspec

Documentation

Wiki

poshspec's People

Contributors

adbertram avatar aspenforester avatar beaudryj avatar cdhunt avatar devblackops avatar markwragg avatar michaeltlombardi avatar sam-martin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

poshspec's Issues

Unable to call Custom functions in poshspec.

I am using Package resource for my infrastructure validation. As my environment has only powershell v4, Get-package function was not there.

I have written a module named Poshspechelper in my machine and written a custom function
Get-InstalledPackage. The function is given below.

function Get-InstalledPackage
{
    [CmdletBinding()]
  param($appName, $appVersion)

  if ((Get-WmiObject win32_operatingsystem).OSArchitecture -notmatch '64')  
  { 
    $keys= (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*')
    $possible_path= 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    if (Test-Path $possible_path)
    {
      $keys+= (Get-ItemProperty $possible_path)
    }
  }  
  else  
  { 
    $keys = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*')
    $possible_path= 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    if (Test-Path $possible_path)
    {
      $keys+= (Get-ItemProperty $possible_path)
    }
    $possible_path= 'HKCU:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
    if (Test-Path $possible_path)
    {
      $keys+= (Get-ItemProperty $possible_path)
    }
  }

  if ($appVersion -eq $null) { 
    @($keys | Where-Object {$_.DisplayName -like $appName -or $_.PSChildName -like $appName}).Length -gt 0
  }
  else{
    $IsAppInstalled =    @($keys | Where-Object {$_.DisplayName -like $appName -or $_.PSChildName -like $appName}).Length -gt 0
    $VersionAvailable =   @($keys | Where-Object {$_.DisplayName -like $appName -or $_.PSChildName -like $appName  } | Where-Object {$_.DisplayVersion -eq $appVersion} ).Length -gt 0
    if ($VersionAvailable)
    {
        $Version = $appVersion
    }    
    $object = [Pscustomobject] @{
        IsInstalled= $IsAppInstalled
        Version= $Version   
    }
    Write-Output $object

  }
}

When i run this with pester like below , it worked as expected. .

Import-module pester
Import-Module PoshspecHelper

Describe "Orca msi validation" {
    It "Package validation" {
        Get-InstalledPackage -appName 'Orca' -appVersion '3.1.3790.0000' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'version' | should be '3.1.3790.0000'
    }
}

But when i call this in package.ps1 file instead of Get-package (like given below) , i am getting error given below the code.


#$expression = {Get-Package -Name '$Target' -ErrorAction SilentlyContinue}
    $expression = { Get-InstalledPackage -appName '$Target' -appVersion $Property -ErrorAction SilentlyContinue}

[-] Package2 property 'version' for 'Orca' should be '3.1.3790.0000' 67.04s
Expected: {3.1.3790.0000}
But was: {}
1: Get-InstalledPackage -appName 'Orca' -appVersion version -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'versio
n' | should be '3.1.3790.0000'
at , : line 1
at , C:\Program Files\WindowsPowerShell\Modules\Poshspec\2.1.12\Private\Invoke-PoshspecExpression.ps1: line 12

Please check and let me know what went wrong . when i debugged it ,it always failed in the below line at Invoke-PoshspecExpression.ps1 file

Invoke-Expression $InputObject.Expression

Path prompt with SoftwareProduct

I get a 'Path[0]:' prompt when using SoftwareProduct for Test-Path. Somehow the $_ in the Test-Path scriptblock is getting lost somewhere in lines 81-82 on get-PoshspecParam.ps1.

  $expressionString = $ExecutionContext.InvokeCommand.ExpandString($expressionString)
  Write-Output -InputObject ([PSCustomObject]@{Name = $nameString; Expression = $expressionString})

I'm getting this error on the same system running PS 5.1 in vscode and the regular host.

image
image

IIS Tests

  • Does the site exist?
  • Whats the status of the site?
  • Does it have a binding for port X?
  • Does it have SSL enabled?
  • Does the app pool exist?
  • Whats the status of the app pool?

Additional Test: UserRightsAssignment

When validating the security configuration of a server it is often necessary to test whether or not a particular account has certain user rights assigned to it or whether a particular user right is assigned to specific users.

We require a flexible implementation that will allow operators to write tests that test either for accounts or for user rights.

Example

UserRightsAssignment ByAccount "BUILTIN\Users" { Should Not Match "SeServiceLogonRight" }
UserRightsAssignment ByRight "SeDebugPrivilege" { Should Be "BUILTIN\Administrators" }

Additional Test: AuditPolicy

It would be useful to be able to verify existing audit policies (as seen/managed by auditpol.exe) using poshspec.

$results = AuditPolicy System 'Security System Extension' { Should Be 'Success' }

Service Function: Get-Service vs Get-WmiObject\Get-CimInstance

Hi there,

The Service function allows you to specify a property which defaults to Status but due to using Get-Service does not allow you to check the StartMode, to get around this I changed the Service function to use

-TestExpression {Get-wmiobject -class win32_service -Filter "Name = '$Target'"}

and changed the default property to state which then lets you test either.

Is there a reason for using Get-Service over WMI or CIM?

Thanks,
Chris

DnsHost - Add server & type of query

Does the above make sense to be added to the DnsHost implementation ?

I am working on project, where we have multiple DNS servers set on the client nodes and want to validate that the name resolution works using each of the DNS server, also at the same time be able to make different type of DNS queries to the DNS server.

Current implementation of the DnsHost does not let you specify a DNS server or the type of DNS query to for a validation test.

For Example - a machine can have multiple DNS servers, so this test should allow specifying a specific DNS server for the name resolution along with the type of DNS query to run for the test e.g MX or an A record query etc.

Describe 'multiple DNS servers' {

    Context 'Test for the DNS server1' {
        DnsHost cloud.local 10.116.2.250  {Should Not BeNullOrEmpty} # query the domain FQDN
        DnsHost mail.cloud.local 10.116.2.250 -Type MX {Should Not BeNullOrEmpty} # test the MX record
    }

    Context 'Test for the DNS server2' {
        DnsHost cloud.local 10.116.2.251 {Should Not BeNullOrEmpty}
        DnsHost mail.cloud.local 10.116.2.251 -Type MX {Should Not BeNullOrEmpty}
    }
}

Poshspec Pester Tests broken from PR #44

PR #44 broke the following poshspec pester contexts. The AD Commands are failing because the module is not found, and the other functions are failing because the tests weren't updated to reflect the automatic expansion of Exists and Not Exist to Should be notnullorempty, and Should not benullorempty; respectively.

Context                    Describe                        Name
-------                    --------                        ----
One Parameter              Get-PoshspecParam               Should return the correct test Expression
One Parameter with a space Get-PoshspecParam               Should return the correct test Expression
Two Parameters             Get-PoshspecParam               Should return the correct test Expression
Three Parameters           Get-PoshspecParam               Should return the correct test Expression
OU Exists                  Get-PoshSpecADOrganizatinalUnit Error occurred in Context block
OU does not exist          Get-PoshSpecADOrganizatinalUnit Error occurred in Context block
Group Exists               Get-PoshSpecADGroup             Error occurred in Context block
Group does not exist       Get-PoshSpecADGroup             Error occurred in Context block
User Exists                Get-PoshSpecADUser              Error occurred in Context block
User does not exist        Get-PoshSpecADUser              Error occurred in Context block
File                       Test Functions                  Should return the correct test Expression
Registry w/o Properties    Test Functions                  Should return the correct test Expression
Hotfix                     Test Functions                  Should return the correct test Expression
Folder                     Test Functions                  Should return the correct test Expression
SoftwareProduct            Test Functions                  Should return the correct test Expression
SoftwareProduct w/Property Test Functions                  Should return the correct test Expression

Full Results (of Just Errors):

    Context One Parameter
      [+] Should return the correct test Name 37ms
      [-] Should return the correct test Expression 36ms
        Expected string length 42 but was 30. Strings differ at index 25.
        Expected: {Get-Item 'name' | should not benullorempty}
        But was:  {Get-Item 'Name' | Should Exist}
        ------------------------------------^
        at <ScriptBlock>, C:\Users\Administrator\Documents\WindowsPowerShell\Modules\poshspec\2.1.16\Tests\poshspec.Test
s.ps1: line 23
        23:                 $results.Expression | Should Be "Get-Item 'name' | should not benullorempty"

    Context One Parameter with a space
      [+] Should return the correct test Name 27ms
      [-] Should return the correct test Expression 11ms
        Expected string length 50 but was 38. Strings differ at index 33.
        Expected: {Get-Item 'Spaced Value' | should not benullorempty}
        But was:  {Get-Item 'Spaced Value' | Should Exist}
        --------------------------------------------^
        at <ScriptBlock>, C:\Users\Administrator\Documents\WindowsPowerShell\Modules\poshspec\2.1.16\Tests\poshspec.Test
s.ps1: line 35
        35:                 $results.Expression | Should Be "Get-Item 'Spaced Value' | should not benullorempty"

    Context Two Parameters
      [+] Should return the correct test Name 28ms
      [-] Should return the correct test Expression 40ms
        Expected string length 98 but was 86. Strings differ at index 81.
        Expected: {Get-Item 'name' 'Something' | Select-Object -ExpandProperty 'Something' | should not benullorempty}
        But was:  {Get-Item 'Name' 'Something' | Select-Object -ExpandProperty 'Something' | Should Exist}
        --------------------------------------------------------------------------------------------^
        at <ScriptBlock>, C:\Users\Administrator\Documents\WindowsPowerShell\Modules\poshspec\2.1.16\Tests\poshspec.Test
s.ps1: line 47
        47:                 $results.Expression | Should Be "Get-Item 'name' 'Something' | Select-Object -ExpandProperty
 'Something' | should not benullorempty"

    Context Three Parameters
      [+] Should return the correct test Name 35ms
      [-] Should return the correct test Expression 12ms
        Expected string length 102 but was 90. Strings differ at index 85.
        Expected: {Get-Item 'name' 'Something' '1' | Select-Object -ExpandProperty 'Something' | should not benullorempt
y}
        But was:  {Get-Item 'Name' 'Something' '1' | Select-Object -ExpandProperty 'Something' | Should Exist}
        ------------------------------------------------------------------------------------------------^
        at <ScriptBlock>, C:\Users\Administrator\Documents\WindowsPowerShell\Modules\poshspec\2.1.16\Tests\poshspec.Test
s.ps1: line 59
        59:                 $results.Expression | Should Be "Get-Item 'name' 'Something' '1' | Select-Object -ExpandProp
erty 'Something' | should not benullorempty"

  Describing Get-PoshSpecADOrganizatinalUnit

    Context OU Exists
      [-] Error occurred in Context block 55ms
        Could not find Command SearchAd
        At C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.3\Functions\Mock.ps1:850 char:9

    Context OU does not exist
      [-] Error occurred in Context block 55ms
        Could not find Command SearchAd
        At C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.3\Functions\Mock.ps1:850 char:9

  Describing Get-PoshSpecADGroup

    Context Group Exists
      [-] Error occurred in Context block 55ms
        Could not find Command SearchAd
        At C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.3\Functions\Mock.ps1:850 char:9

    Context Group does not exist
      [-] Error occurred in Context block 38ms
        Could not find Command SearchAd
        At C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.3\Functions\Mock.ps1:850 char:9

  Describing Get-PoshSpecADUser

    Context User Exists
      [-] Error occurred in Context block 58ms
        Could not find Command SearchAd
        At C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.3\Functions\Mock.ps1:850 char:9

    Context User does not exist
      [-] Error occurred in Context block 61ms
        Could not find Command SearchAd
        At C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.3\Functions\Mock.ps1:850 char:9

    Context File
      [+] Should return the correct test Name 34ms
      [-] Should return the correct test Expression 10ms
        Expected string length 105 but was 48. Strings differ at index 0.
        Expected: {Get-Item -Path 'C:\inetpub\wwwroot\iisstart.htm' -ErrorAction SilentlyContinue | should not benullore
mpty}
        But was:  {'C:\inetpub\wwwroot\iisstart.htm' | Should Exist}
        -----------^
        at <ScriptBlock>, C:\Users\Administrator\Documents\WindowsPowerShell\Modules\poshspec\2.1.16\Tests\poshspec.Test
s.ps1: line 297
        297:                 $results.Expression | Should Be "Get-Item -Path 'C:\inetpub\wwwroot\iisstart.htm' -ErrorAct
ion SilentlyContinue | should not benullorempty"

    Context Registry w/o Properties
      [+] Should return the correct test Name 35ms
      [-] Should return the correct test Expression 12ms
        Expected string length 118 but was 61. Strings differ at index 0.
        Expected: {Get-Item -Path 'HKLM:\SOFTWARE\Microsoft\Rpc\ClientProtocols' -ErrorAction SilentlyContinue | should
not benullorempty}
        But was:  {'HKLM:\SOFTWARE\Microsoft\Rpc\ClientProtocols' | Should Exist}
        -----------^
        at <ScriptBlock>, C:\Users\Administrator\Documents\WindowsPowerShell\Modules\poshspec\2.1.16\Tests\poshspec.Test
s.ps1: line 310
        310:                 $results.Expression | Should Be "Get-Item -Path 'HKLM:\SOFTWARE\Microsoft\Rpc\ClientProtoco
ls' -ErrorAction SilentlyContinue | should not benullorempty"

    Context Hotfix
      [+] Should return the correct test Name 33ms
      [-] Should return the correct test Expression 13ms
        Expected string length 81 but was 69. Strings differ at index 64.
        Expected: {Get-HotFix -Id KB1234567 -ErrorAction SilentlyContinue | should not benullorempty}
        But was:  {Get-HotFix -Id KB1234567 -ErrorAction SilentlyContinue | Should Exist}
        ---------------------------------------------------------------------------^
        at <ScriptBlock>, C:\Users\Administrator\Documents\WindowsPowerShell\Modules\poshspec\2.1.16\Tests\poshspec.Test
s.ps1: line 362
        362:                 $results.Expression | Should Be "Get-HotFix -Id KB1234567 -ErrorAction SilentlyContinue | s
hould not benullorempty"

    Context Folder
      [+] Should return the correct test Name 33ms
      [-] Should return the correct test Expression 13ms
        Expected string length 88 but was 31. Strings differ at index 0.
        Expected: {Get-Item -Path 'C:\ProgramData' -ErrorAction SilentlyContinue | should not benullorempty}
        But was:  {'C:\ProgramData' | should exist}
        -----------^
        at <ScriptBlock>, C:\Users\Administrator\Documents\WindowsPowerShell\Modules\poshspec\2.1.16\Tests\poshspec.Test
s.ps1: line 488
        488:                 $results.Expression | Should Be "Get-Item -Path 'C:\ProgramData' -ErrorAction SilentlyConti
nue | should not benullorempty"

    Context SoftwareProduct
      [+] Should return the correct test Name 34ms
      [-] Should return the correct test Expression 13ms
        Expected string length 270 but was 165. Strings differ at index 0.
        Expected: {@('HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*','HKLM:\\Software\Microsof
t\Windows\CurrentVersion\Uninstall\*') | Where-Object { Test-Path  } | Get-ItemProperty | Where-Object DisplayName -Matc
h 'Microsoft .NET Framework 4.6.1' | Should Exist}
        But was:  {Get-ItemProperty -Path hklm:\\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object Di
splayName -Match 'Microsoft .NET Framework 4.6.1' | Should Exist}
        -----------^
        at <ScriptBlock>, C:\Users\Administrator\Documents\WindowsPowerShell\Modules\poshspec\2.1.16\Tests\poshspec.Test
s.ps1: line 583
        583:                 $results.Expression | Should Be "@('HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVer
sion\Uninstall\*','HKLM:\\Software\Microsoft\Windows\CurrentVersion\Uninstall\*') | Where-Object { Test-Path $_ } | Get-
ItemProperty | Where-Object DisplayName -Match 'Microsoft .NET Framework 4.6.1' | Should Exist"

    Context SoftwareProduct w/Property
      [+] Should return the correct test Name 41ms
      [-] Should return the correct test Expression 10ms
        Expected string length 326 but was 221. Strings differ at index 0.
        Expected: {@('HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*','HKLM:\\Software\Microsof
t\Windows\CurrentVersion\Uninstall\*') | Where-Object { Test-Path  } | Get-ItemProperty | Where-Object DisplayName -Matc
h 'Microsoft .NET Framework 4.6.1' | Select-Object -ExpandProperty 'DisplayVersion' | Should Be 4.6.01055}
        But was:  {Get-ItemProperty -Path hklm:\\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object Di
splayName -Match 'Microsoft .NET Framework 4.6.1' | Select-Object -ExpandProperty 'DisplayVersion' | Should Be 4.6.01055
}
        -----------^
        at <ScriptBlock>, C:\Users\Administrator\Documents\WindowsPowerShell\Modules\poshspec\2.1.16\Tests\poshspec.Test
s.ps1: line 596
        596:                 $results.Expression | Should Be "@('HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVer
sion\Uninstall\*','HKLM:\\Software\Microsoft\Windows\CurrentVersion\Uninstall\*') | Where-Object { Test-Path $_ } | Get-
ItemProperty | Where-Object DisplayName -Match 'Microsoft .NET Framework 4.6.1' | Select-Object -ExpandProperty 'Display
Version' | Should Be 4.6.01055"

Documentation Transition

This project's documentation is somewhat limited. I suggest migrating towards a documentation solution using either MkDocs or GitBook.

Expected Behavior

Users should be able to access a documentation site for the project which includes tutorials, design explanations/concept guides, and reference materials. Preferably, this documentation should be versioned, available as a downloadable artifact, source controlled, and built automatically via CI.

Current Behavior

Documentation exists in limited form as wiki articles in the project, though these are not always up to date.

Possible Solution

We could use either MkDocs or GitBook to create the documentation site. GitBook can also be used to export to PDF/epub/mobi format for artifact download.

I suggest breaking documentation into three broad parts:

  1. Tutorials: This section includes more blog-like documentation conversationally walking prospective users/developers through using and interacting with the project. This is a perfect place to give an example of how to create a plugin or how to set permissions for a role.
  2. Guides: This section is more like an informal set of white papers or design documents explaining design decisions, security concerns, and other topics not best suited to a blog format but which still need to be covered.
  3. Reference: This section is where all of the actual reference documentation (exported comment based help, class references, etc) belong.

Context

I have found that this type of documentation makes using a project much easier from both a normal user standpoint as well as from a contributing developer standpoint. It doesn't have to be written all at once but providing a base level of useful documentation and then iterating on it can help to drive adoption and help answer questions about the project more easily.

New test for volumes

I'm thinking a new test for volumes would be useful. If people agree I'll create a PR implementing it.

describe 'Storage' {
    context 'Capacity' {
        volume 'c' sizeremaining { should begreaterthan 1073741824 }
    }
    context 'Health' {
        volume 'c' operationstatus { should be 'ok' }
        volume 'c' healthstatus { should be 'healthly' }
    }
}

Unable to test PowerShell Auditing Settings via registry

When testing for PowerShell auditing settings I am unable to test for the following value as it contains a "*"

The code below is what I'm currently using which doesn't work. I have tried escaping the * by "*" but that doesn't work.

Registry 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging\ModuleNames\' "*" { Should -Be '*'}

The problem code is the expandproperty as shown below.

Get-ItemProperty HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging\ModuleNames\ | Select-Object -ExpandProperty '*'

Here is the Full Context that I'm using for the PowerShell Audit Settings.

Context -Name 'Powershell Auditing' -Fixture {
        #Turn on Module Logging: Enabled
        Registry 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging\' 'EnableModuleLogging' { Should -BeExactly 1}
        #* Module Names: *
        Registry 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging\ModuleNames\' "*" { Should -Be '*'}
        #* Turn on Powershell Script Block Logging:
        #    * Enabled
        Registry 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging\' 'EnableScriptBlockLogging' { Should -Be 1}
        #* Log script block invocation start / stop events:
        #   * Disabled
        IF (Test-Path 'HKLM\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging\EnableScriptBlockInvocationLogging') {
            #If Value doesn't exist it defaults to 0
            Registry 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging\' 'EnableScriptBlockInvocationLogging' {Should -BeLessOrEqual 0}
        }
    }

Software Product using Split-Path

SoftwareProduct has a call to Split-Path on the target.

Since the is treated as a regex this causes issues when the target is regex encoded with ''

I'm not sure of the purpose of Split-Path maybe cut/paste?

Additional Test: LocalUser

It would be useful to test for the existence/settings of local user accounts, such as Guest. We already have the ability to test for whether or not Local Groups exist.

Example

LocalUser Guest { Should Not BeNullOrEmpty } # Guest account should exist
LocalUser Guest Disabled { Should Be $True }    # And should be disabled.

http test requires Internet Explorer

Running http tests from a headless PowerShell session will fail due to a lack of Internet Explorer.

E.g. running

Describe 'Website' {
    Http "http://localhost" StatusCode { Should Be 200 }
}

From Octopus deploy as a job step results in:

NotSupportedException: The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again. 
14:52:45Info
   at <ScriptBlock>, <No file>: line 1
14:52:45Info
   at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\poshspec\Private\Invoke-PoshspecExpression.ps1: line 12

As far as I can tell none of the Properties supported by this test require complex parsing in IE, so I suggest just adding the -usebasicparsing flag.

[Bug] Package function returns more than one package causing test to fail

Hey guys,

I'd like to report an issue I ran into when using the package function. Here's my test:

Describe 'Common packages & frameworks' {
    Context 'Visual C++ Runtimes' {
        Package 'Microsoft Visual C++ 2005 Redistributable' Version { Should Be '8.0.61001' }
        Package 'Microsoft Visual C++ 2005 Redistributable (x64)' Version { Should Be '8.0.61000' }
    }
}

When we look at the packages it should be fine:

PS C:\> Get-Package | where name -like "*C++ 2005*" | select name, version

Name                                            Version  
----                                            -------  
Microsoft Visual C++ 2005 Redistributable (x64) 8.0.61000
Microsoft Visual C++ 2005 Redistributable       8.0.61001

But here's the thing - it fails with the following error:

Describing Common packages & frameworks
   Context Visual C++ Runtimes
    [-] Package property 'Version' for 'Microsoft Visual C++ 2005 Redistributable' Should Be '8.0.61001' 361ms
      String lengths are both 9. Strings differ at index 8.
      Expected: {8.0.61001}
      But was:  {8.0.61000}
      -------------------^
      1: Get-Package -Name 'Microsoft Visual C++ 2005 Redistributable' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'Version' | Should Be '8.0.61001'
      at Should<End>, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\Assertions\Should.ps1: Zeile 92
      at <ScriptBlock>, <no file>: Zeile 1
      at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\poshspec\2.1.12\Private\Invoke-PoshspecExpression.ps1: Zeile 12
      at Invoke-Test, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\It.ps1: Zeile 253
      at ItImpl, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\It.ps1: Zeile 203
      at It, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\It.ps1: Zeile 117
      at Invoke-PoshspecExpression, C:\Program Files\WindowsPowerShell\Modules\poshspec\2.1.12\Private\Invoke-PoshspecExpression.ps1: Zeile 11
      at Package, C:\Program Files\WindowsPowerShell\Modules\poshspec\2.1.12\Public\Package.ps1: Zeile 42
      at <ScriptBlock>, <no file>: Zeile 3
      at Context, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\Context.ps1: Zeile 66
      at <ScriptBlock>, <no file>: Zeile 2
      at Describe, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\Describe.ps1: Zeile 103
      at <ScriptBlock>, <no file>: Zeile 1
    [+] Package property 'Version' for 'Microsoft Visual C++ 2005 Redistributable (x64)' Should Be '8.0.61000' 359ms

The first package test fails because the Get-Package call in the package function actually returns two results and then expands it into an array and compares the first element of the array against the expected value:

PS C:\> Get-Package -Name 'Microsoft Visual C++ 2005 Redistributable' | select name,version

Name                                            Version
----                                            -------
Microsoft Visual C++ 2005 Redistributable (x64) 8.0.61000
Microsoft Visual C++ 2005 Redistributable       8.0.61001

PS C:\> Get-Package -Name 'Microsoft Visual C++ 2005 Redistributable' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'Version'
8.0.61000
8.0.61001

[Bug] Service function prompts for 'Should' input

Hey guys,

I'd like to report an issue that I ran into when using the service function of poshspec.
This is my test block:

Describe 'Appsense is installed and configured' {
    Context 'Appsense Agents are running' {
        Service 'AppSense Application Manager Agent' { Should Be Running }
        Service 'AppSense Client Communications Agent' { Should Be Running }
        Service 'AppSense EmCoreService' { Should Be Running }
        Service 'AppSense Performance Manager Agent' { Should Be Running }
        Service 'AppSense Watchdog Service' { Should Be Running }
    }
}

Now instead of executing the tests it interactively prompts me for should input, that is my prompt shows this and hangs there forever:

Describing Appsense is installed and configured
   Context Appsense Agents are running
Cmdlet Service at command pipeline position 1
Supply values for the following parameters:
Should: 

Environment:
Server 2012 R2
Pester v3.4.1
Poshspec v2.1.12
PS v5.0.10586.117

more efficient method for package checking

Here is what I typically do-

$apps64=Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
$apps32=Get-ChildItem HKLM:\SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall
$allapps=$apps64+$apps32


  Describe "ActivePerl" {
    It "ActivePerl 5.16.3 Build 1603 (64-bit) should be installed" {
      if ($allapps | gp | ? {$_.DisplayName -eq "ActivePerl 5.16.3 Build 1603 (64-bit)" }) {$activeperl=$true} else {$activeperl=$false}
      $activeperl | Should Be $true
    }
}

This avoids all issues querying win32_product.

Pester 4.x support - Allow for new syntax

Pester 4.x adds new assertion syntax and current version of poshspec uses the 3.x syntax.

Describing WhatEver
Context Services
Checking Status
[-] Error occurred in Context block 299ms
RuntimeException: The It command may only be used from a Pester test script.
at Assert-DescribeInProgress, C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.2\Functions\Describe.ps1: line 200
at ItImpl, C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.2\Functions\It.ps1: line 140
at It, C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.2\Functions\It.ps1: line 117
at Invoke-PoshspecExpression, ..\Modules\poshspec\2.1.12\Private\Invoke-PoshspecExpression.ps1:

pester/Pester#683

Purpose of scriptblock parser in Get-Poshspecparam?

I'm not really sure of the purpose of parsing $TestExpression in Get-Poshspecparam, turning around and constructing a string from it, then invoking it with Invoke-Expression. Can you explain why this is needed?

I'm getting some errors when $TestExpression starts to become a little complex. I don't have much experience dealing with language parsing in PowerShell but my gut feeling is the logic in Get-Poshspecparam may need to be considerably more complex in order to deal with the various scriptblocks thrown at it.

The code below would be an enhancement to the LocalGroup resource which will return the group name and members but fails with a parse exception.

$expression = {
    Get-CimInstance Win32_Group -Filter "Name = '$target' and LocalAccount = 'True'" |
        Select-Object Name, @{Name = 'Members'; Expression = {
            (Get-CimAssociatedInstance -InputObject $_ -ResultClassName Win32_UserAccount).Name}}
}

The members property of the object returned should be able to be inspected with the test below.

LocalGroup 'Administrators' members { should contain 'Administrator' }

This is the error I receive when testing this resource.
poshspec_error

Thoughts?
Brandon

Cannot bind argument to parameter 'Command' because it is null.

Pester 5
PoshSpec 228

When running the following test I get an error

TEST

Describe "sdsjdhs sddhsj sdhsdh shdjshd"{
File C:\workbook\Modules.txt { Should -Exist }
}

ERROR
sdsjdhs sddhsj sdhsdh shdjshd.File 'Modules.txt' Should -Exist 4ms (2ms|2ms)
ParameterBindingValidationException: Cannot bind argument to parameter 'Command' because it is null.
at , C:\Program Files\WindowsPowerShell\Modules\poshspec\2.2.8\Private\Invoke-PoshspecExpression.ps1:18
Tests completed in 40.55s
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0

Appears to be an issue with in Invoke-PoshspecExpression

$InputObject.Expression appears to be null when executed by invoke-expression
Invoke-Expression $InputObject.Expression

This seems to occur with all test function

Please Assist

Variables inside the assertion script block

Since the Assertion in the PoshSpec is placed inside a scriptblock, how does one reference variables in it e.g below works fine when the assertion is hardcoded -:

Describe test {
    Service Bits Status {Should be 'Running'}
}

But when a variable is placed in the assertion, it can't see them e.g.


Describe test {
    $DesiredStatus = 'Running'
    Service Bits Status {Should be $DesiredStatus}
}

Above gives me the error -:

Describing test
 [-] Service property 'Status' for 'Bits' Should be $DesiredStatus 123ms
   Expected: {}
   But was:  {Running}
   1: Get-Service -Name 'Bits' | Select-Object -ExpandProperty 'Status' | Should be $Desir
edStatus
   at <ScriptBlock>, <No file>: line 1
   at <ScriptBlock>, C:\Users\Deepak_Dhami\Documents\GitHub\poshspec\Private\Invoke-Poshsp
ecExpression.ps1: line 12

The idea is to use a CSV file as a template for all the service and their compliant status.
How do I do something like this with PoshSpec ?

Describe 'IT Compliance for Services' {
    $ObjectFromCSV = @([PSCustomObject]@{
        Name='Bits';
        Status='Stopped'
    })

    $ObjectFromCSV.Foreach({
        Service $PSItem.Name Status {Should be $PSItem.Status}

    })
}

CHANGELOG

This project maintains a release notes file which seems to partially be a changelog.

Expected Behavior

Users and developers should be able to review a changelog for this project which adheres to the keepachangelog format and thus meets the following criteria:

  • It’s made for humans, not machines, so legibility is crucial.
  • Easy to link to any section (hence Markdown over plain text).
  • One sub-section per version.
  • List releases in reverse-chronological order (newest on top).
  • Write all dates in YYYY-MM-DD format. (Example: 2012-06-02 for June 2nd, 2012.) It’s international, sensible, and language-independent.
  • Explicitly mention whether the project follows [Semantic Versioning][https://semver.org].
  • Each version should:
    • List its release date in the above format.
    • Group changes to describe their impact on the project, as follows:
      • Added for new features.
      • Changed for changes in existing functionality.
      • Deprecated for once-stable features removed in upcoming releases.
      • Removed for deprecated features removed in this release.
      • Fixed for any bug fixes.
      • Security to invite users to upgrade in case of vulnerabilities.

Current Behavior

The project has a ReleaseNotes file which includes some of this information already but which could be improved upon.

Possible Solution

Use the keepachangelog format and rename the release notes to CHANGELOG or keep a separate and full changelog.

Context

Keeping a standardized changelog makes it easy for people to see and understand what has happened version to version and when those versions were released.

Additional Private Function: Test-RunAsAdmin

Some configurations can only be checked by a process running as admin.

We should have a repeatable, usable private function which handles this so public functions requiring the test do not need to duplicate it.

I'll be submitting this with my existing (and refactored) PR shortly.

How to use "File Should Contain" rule

I'm using Poshspec for some minimal integration tests installing in our CI builds.
I have a new use case to check the contents of some files. But I have problems using the File xxx { Should Contain 'yyy' } rule.

My test file:

PS C:\projects> cat test.txt
hello world

My test script:

PS C:\projects> cat .\test.ps1
#Requires -Module Poshspec

Describe 'Files' {
    File C:\projects\test.txt { Should Exist }
    File C:\projects\test.txt { Should Not Exist }
    File C:\projects\test.txt { Should Contain 'hello' }
    File C:\projects\test.txt { Should Contain 'other' }
}

Running the test doesn't work. It seems that Poshspec just uses the file name and checks if the pattern is contained in the string.

PS C:\projects> .\test.ps1

Describing Files
  [+] File 'test.txt' Should Exist 20ms
  [-] File 'test.txt' Should Not Exist 16ms
    Expected path 'C:\projects\test.txt' to not exist, but it did exist.
    1: 'C:\projects\test.txt' | Should Not Exist
    at Invoke-LegacyAssertion, C:\Program Files\WindowsPowerShell\Modules\Pester\4.3.1\Functions\Assertions\Should.ps1:
line 188
    at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\Poshspec\2.2.1\Private\Invoke-PoshspecExpression.ps1: l
ine 13
  [-] File 'test.txt' Should Contain 'hello' 32ms
    Expected 'hello' to be found in collection C:\projects\test.txt, but it was not found.
    1: 'C:\projects\test.txt' | Should Contain 'hello'
    at Invoke-LegacyAssertion, C:\Program Files\WindowsPowerShell\Modules\Pester\4.3.1\Functions\Assertions\Should.ps1:
line 188
    at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\Poshspec\2.2.1\Private\Invoke-PoshspecExpression.ps1: l
ine 13
  [-] File 'test.txt' Should Contain 'other' 19ms
    Expected 'other' to be found in collection C:\projects\test.txt, but it was not found.
    1: 'C:\projects\test.txt' | Should Contain 'other'
    at Invoke-LegacyAssertion, C:\Program Files\WindowsPowerShell\Modules\Pester\4.3.1\Functions\Assertions\Should.ps1:
line 188
    at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\Poshspec\2.2.1\Private\Invoke-PoshspecExpression.ps1: l
ine 13

Another question BTW: How can I get an exit code != 0 if one of the tests fails, so I can abort my CI build?

[Bug] Package function breaks when package name contains single quotes

Hey guys,

I'd like to report the following error:

Describe 'Appsense is installed and configured' {
    Context 'Appsense Agents are installed' {
        Package "AppSense Application Manager Agent 8.9 SP1" Version { Should Be '8.9.451.0' }
        Package "AppSense Application Manager Configuration 'xzy-Desktop' 8.9.18.0" Version { Should Be '8.9.18.0' }
    }
}

The first package function call works while the second doesn't recognize the name parameter properly:

[-] Package property 'Version' for 'AppSense Application Manager Configuration 'xyz-Desktop' 8.3.0.0' Should Be '8.3.0.0' 67ms
  ParameterBindingException: A positional parameter cannot be found that accepts argument "xyz-Desktop 8.3.0.0".
  at <ScriptBlock>, <No file>: Line 1
  at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\poshspec\2.1.12\Private\Invoke-PoshspecExpression.ps1: Line 12
  at Invoke-Test, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\It.ps1: Line 253
  at ItImpl, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\It.ps1: Line 203
  at It, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\It.ps1: Line 117
  at Invoke-PoshspecExpression, C:\Program Files\WindowsPowerShell\Modules\poshspec\2.1.12\Private\Invoke-PoshspecExpression.ps1: Line 11
  at Package, C:\Program Files\WindowsPowerShell\Modules\poshspec\2.1.12\Public\Package.ps1: Line 42
  at <ScriptBlock>, \\fileserver\citrix$\citrix-scripts\OperationalValidation\WTS\Tests\Diagnostics\Simple\02-Citrix\Appsense.Tests.ps1: Line 9
  at Context, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\Context.ps1: Line 66
  at <ScriptBlock>, \\fileserver\citrix$\citrix-scripts\OperationalValidation\WTS\Tests\Diagnostics\Simple\02-Citrix\Appsense.Tests.ps1: Line 2
  at Describe, C:\Program Files\WindowsPowerShell\Modules\pester\3.4.1\Functions\Describe.ps1: Line 103
  at <ScriptBlock>, \\fileserver\citrix$\citrix-scripts\OperationalValidation\WTS\Tests\Diagnostics\Simple\02-Citrix\Appsense.Tests.ps1: Line 1

The package can be found with the Get-Package cmdlet:

PS C:\> Get-Package -Name "AppSense Application Manager Configuration 'xyz-Desktop' 8.9.18.0"

Name                           Version          Source                         Summary                                                                                                                      
----                           -------          ------                         -------                                                                                                                      
AppSense Application Manage... 8.9.18.0         C:\Program Files\AppSense\M...                                                                                                                     

I guess this is an internal quotation handling problem.

[Feature Request] Ability to run remote tests and grouping of tests

I was working on a similar project as poshspec internally but did not make much progress. Now that I see this project, I don't think I need to invest in creating a new framework altogether. However, here is something I was trying to achieve in my code.

node host1, host2, host3 {
    test1 param1 { should be value1 }
    test2 param2 { should be value2 }
    test3 param3 { should be value3 }
}

The Node block let's me specify a set of remote systems where my the tests must be executed. I was targeting PS Remoting to help me execute the test remotely and assert the result locally. For this, I started modifying Remotely and added support for specifying node names as a part of DSL and supply a credential hash when needed. Here is how the tests are written today.

$CredHash = @{
    'VM1' = (Get-Credential)
}

Describe "Add-Numbers" {
    It "adds positive numbers on two remote systems" {
        Remotely 'VM1','VM2' { 2 + 3 } | Should Be 5
    }

    It "gets verbose message" {
        $sum = Remotely 'VM1','VM2' { Write-Verbose -Verbose "Test Message" }
        $sum.GetVerbose() | Should Be "Test Message"
    }

    It "can pass parameters to remote block with different credentials" {
        $num = 10
        $process = Remotely 'VM1' { param($number) $number + 1 } -ArgumentList $num -CredentialHash $CredHash
        $process | Should Be 11
    }
}

However, this is still not sufficient as I don't have the remote system context in the assert result from Should. Here is where I was thinking that I will put a higher level wrapper on this and create something like what I proposed in my first example.

At the end of all this, there are two goals:

  • Create the ability to execute tests remotely

  • In the test output, I should be able to group the test results based on where they ran

    I am continuing to work on this model and I will try to add the feature to poshspec. I am posting this request here to see if any others have any ideas on how to make this better.

Http tests don't work against https URLs with invalid certs

When attempting to do an http check against a URL with an invalid SSL certificate (which is common in dev/qa environments), the check fails with:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.

I believe the solution is to add:

# Added for dealing with invalid certificates.
add-type -TypeDefinition  @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }

"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

to the http.ps1 file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.