Skip to content

Commit

Permalink
Added additional tests and fixed small bug
Browse files Browse the repository at this point in the history
  • Loading branch information
DCMattyG committed Aug 23, 2023
1 parent 34d4e2d commit e6f93cb
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/azure-ipam-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ jobs:
- name: Invoke Pester Tests
working-directory: tests
env:
IPAM_RESOURCE_GROUP: ${{ needs.deploy.outputs.ipamResourceGroup }}
IPAM_URL: ${{ needs.deploy.outputs.ipamURL }}
IPAM_ENGINE_APP_ID: ${{ needs.deploy.outputs.ipamEngineAppId }}
shell: pwsh
Expand Down
2 changes: 1 addition & 1 deletion engine/app/routers/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ async def create_block_net(
resv_cidrs = list(x['cidr'] for x in target_block['resv'] if not x['settledOn'])
block_net_cidrs += resv_cidrs

ext_cidrs = list(x['cidr'] for x in target_block['externala'])
ext_cidrs = list(x['cidr'] for x in target_block['externals'])
block_net_cidrs += ext_cidrs

for v in target_block['vnets']:
Expand Down
321 changes: 313 additions & 8 deletions tests/azureipam.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ BeforeAll {
[string]$resource,

[Parameter(Mandatory=$True, Position=1)]
[hashtable]$body
[object[]]$body
)

$jsonBody = $body | ConvertTo-Json
Expand All @@ -81,7 +81,7 @@ BeforeAll {
[string]$resource,

[Parameter(Mandatory=$True, Position=1)]
[hashtable]$body
[hashtable[]]$body
)

$jsonBody = $body | ConvertTo-Json
Expand All @@ -101,27 +101,332 @@ BeforeAll {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, Position=0)]
[string]$resource
[string]$resource,

[Parameter(Mandatory=$False, Position=1)]
[string[]]$body
)

