This commit is contained in:
Ben Claussen 2018-05-29 10:43:23 -04:00
parent b73bbcb8dc
commit 8ec7c97890
10 changed files with 3238 additions and 150 deletions

View file

@ -19,7 +19,7 @@
ModuleToProcess = 'NetboxPS.psm1'
# Version number of this module.
ModuleVersion = '1.0.1'
ModuleVersion = '1.0.2'
# ID used to uniquely identify this module
GUID = 'bba9b06c-49c8-47cf-8358-aca7c4e78896'

2
dist/NetboxPS.psd1 vendored
View file

@ -19,7 +19,7 @@
ModuleToProcess = 'NetboxPS.psm1'
# Version number of this module.
ModuleVersion = '1.0.1'
ModuleVersion = '1.0.2'
# ID used to uniquely identify this module
GUID = 'bba9b06c-49c8-47cf-8358-aca7c4e78896'

1408
dist/NetboxPS.psm1 vendored

File diff suppressed because it is too large Load diff

512
dist/Tests/DCIM.Devices.Tests.ps1 vendored Normal file
View file

@ -0,0 +1,512 @@
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.152
Created on: 5/22/2018 4:48 PM
Created by: Ben Claussen
Organization: NEOnet
Filename: DCIM.Tests.ps1
===========================================================================
.DESCRIPTION
DCIM tests.
#>
Import-Module Pester
Remove-Module NetboxPS -Force -ErrorAction SilentlyContinue
$ModulePath = "$PSScriptRoot\..\dist\NetboxPS.psd1"
if (Test-Path $ModulePath) {
Import-Module $ModulePath -ErrorAction Stop
}
Describe -Name "DCIM Devices Tests" -Tag 'DCIM', 'Devices' -Fixture {
Mock -CommandName 'CheckNetboxIsConnected' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return $true
}
Mock -CommandName 'Invoke-RestMethod' -Verifiable -ModuleName 'NetboxPS' -MockWith {
# Return a hashtable of the items we would normally pass to Invoke-RestMethod
return [ordered]@{
'Method' = $Method
'Uri' = $Uri
'Headers' = $Headers
'Timeout' = $Timeout
'ContentType' = $ContentType
'Body' = $Body
}
}
Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
}
Mock -CommandName 'Get-NetboxHostname' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return 'netbox.domain.com'
}
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
$script:NetboxConfig.Choices.DCIM = (Get-Content "$PSScriptRoot\DCIMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "Get-NetboxDCIMDevice" -Fixture {
It "Should request the default number of devices" {
$Result = Get-NetboxDCIMDevice
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMDevice -Limit 10 -Offset 100
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a query" {
$Result = Get-NetboxDCIMDevice -Query 'testdevice'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?q=testdevice'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with an escaped query" {
$Result = Get-NetboxDCIMDevice -Query 'test device'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?q=test+device'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a name" {
$Result = Get-NetboxDCIMDevice -Name 'testdevice'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?name=testdevice'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a single ID" {
$Result = Get-NetboxDCIMDevice -Id 10
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request a device by ID from the pipeline" {
$Result = [pscustomobject]@{
'id' = 10
} | Get-NetboxDCIMDevice
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with multiple IDs" {
$Result = Get-NetboxDCIMDevice -Id 10, 12, 15
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?id__in=10,12,15'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request a status" {
$Result = Get-NetboxDCIMDevice -Status 'Active'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?status=1'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should throw for an invalid status" {
{
Get-NetboxDCIMDevice -Status 'Fake'
} | Should -Throw
}
It "Should request devices that are a PDU" {
$Result = Get-NetboxDCIMDevice -Is_PDU $True
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/devices/?is_pdu=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
}
Context -Name "Get-NetboxDCIMDeviceType" -Fixture {
It "Should request the default number of devices types" {
$Result = Get-NetboxDCIMDeviceType
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMDeviceType -Limit 10 -Offset 100
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a query" {
$Result = Get-NetboxDCIMDeviceType -Query 'testdevice'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?q=testdevice'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with an escaped query" {
$Result = Get-NetboxDCIMDeviceType -Query 'test device'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?q=test+device'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a slug" {
$Result = Get-NetboxDCIMDeviceType -Slug 'testdevice'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?slug=testdevice'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a single ID" {
$Result = Get-NetboxDCIMDeviceType -Id 10
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with multiple IDs" {
$Result = Get-NetboxDCIMDeviceType -Id 10, 12, 15
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?id__in=10,12,15'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request a device type that is PDU" {
$Result = Get-NetboxDCIMDeviceType -Is_PDU $true
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?is_pdu=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
}
Context -Name "Get-NetboxDCIMDeviceRole" -Fixture {
It "Should request the default number of devices types" {
$Result = Get-NetboxDCIMDeviceRole
Assert-VerifiableMock
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request a device role by Id" {
$Result = Get-NetboxDCIMDeviceRole -Id 10
Assert-VerifiableMock
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request multiple roles by Id" {
$Result = Get-NetboxDCIMDeviceRole -Id 10, 12
Assert-VerifiableMock
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 2 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET', 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/10/', 'https://netbox.domain.com/api/dcim/device-roles/12/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
It "Should request single role by Id and color" {
$Result = Get-NetboxDCIMDeviceRole -Id 10 -Color '0fab12'
Assert-VerifiableMock
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/10/?color=0fab12'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request multiple roles by Id and color" {
$Result = Get-NetboxDCIMDeviceRole -Id 10, 12 -Color '0fab12'
Assert-VerifiableMock
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 2 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET', 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/10/?color=0fab12', 'https://netbox.domain.com/api/dcim/device-roles/12/?color=0fab12'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMDeviceRole -Limit 10 -Offset 100
Assert-VerifiableMock
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a slug" {
$Result = Get-NetboxDCIMDeviceRole -Slug 'testdevice'
Assert-VerifiableMock
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/?slug=testdevice'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a name" {
$Result = Get-NetboxDCIMDeviceRole -Name 'TestRole'
Assert-VerifiableMock
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/?name=TestRole'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request those that are VM role" {
$Result = Get-NetboxDCIMDeviceRole -VM_Role $true
Assert-VerifiableMock
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/?vm_role=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
}
Context -Name "New-NetboxDCIMDevice" -Fixture {
It "Should create a new device" {
$Result = New-NetboxDCIMDevice -Name "newdevice" -Device_Role 4 -Device_Type 10 -Site 1 -Face 0
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"site":1,"face":0,"name":"newdevice","status":1,"device_type":10,"device_role":4}'
}
It "Should throw because of an invalid status" {
{
New-NetboxDCIMDevice -Name "newdevice" -Device_Role 4 -Device_Type 10 -Site 1 -Status 5555
} | Should -Throw
}
}
Mock -CommandName "Get-NetboxDCIMDevice" -ModuleName NetboxPS -MockWith {
return [pscustomobject]@{
'Id' = $Id
'Name' = $Name
}
}
Context -Name "Set-NetboxDCIMDevice" -Fixture {
It "Should set a device to a new name" {
$Result = Set-NetboxDCIMDevice -Id 1234 -Name 'newtestname' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"newtestname"}'
}
It "Should set a device with new properties" {
$Result = Set-NetboxDCIMDevice -Id 1234 -Name 'newtestname' -Cluster 10 -Platform 20 -Site 15 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"cluster":10,"platform":20,"name":"newtestname","site":15}'
}
It "Should set multiple devices with new properties" {
$Result = Set-NetboxDCIMDevice -Id 1234, 3214 -Cluster 10 -Platform 20 -Site 15 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/1234/', 'https://netbox.domain.com/api/dcim/devices/3214/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"cluster":10,"platform":20,"site":15}', '{"cluster":10,"platform":20,"site":15}'
}
It "Should set multiple devices with new properties from the pipeline" {
$Result = @(
[pscustomobject]@{
'id' = 4432
},
[pscustomobject]@{
'id' = 3241
}
) | Set-NetboxDCIMDevice -Cluster 10 -Platform 20 -Site 15 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/4432/', 'https://netbox.domain.com/api/dcim/devices/3241/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"cluster":10,"platform":20,"site":15}', '{"cluster":10,"platform":20,"site":15}'
}
}
Context -Name "Remove-NetboxDCIMDevice" -Fixture {
It "Should remove a device" {
$Result = Remove-NetboxDCIMDevice -Id 10 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should remove multiple devices" {
$Result = Remove-NetboxDCIMDevice -Id 10, 12 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/10/', 'https://netbox.domain.com/api/dcim/devices/12/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
It "Should remove a device from the pipeline" {
$Result = Get-NetboxDCIMDevice -Id 20 | Remove-NetboxDCIMDevice -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/20/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should remove mulitple devices from the pipeline" {
$Result = @(
[pscustomobject]@{
'Id' = 30
},
[pscustomobject]@{
'Id' = 40
}
) | Remove-NetboxDCIMDevice -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/30/', 'https://netbox.domain.com/api/dcim/devices/40/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
}
}
}

505
dist/Tests/DCIM.Interfaces.Tests.ps1 vendored Normal file
View file

@ -0,0 +1,505 @@
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.152
Created on: 5/24/2018 10:50 AM
Created by: Ben Claussen
Organization: NEOnet
Filename: DCIM.Interfaces.Tests.ps1
===========================================================================
.DESCRIPTION
A description of the file.
#>
Import-Module Pester
Remove-Module NetboxPS -Force -ErrorAction SilentlyContinue
$ModulePath = "$PSScriptRoot\..\dist\NetboxPS.psd1"
if (Test-Path $ModulePath) {
Import-Module $ModulePath -ErrorAction Stop
}
Describe -Name "DCIM Interfaces Tests" -Tag 'DCIM', 'Interfaces' -Fixture {
Mock -CommandName 'CheckNetboxIsConnected' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return $true
}
Mock -CommandName 'Invoke-RestMethod' -Verifiable -ModuleName 'NetboxPS' -MockWith {
# Return a hashtable of the items we would normally pass to Invoke-RestMethod
return [ordered]@{
'Method' = $Method
'Uri' = $Uri
'Headers' = $Headers
'Timeout' = $Timeout
'ContentType' = $ContentType
'Body' = $Body
}
}
Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
}
Mock -CommandName 'Get-NetboxHostname' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return 'netbox.domain.com'
}
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
$script:NetboxConfig.Choices.DCIM = (Get-Content "$PSScriptRoot\DCIMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "Get-NetboxDCIMInterface" -Fixture {
It "Should request the default number of interfaces" {
$Result = Get-NetboxDCIMInterface
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMInterface -Limit 10 -Offset 100
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with enabled" {
$Result = Get-NetboxDCIMInterface -Enabled $true
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/?enabled=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a form factor name" {
$Result = Get-NetboxDCIMInterface -Form_Factor '10GBASE-T (10GE)'
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/?form_factor=1150'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should throw for an invalid form factor" {
{
Get-NetboxDCIMInterface -Form_Factor 'Fake'
} | Should -Throw
}
It "Should request devices that are mgmt only" {
$Result = Get-NetboxDCIMInterface -MGMT_Only $True
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/?mgmt_only=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request an interface from the pipeline" {
$Result = [pscustomobject]@{
'Id' = 1234
} | Get-NetboxDCIMInterface
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
}
Context -Name "Add-NetboxDCIMInterface" -Fixture {
It "Should add a basic interface to a device" {
$Result = Add-NetboxDCIMInterface -Device 111 -Name "TestInterface"
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"TestInterface","device":111}'
}
It "Should add an interface to a device with lots of properties" {
$paramAddNetboxDCIMInterface = @{
Device = 123
Name = "TestInterface"
Form_Factor = '10GBASE-T (10GE)'
MTU = 9000
MGMT_Only = $true
Description = 'Test Description'
Mode = 'Access'
}
$Result = Add-NetboxDCIMInterface @paramAddNetboxDCIMInterface
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"mtu":9000,"mgmt_only":true,"description":"Test Description","mode":100,"name":"TestInterface","device":123,"form_factor":1150}'
}
It "Should add an interface with multiple tagged VLANs" {
$Result = Add-NetboxDCIMInterface -Device 444 -Name "TestInterface" -Mode 'Tagged' -Tagged_VLANs 1, 2, 3, 4
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"mode":200,"name":"TestInterface","device":444,"tagged_vlans":[1,2,3,4]}'
}
It "Should throw for invalid mode" {
{
Add-NetboxDCIMInterface -Device 321 -Name "Test123" -Mode 'Fake'
} | Should -Throw
}
It "Should throw for out of range VLAN" {
{
Add-NetboxDCIMInterface -Device 321 -Name "Test123" -Untagged_VLAN 4100
} | Should -Throw
}
}
Mock -CommandName "Get-NetboxDCIMInterface" -ModuleName "NetboxPS" -MockWith {
return [pscustomobject]@{
'Id' = $Id
}
}
Context -Name "Set-NetboxDCIMInterface" -Fixture {
It "Should set an interface to a new name" {
$Result = Set-NetboxDCIMInterface -Id 123 -Name "TestInterface"
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 1 -Exactly -Scope 'It'
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/123/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"TestInterface"}'
}
It "Should set multiple interfaces to a new name" {
$Result = Set-NetboxDCIMInterface -Id 456, 789 -Name "TestInterface"
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 2 -Exactly -Scope 'It'
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/456/', 'https://netbox.domain.com/api/dcim/interfaces/789/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"name":"TestInterface"}', '{"name":"TestInterface"}'
}
It "Should set multiple interfaces to a new name from the pipeline" {
$Result = @(
[pscustomobject]@{
'Id' = 1234
},
[pscustomobject]@{
'Id' = 4231
}
) | Set-NetboxDCIMInterface -Name "TestInterface"
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 2 -Exactly -Scope 'It'
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/1234/', 'https://netbox.domain.com/api/dcim/interfaces/4231/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"name":"TestInterface"}', '{"name":"TestInterface"}'
}
It "Should throw for invalid form factor" {
{
Set-NetboxDCIMInterface -Id 1234 -Form_Factor 'fake'
} | Should -Throw
}
}
Context -Name "Remove-NetboxDCIMInterface" -Fixture {
It "Should remove an interface" {
$Result = Remove-NetboxDCIMInterface -Id 10 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should remove multiple interfaces" {
$Result = Remove-NetboxDCIMInterface -Id 10, 12 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/10/', 'https://netbox.domain.com/api/dcim/interfaces/12/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
It "Should remove an interface from the pipeline" {
$Result = Get-NetboxDCIMInterface -Id 20 | Remove-NetboxDCIMInterface -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/20/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should remove mulitple interfaces from the pipeline" {
$Result = @(
[pscustomobject]@{
'Id' = 30
},
[pscustomobject]@{
'Id' = 40
}
) | Remove-NetboxDCIMInterface -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/30/', 'https://netbox.domain.com/api/dcim/interfaces/40/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
}
Context -Name "Get-NetboxDCIMInterfaceConnection" -Fixture {
It "Should request the default number of interface connections" {
$Result = Get-NetboxDCIMInterfaceConnection
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interface-connections/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMInterfaceConnection -Limit 10 -Offset 100
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interface-connections/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request connected interfaces" {
$Result = Get-NetboxDCIMInterfaceConnection -Connection_Status 'Connected'
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interface-connections/?connection_status=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should throw for an invalid connection status" {
{
Get-NetboxDCIMInterfaceConnection -Connection_Status 'Fake'
} | Should -Throw
}
}
Context -Name "Add-NetboxDCIMInterfaceConnection" -Fixture {
It "Should add a new interface connection" {
$Result = Add-NetboxDCIMInterfaceConnection -Interface_A 21 -Interface_B 22
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interface-connections/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"interface_b":22,"interface_a":21}'
}
It "Should throw because of an invalid connection status" {
{
Add-NetboxDCIMInterfaceConnection -Interface_A 21 -Interface_B 22 -Connection_Status 'fake'
} | Should -Throw
}
}
Mock -CommandName "Get-NetboxDCIMInterfaceConnection" -ModuleName 'NetboxPS' -MockWith {
[pscustomobject]@{
'Id' = $Id
}
}
Context -Name "Set-NetboxDCIMInterfaceConnection" -Fixture {
It "Should set an interface connection" {
$Result = Set-NetboxDCIMInterfaceConnection -Id 123 -Interface_B 2 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interface-connections/123/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"interface_b":2}'
}
It "Should set multiple interface connections to a new status" {
$Result = Set-NetboxDCIMInterfaceConnection -Id 456, 789 -Connection_Status 'Planned' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interface-connections/456/', 'https://netbox.domain.com/api/dcim/interface-connections/789/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"connection_status":false}', '{"connection_status":false}'
}
It "Should set an interface connection from the pipeline" {
$Result = [pscustomobject]@{
'id' = 3
} | Set-NetboxDCIMInterfaceConnection -Connection_Status 'Planned' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interface-connections/3/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"connection_status":false}'
}
It "Should set multiple interface connections from the pipeline" {
$Result = @(
[pscustomobject]@{
'id' = 456
},
[pscustomobject]@{
'id' = 789
}
) | Set-NetboxDCIMInterfaceConnection -Connection_Status 'Planned' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interface-connections/456/', 'https://netbox.domain.com/api/dcim/interface-connections/789/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"connection_status":false}', '{"connection_status":false}'
}
It "Should throw trying to set multiple connections to the same interface" {
{
Set-NetboxDCIMInterfaceConnection -Id 456, 789 -Interface_B 22 -Force
} | Should -Throw -ExpectedMessage "Cannot set multiple connections to the same interface"
}
}
Context -Name "Remove-NetboxDCIMInterfaceConnection" -Fixture {
It "Should remove an interface connection" {
$Result = Remove-NetboxDCIMInterfaceConnection -Id 10 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterfaceConnection' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interface-connections/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should remove multiple interface connections" {
$Result = Remove-NetboxDCIMInterfaceConnection -Id 10, 12 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterfaceConnection' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interface-connections/10/', 'https://netbox.domain.com/api/dcim/interface-connections/12/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
It "Should remove an interface connection from the pipeline" {
$Result = Get-NetboxDCIMInterfaceConnection -Id 20 | Remove-NetboxDCIMInterfaceConnection -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterfaceConnection' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interface-connections/20/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should remove mulitple interface connections from the pipeline" {
$Result = @(
[pscustomobject]@{
'Id' = 30
},
[pscustomobject]@{
'Id' = 40
}
) | Remove-NetboxDCIMInterfaceConnection -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterfaceConnection' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interface-connections/30/', 'https://netbox.domain.com/api/dcim/interface-connections/40/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
}
}
}

