Skip to content

Commit

Permalink
Added initial Pester test and limited repo clone to specific directories
Browse files Browse the repository at this point in the history
  • Loading branch information
DCMattyG committed Aug 22, 2023
1 parent 0742353 commit 30f1cc6
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 3 deletions.
42 changes: 39 additions & 3 deletions .github/workflows/azure-ipam-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
deploy:
runs-on: ubuntu-latest
outputs:
ipamURL: ${{ steps.pwshScript.outputs.ipamURL }}
ipamUIAppId: ${{ steps.pwshScript.outputs.ipamUIAppId }}
ipamEngineAppId: ${{ steps.pwshScript.outputs.ipamEngineAppId }}
ipamSuffix: ${{ steps.pwshScript.outputs.ipamSuffix }}
Expand All @@ -31,7 +32,7 @@ jobs:
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module Az, Microsoft.Graph -Force
Install-Module Az, Microsoft.Graph -AllowClobber -Force
# - name: "Azure Login"
# uses: azure/login@v1
Expand All @@ -49,6 +50,9 @@ jobs:

- name: Checkout Azure IPAM Code
uses: actions/checkout@v3
with:
sparse-checkout: |
deploy
- name: Deploy Azure IPAM
working-directory: "deploy"
Expand Down Expand Up @@ -94,15 +98,47 @@ jobs:
sleep 30
echo "Time: $(date +'%T')"
cleanup:
test:
runs-on: ubuntu-latest
needs: [ deploy, sleep ]
steps:
- name: Install Testing Prerequisites
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module Az, Pester -AllowClobber -Force
- name: "Azure Login"
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
enable-AzPSSession: true

- name: Checkout Azure IPAM Code
uses: actions/checkout@v3
with:
sparse-checkout: |
tests
- name: Test Azure IPAM via Pester
working-directory: "tests"
env:
IPAM_URL: ${{ needs.deploy.outputs.ipamURL }}
IPAM_ENGINE_APP_ID: ${{ needs.deploy.outputs.ipamEngineAppId }}
shell: pwsh
run: |
Import-Module Pester -PassThru
Invoke-Pester -Output Detailed ./azureipam.tests.ps1 -PassThru
cleanup:
runs-on: ubuntu-latest
needs: [ deploy, sleep, test ]
steps:
- name: Install Deployment Prerequisites
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module Az -Force
Install-Module Az -AllowClobber -Force
- name: "Azure Login"
uses: azure/login@v1
Expand Down
1 change: 1 addition & 0 deletions deploy/deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ process {
Write-Host
Stop-Transcript | Out-Null

Write-Output "ipamURL=https://$($deployment.Outputs["appServiceHostName"].Value)" >> $Env:GITHUB_OUTPUT
Write-Output "ipamUIAppId=$($appDetails.UIAppId)" >> $Env:GITHUB_OUTPUT
Write-Output "ipamEngineAppId=$($appDetails.EngineAppId)" >> $Env:GITHUB_OUTPUT
Write-Output "ipamSuffix=$($deployment.Outputs["suffix"].Value)" >> $Env:GITHUB_OUTPUT
Expand Down
127 changes: 127 additions & 0 deletions tests/azureipam.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
BeforeAll {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Set-StrictMode -Version Latest

[string]$baseUrl = "$env:IPAM_URL/api"
[System.Security.SecureString]$accessToken = ConvertTo-SecureString (Get-AzAccessToken -ResourceUrl api://$env:IPAM_ENGINE_APP_ID).Token -AsPlainText
[hashtable]$headers = @{
"Content-Type" = "application/json"
}

# GET API Request
Function Get-ApiResource {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True, Position=0)]
[string]$resource
)

$response = Invoke-RestMethod `
-Method Get `
-Authentication Bearer `
-Token $accessToken `
-Uri "${baseUrl}${resource}" `
-Headers $headers

Write-Output $response
}

# POST API Request
Function New-ApiResource {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, Position=0)]
[string]$resource,

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

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

Write-Output $response
}

# PUT API Request
Function Set-ApiResource {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, Position=0)]
[string]$resource,

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

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

Write-Output $response
}

# PATCH API Request
Function Update-ApiResource {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, Position=0)]
[string]$resource,

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

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

Write-Output $response
}

# DELETE API Request
Function Remove-ApiResource {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, Position=0)]
[string]$resource
)

$response = Invoke-RestMethod `
-Method Delete `
-Autjentication Bearer `
-Token $accessToken `
-Uri "${baseUrl}${resource}" `
-Headers $headers

Write-Output $response
}
}

Describe 'Get-Posts' {

It 'Spaces is empty' {

$spaces = Get-ApiResource '/spaces'

$spaces | Should -Be $null
}

}

0 comments on commit 30f1cc6

Please sign in to comment.