PSSubnetCarver.Tests.ps1
Describe 'Set-SCContext' { It 'Sets a context named "default" when no -Name is supplied' { Set-SCContext -RootAddressSpace '10.0.0.0/8' Get-SCContext -Name default | Should -Not -BeNullOrEmpty } It 'Sets a context with the -Name supplied' { Set-SCContext -Name Subnet1 -RootAddressSpace '10.0.0.0/8' Get-SCContext -Name Subnet1 | Should -Not -BeNullOrEmpty } It 'Sets the RootAddressSpace accurately' { Set-SCContext -RootAddressSpace '10.0.0.0/8' Get-SCContext -Name default | Select-Object -ExpandProperty RootIPAddressRange | Should -Be ([qIPAM.Models.IPAddressRange]'10.0.0.0/8') } It 'Should not have a case-sensitive context name' { Set-SCContext -RootAddressSpace '10.0.0.0/8' -Name DEFAULT Get-SCContext -Name default | Should -Not -BeNullOrEmpty } } Describe 'Get-SCSubnet' { BeforeAll { Set-SCContext -RootAddressSpace '10.0.0.0/8' Set-SCContext -Name Subnet1 -RootAddressSpace '10.0.0.0/16' Set-SCContext -Name Subnet2 -RootAddressSpace '10.1.0.0/16' Set-SCContext -Name Subnet3 -RootAddressSpace '10.2.0.0/16' } It 'Should reserve from the "default" context if no name is supplied' { Get-SCSubnet -ReserveCIDR 16 | Should -Be ([qIPAM.Models.IPAddressRange]'10.0.0.0/16') } It 'Should reserve from the named context if a -Name is provided' { Get-SCSubnet -ReserveCIDR 24 -ContextName Subnet1 | Should -Be ([qIPAM.Models.IPAddressRange]'10.0.0.0/24') Get-SCSubnet -ReserveCIDR 24 -ContextName Subnet2 | Should -Be ([qIPAM.Models.IPAddressRange]'10.1.0.0/24') Get-SCSubnet -ReserveCIDR 24 -ContextName Subnet3 | Should -Be ([qIPAM.Models.IPAddressRange]'10.2.0.0/24') } It 'Should reserve the specified IP address range when -ReserveIPAddress is used' { Get-SCSubnet -ReserveIPAddress '10.0.2.0/24' -ContextName Subnet1 | Should -Be ([qIPAM.Models.IPAddressRange]'10.0.2.0/24') Get-SCSubnet -ReserveIPAddress '10.1.2.0/24' -ContextName Subnet2 | Should -Be ([qIPAM.Models.IPAddressRange]'10.1.2.0/24') Get-SCSubnet -ReserveIPAddress '10.2.2.0/24' -ContextName Subnet3 | Should -Be ([qIPAM.Models.IPAddressRange]'10.2.2.0/24') } It 'Should backfill when asked for the next available space, and there is room in between consumed ranges' { Get-SCSubnet -ReserveCIDR 24 -ContextName Subnet1 | Should -Be ([qIPAM.Models.IPAddressRange]'10.0.1.0/24') Get-SCSubnet -ReserveCIDR 24 -ContextName Subnet2 | Should -Be ([qIPAM.Models.IPAddressRange]'10.1.1.0/24') Get-SCSubnet -ReserveCIDR 24 -ContextName Subnet3 | Should -Be ([qIPAM.Models.IPAddressRange]'10.2.1.0/24') } It 'Should provide the smallest available CIDR block when -ReserveCount is used' { Get-SCSubnet -ReserveCount 250 -ContextName Subnet1 | Select-Object -ExpandProperty CIDR | Should -Be 24 Get-SCSubnet -ReserveCount 1000 -ContextName Subnet1 | Select-Object -ExpandProperty CIDR | Should -Be 22 Get-SCSubnet -ReserveCount 8000 -ContextName Subnet1 | Select-Object -ExpandProperty CIDR | Should -Be 19 } } Describe 'Clear-SCContext' { BeforeAll { Set-SCContext -RootAddressSpace '10.0.0.0/8' -ConsumedIPRanges '10.1.0.0/16', '10.2.0.0/16' Set-SCContext -RootAddressSpace '10.1.0.0/16' -ConsumedIPRanges '10.1.0.0/24', '10.1.1.0/24' -Name Subnet1 Set-SCContext -RootAddressSpace '10.2.0.0/16' -ConsumedIPRanges '10.2.0.0/24', '10.2.1.0/24' -Name Subnet2 } It 'Should clear the "default" context if no -ContextName is provided' { Get-SCContext -Name default | Select-Object -ExpandProperty ConsumedRanges | Measure-Object | Select-Object -ExpandProperty Count | Should -Be 2 Clear-SCContext Get-SCContext -Name default | Select-Object -ExpandProperty ConsumedRanges | Measure-Object | Select-Object -ExpandProperty Count | Should -Be 0 } It 'Should only clear the context named' { Get-SCContext -Name Subnet1 | Select-Object -ExpandProperty ConsumedRanges | Measure-Object | Select-Object -ExpandProperty Count | Should -Be 2 Get-SCContext -Name Subnet2 | Select-Object -ExpandProperty ConsumedRanges | Measure-Object | Select-Object -ExpandProperty Count | Should -Be 2 Clear-SCContext -Name Subnet1 Get-SCContext -Name Subnet1 | Select-Object -ExpandProperty ConsumedRanges | Measure-Object | Select-Object -ExpandProperty Count | Should -Be 0 Get-SCContext -Name Subnet2 | Select-Object -ExpandProperty ConsumedRanges | Measure-Object | Select-Object -ExpandProperty Count | Should -Be 2 } } Describe 'Rename-SCContext' { BeforeAll { Set-SCContext -Name Subnet1 -RootAddressSpace '10.0.0.0/16' -ConsumedIPRanges '10.0.0.0/24', '10.0.1.0/24' Set-SCContext -Name Subnet2 -RootAddressSpace '10.1.0.0/16' -ConsumedIPRanges '10.1.0.0/24', '10.1.1.0/24' } It 'Should only rename the specified context' { $subnet1ContextModel = Get-SCContext -Name Subnet1 $subnet2ContextModel = Get-SCContext -Name Subnet2 Rename-SCContext -OldContextName Subnet1 -NewContextName NewSubnet1 Get-SCContext -Name NewSubnet1 | Should -Not -BeNullOrEmpty Get-SCContext -Name Subnet2 | Should -Not -BeNullOrEmpty Get-SCContext -Name NewSubnet1 | Select-Object -ExpandProperty RootIPAddressRange | Should -Be $subnet1ContextModel.RootIPAddressRange Get-SCContext -Name Subnet2 | Select-Object -ExpandProperty RootIPAddressRange | Should -Be $subnet2ContextModel.RootIPAddressRange Get-SCContext -Name NewSubnet1 | Select-Object -ExpandProperty ConsumedRanges | Measure-Object | Select-Object -ExpandProperty Count | Should -Be ($subnet1ContextModel | Select-Object -ExpandProperty ConsumedRanges | Measure-Object | Select-Object -ExpandProperty Count) Get-SCContext -Name Subnet2 | Select-Object -ExpandProperty ConsumedRanges | Measure-Object | Select-Object -ExpandProperty Count | Should -Be ($subnet2ContextModel | Select-Object -ExpandProperty ConsumedRanges | Measure-Object | Select-Object -ExpandProperty Count) } } |