From a130c1b6bf5d85ca21a448446c998c18899fe321 Mon Sep 17 00:00:00 2001 From: Elle <10051455+BatmanAMA@users.noreply.github.com> Date: Thu, 23 Aug 2018 16:45:50 -0500 Subject: [PATCH] Update 2.1.3 - More Patching * Repair Get-NB* to work for objects with no custom fields. * Patch New-nbObject to understand lookups. * Fix a few tests. Some pester bugs. * Patch -Search on Get-nb*. * Change Pester options for local "Invoke-Build" runs. --- module/Public/Get-nbObject.ps1 | 2 +- module/Public/New-nbObject.ps1 | 2 +- module/Version | 2 +- module/powerbox.psd1 | 4 +- powerbox.build.ps1 | 11 ++--- test/new-nbobject.tests.ps1 | 87 ++++++++++++++++------------------ test/set-nbobject.tests.ps1 | 42 ++++++++++++++++ 7 files changed, 92 insertions(+), 58 deletions(-) diff --git a/module/Public/Get-nbObject.ps1 b/module/Public/Get-nbObject.ps1 index 41f09c3..40553a1 100644 --- a/module/Public/Get-nbObject.ps1 +++ b/module/Public/Get-nbObject.ps1 @@ -63,7 +63,7 @@ function Get-nbObject { $Query['q'] = $Search } else { $Query = @{ - s = $Search + q = $Search } } } diff --git a/module/Public/New-nbObject.ps1 b/module/Public/New-nbObject.ps1 index fde4cff..8a66846 100644 --- a/module/Public/New-nbObject.ps1 +++ b/module/Public/New-nbObject.ps1 @@ -68,7 +68,7 @@ function New-nbObject { $Name = $Property.name -replace '-' -replace ':' $value = $Property.value if ($name -in $lookup.keys) { - $value = ConvertTo-nbID -source $value -value $name + $value = ConvertTo-nbID -source $lookup[$name] -value $value } if ($name -in $CustomProperties) { $mapObject.custom_fields[$name] = $value diff --git a/module/Version b/module/Version index fd3d3ec..0f5c099 100644 --- a/module/Version +++ b/module/Version @@ -1 +1 @@ -2.1.1 +2.1.3 diff --git a/module/powerbox.psd1 b/module/powerbox.psd1 index 21c8698..c6d11e3 100644 --- a/module/powerbox.psd1 +++ b/module/powerbox.psd1 @@ -3,7 +3,7 @@ # # Generated by: batmanama # -# Generated on: 8/22/2018 +# Generated on: 8/23/2018 # @{ @@ -12,7 +12,7 @@ RootModule = 'powerbox.psm1' # Version number of this module. -ModuleVersion = '2.1.1' +ModuleVersion = '2.1.3' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/powerbox.build.ps1 b/powerbox.build.ps1 index f1803e6..a75cfaa 100644 --- a/powerbox.build.ps1 +++ b/powerbox.build.ps1 @@ -129,8 +129,9 @@ task Test -If { $Discovery.HasTests -and $Settings.ShouldTest } { CodeCoverage = $files } if (!$ENV:APPVEYOR) { + $PesterSettings['CodeCoverage'] = '' $PesterSettings['PesterOption'] = @{IncludeVSCodeMarker = $true} - #$PesterSettings['Show'] = "Fails" + $PesterSettings['Show'] = "Fails" } $Tests = (Invoke-Pester @PesterSettings) foreach ($test in $Tests.TestResult) { @@ -157,14 +158,12 @@ task Test -If { $Discovery.HasTests -and $Settings.ShouldTest } { StdErr = $test.FailureMessage } Add-AppveyorTest @appveyorTest -verbose - } else { - $test | Select-Object Result, Name } } - $cov = ($Tests.CodeCoverage.NumberOfCommandsExecuted / $Tests.CodeCoverage.NumberOfCommandsAnalyzed * 100) - $cov = [System.Math]::Round($cov, 2) - "Code Coverage: $cov%" if ($ENV:AppVeyor) { + $cov = ($Tests.CodeCoverage.NumberOfCommandsExecuted / $Tests.CodeCoverage.NumberOfCommandsAnalyzed * 100) + $cov = [System.Math]::Round($cov, 2) + "Code Coverage: $cov%" $Sev = if ($cov -lt 50) {"Error"} elseif ($cov -lt 100) {"Warning"} else {"Information"} Add-AppveyorMessage "Code Coverage: $cov%" -Category $Sev exit $tests.FailedCount diff --git a/test/new-nbobject.tests.ps1 b/test/new-nbobject.tests.ps1 index edb8c15..31b604e 100644 --- a/test/new-nbobject.tests.ps1 +++ b/test/new-nbobject.tests.ps1 @@ -1,47 +1,40 @@ -#Describe 'New object passes stuff through' { -# it "should call invoke-nbApi with the proper passthrough" { -# $token = ConvertTo-SecureString -String "APITOKEN" -AsPlainText -Force -# Connect-nbAPI -APIurl 'http://example.com' -Token $token -# $object = @{ -# name = "NewDevice" -# serial = "Example" -# } -# #need to insert the object into the parameter filter -# #$ParameterFilter = { -# # $body -eq 'BODYTEXT' -and -# # $resource -eq "dcim/devices" -and -# # $HttpVerb -eq [Microsoft.PowerShell.Commands.WebRequestMethod]::Post -# #} -# #$ParameterFilter = $ParameterFilter.ToString() -replace 'BODYTEXT', ($object | ConvertTo-Json -Compress) -# #$ParameterFilter = [scriptblock]::Create($ParameterFilter) -# Mock Invoke-nbApi -MockWith {} -ModuleName powerbox #-Verifiable -ParameterFilter $ParameterFilter -# -# New-nbObject -Resource dcim/devices @object -# Assert-MockCalled -CommandName Invoke-nbApi -Times 1 -# #Assert-VerifiableMock -# } -# it "Should passsthrough even with lookups" { -# $object = @{ -# name = "NewDevice" -# serial = "Example" -# device_type = "Something" -# } -# $lookup = @{ -# device_type = 'dcim/device-types' -# } -# Mock Invoke-nbApi { } # -ModuleName powerbox #-Verifiable -ParameterFilter { -# # $body -eq '{"Name":"NewDevice","Serial":"Example","device-type":0}' -and -# # $resource -eq 'dcim/devices' -and -# # $HttpVerb -eq [Microsoft.PowerShell.Commands.WebRequestMethod]::Post -# #} -# Mock ConvertTo-nbID {return 0} #-ModuleName powerbox #-Verifiable -ParameterFilter { -# # $source -eq 'dcim/device-types' -and -# # $value -eq 'Something' -# #} -# {New-nbObject -Resource dcim/devices -Lookup $lookup @object} | -# Should -not -Throw -# Assert-MockCalled -CommandName Invoke-nbApi -Times 1 -Exactly -# Assert-MockCalled -CommandName ConvertTo-nbID -Times 1 -Exactly -# #Assert-VerifiableMock -# } -#} +Describe 'New object passes stuff through' { + BeforeEach { + $token = ConvertTo-SecureString -String "APITOKEN" -AsPlainText -Force + Connect-nbAPI -APIurl 'http://example.com' -Token $token + } + it "should call invoke-nbApi with the proper passthrough" { + $object = [pscustomobject]@{ + name = "NewDevice" + serial = "Example" + } + + Mock Invoke-nbApi -MockWith {} -ModuleName powerbox -Verifiable + {New-nbObject -Resource dcim/devices -Object $object} | should -not -Throw + #TODO: Submit bug report to pester on why this doesn't work with the parameter filter. + Assert-MockCalled -CommandName Invoke-nbApi -Times 1 -ModuleName powerbox <#-ParameterFilter { + $body -eq '{"Name":"NewDevice","Serial":"Example"}' -and + $resource -eq "dcim/devices" -and + $HttpVerb -eq 'POST' + }#> + } + it "Should passsthrough even with lookups" { + $object = [pscustomobject]@{ + name = "NewDevice" + serial = "Example" + device_type = "Something" + } + $lookup = @{ + device_type = 'dcim/device-types' + } + Mock Invoke-nbApi {} -ModuleName powerbox -Verifiable + Mock ConvertTo-nbID {return 0} -ModuleName powerbox + {New-nbObject -Resource dcim/devices -Lookup $lookup -object $object } | Should -not -Throw + Assert-MockCalled -CommandName Invoke-nbApi -Times 2 -Exactly -ModuleName powerbox <#-ParameterFilter { + $body -eq '{"Name":"NewDevice","Serial":"Example","device-type":0}' -and + $resource -eq 'dcim/devices' -and + $HttpVerb -eq [Microsoft.PowerShell.Commands.WebRequestMethod]::Post + }#> + Assert-MockCalled -CommandName ConvertTo-nbID -Times 1 -Exactly -ModuleName powerbox + } +} diff --git a/test/set-nbobject.tests.ps1 b/test/set-nbobject.tests.ps1 index e69de29..3d0c55e 100644 --- a/test/set-nbobject.tests.ps1 +++ b/test/set-nbobject.tests.ps1 @@ -0,0 +1,42 @@ +Describe 'Set object passes stuff through' { + BeforeEach { + $token = ConvertTo-SecureString -String "APITOKEN" -AsPlainText -Force + Connect-nbAPI -APIurl 'http://example.com' -Token $token + } + it "should call invoke-nbApi with the proper passthrough" { + $object = [pscustomobject]@{ + name = "NewDevice" + serial = "Example" + } + + Mock Invoke-nbApi -MockWith {} -ModuleName powerbox -Verifiable + {Set-nbObject -Id 1 -Resource dcim/devices -Object $object} | should -not -Throw + #TODO: Submit bug report to pester on why this doesn't work with the parameter filter. + Assert-MockCalled -CommandName Invoke-nbApi -Times 1 -ModuleName powerbox <#-ParameterFilter { + $Id -eq 1 -and + $body -eq '{"Name":"NewDevice","Serial":"Example"}' -and + $resource -eq "dcim/devices" -and + $HttpVerb -eq 'POST' + }#> + } + it "Should passsthrough even with lookups" { + $object = [pscustomobject]@{ + name = "NewDevice" + serial = "Example" + device_type = "Something" + } + $lookup = @{ + device_type = 'dcim/device-types' + } + Mock Invoke-nbApi {} -ModuleName powerbox -Verifiable + Mock ConvertTo-nbID {return 0} -ModuleName powerbox + {Set-nbObject -Id 1 -Resource dcim/devices -Lookup $lookup -object $object } | Should -not -Throw + Assert-MockCalled -CommandName Invoke-nbApi -Times 2 -Exactly -ModuleName powerbox <#-ParameterFilter { + $Id -eq 1 -and + $body -eq '{"Name":"NewDevice","Serial":"Example","device-type":0}' -and + $resource -eq 'dcim/devices' -and + $HttpVerb -eq [Microsoft.PowerShell.Commands.WebRequestMethod]::Post + }#> + Assert-MockCalled -CommandName ConvertTo-nbID -Times 1 -Exactly -ModuleName powerbox + } +}