mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-13 18:02:29 +00:00
Removing old files
This commit is contained in:
parent
895b16450a
commit
53a64915b5
5 changed files with 0 additions and 3484 deletions
|
|
@ -1,127 +0,0 @@
|
|||
<#
|
||||
===========================================================================
|
||||
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.148
|
||||
Created on: 2/28/2018 11:04 AM
|
||||
Created by: Ben Claussen
|
||||
Organization: NEOnet
|
||||
Filename: NetboxPS.psd1
|
||||
-------------------------------------------------------------------------
|
||||
Module Manifest
|
||||
-------------------------------------------------------------------------
|
||||
Module Name: NetboxPS
|
||||
===========================================================================
|
||||
#>
|
||||
|
||||
|
||||
@{
|
||||
|
||||
# Script module or binary module file associated with this manifest
|
||||
ModuleToProcess = 'NetboxPS.psm1'
|
||||
|
||||
# Version number of this module.
|
||||
ModuleVersion = '1.0.0.0'
|
||||
|
||||
# ID used to uniquely identify this module
|
||||
GUID = 'bba9b06c-49c8-47cf-8358-aca7c4e78896'
|
||||
|
||||
# Author of this module
|
||||
Author = 'Ben Claussen'
|
||||
|
||||
# Company or vendor of this module
|
||||
CompanyName = 'NEOnet'
|
||||
|
||||
# Copyright statement for this module
|
||||
Copyright = '(c) 2018. All rights reserved.'
|
||||
|
||||
# Description of the functionality provided by this module
|
||||
Description = 'A Powershell wrapper for Netbox API'
|
||||
|
||||
# Minimum version of the Windows PowerShell engine required by this module
|
||||
PowerShellVersion = '5.0'
|
||||
|
||||
# Name of the Windows PowerShell host required by this module
|
||||
PowerShellHostName = ''
|
||||
|
||||
# Minimum version of the Windows PowerShell host required by this module
|
||||
PowerShellHostVersion = ''
|
||||
|
||||
# Minimum version of the .NET Framework required by this module
|
||||
DotNetFrameworkVersion = '2.0'
|
||||
|
||||
# Minimum version of the common language runtime (CLR) required by this module
|
||||
CLRVersion = '2.0.50727'
|
||||
|
||||
# Processor architecture (None, X86, Amd64, IA64) required by this module
|
||||
ProcessorArchitecture = 'None'
|
||||
|
||||
# Modules that must be imported into the global environment prior to importing
|
||||
# this module
|
||||
RequiredModules = @()
|
||||
|
||||
# Assemblies that must be loaded prior to importing this module
|
||||
RequiredAssemblies = @('System.Web')
|
||||
|
||||
# Script files (.ps1) that are run in the caller's environment prior to
|
||||
# importing this module
|
||||
ScriptsToProcess = @()
|
||||
|
||||
# Type files (.ps1xml) to be loaded when importing this module
|
||||
TypesToProcess = @()
|
||||
|
||||
# Format files (.ps1xml) to be loaded when importing this module
|
||||
FormatsToProcess = @()
|
||||
|
||||
# Modules to import as nested modules of the module specified in
|
||||
# ModuleToProcess
|
||||
NestedModules = @()
|
||||
|
||||
# Functions to export from this module
|
||||
FunctionsToExport = '*' #For performanace, list functions explicity
|
||||
|
||||
# Cmdlets to export from this module
|
||||
CmdletsToExport = '*'
|
||||
|
||||
# Variables to export from this module
|
||||
VariablesToExport = '*'
|
||||
|
||||
# Aliases to export from this module
|
||||
AliasesToExport = '*' #For performanace, list alias explicity
|
||||
|
||||
# List of all modules packaged with this module
|
||||
ModuleList = @()
|
||||
|
||||
# List of all files packaged with this module
|
||||
FileList = @()
|
||||
|
||||
# Private data to pass to the module specified in ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
|
||||
PrivateData = @{
|
||||
|
||||
#Support for PowerShellGet galleries.
|
||||
PSData = @{
|
||||
|
||||
# Tags applied to this module. These help with module discovery in online galleries.
|
||||
Tags = @('Netbox', 'API', 'DCIM', 'IPAM')
|
||||
|
||||
# A URL to the license for this module.
|
||||
# LicenseUri = ''
|
||||
|
||||
# A URL to the main website for this project.
|
||||
ProjectUri = 'https://github.com/benclaussen/NetboxPS'
|
||||
|
||||
# A URL to an icon representing this module.
|
||||
# IconUri = ''
|
||||
|
||||
# ReleaseNotes of this module
|
||||
# ReleaseNotes = ''
|
||||
|
||||
} # End of PSData hashtable
|
||||
|
||||
} # End of PrivateData hashtable
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,481 +0,0 @@
|
|||
<#
|
||||
.NOTES
|
||||
===========================================================================
|
||||
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.150
|
||||
Created on: 5/8/2018 11:36 AM
|
||||
Created by: Ben Claussen
|
||||
Organization: NEOnet
|
||||
Filename: Helpers.Tests.ps1
|
||||
===========================================================================
|
||||
.DESCRIPTION
|
||||
Helper functions Pester 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 "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
|
||||
It "Should throw because we are not connected" {
|
||||
{
|
||||
Check-NetboxIsConnected
|
||||
} | Should -Throw
|
||||
}
|
||||
|
||||
Mock -CommandName 'CheckNetboxIsConnected' -MockWith {
|
||||
return $true
|
||||
} -ModuleName 'NetboxPS'
|
||||
|
||||
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
|
||||
Context -Name "Building URIBuilder" -Fixture {
|
||||
It "Should give a basic URI object" {
|
||||
BuildNewURI -HostName 'netbox.domain.com' | Should -BeOfType [System.UriBuilder]
|
||||
}
|
||||
|
||||
It "Should generate a URI using only a supplied hostname" {
|
||||
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com"
|
||||
$URIBuilder.Host | Should -BeExactly 'netbox.domain.com'
|
||||
$URIBuilder.Path | Should -BeExactly 'api//'
|
||||
$URIBuilder.Scheme | Should -Be 'https'
|
||||
$URIBuilder.Port | Should -Be 443
|
||||
$URIBuilder.URI.AbsoluteUri | Should -Be 'https://netbox.domain.com/api//'
|
||||
}
|
||||
|
||||
It "Should generate a URI using a hostname and segments" {
|
||||
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2'
|
||||
$URIBuilder.Host | Should -BeExactly 'netbox.domain.com'
|
||||
$URIBuilder.Path | Should -BeExactly 'api/seg1/seg2/'
|
||||
$URIBuilder.URI.AbsoluteUri | Should -BeExactly 'https://netbox.domain.com/api/seg1/seg2/'
|
||||
}
|
||||
|
||||
It "Should generate a URI using insecure HTTP and default to port 80" {
|
||||
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2' -HTTPS $false -WarningAction 'SilentlyContinue'
|
||||
$URIBuilder.Scheme | Should -Be 'http'
|
||||
$URIBuilder.Port | Should -Be 80
|
||||
$URIBuilder.URI.AbsoluteURI | Should -Be 'http://netbox.domain.com/api/seg1/seg2/'
|
||||
}
|
||||
|
||||
It "Should generate a URI using HTTPS on port 1234" {
|
||||
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2' -Port 1234
|
||||
$URIBuilder.Scheme | Should -Be 'https'
|
||||
$URIBuilder.Port | Should -Be 1234
|
||||
$URIBuilder.URI.AbsoluteURI | Should -BeExactly 'https://netbox.domain.com:1234/api/seg1/seg2/'
|
||||
}
|
||||
|
||||
It "Should generate a URI using HTTP on port 4321" {
|
||||
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2' -HTTPS $false -Port 4321 -WarningAction 'SilentlyContinue'
|
||||
$URIBuilder.Scheme | Should -Be 'http'
|
||||
$URIBuilder.Port | Should -Be 4321
|
||||
$URIBuilder.URI.AbsoluteURI | Should -BeExactly 'http://netbox.domain.com:4321/api/seg1/seg2/'
|
||||
}
|
||||
|
||||
It "Should generate a URI with parameters" {
|
||||
$URIParameters = @{
|
||||
'param1' = 'paramval1'
|
||||
'param2' = 'paramval2'
|
||||
}
|
||||
|
||||
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2' -Parameters $URIParameters
|
||||
$URIBuilder.Query | Should -BeExactly '?param1=paramval1¶m2=paramval2'
|
||||
$URIBuilder.URI.AbsoluteURI | Should -BeExactly 'https://netbox.domain.com/api/seg1/seg2/?param1=paramval1¶m2=paramval2'
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Building URI components" -Fixture {
|
||||
It "Should give a basic hashtable" {
|
||||
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'param1' = 1}
|
||||
|
||||
$URIComponents | Should -BeOfType [hashtable]
|
||||
$URIComponents.Keys.Count | Should -BeExactly 2
|
||||
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
|
||||
$URIComponents.Segments | Should -Be @("segment1", "segment2")
|
||||
$URIComponents.Parameters.Count | Should -BeExactly 1
|
||||
$URIComponents.Parameters | Should -BeOfType [hashtable]
|
||||
$URIComponents.Parameters['param1'] | Should -Be 1
|
||||
}
|
||||
|
||||
It "Should add a single ID parameter to the segments" {
|
||||
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'id' = 123}
|
||||
|
||||
$URIComponents | Should -BeOfType [hashtable]
|
||||
$URIComponents.Keys.Count | Should -BeExactly 2
|
||||
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
|
||||
$URIComponents.Segments | Should -Be @("segment1", "segment2", '123')
|
||||
$URIComponents.Parameters.Count | Should -BeExactly 0
|
||||
$URIComponents.Parameters | Should -BeOfType [hashtable]
|
||||
}
|
||||
|
||||
It "Should add multiple IDs to the parameters id__in" {
|
||||
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'id' = "123", "456"}
|
||||
|
||||
$URIComponents | Should -BeOfType [hashtable]
|
||||
$URIComponents.Keys.Count | Should -BeExactly 2
|
||||
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
|
||||
$URIComponents.Segments | Should -Be @("segment1", "segment2")
|
||||
$URIComponents.Parameters.Count | Should -BeExactly 1
|
||||
$URIComponents.Parameters | Should -BeOfType [hashtable]
|
||||
$URIComponents.Parameters['id__in'] | Should -Be '123,456'
|
||||
}
|
||||
|
||||
It "Should skip a particular parameter name" {
|
||||
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'param1' = 1; 'param2' = 2} -SkipParameterByName 'param2'
|
||||
|
||||
$URIComponents | Should -BeOfType [hashtable]
|
||||
$URIComponents.Keys.Count | Should -BeExactly 2
|
||||
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
|
||||
$URIComponents.Segments | Should -Be @("segment1", "segment2")
|
||||
$URIComponents.Parameters.Count | Should -BeExactly 1
|
||||
$URIComponents.Parameters | Should -BeOfType [hashtable]
|
||||
$URIComponents.Parameters['param1'] | Should -Be 1
|
||||
$URIComponents.Parameters['param2'] | Should -BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should add a query (q) parameter" {
|
||||
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'query' = 'mytestquery'}
|
||||
|
||||
$URIComponents | Should -BeOfType [hashtable]
|
||||
$URIComponents.Keys.Count | Should -BeExactly 2
|
||||
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
|
||||
$URIComponents.Segments | Should -Be @("segment1", "segment2")
|
||||
$URIComponents.Parameters.Count | Should -BeExactly 1
|
||||
$URIComponents.Parameters | Should -BeOfType [hashtable]
|
||||
$URIComponents.Parameters['q'] | Should -Be 'mytestquery'
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Invoking request tests" -Fixture {
|
||||
Mock -CommandName 'Invoke-RestMethod' -Verifiable -MockWith {
|
||||
# Return an object of the items we would normally pass to Invoke-RestMethod
|
||||
return [pscustomobject]@{
|
||||
'Method' = $Method
|
||||
'Uri' = $Uri
|
||||
'Headers' = $Headers
|
||||
'Timeout' = $Timeout
|
||||
'ContentType' = $ContentType
|
||||
'Body' = $Body
|
||||
'results' = 'Only results'
|
||||
}
|
||||
}
|
||||
|
||||
Mock -CommandName 'Get-NetboxCredentials' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
||||
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
||||
}
|
||||
|
||||
It "Should return direct results instead of the raw request" {
|
||||
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2'
|
||||
|
||||
$Result = InvokeNetboxRequest -URI $URIBuilder
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result | Should -BeOfType [string]
|
||||
$Result | Should -BeExactly "Only results"
|
||||
}
|
||||
|
||||
It "Should generate a basic request" {
|
||||
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2'
|
||||
|
||||
$Result = InvokeNetboxRequest -URI $URIBuilder -Raw
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be $URIBuilder.Uri.AbsoluteUri
|
||||
$Result.Headers | Should -BeOfType [System.Collections.HashTable]
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
$Result.Timeout | Should -Be 5
|
||||
$Result.ContentType | Should -Be 'application/json'
|
||||
$Result.Body | Should -Be $null # We did not supply a body
|
||||
}
|
||||
|
||||
It "Should generate a POST request with body" {
|
||||
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2'
|
||||
|
||||
$Result = InvokeNetboxRequest -URI $URIBuilder -Method POST -Body @{
|
||||
'bodyparam1' = 'val1'
|
||||
} -Raw
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'POST'
|
||||
$Result.Body | Should -Be '{"bodyparam1":"val1"}'
|
||||
}
|
||||
|
||||
It "Should generate a POST request with an extra header" {
|
||||
$Headers = @{
|
||||
'Connection' = 'keep-alive'
|
||||
}
|
||||
|
||||
$Body = @{
|
||||
'bodyparam1' = 'val1'
|
||||
}
|
||||
|
||||
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2'
|
||||
|
||||
$Result = InvokeNetboxRequest -URI $URIBuilder -Method POST -Body $Body -Headers $Headers -Raw
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'POST'
|
||||
$Result.Body | Should -Be '{"bodyparam1":"val1"}'
|
||||
$Result.Headers.Count | Should -BeExactly 2
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
$Result.Headers.Connection | Should -Be "keep-alive"
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid method" {
|
||||
{
|
||||
$URI = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2'
|
||||
InvokeNetboxRequest -URI $URI -Method 'Fake'
|
||||
} | Should -Throw
|
||||
}
|
||||
|
||||
It "Should throw because of an out-of-range timeout" {
|
||||
{
|
||||
$URI = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2'
|
||||
InvokeNetboxRequest -URI $URI -Timeout 61
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
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'
|
||||
|
||||
It "Should return a valid integer for status when provided a name" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 'Active'
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should return a valid integer for status when provided an integer" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 0
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 0
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid choice" {
|
||||
{
|
||||
ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 'Fake'
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "IPAM choices" -Fixture {
|
||||
$MajorObject = 'IPAM'
|
||||
|
||||
Context -Name "aggregate:family" -Fixture {
|
||||
$ChoiceName = 'aggregate:family'
|
||||
|
||||
It "Should return a valid integer when provided a name" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 4
|
||||
}
|
||||
|
||||
It "Should return a valid integer when provided an integer" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 4
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid choice" {
|
||||
{
|
||||
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "prefix:family" {
|
||||
$ChoiceName = 'prefix:family'
|
||||
|
||||
It "Should return a valid integer when provided a name" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 4
|
||||
}
|
||||
|
||||
It "Should return a valid integer when provided an integer" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 4
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid choice" {
|
||||
{
|
||||
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "prefix:status" {
|
||||
$ChoiceName = 'prefix: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 1
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid choice" {
|
||||
{
|
||||
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "ip-address:family" {
|
||||
$ChoiceName = 'ip-address:family'
|
||||
|
||||
It "Should return a valid integer when provided a name" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 4
|
||||
}
|
||||
|
||||
It "Should return a valid integer when provided an integer" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 4
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid choice" {
|
||||
{
|
||||
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "ip-address:status" {
|
||||
$ChoiceName = 'ip-address: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 1
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid choice" {
|
||||
{
|
||||
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "ip-address:role" {
|
||||
$ChoiceName = 'ip-address:role'
|
||||
|
||||
It "Should return a valid integer when provided a name" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Anycast'
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 30
|
||||
}
|
||||
|
||||
It "Should return a valid integer when provided an integer" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 30
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 30
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid choice" {
|
||||
{
|
||||
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "vlan:status" {
|
||||
$ChoiceName = 'vlan: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 1
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid choice" {
|
||||
{
|
||||
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "service:protocol" {
|
||||
$ChoiceName = 'service:protocol'
|
||||
|
||||
It "Should return a valid integer when provided a name" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'TCP'
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 6
|
||||
}
|
||||
|
||||
It "Should return a valid integer when provided an integer" {
|
||||
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 6
|
||||
|
||||
$Result | Should -BeOfType [uint16]
|
||||
$Result | Should -BeExactly 6
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid choice" {
|
||||
{
|
||||
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,478 +0,0 @@
|
|||
<#
|
||||
.NOTES
|
||||
===========================================================================
|
||||
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.150
|
||||
Created on: 5/10/2018 3:41 PM
|
||||
Created by: Ben Claussen
|
||||
Organization: NEOnet
|
||||
Filename: IPAM.Tests.ps1
|
||||
===========================================================================
|
||||
.DESCRIPTION
|
||||
IPAM Pester 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 "IPAM tests" -Tag 'Ipam' -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-NetboxCredentials' -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.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
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with limit and offset" {
|
||||
$Result = Get-NetboxIPAMAggregate -Limit 10 -Offset 12
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?offset=12&limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with a query" {
|
||||
$Result = Get-NetboxIPAMAggregate -Query '10.10.0.0'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?q=10.10.0.0'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with an escaped query" {
|
||||
$Result = Get-NetboxIPAMAggregate -Query 'my aggregate'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?q=my+aggregate'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with a single ID" {
|
||||
$Result = Get-NetboxIPAMAggregate -Id 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/10/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with multiple IDs" {
|
||||
$Result = Get-NetboxIPAMAggregate -Id 10, 12, 15
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?id__in=10,12,15'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Get-NetboxIPAMAddress" -Fixture {
|
||||
It "Should request the default number of addresses" {
|
||||
$Result = Get-NetboxIPAMAddress
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with limit and offset" {
|
||||
$Result = Get-NetboxIPAMAddress -Limit 10 -Offset 12
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?offset=12&limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with a query" {
|
||||
$Result = Get-NetboxIPAMAddress -Query '10.10.10.10'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?q=10.10.10.10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with an escaped query" {
|
||||
$Result = Get-NetboxIPAMAddress -Query 'my ip address'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?q=my+ip+address'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with a single ID" {
|
||||
$Result = Get-NetboxIPAMAddress -Id 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/10/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with multiple IDs" {
|
||||
$Result = Get-NetboxIPAMAddress -Id 10, 12, 15
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?id__in=10,12,15'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with a family number" {
|
||||
$Result = Get-NetboxIPAMAddress -Family 4
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?family=4'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a family name" {
|
||||
$Result = Get-NetboxIPAMAddress -Family 'IPv4'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?family=4'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Get-NetboxIPAMAvailableIP" -Fixture {
|
||||
It "Should request the default number of available IPs" {
|
||||
$Result = Get-NetboxIPAMAvailableIP -Prefix_Id 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/10/available-ips/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request 10 available IPs" {
|
||||
$Result = Get-NetboxIPAMAvailableIP -Prefix_Id 1504 -NumberOfIPs 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/1504/available-ips/?limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Get-NetboxIPAMPrefix" -Fixture {
|
||||
It "Should request the default number of prefixes" {
|
||||
$Result = Get-NetboxIPAMPrefix
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with limit and offset" {
|
||||
$Result = Get-NetboxIPAMPrefix -Limit 10 -Offset 12
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?offset=12&limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with a query" {
|
||||
$Result = Get-NetboxIPAMPrefix -Query '10.10.10.10'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?q=10.10.10.10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with an escaped query" {
|
||||
$Result = Get-NetboxIPAMPrefix -Query 'my ip address'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?q=my+ip+address'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with a single ID" {
|
||||
$Result = Get-NetboxIPAMPrefix -Id 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/10/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with multiple IDs" {
|
||||
$Result = Get-NetboxIPAMPrefix -Id 10, 12, 15
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?id__in=10,12,15'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with VLAN vID" {
|
||||
$Result = Get-NetboxIPAMPrefix -VLAN_VID 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?vlan_vid=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should request with family of 4" {
|
||||
$Result = Get-NetboxIPAMPrefix -Family 4
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?family=4'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
|
||||
It "Should throw because the mask length is too large" {
|
||||
{
|
||||
Get-NetboxIPAMPrefix -Mask_length 128
|
||||
} | Should -Throw
|
||||
}
|
||||
|
||||
It "Should throw because the mask length is too small" {
|
||||
{
|
||||
Get-NetboxIPAMPrefix -Mask_length -1
|
||||
} | Should -Throw
|
||||
}
|
||||
|
||||
It "Should not throw because the mask length is just right" {
|
||||
{
|
||||
Get-NetboxIPAMPrefix -Mask_length 24
|
||||
} | Should -Not -Throw
|
||||
}
|
||||
|
||||
It "Should request with mask length 24" {
|
||||
$Result = Get-NetboxIPAMPrefix -Mask_length 24
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?mask_length=24'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Headers.Authorization | Should -Be "Token faketoken"
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Add-NetboxIPAMAddress" -Fixture {
|
||||
It "Should add a basic IP address" {
|
||||
$Result = Add-NetboxIPAMAddress -Address '10.0.0.1/24'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'POST'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"status":1,"address":"10.0.0.1/24"}'
|
||||
}
|
||||
|
||||
It "Should add an IP with a status and role names" {
|
||||
$Result = Add-NetboxIPAMAddress -Address '10.0.0.1/24' -Status 'Reserved' -Role 'Anycast'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'POST'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"status":2,"address":"10.0.0.1/24","role":30}'
|
||||
}
|
||||
|
||||
It "Should add an IP with a status and role values" {
|
||||
$Result = Add-NetboxIPAMAddress -Address '10.0.1.1/24' -Status '1' -Role '10'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'POST'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"status":1,"address":"10.0.1.1/24","role":10}'
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
$Result = Remove-NetboxIPAMAddress -Id '4109' -Force
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1
|
||||
|
||||
$Result.Method | Should -Be 'DELETE'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$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
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
$Result = Remove-NetboxIPAMAddress -Id 4109, 4110 -Force
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2
|
||||
|
||||
$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
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
$Result = Set-NetboxIPAMAddress -Id '4109' -Status 2 -Force
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1
|
||||
|
||||
$Result.Method | Should -Be 'PATCH'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$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
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1
|
||||
|
||||
$Result.Method | Should -Be 'PATCH'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"vrf":10,"description":"Test description","tenant":14}'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,404 +0,0 @@
|
|||
<#
|
||||
.NOTES
|
||||
===========================================================================
|
||||
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.150
|
||||
Created on: 5/8/2018 4:01 PM
|
||||
Created by: Ben Claussen
|
||||
Organization: NEOnet
|
||||
Filename: Virtualization.Tests.ps1
|
||||
===========================================================================
|
||||
.DESCRIPTION
|
||||
Virtualization Pester 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 "Virtualization tests" -Tag 'Virtualization' -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-NetboxCredentials' -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.Virtualization = (Get-Content "$PSScriptRoot\VirtualizationChoices.json" -ErrorAction Stop | ConvertFrom-Json)
|
||||
|
||||
Context -Name "Get-NetboxVirtualMachine" -Fixture {
|
||||
It "Should request the default number of VMs" {
|
||||
$Result = Get-NetboxVirtualMachine
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with limit and offset" {
|
||||
$Result = Get-NetboxVirtualMachine -Limit 10 -Offset 12
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/?offset=12&limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a query" {
|
||||
$Result = Get-NetboxVirtualMachine -Query 'testvm'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/?q=testvm'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with an escaped query" {
|
||||
$Result = Get-NetboxVirtualMachine -Query 'test vm'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/?q=test+vm'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a name" {
|
||||
$Result = Get-NetboxVirtualMachine -Name 'testvm'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/?name=testvm'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a single ID" {
|
||||
$Result = Get-NetboxVirtualMachine -Id 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/10/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with multiple IDs" {
|
||||
$Result = Get-NetboxVirtualMachine -Id 10, 12, 15
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/?id__in=10,12,15'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request a status" {
|
||||
$Result = Get-NetboxVirtualMachine -Status 'Active'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/?status=1'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Get-NetboxVirtualMachineInterface" -Fixture {
|
||||
It "Should request the default number of interfaces" {
|
||||
$Result = Get-NetboxVirtualMachineInterface
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a limit and offset" {
|
||||
$Result = Get-NetboxVirtualMachineInterface -Limit 10 -Offset 12
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/?offset=12&limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request a interface with a specific ID" {
|
||||
$Result = Get-NetboxVirtualMachineInterface -Id 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/10/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request a name" {
|
||||
$Result = Get-NetboxVirtualMachineInterface -Name 'Ethernet0'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/?name=Ethernet0'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a VM ID" {
|
||||
$Result = Get-NetboxVirtualMachineInterface -Virtual_Machine_Id 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/?virtual_machine_id=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with Enabled" {
|
||||
$Result = Get-NetboxVirtualMachineInterface -Enabled $true
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/?enabled=true'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Get-NetboxVirtualMachineCluster" -Fixture {
|
||||
It "Should request the default number of clusters" {
|
||||
$Result = Get-NetboxVirtualizationCluster
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/clusters/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with limit and offset" {
|
||||
$Result = Get-NetboxVirtualizationCluster -Limit 10 -Offset 12
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/clusters/?offset=12&limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a query" {
|
||||
$Result = Get-NetboxVirtualizationCluster -Query 'testcluster'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/clusters/?q=testcluster'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with an escaped query" {
|
||||
$Result = Get-NetboxVirtualizationCluster -Query 'test cluster'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/clusters/?q=test+cluster'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a name" {
|
||||
$Result = Get-NetboxVirtualizationCluster -Name 'testcluster'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/clusters/?name=testcluster'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a single ID" {
|
||||
$Result = Get-NetboxVirtualizationCluster -Id 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/clusters/10/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with multiple IDs" {
|
||||
$Result = Get-NetboxVirtualizationCluster -Id 10, 12, 15
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/clusters/?id__in=10,12,15'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Get-NetboxVirtualMachineClusterGroup" -Fixture {
|
||||
It "Should request the default number of cluster groups" {
|
||||
$Result = Get-NetboxVirtualizationClusterGroup
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/cluster-groups/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with limit and offset" {
|
||||
$Result = Get-NetboxVirtualizationClusterGroup -Limit 10 -Offset 12
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/cluster-groups/?offset=12&limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a name" {
|
||||
$Result = Get-NetboxVirtualizationClusterGroup -Name 'testclustergroup'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/cluster-groups/?name=testclustergroup'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a slug" {
|
||||
$Result = Get-NetboxVirtualizationClusterGroup -Slug 'test-cluster-group'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/cluster-groups/?slug=test-cluster-group'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Add-NetboxVirtualMachine" -Fixture {
|
||||
It "Should add a basic VM" {
|
||||
$Result = Add-NetboxVirtualMachine -Name 'testvm' -Cluster 1
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'POST'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"cluster":1,"name":"testvm","status":1}'
|
||||
}
|
||||
|
||||
It "Should add a VM with CPUs, Memory, Disk, tenancy, and comments" {
|
||||
$Result = Add-NetboxVirtualMachine -Name 'testvm' -Cluster 1 -Status Active -vCPUs 4 -Memory 4096 -Tenant 11 -Disk 50 -Comments "these are comments"
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'POST'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"tenant":11,"comments":"these are comments","disk":50,"memory":4096,"name":"testvm","cluster":1,"status":1,"vcpus":4}'
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Add-NetboxVirtualInterface" -Fixture {
|
||||
It "Should add a basic interface" {
|
||||
$Result = Add-NetboxVirtualInterface -Name 'Ethernet0' -Virtual_Machine 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'POST'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"virtual_machine":10,"name":"Ethernet0","enabled":true}'
|
||||
}
|
||||
|
||||
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"
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'POST'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"mtu":1500,"description":"Test description","enabled":true,"virtual_machine":10,"name":"Ethernet0","mac_address":"11:22:33:44:55:66"}'
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Set-NetboxVirtualMachine" -Fixture {
|
||||
Mock -CommandName "Get-NetboxVirtualMachine" -ModuleName NetboxPS -MockWith {
|
||||
return @{
|
||||
'Id' = 1234
|
||||
'Name' = 'TestVM'
|
||||
}
|
||||
}
|
||||
|
||||
It "Should set a VM to a new name" {
|
||||
$Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Force
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'PATCH'
|
||||
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"name":"newtestname"}'
|
||||
}
|
||||
|
||||
It "Should set a VM with a new name, cluster, platform, and status" {
|
||||
$Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Cluster 10 -Platform 15 -Status 'Offline' -Force
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'PATCH'
|
||||
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"cluster":10,"platform":15,"name":"newtestname","status":0}'
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid status" {
|
||||
{ Set-NetboxVirtualMachine -Id 1234 -Status 'Fake' -Force } | Should -Throw
|
||||
|
||||
Assert-VerifiableMock
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Reference in a new issue