File size: 11,192 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$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 -ErrorAction 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
$testMof = Get-Content -Path ${script:createmof} -Raw
$currentTimeZone = [System.TimeZoneInfo]::Local
# this date is simply the same one used in the test mof
# the minutes must be padded to 3 digits proceeded by a '+' or '-'
# when east of UTC 0 we must add a '+'
$offsetMinutes = ($currentTimeZone.GetUtcOffset([datetime]::new(2008, 01, 01, 0, 0, 0))).TotalMinutes
$UTCOffset = "{0:+000;-000}" -f $offsetMinutes
$testMof = $testMof.Replace("<UTCOffSet>", $UTCOffset)
Set-Content -Path $testDrive\testmof.mof -Value $testMof
$result = MofComp.exe $testDrive\testmof.mof
$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 retrieve an object by datetime" @ItSkipOrPending {
$result = Get-CimTest -DateTime ([datetime]::new(2008,01,01,0,0,0))
@($result).Count | Should -Be 1
$result.field1 | Should -Be "instance 1"
}
It "The Get-CimTest cmdlet should return the proper error if the instance does not exist" @ItSkipOrPending {
{ Get-CimTest -ErrorAction Stop -id "ThisIdDoesNotExist" } | Should -Throw -ErrorId "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 } | Should -Throw -ErrorId "ParameterArgumentTransformationError,New-CimTest"
# just make sure that it wasn't added
Get-CimTest -id $instanceArgs.Id -ErrorAction 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 -ErrorAction 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
}
}
}
|