121
dist/Tests/DCIM.Platforms.Tests.ps1 vendored Normal file
View file

@ -0,0 +1,121 @@
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.152
Created on: 5/25/2018 1:03 PM
Created by: Ben Claussen
Organization: NEOnet
Filename: DCIM.Platforms.Tests.ps1
===========================================================================
.DESCRIPTION
A description of the file.
#>
Import-Module Pester
Remove-Module NetboxPS -Force -ErrorAction SilentlyContinue
$ModulePath = "$PSScriptRoot\..\dist\NetboxPS.psd1"
if (Test-Path $ModulePath) {
Import-Module $ModulePath -ErrorAction Stop
}
Describe -Name "DCIM Platforms Tests" -Tag 'DCIM', 'platforms' -Fixture {
Mock -CommandName 'CheckNetboxIsConnected' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return $true
}
Mock -CommandName 'Invoke-RestMethod' -Verifiable -ModuleName 'NetboxPS' -MockWith {
# Return a hashtable of the items we would normally pass to Invoke-RestMethod
return [ordered]@{
'Method' = $Method
'Uri' = $Uri
'Headers' = $Headers
'Timeout' = $Timeout
'ContentType' = $ContentType
'Body' = $Body
}
}
Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
}
Mock -CommandName 'Get-NetboxHostname' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return 'netbox.domain.com'
}
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
Context -Name "Get-NetboxDCIMPlatform" -Fixture {
It "Should request the default number of platforms" {
$Result = Get-NetboxDCIMPlatform
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/platforms/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMPlatform -Limit 10 -Offset 100
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/platforms/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a platform name" {
$Result = Get-NetboxDCIMPlatform -Name "Windows Server 2016"
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/platforms/?name=Windows+Server+2016'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request a platform by manufacturer" {
$Result = Get-NetboxDCIMPlatform -Manufacturer 'Cisco'
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/platforms/?manufacturer=Cisco'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request a platform by ID" {
$Result = Get-NetboxDCIMPlatform -Id 10
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/platforms/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request multiple platforms by ID" {
$Result = Get-NetboxDCIMPlatform -Id 10, 20
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 2 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET', 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/platforms/10/', 'https://netbox.domain.com/api/dcim/platforms/20/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
}
}
}

