From f624d07390486b83ed5e78a2812162af8cbe1d0f Mon Sep 17 00:00:00 2001 From: Jim Truher Date: Fri, 4 Aug 2017 15:50:57 -0700 Subject: [PATCH 1/4] Simple CDXML tests for Get and Remove Includes MOF files to create and delete class and instances which are used by the tests --- test/powershell/engine/Cdxml/Cdxml.Tests.ps1 | 111 ++++++++++++++++++ .../Cdxml/assets/CimTest/CdxmlTest.psd1 | 17 +++ .../engine/Cdxml/assets/CimTest/CimTest.cdxml | 47 ++++++++ .../Cdxml/assets/CimTest/CreateCimTest.mof | 33 ++++++ .../Cdxml/assets/CimTest/DeleteCimTest.mof | 2 + 5 files changed, 210 insertions(+) create mode 100644 test/powershell/engine/Cdxml/Cdxml.Tests.ps1 create mode 100644 test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 create mode 100644 test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml create mode 100644 test/powershell/engine/Cdxml/assets/CimTest/CreateCimTest.mof create mode 100644 test/powershell/engine/Cdxml/assets/CimTest/DeleteCimTest.mof diff --git a/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 b/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 new file mode 100644 index 00000000000..1eb1f48a1b1 --- /dev/null +++ b/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 @@ -0,0 +1,111 @@ +$script:CimClassName = "PSCore_CimTest" +$script:CimNamespace = "root/default" +$script:moduleDir = join-path $PSScriptRoot assets CimTest +$script:deleteMof = join-path $moduleDir DeleteCimTest.mof +$script:createMof = join-path $moduleDir CreateCimTest.mof + +$CimCmdletArgs = @{ + Namespace = ${script:CimNamespace} + ClassName = ${script:CimClassName} + ErrorAction = "SilentlyContinue" + } + +$script:ItSkipOrPending = @{} + +function Test-CimTestClass { + (Get-CimClass @CimCmdletArgs) -ne $null +} +function Test-CimTestInstance { + (Get-CimInstance @CimCmdletArgs) -ne $null +} +Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { + BeforeAll { + $skipNotWindows = ! $IsWindows + if ( $skipNotWindows ) { + $script:ItSkipOrPending = @{ Skip = $true } + return + } + + # start from a clean slate, remove the instances and the + # class if they exist + if ( Test-CimTestClass ) { + if ( Test-CimTestInstance ) { + Get-CimInstance @CimCmdletArgs | Remove-CimInstance + } + $result = MofComp.exe $deleteMof + if ( $LASTEXITCODE -ne 0 ) { + $script:ItSkipOrPending = @{ Pending = $true } + return + } + } + + # create the class and instances + $result = MofComp.exe ${script:createMof} + + if ( $LASTEXITCODE -ne 0 ) { + $script:ItSkipOrPending = @{ Pending = $true } + return + } + # now load the cdxml module + if ( Get-Module CimTest ) { + Remove-Module -force CimTest + } + Import-Module -force ${script:ModuleDir} + } + AfterAll { + if ( $skipNotWindows ) { + return + } + Remove-Module CimTest -Force + + $result = MofComp.exe $deleteMof + if ( $LASTEXITCODE -ne 0 ) { + Write-Warning "Could not remove PSCore_CimTest class" + } + } + + 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 2 + ($result.Name | sort-object ) -join "," | Should Be "Get-CimTest,Remove-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" + } + } + + Context "Remove-CimTest cmdlet" { + 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 2 + ($result.id |sort-object) -join "," | should be "3,4" + } + } + +} diff --git a/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 b/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 new file mode 100644 index 00000000000..1892dd59a9e --- /dev/null +++ b/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 @@ -0,0 +1,17 @@ +@{ + 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' ) + HelpInfoUri = "https://go.microsoft.com/fwlink/?linkid=390832" +} + + diff --git a/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml b/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml new file mode 100644 index 00000000000..9c290405ab2 --- /dev/null +++ b/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml @@ -0,0 +1,47 @@ + + + 1.0.0.0 + CimTest + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/powershell/engine/Cdxml/assets/CimTest/CreateCimTest.mof b/test/powershell/engine/Cdxml/assets/CimTest/CreateCimTest.mof new file mode 100644 index 00000000000..2bbf364dd1b --- /dev/null +++ b/test/powershell/engine/Cdxml/assets/CimTest/CreateCimTest.mof @@ -0,0 +1,33 @@ +class PSCore_CimTest +{ + [key] string id; + string field1; + sint32 field2; +}; + +instance of PSCore_CimTest +{ + id = "1"; + field1 = "instance 1"; + field2 = -1; +}; + +instance of PSCore_CimTest +{ + id = "2"; + field1 = "instance 2"; + field2 = -2; +}; + +instance of PSCore_CimTest +{ + id = "3"; + field1 = "instance 3"; + field2 = -3; +}; +instance of PSCore_CimTest +{ + id = "4"; + field1 = "instance 4"; + field2 = -4; +}; diff --git a/test/powershell/engine/Cdxml/assets/CimTest/DeleteCimTest.mof b/test/powershell/engine/Cdxml/assets/CimTest/DeleteCimTest.mof new file mode 100644 index 00000000000..4f9b9e8aeb4 --- /dev/null +++ b/test/powershell/engine/Cdxml/assets/CimTest/DeleteCimTest.mof @@ -0,0 +1,2 @@ +#pragma namespace ("\\\\.\\root\\default") +#pragma deleteclass("PSCore_CimTest", NOFAIL) From 6f09fe67b6f5d3a665dc05309ba3bd57d9986728 Mon Sep 17 00:00:00 2001 From: Jim Truher Date: Tue, 8 Aug 2017 17:13:44 -0700 Subject: [PATCH 2/4] Additional tests - All CRUD operations are now covered updated create mof file to support associations as well as complex objects updated delete mof file to remove all classes added an enum to cdxml (not yet used) updated psd1 to export new cmdlets for set and new --- test/powershell/engine/Cdxml/Cdxml.Tests.ps1 | 132 +++++++++++++++++- .../Cdxml/assets/CimTest/CdxmlTest.psd1 | 2 +- .../engine/Cdxml/assets/CimTest/CimTest.cdxml | 74 +++++++++- .../Cdxml/assets/CimTest/CreateCimTest.mof | 41 +++++- .../Cdxml/assets/CimTest/DeleteCimTest.mof | 5 +- 5 files changed, 240 insertions(+), 14 deletions(-) diff --git a/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 b/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 index 1eb1f48a1b1..12abe205974 100644 --- a/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 +++ b/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 @@ -1,4 +1,4 @@ -$script:CimClassName = "PSCore_CimTest" +$script:CimClassName = "PSCore_CimTest1" $script:CimNamespace = "root/default" $script:moduleDir = join-path $PSScriptRoot assets CimTest $script:deleteMof = join-path $moduleDir DeleteCimTest.mof @@ -15,9 +15,11 @@ $script:ItSkipOrPending = @{} function Test-CimTestClass { (Get-CimClass @CimCmdletArgs) -ne $null } + function Test-CimTestInstance { (Get-CimInstance @CimCmdletArgs) -ne $null } + Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { BeforeAll { $skipNotWindows = ! $IsWindows @@ -56,7 +58,9 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { if ( $skipNotWindows ) { return } - Remove-Module CimTest -Force + if ( get-module CimTest ) { + Remove-Module CimTest -Force + } $result = MofComp.exe $deleteMof if ( $LASTEXITCODE -ne 0 ) { @@ -71,8 +75,8 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { It "The CimTest module should have the proper cmdlets" @ItSkipOrPending { $result = Get-Command -Module CimTest - $result.Count | Should Be 2 - ($result.Name | sort-object ) -join "," | Should Be "Get-CimTest,Remove-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" { @@ -91,9 +95,43 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { @($result).Count | should be 3 ( $result.id | sort-object ) -join "," | Should be "1,2,4" } + It "The Get-CimTest cmdlet should work as a job" @ItSkipOrPending { + try { + $job = Get-CimTest -AsJob + $result = $null + $i = 0 + # 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 + do { + if ( $job.State -eq "Completed" ) + { + $result = $job | Receive-Job + break + } + start-sleep 1 + } while ( $i++ -lt 10 ) + $result.Count | should be 4 + ( $result.id | sort-object ) -join "," | Should be "1,2,3,4" + } + finally { + if ( $job ) { + $job | Remove-Job -force + } + } + } } Context "Remove-CimTest cmdlet" { + BeforeEach { + Get-CimTest | Remove-CimTest + 1..4 | %{ 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 @@ -103,9 +141,91 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { It "The Remove-CimTest cmdlet should remove piped objects" @ItSkipOrPending { Get-CimTest -id 2 | Remove-CimTest $result = Get-CimTest - @($result).Count | should be 2 - ($result.id |sort-object) -join "," | should be "3,4" + @($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 + $i = 0 + # 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 + do { + if ( $job.State -eq "Completed" ) + { + break + } + start-sleep 1 + } while ( $i++ -lt 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 | ?{$_.id -eq "telephone"} + $result.field2 | should be 0 + $result.field1 | Should be $instanceArgs.field1 } } + 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 + } + + } + } diff --git a/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 b/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 index 1892dd59a9e..8f77e699e42 100644 --- a/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 +++ b/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 @@ -10,7 +10,7 @@ NestedModules = @( 'CimTest.cdxml') AliasesToExport = @() CmdletsToExport = @() - FunctionsToExport = @( 'Get-CimTest', 'Remove-CimTest' ) + FunctionsToExport = @( 'Get-CimTest', 'Remove-CimTest', 'New-CimTest', 'Set-CimTest' ) HelpInfoUri = "https://go.microsoft.com/fwlink/?linkid=390832" } diff --git a/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml b/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml index 9c290405ab2..05f7ff7c68c 100644 --- a/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml +++ b/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml @@ -1,5 +1,5 @@ - + 1.0.0.0 CimTest @@ -42,6 +42,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/powershell/engine/Cdxml/assets/CimTest/CreateCimTest.mof b/test/powershell/engine/Cdxml/assets/CimTest/CreateCimTest.mof index 2bbf364dd1b..570c52efee8 100644 --- a/test/powershell/engine/Cdxml/assets/CimTest/CreateCimTest.mof +++ b/test/powershell/engine/Cdxml/assets/CimTest/CreateCimTest.mof @@ -1,31 +1,62 @@ -class PSCore_CimTest +class PSCore_subclass +{ + [key] string id; + string subfield1; + sint32 subfield2; +}; + +instance of PSCore_subclass +{ + id = "s1"; + subfield1 = "sub thing"; + subfield2 = 100; +}; + +[HasClassRefs, Association] +class PSCore_Association +{ + [key, classref{ "PSCore_Test1", "PSCore_Test2" }] + object ref ep1; + [key] object ref ep2; +}; + +class PSCore_Test2 +{ + [key] string id; + string field1; + sint32 field2; +}; + +class PSCore_Test1 { [key] string id; string field1; sint32 field2; + PSCore_subclass subclass; }; -instance of PSCore_CimTest +instance of PSCore_Test1 { id = "1"; field1 = "instance 1"; field2 = -1; + subclass = instance of PSCore_subclass{Id="10";subfield1="yup";subfield2=200;}; }; -instance of PSCore_CimTest +instance of PSCore_Test1 { id = "2"; field1 = "instance 2"; field2 = -2; }; -instance of PSCore_CimTest +instance of PSCore_Test1 { id = "3"; field1 = "instance 3"; field2 = -3; }; -instance of PSCore_CimTest +instance of PSCore_Test1 { id = "4"; field1 = "instance 4"; diff --git a/test/powershell/engine/Cdxml/assets/CimTest/DeleteCimTest.mof b/test/powershell/engine/Cdxml/assets/CimTest/DeleteCimTest.mof index 4f9b9e8aeb4..cd84ab177fe 100644 --- a/test/powershell/engine/Cdxml/assets/CimTest/DeleteCimTest.mof +++ b/test/powershell/engine/Cdxml/assets/CimTest/DeleteCimTest.mof @@ -1,2 +1,5 @@ #pragma namespace ("\\\\.\\root\\default") -#pragma deleteclass("PSCore_CimTest", NOFAIL) +#pragma deleteclass("PSCore_Test1", NOFAIL) +#pragma deleteclass("PSCore_Test2", NOFAIL) +#pragma deleteclass("PSCore_subclass", NOFAIL) +#pragma deleteclass("PSCore_Association", NOFAIL) From 5279b030ffbc35e297e2790e996a9a52bc14f883 Mon Sep 17 00:00:00 2001 From: Jim Truher Date: Wed, 9 Aug 2017 13:45:16 -0700 Subject: [PATCH 3/4] Add tests for exceptions and support whatif --- test/powershell/engine/Cdxml/Cdxml.Tests.ps1 | 59 +++++++++++++++---- .../engine/Cdxml/assets/CimTest/CimTest.cdxml | 9 ++- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 b/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 index 12abe205974..45599067075 100644 --- a/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 +++ b/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 @@ -54,6 +54,7 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { } Import-Module -force ${script:ModuleDir} } + AfterAll { if ( $skipNotWindows ) { return @@ -61,22 +62,23 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { if ( get-module CimTest ) { Remove-Module CimTest -Force } - $result = MofComp.exe $deleteMof if ( $LASTEXITCODE -ne 0 ) { Write-Warning "Could not remove PSCore_CimTest class" } } - It "The CimTest module should have been loaded" @ItSkipOrPending { - $result = Get-Module CimTest - $result.ModuleBase | should be ${script:ModuleDir} - } + 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" + 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" { @@ -85,16 +87,23 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { $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 @@ -120,6 +129,11 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { } } } + + 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" { @@ -132,18 +146,21 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { } } } + 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 @@ -183,10 +200,31 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { $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" @@ -225,7 +263,6 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { $result.field1 | Should be "yet another value" $result.field2 | should be 33 } - } } diff --git a/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml b/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml index 05f7ff7c68c..6bae1957664 100644 --- a/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml +++ b/test/powershell/engine/Cdxml/assets/CimTest/CimTest.cdxml @@ -22,7 +22,7 @@ - + @@ -44,7 +44,7 @@ - + @@ -81,7 +81,7 @@ - + @@ -104,6 +104,9 @@ + + + From 0af83e29f31f15bad750f5be778cd596eff27321 Mon Sep 17 00:00:00 2001 From: Jim Truher Date: Mon, 14 Aug 2017 15:03:24 -0700 Subject: [PATCH 4/4] Add hardening to test code to handle missing or misbehaving MOFCOMP If there is a problem with mofcomp execution, the tests will fail rather than being marked as pending (feedback from DanTra) use cmdlet names rather than aliases for foreach-object and where-object Script analyzer report is clean Also remove unneeded empty lines and helpinfouri from psd1 file --- test/powershell/engine/Cdxml/Cdxml.Tests.ps1 | 77 ++++++++++--------- .../Cdxml/assets/CimTest/CdxmlTest.psd1 | 3 - 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 b/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 index 45599067075..52e374cb8bb 100644 --- a/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 +++ b/test/powershell/engine/Cdxml/Cdxml.Tests.ps1 @@ -1,8 +1,8 @@ $script:CimClassName = "PSCore_CimTest1" $script:CimNamespace = "root/default" -$script:moduleDir = join-path $PSScriptRoot assets CimTest -$script:deleteMof = join-path $moduleDir DeleteCimTest.mof -$script:createMof = join-path $moduleDir CreateCimTest.mof +$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} @@ -13,11 +13,11 @@ $CimCmdletArgs = @{ $script:ItSkipOrPending = @{} function Test-CimTestClass { - (Get-CimClass @CimCmdletArgs) -ne $null + $null -eq (Get-CimClass @CimCmdletArgs) } function Test-CimTestInstance { - (Get-CimInstance @CimCmdletArgs) -ne $null + $null -eq (Get-CimInstance @CimCmdletArgs) } Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { @@ -28,26 +28,39 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { return } - # start from a clean slate, remove the instances and the - # class if they exist + # 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 - if ( $LASTEXITCODE -ne 0 ) { - $script:ItSkipOrPending = @{ Pending = $true } + $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} - - if ( $LASTEXITCODE -ne 0 ) { - $script:ItSkipOrPending = @{ Pending = $true } + $script:MofCompReturnCode = $LASTEXITCODE + if ( $script:MofCompReturnCode -ne 0 ) { return } + # now load the cdxml module if ( Get-Module CimTest ) { Remove-Module -force CimTest @@ -62,12 +75,18 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { if ( get-module CimTest ) { Remove-Module CimTest -Force } - $result = MofComp.exe $deleteMof + $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 @@ -108,18 +127,11 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { try { $job = Get-CimTest -AsJob $result = $null - $i = 0 # 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 - do { - if ( $job.State -eq "Completed" ) - { - $result = $job | Receive-Job - break - } - start-sleep 1 - } while ( $i++ -lt 10 ) + $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" } @@ -139,10 +151,10 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { Context "Remove-CimTest cmdlet" { BeforeEach { Get-CimTest | Remove-CimTest - 1..4 | %{ New-CimInstance -namespace root/default -class PSCore_Test1 -property @{ + 1..4 | Foreach-Object { New-CimInstance -namespace root/default -class PSCore_Test1 -property @{ id = "$_" field1 = "field $_" - field2 = 10 * $_ + field2 = 10 * $_ } } } @@ -156,27 +168,20 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { It "The Remove-CimTest cmdlet should remove piped objects" @ItSkipOrPending { Get-CimTest -id 2 | Remove-CimTest - $result = Get-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 - $i = 0 # 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 - do { - if ( $job.State -eq "Completed" ) - { - break - } - start-sleep 1 - } while ( $i++ -lt 10 ) - $result = Get-CimTest + $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" } @@ -196,7 +201,7 @@ Describe "Cdxml cmdlets are supported" -Tag CI,RequireAdminOnWindows { field2 = 0 } New-CimTest @instanceArgs - $result = Get-CimInstance -namespace root/default -class PSCore_Test1 | ?{$_.id -eq "telephone"} + $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 } diff --git a/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 b/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 index 8f77e699e42..23214f45ed0 100644 --- a/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 +++ b/test/powershell/engine/Cdxml/assets/CimTest/CdxmlTest.psd1 @@ -11,7 +11,4 @@ AliasesToExport = @() CmdletsToExport = @() FunctionsToExport = @( 'Get-CimTest', 'Remove-CimTest', 'New-CimTest', 'Set-CimTest' ) - HelpInfoUri = "https://go.microsoft.com/fwlink/?linkid=390832" } - -