Skip to content

Commit

Permalink
Update 2.1.3 - More Patching
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
BatmanAMA authored Aug 23, 2018
1 parent 4f3d626 commit a130c1b
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 58 deletions.
2 changes: 1 addition & 1 deletion module/Public/Get-nbObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function Get-nbObject {
$Query['q'] = $Search
} else {
$Query = @{
s = $Search
q = $Search
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion module/Public/New-nbObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion module/Version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.1
2.1.3
4 changes: 2 additions & 2 deletions module/powerbox.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: batmanama
#
# Generated on: 8/22/2018
# Generated on: 8/23/2018
#

@{
Expand All @@ -12,7 +12,7 @@
RootModule = 'powerbox.psm1'

# Version number of this module.
ModuleVersion = '2.1.1'
ModuleVersion = '2.1.3'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
11 changes: 5 additions & 6 deletions powerbox.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
87 changes: 40 additions & 47 deletions test/new-nbobject.tests.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
42 changes: 42 additions & 0 deletions test/set-nbobject.tests.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit a130c1b

Please sign in to comment.