315
dist/Tests/DCIM.Tests.ps1 vendored Normal file
View file

@ -0,0 +1,315 @@
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.152
Created on: 5/22/2018 4:48 PM
Created by: Ben Claussen
Organization: NEOnet
Filename: DCIM.Tests.ps1
===========================================================================
.DESCRIPTION
DCIM tests.
#>
Import-Module Pester
Remove-Module NetboxPS -Force -ErrorAction SilentlyContinue
$ModulePath = "$PSScriptRoot\..\dist\NetboxPS.psd1"
if (Test-Path $ModulePath) {
Import-Module $ModulePath -ErrorAction Stop
}
Describe -Name "DCIM Tests" -Tag 'DCIM' -Fixture {
Mock -CommandName 'CheckNetboxIsConnected' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return $true
}
Mock -CommandName 'Invoke-RestMethod' -Verifiable -ModuleName 'NetboxPS' -MockWith {
# Return a hashtable of the items we would normally pass to Invoke-RestMethod
return [ordered]@{
'Method' = $Method
'Uri' = $Uri
'Headers' = $Headers
'Timeout' = $Timeout
'ContentType' = $ContentType
'Body' = $Body
}
}
Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
}
Mock -CommandName 'Get-NetboxHostname' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return 'netbox.domain.com'
}
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
$script:NetboxConfig.Choices.DCIM = (Get-Content "$PSScriptRoot\DCIMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "Get-NetboxDCIMDevice" -Fixture {
It "Should request the default number of devices" {
$Result = Get-NetboxDCIMDevice
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMDevice -Limit 10 -Offset 100
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a query" {
$Result = Get-NetboxDCIMDevice -Query 'testdevice'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?q=testdevice'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with an escaped query" {
$Result = Get-NetboxDCIMDevice -Query 'test device'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?q=test+device'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a name" {
$Result = Get-NetboxDCIMDevice -Name 'testdevice'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?name=testdevice'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a single ID" {
$Result = Get-NetboxDCIMDevice -Id 10
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with multiple IDs" {
$Result = Get-NetboxDCIMDevice -Id 10, 12, 15
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?id__in=10,12,15'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request a status" {
$Result = Get-NetboxDCIMDevice -Status 'Active'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?status=1'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should throw for an invalid status" {
{
Get-NetboxDCIMDevice -Status 'Fake'
} | Should -Throw
}
It "Should request devices that are a PDU" {
$Result = Get-NetboxDCIMDevice -Is_PDU $True
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/devices/?is_pdu=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
}
Context -Name "Get-NetboxDCIMDeviceType" -Fixture {
It "Should request the default number of devices types" {
$Result = Get-NetboxDCIMDeviceType
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMDeviceType -Limit 10 -Offset 100
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a query" {
$Result = Get-NetboxDCIMDeviceType -Query 'testdevice'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?q=testdevice'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with an escaped query" {
$Result = Get-NetboxDCIMDeviceType -Query 'test device'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?q=test+device'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a slug" {
$Result = Get-NetboxDCIMDeviceType -Slug 'testdevice'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?slug=testdevice'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a single ID" {
$Result = Get-NetboxDCIMDeviceType -Id 10
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with multiple IDs" {
$Result = Get-NetboxDCIMDeviceType -Id 10, 12, 15
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?id__in=10,12,15'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request a device type that is PDU" {
$Result = Get-NetboxDCIMDeviceType -Is_PDU $true
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?is_pdu=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
}
Context -Name "Get-NetboxDCIMDeviceRole" -Fixture {
It "Should request the default number of devices types" {
$Result = Get-NetboxDCIMDeviceRole
Assert-VerifiableMock
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMDeviceRole -Limit 10 -Offset 100
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a slug" {
$Result = Get-NetboxDCIMDeviceRole -Slug 'testdevice'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/?slug=testdevice'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a name" {
$Result = Get-NetboxDCIMDeviceRole -Name 'TestRole'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/?name=TestRole'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request those that are VM role" {
$Result = Get-NetboxDCIMDeviceRole -VM_Role $true
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/?vm_role=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
}
}
}

View file

@ -244,11 +244,9 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
}
Context -Name "Validating choices" -Fixture {
$script:NetboxConfig.Choices.Virtualization = (Get-Content "$PSScriptRoot\VirtualizationChoices.json" -ErrorAction Stop | ConvertFrom-Json)
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "Virtualization choices" -Fixture {
$MajorObject = 'Virtualization'
$script:NetboxConfig.Choices.Virtualization = (Get-Content "$PSScriptRoot\VirtualizationChoices.json" -ErrorAction Stop | ConvertFrom-Json)
It "Should return a valid integer for status when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 'Active'
@ -273,6 +271,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
Context -Name "IPAM choices" -Fixture {
$MajorObject = 'IPAM'
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "aggregate:family" -Fixture {
$ChoiceName = 'aggregate:family'
@ -467,10 +466,248 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
}
}
Context -Name "DCIM choices" -Fixture {
$MajorObject = 'DCIM'
$script:NetboxConfig.Choices.DCIM = (Get-Content "$PSScriptRoot\DCIMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "device:face" -Fixture {
$ChoiceName = 'device:face'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Front'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 0
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
} | Should -Throw
}
}
Context -Name "device:status" -Fixture {
$ChoiceName = 'device:status'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 0
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
} | Should -Throw
}
}
Context -Name "console-port:connection_status" -Fixture {
$ChoiceName = 'console-port:connection_status'
It "Should return a valid string when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Planned'
$Result | Should -BeOfType [bool]
$Result | Should -Be $false
}
It "Should return a valid string when provided a string" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'false'
$Result | Should -BeOfType [bool]
$Result | Should -Be $false
}
It "Should return a valid string when provided a boolean" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue $true
$Result | Should -BeOfType [bool]
$Result | Should -Be $true
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
} | Should -Throw
}
}
Context -Name "interface:form_factor" -Fixture {
$ChoiceName = 'interface:form_factor'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue '10GBASE-CX4 (10GE)'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1170
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1500
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1500
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
} | Should -Throw
}
}
Context -Name "interface-connection:connection_status" -Fixture {
$ChoiceName = 'interface-connection:connection_status'
It "Should return a valid string when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Planned'
$Result | Should -BeOfType [bool]
$Result | Should -Be $false
}
It "Should return a valid string when provided a string" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'false'
$Result | Should -BeOfType [bool]
$Result | Should -Be $false
}
It "Should return a valid string when provided a boolean" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue $true
$Result | Should -BeOfType [bool]
$Result | Should -Be $true
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
} | Should -Throw
}
}
Context -Name "interface-template:form_factor" -Fixture {
$ChoiceName = 'interface-template:form_factor'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue '10GBASE-CX4 (10GE)'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1170
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1500
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1500
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
} | Should -Throw
}
}
Context -Name "power-port:connection_status" -Fixture {
$ChoiceName = 'power-port:connection_status'
It "Should return a valid string when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Planned'
$Result | Should -BeOfType [bool]
$Result | Should -Be $false
}
It "Should return a valid string when provided a string" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'false'
$Result | Should -BeOfType [bool]
$Result | Should -Be $false
}
It "Should return a valid string when provided a boolean" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue $true
$Result | Should -BeOfType [bool]
$Result | Should -Be $true
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
} | Should -Throw
}
}
Context -Name "rack:type" -Fixture {
$ChoiceName = 'rack:type'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue '2-post frame'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 100
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 300
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 300
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
} | Should -Throw
}
}
Context -Name "rack:width" -Fixture {
$ChoiceName = 'rack:width'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue '19 inches'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 19
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 23
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 23
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
} | Should -Throw
}
}
}
}
}
}
@ -479,3 +716,11 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {

View file

@ -48,11 +48,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "VerifyIPAMChoices" -Fixture {
#It "Should return a valid integer"
}
Context -Name "Get-NetboxIPAMAggregate" -Fixture {
It "Should request the default number of aggregates" {
$Result = Get-NetboxIPAMAggregate
@ -388,18 +384,18 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
}
Context -Name "Remove-NetboxIPAMAddress" -Fixture {
It "Should remove a single IP" {
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
return @{
'address' = '10.1.1.1/24'
'id' = 4109
}
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
return @{
'address' = "10.1.1.1/$Id"
'id' = $id
}
$Result = Remove-NetboxIPAMAddress -Id '4109' -Force
}
It "Should remove a single IP" {
$Result = Remove-NetboxIPAMAddress -Id 4109 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'DELETE'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
@ -407,24 +403,43 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Body | Should -Be $null
}
It "Should remove multiple IPs" {
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
return @(
@{
'address' = '10.1.1.1/24'
'id' = 4109
},
@{
'address' = '10.1.1.2/24'
'id' = 4110
}
)
}
It "Should remove a single IP from the pipeline" {
$Result = [pscustomobject]@{
'id' = 4110
} | Remove-NetboxIPAMAddress -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'DELETE'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4110/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be $null
}
It "Should remove multiple IPs" {
$Result = Remove-NetboxIPAMAddress -Id 4109, 4110 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2 -Scope 'It' -Exactly
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/', 'https://netbox.domain.com/api/ipam/ip-addresses/4110/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
It "Should remove multiple IPs from the pipeline" {
$Result = @(
[pscustomobject]@{
'id' = 4109
},
[pscustomobject]@{
'id' = 4110
}
) | Remove-NetboxIPAMAddress -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2 -Scope 'It' -Exactly
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/', 'https://netbox.domain.com/api/ipam/ip-addresses/4110/'
@ -433,18 +448,18 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
}
Context -Name "Set-NetboxIPAMAddress" -Fixture {
It "Should set an IP with a new status" {
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
return @{
'address' = '10.1.1.1/24'
'id' = 4109
}
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
return @{
'address' = '10.1.1.1/24'
'id' = $id
}
$Result = Set-NetboxIPAMAddress -Id '4109' -Status 2 -Force
}
It "Should set an IP with a new status" {
$Result = Set-NetboxIPAMAddress -Id 4109 -Status 2 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope "It" -Exactly
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
@ -452,17 +467,62 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Body | Should -Be '{"status":2}'
}
It "Should set an IP with VRF, Tenant, and Description" {
$Result = Set-NetboxIPAMAddress -Id 4109 -VRF 10 -Tenant 14 -Description 'Test description' -Force
It "Should set an IP from the pipeline" {
$Result = [pscustomobject]@{
'Id' = 4501
} | Set-NetboxIPAMAddress -VRF 10 -Tenant 14 -Description 'Test description' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope "It" -Exactly
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4501/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"vrf":10,"description":"Test description","tenant":14}'
}
It "Should set mulitple IPs to a new status" {
$Result = Set-NetboxIPAMAddress -Id 4109, 4555 -Status 2 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2 -Scope "It" -Exactly
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/', 'https://netbox.domain.com/api/ipam/ip-addresses/4555/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"status":2}', '{"status":2}'
}
It "Should set an IP with VRF, Tenant, and Description" {
$Result = Set-NetboxIPAMAddress -Id 4110 -VRF 10 -Tenant 14 -Description 'Test description' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope "It" -Exactly
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4110/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"vrf":10,"description":"Test description","tenant":14}'
}
It "Should set multiple IPs from the pipeline" {
$Result = @(
[pscustomobject]@{
'Id' = 4501
},
[pscustomobject]@{
'Id' = 4611
}
) | Set-NetboxIPAMAddress -Status 2 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2 -Scope "It" -Exactly
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4501/', 'https://netbox.domain.com/api/ipam/ip-addresses/4611/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"status":2}', '{"status":2}'
}
}
}
}

View file

@ -128,6 +128,10 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/?status=1'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should throw for an invalid status" {
{ Get-NetboxVirtualMachine -Status 'Fake' } | Should -Throw
}
}
Context -Name "Get-NetboxVirtualMachineInterface" -Fixture {
@ -334,9 +338,9 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
}
}
Context -Name "Add-NetboxVirtualInterface" -Fixture {
Context -Name "Add-NetboxVirtualMachineInterface" -Fixture {
It "Should add a basic interface" {
$Result = Add-NetboxVirtualInterface -Name 'Ethernet0' -Virtual_Machine 10
$Result = Add-NetboxVirtualMachineInterface -Name 'Ethernet0' -Virtual_Machine 10
Assert-VerifiableMock
@ -347,7 +351,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
}
It "Should add an interface with a MAC, MTU, and Description" {
$Result = Add-NetboxVirtualInterface -Name 'Ethernet0' -Virtual_Machine 10 -Mac_Address '11:22:33:44:55:66' -MTU 1500 -Description "Test description"
$Result = Add-NetboxVirtualMachineInterface -Name 'Ethernet0' -Virtual_Machine 10 -Mac_Address '11:22:33:44:55:66' -MTU 1500 -Description "Test description"
Assert-VerifiableMock
@ -358,18 +362,20 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
}
}
Context -Name "Set-NetboxVirtualMachine" -Fixture {
Mock -CommandName "Get-NetboxVirtualMachine" -ModuleName NetboxPS -MockWith {
return @{
'Id' = 1234
'Name' = 'TestVM'
}
Mock -CommandName "Get-NetboxVirtualMachine" -ModuleName NetboxPS -MockWith {
return [pscustomobject]@{
'Id' = $Id
'Name' = $Name
}
}
Context -Name "Set-NetboxVirtualMachine" -Fixture {
It "Should set a VM to a new name" {
$Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/'
@ -381,6 +387,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Cluster 10 -Platform 15 -Status 'Offline' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/'
@ -392,17 +399,19 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
{ Set-NetboxVirtualMachine -Id 1234 -Status 'Fake' -Force } | Should -Throw
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 0 -Exactly -Scope 'It'
}
}
Mock -CommandName "Get-NetboxVirtualMachineInterface" -ModuleName NetboxPS -MockWith {
return [pscustomobject]@{
'Id' = $Id
'Name' = $Name
}
}
Context -Name "Set-NetboxVirtualMachineInterface" -Fixture {
Mock -CommandName "Get-NetboxVirtualMachineInterface" -ModuleName NetboxPS -MockWith {
return @{
'Id' = 1234
'Name' = 'TestVM'
}
}
It "Should set an interface to a new name" {
$Result = Set-NetboxVirtualMachineInterface -Id 1234 -Name 'newtestname' -Force
@ -437,15 +446,6 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
}
It "Should set multiple interfaces to a new name" {
Mock -CommandName "Get-NetboxVirtualMachineInterface" -ModuleName NetboxPS -MockWith {
return @(
@{
'Id' = $Id
'Name' = 'TestVM'
}
)
}
$Result = Set-NetboxVirtualMachineInterface -Id 1234, 4321 -Name 'newtestname' -Force
Assert-VerifiableMock
@ -456,6 +456,78 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"name":"newtestname"}', '{"name":"newtestname"}'
}
It "Should set multiple interfaces to a new name from the pipeline" {
$Result = @(
[pscustomobject]@{
'Id' = 4123
},
[pscustomobject]@{
'Id' = 4321
}
) | Set-NetboxVirtualMachineInterface -Name 'newtestname' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName Get-NetboxVirtualMachineInterface -Times 2 -Scope 'It' -Exactly
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/4123/', 'https://netbox.domain.com/api/virtualization/interfaces/4321/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"name":"newtestname"}', '{"name":"newtestname"}'
}
}
Context -Name "Remove-NetboxVirtualMachine" -Fixture {
It "Should remove a single VM" {
$Result = Remove-NetboxVirtualMachine -Id 4125 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/4125/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should remove mulitple VMs" {
$Result = Remove-NetboxVirtualMachine -Id 4125, 4132 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/4125/', 'https://netbox.domain.com/api/virtualization/virtual-machines/4132/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
It "Should remove a VM from the pipeline" {
$Result = Get-NetboxVirtualMachine -Id 4125 | Remove-NetboxVirtualMachine -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/4125/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should remove multiple VMs from the pipeline" {
$Result = @(
[pscustomobject]@{
'Id' = 4125
},
[pscustomobject]@{
'Id' = 4132
}
) | Remove-NetboxVirtualMachine -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/4125/', 'https://netbox.domain.com/api/virtualization/virtual-machines/4132/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
}
}
}