$jsonBody = $body | ConvertTo-Json -AsArray
$response = Invoke-RestMethod `
-Method Delete `
-Autjentication Bearer `
-Authentication Bearer `
-Token $accessToken `
-Uri "${baseUrl}${resource}" `
-Headers $headers
-Headers $headers `
-Body $jsonBody

Write-Output $response
}
}

Describe 'Get-Posts' {
Context 'Spaces' {
It 'Verify No Spaces Exist' {

$spaces = Get-ApiResource '/spaces'

$spaces | Should -Be $null
}

It 'Create Two Spaces' {
$spaceA = @{
name = 'TestSpace01'
desc = 'Test Space 1'
}

It 'Spaces is empty' {
$spaceB = @{
name = 'TestSpace02'
desc = 'Test Space 2'
}

New-ApiResource '/spaces' $spaceA
New-ApiResource '/spaces' $spaceB

$spaces = Get-ApiResource '/spaces'

$spaces | Should -Be $null
$spaces.Count | Should -Be 2
$spaces.Name -contains 'TestSpace01' | Should -Be $true
$spaces.Name -contains 'TestSpace02' | Should -Be $true
}

It 'Delete a Space' {
Remove-ApiResource '/spaces/TestSpace02'

$spaces = Get-ApiResource '/spaces'

$spaces.Count | Should -Be 1
$spaces.Name -contains 'TestSpace01' | Should -Be $true
$spaces.Name -contains 'TestSpace02' | Should -Be $false
}

It 'Update a Space' {
$update = @(
@{
op = 'replace'
path = '/name'
value = 'TestSpaceA'
}
@{
op = 'replace'
path = '/desc'
value = 'Test Space A'
}
)

Update-ApiResource '/spaces/TestSpace01' $update

$spaces = Get-ApiResource '/spaces'

$spaces.Count | Should -Be 1
$spaces[0].Name -eq 'TestSpaceA' | Should -Be $true
$spaces[0].Desc -eq 'Test Space A' | Should -Be $true
}
}

Context 'Blocks' {
It 'Verify No Blocks Exist' {

$blocks = Get-ApiResource '/spaces/TestSpaceA/blocks'

$blocks | Should -Be $null
}

It 'Create Two Blocks' {
$blockA = @{
name = 'TestBlock01'
cidr = '10.0.0.0/16'
}

$blockB = @{
name = 'TestBlock02'
cidr = '192.168.0.0/24'
}

New-ApiResource '/spaces/TestSpaceA/blocks' $blockA
New-ApiResource '/spaces/TestSpaceA/blocks' $blockB

$blocks = Get-ApiResource '/spaces/TestSpaceA/blocks'

$blocks.Count | Should -Be 2
$blocks.Name -contains 'TestBlock01' | Should -Be $true
$blocks.Name -contains 'TestBlock02' | Should -Be $true
}

It 'Delete a Block' {
Remove-ApiResource '/spaces/TestSpaceA/blocks/TestBlock02'

$blocks = Get-ApiResource '/spaces/TestSpaceA/blocks'

$blocks.Count | Should -Be 1
$blocks.Name -contains 'TestBlock01' | Should -Be $true
$blocks.Name -contains 'TestBlock02' | Should -Be $false
}

It 'Update a Block' {
$update = @(
@{
op = 'replace'
path = '/name'
value = 'TestBlockA'
}
@{
op = 'replace'
path = '/cidr'
value = '10.1.0.0/16'
}
)

Update-ApiResource '/spaces/TestSpaceA/blocks/TestBlock01' $update

$blocks = Get-ApiResource '/spaces/TestSpaceA/blocks'

$blocks.Count | Should -Be 1
$blocks[0].Name -eq 'TestBlockA' | Should -Be $true
$blocks[0].Cidr -eq '10.1.0.0/16' | Should -Be $true
}
}

Context 'Networks' {
It 'Verify No Networks Exist in Block' {

$networks = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/networks'

$networks | Should -Be $null
}

It 'Add a Virtual Network to a Block' {
$script:newNetA = New-AzVirtualNetwork `
-Name 'TestVNet01' `
-ResourceGroupName $env:IPAM_RESOURCE_GROUP `
-Location 'westus3' `
-AddressPrefix '10.1.0.0/24'

Start-Sleep -Seconds 60

$body = @{
id = $script:newNetA.Id
}

$block = New-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/networks' $body

$($block.vnets | Select-Object -ExpandProperty id) -contains $script:newNetA.Id | Should -Be $true
}

It 'Replace Block Virtual Networks' {
$script:newNetB = New-AzVirtualNetwork `
-Name 'TestVNet02' `
-ResourceGroupName $env:IPAM_RESOURCE_GROUP `
-Location 'westus3' `
-AddressPrefix '10.1.1.0/24'

Start-Sleep -Seconds 60

$body = @(
$script:newNetA.Id
$script:newNetB.Id
)

Set-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/networks' $body
$networks = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/networks'

$($networks | Select-Object -ExpandProperty id) -contains $script:newNetA.Id | Should -Be $true
$($networks | Select-Object -ExpandProperty id) -contains $script:newNetB.Id | Should -Be $true
}

It 'Delete Block Virtual Network' {
$body = @(
$script:newNetB.Id
)

Remove-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/networks' $body
$networks = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/networks'

$($networks | Select-Object -ExpandProperty id) -contains $script:newNetA.Id | Should -Be $true
$($networks | Select-Object -ExpandProperty id) -contains $script:newNetB.Id | Should -Be $false
}
}

Context 'External Networks' {
It 'Verify No External Networks Exist in Block' {

$externals = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/externals'

$externals | Should -Be $null
}

It 'Add an External Network to Block' {
$script:externalA = @{
name = "ExternalNetA"
desc = "External Network A"
cidr = "10.1.1.0/24"
}

New-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/externals' $script:externalA
$externals = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/externals'

$externals.Name -contains "ExternalNetA" | Should -Be $true
}

It 'Replace Block External Networks' {
$script:externalB = @{
name = "ExternalNetB"
desc = "External Network B"
cidr = "10.1.2.0/24"
}

$script:externalC = @{
name = "ExternalNetC"
desc = "External Network C"
cidr = "10.1.3.0/24"
}

$body = @(
$script:externalA
$script:externalB
$script:externalC
)

Set-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/externals' $body
$externals = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/externals'

$externals.Name -contains "ExternalNetA" | Should -Be $true
$externals.Name -contains "ExternalNetB" | Should -Be $true
}

It 'Delete Block External Network' {
$body = @(
$script:externalC.name
)

Remove-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/externals' $body
$externals = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/externals'

$externals.Name -contains "ExternalNetA" | Should -Be $true
$externals.Name -contains "ExternalNetB" | Should -Be $true
$externals.Name -contains "ExternalNetC" | Should -Be $false
}

It 'Get Specific Block External Network' {

$external = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/externals/ExternalNetB'

$external.Name | Should -Be "ExternalNetB"
$external.Desc | Should -Be "External Network B"
$external.Cidr | Should -Be "10.1.2.0/24"
}

It 'Delete Specific Block External Network' {

Remove-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/externals/ExternalNetB'
$externals = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/externals'

$externals.Name -contains "ExternalNetA" | Should -Be $true
$externals.Name -contains "ExternalNetB" | Should -Be $false
$externals.Name -contains "ExternalNetC" | Should -Be $false
}
}

Context 'Reservations' {
It 'Verify No Reservations Exist in Block' {

$reservations = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/reservations'

$reservations | Should -Be $null
}

It 'Create Block Reservation' {
$body = @{
size = 24
desc = "Test Reservation A"
}

$script:reservationA = New-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/reservations' $body
$reservations = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/reservations'

$reservations.Count | Should -Be 1
$reservations[0].Space -eq "TestSpaceA" | Should -Be $true
$reservations[0].Block -eq "TestBlockA" | Should -Be $true
$reservations[0].Desc -eq "Test Reservation A" | Should -Be $true
$reservations[0].Cidr -eq "10.1.2.0/24" | Should -Be $true
$reservations[0].SettledOn -eq $null | Should -Be $true
}

It 'Import Virtual Network via Reservation ID' {
$script:newNetC = New-AzVirtualNetwork `
-Name 'TestVNet03' `
-ResourceGroupName $env:IPAM_RESOURCE_GROUP `
-Location 'westus3' `
-AddressPrefix $script:reservationA.Cidr `
-Tag $script:reservationA.Tag

Start-Sleep -Seconds 180

$networks = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/networks'
$reservations = Get-ApiResource '/spaces/TestSpaceA/blocks/TestBlockA/reservations'

$($networks | Select-Object -ExpandProperty id) -contains $script:newNetA.Id | Should -Be $true
$($networks | Select-Object -ExpandProperty id) -contains $script:newNetC.Id | Should -Be $true
$reservations[0].SettledOn -eq $null | Should -Be $false
$reservations[0].Status -eq "fulfilled" | Should -Be $true
}
}

0 comments on commit e6f93cb

Please sign in to comment.