-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Improve test coverage for CDXML cmdlet infrastructure #4537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
daxian-dbw
merged 4 commits into
PowerShell:master
from
JamesWTruher:jameswtruher/cdxmltest001
Aug 16, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f624d07
Simple CDXML tests for Get and Remove
JamesWTruher 6f09fe6
Additional tests - All CRUD operations are now covered
JamesWTruher 5279b03
Add tests for exceptions and support whatif
JamesWTruher 0af83e2
Add hardening to test code to handle missing or misbehaving MOFCOMP
JamesWTruher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,273 @@ | ||
| $script:CimClassName = "PSCore_CimTest1" | ||
| $script:CimNamespace = "root/default" | ||
| $script:moduleDir = Join-Path -Path $PSScriptRoot -ChildPath assets -AdditionalChildPath CimTest | ||
| $script:deleteMof = Join-Path -Path $moduleDir -ChildPath DeleteCimTest.mof | ||
| $script:createMof = Join-Path -Path $moduleDir -ChildPath CreateCimTest.mof | ||
|
|
||
| $CimCmdletArgs = @{ | ||
| Namespace = ${script:CimNamespace} | ||
| ClassName = ${script:CimClassName} | ||
| ErrorAction = "SilentlyContinue" | ||
| } | ||
|
|
||
| $script:ItSkipOrPending = @{} | ||
|
|
||
| function Test-CimTestClass { | ||
| $null -eq (Get-CimClass @CimCmdletArgs) | ||
| } | ||
|
|
||
| function Test-CimTestInstance { | ||
| $null -eq (Get-CimInstance @CimCmdletArgs) | ||
| } | ||
|
|
||
| Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { | ||
| BeforeAll { | ||
| $skipNotWindows = ! $IsWindows | ||
| if ( $skipNotWindows ) { | ||
| $script:ItSkipOrPending = @{ Skip = $true } | ||
| return | ||
| } | ||
|
|
||
| # if MofComp does not exist, we shouldn't bother moving forward | ||
| # there is a possibility that we could be on Windows, but MofComp | ||
| # isn't present, in any event we will mark these tests as skipped | ||
| # since the environment won't support loading the test classes | ||
| if ( (Get-Command -ea SilentlyContinue Mofcomp.exe) -eq $null ) { | ||
| $script:ItSkipOrPending = @{ Skip = $true } | ||
| return | ||
| } | ||
|
|
||
| # start from a clean slate, remove the instances and the | ||
| # classes if they exist | ||
| if ( Test-CimTestClass ) { | ||
| if ( Test-CimTestInstance ) { | ||
| Get-CimInstance @CimCmdletArgs | Remove-CimInstance | ||
| } | ||
| # if there's a failure with mofcomp then we will have trouble | ||
| # executing the tests. Keep track of the exit code | ||
| $result = MofComp.exe $deleteMof | ||
| $script:MofCompReturnCode = $LASTEXITCODE | ||
| if ( $script:MofCompReturnCode -ne 0 ) { | ||
| return | ||
| } | ||
| } | ||
|
|
||
| # create the class and instances | ||
| # and track the exitcode for the compilation of the mof file | ||
| # if there's a problem, there's no reason to keep going | ||
| $result = MofComp.exe ${script:createMof} | ||
| $script:MofCompReturnCode = $LASTEXITCODE | ||
| if ( $script:MofCompReturnCode -ne 0 ) { | ||
| return | ||
| } | ||
|
|
||
| # now load the cdxml module | ||
| if ( Get-Module CimTest ) { | ||
| Remove-Module -force CimTest | ||
| } | ||
| Import-Module -force ${script:ModuleDir} | ||
| } | ||
|
|
||
| AfterAll { | ||
| if ( $skipNotWindows ) { | ||
| return | ||
| } | ||
| if ( get-module CimTest ) { | ||
| Remove-Module CimTest -Force | ||
| } | ||
| $null = MofComp.exe $deleteMof | ||
| if ( $LASTEXITCODE -ne 0 ) { | ||
| Write-Warning "Could not remove PSCore_CimTest class" | ||
| } | ||
| } | ||
|
|
||
| BeforeEach { | ||
| If ( $script:MofCompReturnCode -ne 0 ) { | ||
| throw "MofComp.exe failed with exit code $MofCompReturnCode" | ||
| } | ||
| } | ||
|
|
||
| Context "Module level tests" { | ||
| It "The CimTest module should have been loaded" @ItSkipOrPending { | ||
| $result = Get-Module CimTest | ||
| $result.ModuleBase | should be ${script:ModuleDir} | ||
| } | ||
|
|
||
| It "The CimTest module should have the proper cmdlets" @ItSkipOrPending { | ||
| $result = Get-Command -Module CimTest | ||
| $result.Count | Should Be 4 | ||
| ($result.Name | sort-object ) -join "," | Should Be "Get-CimTest,New-CimTest,Remove-CimTest,Set-CimTest" | ||
| } | ||
| } | ||
|
|
||
| Context "Get-CimTest cmdlet" { | ||
| It "The Get-CimTest cmdlet should return 4 objects" @ItSkipOrPending { | ||
| $result = Get-CimTest | ||
| $result.Count | should be 4 | ||
| ($result.id |sort-object) -join "," | should be "1,2,3,4" | ||
| } | ||
|
|
||
| It "The Get-CimTest cmdlet should retrieve an object via id" @ItSkipOrPending { | ||
| $result = Get-CimTest -id 1 | ||
| @($result).Count | should be 1 | ||
| $result.field1 | Should be "instance 1" | ||
| } | ||
|
|
||
| It "The Get-CimTest cmdlet should retrieve an object by piped id" @ItSkipOrPending { | ||
| $result = 1,2,4 | foreach-object { [pscustomobject]@{ id = $_ } } | Get-CimTest | ||
| @($result).Count | should be 3 | ||
| ( $result.id | sort-object ) -join "," | Should be "1,2,4" | ||
| } | ||
|
|
||
| It "The Get-CimTest cmdlet should return the proper error if the instance does not exist" @ItSkipOrPending { | ||
| { Get-CimTest -ea stop -id "ThisIdDoesNotExist" } | ShouldBeErrorId "CmdletizationQuery_NotFound_Id,Get-CimTest" | ||
| } | ||
|
|
||
| It "The Get-CimTest cmdlet should work as a job" @ItSkipOrPending { | ||
| try { | ||
| $job = Get-CimTest -AsJob | ||
| $result = $null | ||
| # wait up to 10 seconds, then the test will fail | ||
| # we need to wait long enough, but not too long | ||
| # the time can be adjusted | ||
| $null = Wait-Job -Job $job -timeout 10 | ||
| $result = $job | Receive-Job | ||
| $result.Count | should be 4 | ||
| ( $result.id | sort-object ) -join "," | Should be "1,2,3,4" | ||
| } | ||
| finally { | ||
| if ( $job ) { | ||
| $job | Remove-Job -force | ||
| } | ||
| } | ||
| } | ||
|
|
||
| It "Should be possible to invoke a method on an object returned by Get-CimTest" @ItSkipOrPending { | ||
| $result = Get-CimTest | Select-Object -first 1 | ||
| $result.GetCimSessionInstanceId() | Should BeOfType [guid] | ||
| } | ||
| } | ||
|
|
||
| Context "Remove-CimTest cmdlet" { | ||
| BeforeEach { | ||
| Get-CimTest | Remove-CimTest | ||
| 1..4 | Foreach-Object { New-CimInstance -namespace root/default -class PSCore_Test1 -property @{ | ||
| id = "$_" | ||
| field1 = "field $_" | ||
| field2 = 10 * $_ | ||
| } | ||
| } | ||
| } | ||
|
|
||
| It "The Remote-CimTest cmdlet should remove objects by id" @ItSkipOrPending { | ||
| Remove-CimTest -id 1 | ||
| $result = Get-CimTest | ||
| $result.Count | should be 3 | ||
| ($result.id |sort-object) -join "," | should be "2,3,4" | ||
| } | ||
|
|
||
| It "The Remove-CimTest cmdlet should remove piped objects" @ItSkipOrPending { | ||
| Get-CimTest -id 2 | Remove-CimTest | ||
| $result = Get-CimTest | ||
| @($result).Count | should be 3 | ||
| ($result.id |sort-object) -join "," | should be "1,3,4" | ||
| } | ||
|
|
||
| It "The Remove-CimTest cmdlet should work as a job" @ItSkipOrPending { | ||
| try { | ||
| $job = Get-CimTest -id 3 | Remove-CimTest -asjob | ||
| $result = $null | ||
| # wait up to 10 seconds, then the test will fail | ||
| # we need to wait long enough, but not too long | ||
| # the time can be adjusted | ||
| $null = Wait-Job -Job $job -Timeout 10 | ||
| $result = Get-CimTest | ||
| @($result).Count | should be 3 | ||
| ($result.id |sort-object) -join "," | should be "1,2,4" | ||
| } | ||
| finally { | ||
| if ( $job ) { | ||
| $job | Remove-Job -force | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Context "New-CimTest operations" { | ||
| It "Should create a new instance" @ItSkipOrPending { | ||
| $instanceArgs = @{ | ||
| id = "telephone" | ||
| field1 = "television" | ||
| field2 = 0 | ||
| } | ||
| New-CimTest @instanceArgs | ||
| $result = Get-CimInstance -namespace root/default -class PSCore_Test1 | Where-Object {$_.id -eq "telephone"} | ||
| $result.field2 | should be 0 | ||
| $result.field1 | Should be $instanceArgs.field1 | ||
| } | ||
|
|
||
| It "Should return the proper error if called with an improper value" @ItSkipOrPending { | ||
| $instanceArgs = @{ | ||
| Id = "error validation" | ||
| field1 = "a string" | ||
| field2 = "a bad string" # this needs to be an int | ||
| } | ||
| { New-CimTest @instanceArgs } | ShouldBeErrorId "ParameterArgumentTransformationError,New-CimTest" | ||
| # just make sure that it wasn't added | ||
| Get-CimTest -id $instanceArgs.Id -ea SilentlyContinue | Should BeNullOrEmpty | ||
| } | ||
|
|
||
| It "Should support -whatif" @ItSkipOrPending { | ||
| $instanceArgs = @{ | ||
| Id = "1000" | ||
| field1 = "a string" | ||
| field2 = 111 | ||
| Whatif = $true | ||
| } | ||
| New-CimTest @instanceArgs | ||
| Get-CimTest -id $instanceArgs.Id -ea SilentlyContinue | Should BeNullOrEmpty | ||
| } | ||
| } | ||
|
|
||
| Context "Set-CimTest operations" { | ||
| It "Should set properties on an instance" @ItSkipOrPending { | ||
| $instanceArgs = @{ | ||
| id = "updateTest1" | ||
| field1 = "updatevalue" | ||
| field2 = 100 | ||
| } | ||
| $newValues = @{ | ||
| id = "updateTest1" | ||
| field2 = 22 | ||
| field1 = "newvalue" | ||
| } | ||
| New-CimTest @instanceArgs | ||
| $result = Get-CimTest -id $instanceArgs.id | ||
| $result.field2 | should be $instanceArgs.field2 | ||
| $result.field1 | Should be $instanceArgs.field1 | ||
| Set-CimTest @newValues | ||
| $result = Get-CimTest -id $newValues.id | ||
| $result.field1 | Should be $newValues.field1 | ||
| $result.field2 | should be $newValues.field2 | ||
| } | ||
|
|
||
| It "Should set properties on an instance via pipeline" @ItSkipOrPending { | ||
| $instanceArgs = @{ | ||
| id = "updateTest2" | ||
| field1 = "updatevalue" | ||
| field2 = 100 | ||
| } | ||
| New-CimTest @instanceArgs | ||
| $result = Get-CimTest -id $instanceArgs.id | ||
| $result.field2 | should be $instanceArgs.field2 | ||
| $result.field1 | Should be $instanceArgs.field1 | ||
| $result.field1 = "yet another value" | ||
| $result.field2 = 33 | ||
| $result | Set-CimTest | ||
| $result = Get-CimTest -id $instanceArgs.id | ||
| $result.field1 | Should be "yet another value" | ||
| $result.field2 | should be 33 | ||
| } | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra line. |
||
| } | ||
14 changes: 14 additions & 0 deletions
14
test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| @{ | ||
| GUID = '41486F7D-842F-40F1-ACE4-8405F9C2ED9B' | ||
| Author="Microsoft Corporation" | ||
| CompanyName="Microsoft Corporation" | ||
| Copyright="� Microsoft Corporation. All rights reserved." | ||
| ModuleVersion = '2.0.0.0' | ||
| PowerShellVersion = '3.0' | ||
| FormatsToProcess = @() | ||
| TypesToProcess = @() | ||
| NestedModules = @( 'CimTest.cdxml') | ||
| AliasesToExport = @() | ||
| CmdletsToExport = @() | ||
| FunctionsToExport = @( 'Get-CimTest', 'Remove-CimTest', 'New-CimTest', 'Set-CimTest' ) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use a direct $IsWindows test versus a variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it was clearer here
-skip(I've seen other tests do exactly the opposite of what was wanted)