Comments (12)
@michaeltlombardi I've just had a look at the CimObject function and the problem is that it's currently lacking a filter property. It only works with CIM classes that return singletons, e.g.:
PS C:\Users\User> describe 'example' { CimObject Win32_Computersystem Manufacturer { Should Be 'LENOVO' }}
Describing example
[+] CimObject property 'Manufacturer' for 'Win32_Computersystem' Should Be 'LENOVO' 106ms
Whereas classes that return multiple instances are going to break the CimObject function, e.g. your example:
PS C:\Users\User> describe 'example' { CimObject Win32_Service Status { Should Be 'OK' }}
Describing example
[-] CimObject property 'Status' for 'Win32_Service' Should Be 'OK' 6.25s
Expected string length 2 but was 7. Strings differ at index 0.
Expected: {OK}
But was: {UNKNOWN}
-----------^
from poshspec.
Actually, thinking about it now, you should use the CimObject test, not the hacky thing I suggested above; Then your test would look like:
Describe 'Appsense is installed and configured' {
Context 'Appsense Agents are running' {
Service 'AppSense Application Manager Agent' status { Should Be 'Running' }
CimObject Win32_Service Status { Should Be 'OK' }
}
}
from poshspec.
This is because the Service
function expects an additional parameter specifying the property
to test against.
Something like this will work:
Describe 'Appsense is installed and configured' {
Context 'Appsense Agents are running' {
Service 'AppSense Application Manager Agent' status { Should Be Running }
}
}
That leads to the question of whether the function should handle this scenario and default to testing the status
of the service is no property is specified. What do you think @cdhunt? This would be a simple change to the function.
from poshspec.
I believe it makes sense to be able to check a few more properties such as path, startup type and recovery options.
The wiki and the function's help omit the status parameter name which is why I ran into this issue in the first place (I haven't had the time to check the source code myself).
from poshspec.
@megamorf I believe you can test for other properties that you can retrieve with Get-Service
(Display Name, StartType, CanStop, etc) - however, some, like Path
are returned by Get-CimInstance -ClassName Win32_Service
from poshspec.
Yeah, but wouldn't it make sense to construct an object under the hood that has more details than the regular ServiceController object returned by Get-Service? Essentially the user should be able to test against as many settings as possible.
from poshspec.
The only problem with making that change now is that it could, potentially, break existing tests:
PS C:\> get-service | select -first 1 | fl *
Name : AdobeARMservice
RequiredServices : {}
CanPauseAndContinue : False
CanShutdown : False
CanStop : True
DisplayName : Adobe Acrobat Update Service
DependentServices : {}
MachineName : .
ServiceName : AdobeARMservice
ServicesDependedOn : {}
ServiceHandle : SafeServiceHandle
Status : Running
ServiceType : Win32OwnProcess
StartType : Automatic
Site :
Container :
PS C:\> get-ciminstance -ClassName win32_service | select -first 1 | fl *
Name : AdobeARMservice
Status : OK
ExitCode : 0
DesktopInteract : False
ErrorControl : Ignore
PathName : "C:\Program Files (x86)\Common Files\Adobe\ARM\1.0\armsvc.exe"
ServiceType : Own Process
StartMode : Auto
Caption : Adobe Acrobat Update Service
Description : Adobe Acrobat Updater keeps your Adobe software up to date.
InstallDate :
CreationClassName : Win32_Service
Started : True
SystemCreationClassName : Win32_ComputerSystem
SystemName : MMXL2410KX5
AcceptPause : False
AcceptStop : True
DisplayName : Adobe Acrobat Update Service
ServiceSpecificExitCode : 0
StartName : LocalSystem
State : Running
TagId : 0
CheckPoint : 0
DelayedAutoStart : False
ProcessId : 2388
WaitHint : 0
PSComputerName :
CimClass : root/cimv2:Win32_Service
CimInstanceProperties : {Caption, Description, InstallDate, Name...}
CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties
Notice that the status property still exists but is different, CanStop
becomes AcceptStop
, etc.
Since most of the already-written tests will be checking for status, this could potentially break them pretty badly.
An alternative would be to add a Qualifier
parameter and let people choose between service objects and the CIM instance as below, where the default option is to return the results of Get-Service so as not to break existing tests.
Describe 'Appsense is installed and configured' {
Context 'Appsense Agents are running' {
Service 'AppSense Application Manager Agent' status { Should Be 'Running' }
Service 'AppSense Application Manager Agent' status AsService { Should Be 'Running' }
Service 'AppSense Application Manager Agent' status AsCim { Should Be 'OK' }
}
}
from poshspec.
That's probably something that needs fixing then. Looks like a Qualifier
would need to be added to the CimObject test.
from poshspec.
@michaeltlombardi @megamorf
I was going through this issue and thought this might interest you as well.
Have sort of incomplete implementation of PoshSpec in my fork, where it accepts either a string or a hashtable as the input to the Target parameter.
This allows me to solve the above issue by using below :
Describe 'CIMObjects' {
CimObject Win32_OperatingSystem SystemDirectory { Should Be C:\WINDOWS\system32 }
CimObject @{ClassName='Win32_service';Filter="Name='WinRm'"} Status {Should be 'Ok'}
}
You can take a look in my branch and let me know how this looks.
from poshspec.
That's a great idea, @DexterPOSH.
from poshspec.
@cdhunt Cool, I will work more on updating some of the public types and create a pull request.
from poshspec.
Getting this back on topic. The behavior @megamorf described is not the expected behavior. The Parameter Sets are not binding as expected. It should default to checking the Status property if nothing is provided.
from poshspec.
Related Issues (20)
- Unable to call Custom functions in poshspec.
- Http tests don't work against https URLs with invalid certs HOT 4
- Additional Test: AuditPolicy
- Additional Test: LocalUser
- Additional Test: UserRightsAssignment
- Additional Private Function: Test-RunAsAdmin
- Service Function: Get-Service vs Get-WmiObject\Get-CimInstance HOT 1
- Pester 4.x support - Allow for new syntax HOT 2
- Documentation Transition HOT 3
- CHANGELOG HOT 1
- Poshspec Pester Tests broken from PR #44 HOT 1
- more efficient method for package checking HOT 1
- Path prompt with SoftwareProduct HOT 9
- How to use "File Should Contain" rule HOT 4
- PowerShell Gallery shows Pester functions in this module HOT 1
- Update Wiki HOT 2
- Unable to test PowerShell Auditing Settings via registry HOT 2
- Cannot bind argument to parameter 'Command' because it is null. HOT 1
- Compatibility with latest versions of pester
- Issues
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from poshspec.